2929
3030@patch ("xcengine.core.ScriptCreator.__init__" )
3131@pytest .mark .parametrize ("tag" , [None , "bar" ])
32- @pytest .mark .parametrize ("use_env" , [False , True ])
33- def test_image_builder_init (init_mock , tmp_path , tag , use_env ):
32+ @pytest .mark .parametrize ("env_file_name" , ["environment.yml" , "foo.yaml" , None ])
33+ @pytest .mark .parametrize ("use_env_file_param" , [False , True ])
34+ def test_image_builder_init (
35+ init_mock ,
36+ tmp_path : pathlib .Path ,
37+ tag : str | None ,
38+ env_file_name : str | None ,
39+ use_env_file_param : bool ,
40+ ):
3441 nb_path = tmp_path / "foo.ipynb"
3542 nb_path .touch ()
36- if use_env :
37- environment = tmp_path / "environment.yml"
38- environment .touch ()
43+ if env_file_name is not None :
44+ environment_path = tmp_path / env_file_name
45+ environment_path .touch ()
3946 else :
40- environment = None
47+ environment_path = None
4148 build_path = tmp_path / "build"
4249 build_path .mkdir ()
4350 init_mock .return_value = None
4451 ib = ImageBuilder (
4552 notebook = nb_path ,
46- environment = environment ,
53+ environment = environment_path if use_env_file_param else None ,
4754 build_dir = build_path ,
4855 tag = tag ,
4956 )
5057 assert ib .notebook == nb_path
5158 assert ib .build_dir == build_path
52- assert ib .environment == environment
59+ expected_env = environment_path if (use_env_file_param or env_file_name == "environment.yml" ) else None
60+ assert ib .environment == expected_env
5361 if tag is None :
5462 assert abs (
5563 datetime .datetime .now (datetime .UTC )
@@ -97,12 +105,13 @@ def test_runner_init_with_image():
97105 )
98106 assert runner .image == image
99107
108+
100109@pytest .mark .parametrize ("keep" , [False , True ])
101110def test_runner_run_keep (keep : bool ):
102111 runner = xcengine .core .ContainerRunner (
103112 image := Mock (docker .models .images .Image ),
104113 None ,
105- client := Mock (DockerClient )
114+ client := Mock (DockerClient ),
106115 )
107116 image .tags = []
108117 client .containers .run .return_value = (container := MagicMock (Container ))
@@ -118,26 +127,32 @@ def test_runner_sigint():
118127 runner = xcengine .core .ContainerRunner (
119128 image := Mock (docker .models .images .Image ),
120129 None ,
121- client := Mock (DockerClient )
130+ client := Mock (DockerClient ),
122131 )
123132 image .tags = []
124133 client .containers .run .return_value = (container := Mock (Container ))
125134 container .status = "running"
135+
126136 def container_stop ():
127137 container .status = "stopped"
138+
128139 container .stop = container_stop
129140 pid = os .getpid ()
130141
131142 old_alarm_handler = signal .getsignal (signal .SIGALRM )
143+
132144 class AlarmException (Exception ):
133145 pass
146+
134147 def alarm_handler (signum , frame ):
135148 raise AlarmException ()
149+
136150 signal .signal (signal .SIGALRM , alarm_handler )
137151
138152 def interrupt_process ():
139153 time .sleep (1 ) # allow one second for runner to start
140154 os .kill (pid , signal .SIGINT )
155+
141156 thread = threading .Thread (target = interrupt_process , daemon = True )
142157 thread .start ()
143158
0 commit comments