Skip to content

Commit beb05a2

Browse files
committed
Improve CLI unit tests
1 parent f93cf70 commit beb05a2

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

test/test_cli.py

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from unittest.mock import patch, ANY
1+
from unittest.mock import patch, ANY, MagicMock
22
import pytest
33
from click.testing import CliRunner
44

@@ -59,14 +59,33 @@ def test_make_script(
5959
assert result.exit_code == 0
6060

6161

62-
@patch("xcengine.core.ImageBuilder.__init__")
63-
@patch("xcengine.core.ImageBuilder.build")
64-
def test_image_build(build_mock, init_mock, tmp_path):
62+
@patch("xcengine.cli.ImageBuilder")
63+
def test_image_build(builder_mock, tmp_path):
6564
nb_path = tmp_path / "foo.ipynb"
6665
nb_path.touch()
6766
runner = CliRunner()
6867
tag = "foo"
68+
instance_mock = builder_mock.return_value = MagicMock()
6969
result = runner.invoke(cli, ["image", "build", "--tag", tag, str(nb_path)])
70-
init_mock.assert_called_once_with(
70+
assert result.exit_code == 0
71+
builder_mock.assert_called_once_with(
7172
notebook=nb_path, environment=None, tag=tag, build_dir=ANY
7273
)
74+
instance_mock.build.assert_called_once_with()
75+
76+
@patch("xcengine.cli.ContainerRunner")
77+
def test_image_run(runner_mock):
78+
cli_runner = CliRunner()
79+
instance_mock = runner_mock.return_value = MagicMock()
80+
result = cli_runner.invoke(
81+
cli,
82+
["image", "run", "foo"]
83+
)
84+
runner_mock.assert_called_once_with(image="foo", output_dir=None)
85+
assert result.exit_code == 0
86+
instance_mock.run.assert_called_once_with(
87+
run_batch=False,
88+
host_port=None,
89+
from_saved=False,
90+
keep=False,
91+
)

xcengine/cli.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
import click
1414
import yaml
15-
from click.core import ParameterSource
1615

1716
from .core import ScriptCreator, ImageBuilder, ContainerRunner
1817

@@ -153,16 +152,20 @@ def build(
153152
eoap: pathlib.Path,
154153
) -> None:
155154
init_args = dict(notebook=notebook, environment=environment, tag=tag)
155+
pathlib.Path("/home/pont/AAAAARGH").touch()
156156
if build_dir:
157157
image_builder = ImageBuilder(build_dir=build_dir, **init_args)
158158
os.makedirs(build_dir, exist_ok=True)
159159
image = image_builder.build()
160160
else:
161161
with tempfile.TemporaryDirectory() as temp_dir:
162+
pathlib.Path("/home/pont/gothere1").touch()
162163
image_builder = ImageBuilder(
163164
build_dir=pathlib.Path(temp_dir), **init_args
164165
)
166+
pathlib.Path("/home/pont/gothere2").touch()
165167
image = image_builder.build()
168+
pathlib.Path("/home/pont/gothere3").touch()
166169
if eoap:
167170
eoap.write_text(yaml.dump(image_builder.create_cwl()))
168171
print(f"Built image with tags {image.tags}")

0 commit comments

Comments
 (0)