@@ -59,6 +59,7 @@ class TestPrepareDependencyArchive:
5959 @patch ("datacustomcode.deploy.shutil.copy" )
6060 @patch ("datacustomcode.deploy.tempfile.TemporaryDirectory" )
6161 @patch ("datacustomcode.deploy.os.path.join" )
62+ @patch ("datacustomcode.deploy.os.path.dirname" )
6263 @patch ("datacustomcode.deploy.os.makedirs" )
6364 @patch ("datacustomcode.deploy.docker_build_cmd" )
6465 @patch ("datacustomcode.deploy.docker_run_cmd" )
@@ -67,6 +68,7 @@ def test_prepare_dependency_archive_image_exists(
6768 mock_docker_run_cmd ,
6869 mock_docker_build_cmd ,
6970 mock_makedirs ,
71+ mock_dirname ,
7072 mock_join ,
7173 mock_temp_dir ,
7274 mock_copy ,
@@ -82,8 +84,34 @@ def test_prepare_dependency_archive_image_exists(
8284 # Mock cmd_output to return image ID (indicating image exists)
8385 mock_cmd_output .return_value = "abc123"
8486
85- # Mock os.path.join for archive path
86- mock_join .return_value = "/tmp/test_dir/native_dependencies.tar.gz"
87+ # Mock os.path.dirname to handle different calls
88+ def dirname_side_effect (path ):
89+ if path == "/test/dir" :
90+ return "/test"
91+ elif path == "payload/py-files" :
92+ return "payload"
93+ else :
94+ # For other paths, do simple string manipulation
95+ return path .rsplit ("/" , 1 )[0 ] if "/" in path else ""
96+
97+ mock_dirname .side_effect = dirname_side_effect
98+
99+ # Mock os.path.join to handle different calls
100+ def join_side_effect (* args ):
101+ if args == ("/test" , "requirements.txt" ):
102+ return "/test/requirements.txt"
103+ elif args == ("/test" , "build_native_dependencies.sh" ):
104+ return "/test/build_native_dependencies.sh"
105+ elif args == ("/tmp/test_dir" , "native_dependencies.tar.gz" ):
106+ return "/tmp/test_dir/native_dependencies.tar.gz"
107+ elif args == ("payload" , "archives" , "native_dependencies.tar.gz" ):
108+ return "payload/archives/native_dependencies.tar.gz"
109+ elif args == ("payload" , "archives" ):
110+ return "payload/archives"
111+ else :
112+ return "/" .join (args )
113+
114+ mock_join .side_effect = join_side_effect
87115
88116 # Mock the docker command functions
89117 mock_docker_build_cmd .return_value = "mock build command"
@@ -97,13 +125,13 @@ def test_prepare_dependency_archive_image_exists(
97125 # Verify docker build command was not called (since image already exists)
98126 mock_docker_build_cmd .assert_not_called ()
99127
100- # Verify files were copied to temp directory
101- mock_copy .assert_any_call ("requirements.txt" , "/tmp/test_dir" )
102- mock_copy .assert_any_call ("build_native_dependencies.sh" , "/tmp/test_dir" )
128+ # Verify files were copied to temp directory from parent directory
129+ mock_copy .assert_any_call ("/test/ requirements.txt" , "/tmp/test_dir" )
130+ mock_copy .assert_any_call ("/test/ build_native_dependencies.sh" , "/tmp/test_dir" )
103131
104132 # Verify docker run command was called
105133 mock_docker_run_cmd .assert_called_once_with ("default" , "/tmp/test_dir" )
106- mock_cmd_output .assert_any_call ("mock run command" , env = ANY )
134+ mock_cmd_output .assert_any_call ("mock run command" , env = ANY , cwd = "/test" )
107135
108136 # Verify archives directory was created
109137 mock_makedirs .assert_called_once_with ("payload/archives" , exist_ok = True )
@@ -118,6 +146,7 @@ def test_prepare_dependency_archive_image_exists(
118146 @patch ("datacustomcode.deploy.shutil.copy" )
119147 @patch ("datacustomcode.deploy.tempfile.TemporaryDirectory" )
120148 @patch ("datacustomcode.deploy.os.path.join" )
149+ @patch ("datacustomcode.deploy.os.path.dirname" )
121150 @patch ("datacustomcode.deploy.os.makedirs" )
122151 @patch ("datacustomcode.deploy.docker_build_cmd" )
123152 @patch ("datacustomcode.deploy.docker_run_cmd" )
@@ -126,6 +155,7 @@ def test_prepare_dependency_archive_build_image(
126155 mock_docker_run_cmd ,
127156 mock_docker_build_cmd ,
128157 mock_makedirs ,
158+ mock_dirname ,
129159 mock_join ,
130160 mock_temp_dir ,
131161 mock_copy ,
@@ -142,8 +172,33 @@ def test_prepare_dependency_archive_build_image(
142172 # and then return some value for subsequent calls
143173 mock_cmd_output .side_effect = [None , None , None , None ]
144174
145- # Mock os.path.join for archive path
146- mock_join .return_value = "/tmp/test_dir/native_dependencies.tar.gz"
175+ # Mock os.path.dirname to handle different calls
176+ def dirname_side_effect (path ):
177+ if path == "/test/dir" :
178+ return "/test"
179+ elif path == "payload/py-files" :
180+ return "payload"
181+ else :
182+ return path .rsplit ("/" , 1 )[0 ] if "/" in path else ""
183+
184+ mock_dirname .side_effect = dirname_side_effect
185+
186+ # Mock os.path.join to handle different calls
187+ def join_side_effect (* args ):
188+ if args == ("/test" , "requirements.txt" ):
189+ return "/test/requirements.txt"
190+ elif args == ("/test" , "build_native_dependencies.sh" ):
191+ return "/test/build_native_dependencies.sh"
192+ elif args == ("/tmp/test_dir" , "native_dependencies.tar.gz" ):
193+ return "/tmp/test_dir/native_dependencies.tar.gz"
194+ elif args == ("payload" , "archives" , "native_dependencies.tar.gz" ):
195+ return "payload/archives/native_dependencies.tar.gz"
196+ elif args == ("payload" , "archives" ):
197+ return "payload/archives"
198+ else :
199+ return "/" .join (args )
200+
201+ mock_join .side_effect = join_side_effect
147202
148203 # Mock the docker command functions
149204 mock_docker_build_cmd .return_value = "mock build command"
@@ -156,15 +211,15 @@ def test_prepare_dependency_archive_build_image(
156211
157212 # Verify docker build command was called
158213 mock_docker_build_cmd .assert_called_once_with ("default" )
159- mock_cmd_output .assert_any_call ("mock build command" , env = ANY )
214+ mock_cmd_output .assert_any_call ("mock build command" , env = ANY , cwd = "/test" )
160215
161- # Verify files were copied to temp directory
162- mock_copy .assert_any_call ("requirements.txt" , "/tmp/test_dir" )
163- mock_copy .assert_any_call ("build_native_dependencies.sh" , "/tmp/test_dir" )
216+ # Verify files were copied to temp directory from parent directory
217+ mock_copy .assert_any_call ("/test/ requirements.txt" , "/tmp/test_dir" )
218+ mock_copy .assert_any_call ("/test/ build_native_dependencies.sh" , "/tmp/test_dir" )
164219
165220 # Verify docker run command was called
166221 mock_docker_run_cmd .assert_called_once_with ("default" , "/tmp/test_dir" )
167- mock_cmd_output .assert_any_call ("mock run command" , env = ANY )
222+ mock_cmd_output .assert_any_call ("mock run command" , env = ANY , cwd = "/test" )
168223
169224 # Verify archives directory was created
170225 mock_makedirs .assert_called_once_with ("payload/archives" , exist_ok = True )
@@ -222,6 +277,7 @@ def test_prepare_dependency_archive_docker_build_failure(
222277 @patch ("datacustomcode.deploy.shutil.copy" )
223278 @patch ("datacustomcode.deploy.tempfile.TemporaryDirectory" )
224279 @patch ("datacustomcode.deploy.os.path.join" )
280+ @patch ("datacustomcode.deploy.os.path.dirname" )
225281 @patch ("datacustomcode.deploy.os.makedirs" )
226282 @patch ("datacustomcode.deploy.docker_build_cmd" )
227283 @patch ("datacustomcode.deploy.docker_run_cmd" )
@@ -230,6 +286,7 @@ def test_prepare_dependency_archive_docker_run_failure(
230286 mock_docker_run_cmd ,
231287 mock_docker_build_cmd ,
232288 mock_makedirs ,
289+ mock_dirname ,
233290 mock_join ,
234291 mock_temp_dir ,
235292 mock_copy ,
@@ -252,15 +309,35 @@ def test_prepare_dependency_archive_docker_run_failure(
252309 ), # Run fails
253310 ]
254311
312+ # Mock os.path.dirname to handle different calls
313+ def dirname_side_effect (path ):
314+ if path == "/test/dir" :
315+ return "/test"
316+ else :
317+ return path .rsplit ("/" , 1 )[0 ] if "/" in path else ""
318+
319+ mock_dirname .side_effect = dirname_side_effect
320+
321+ # Mock os.path.join to handle different calls
322+ def join_side_effect (* args ):
323+ if args == ("/test" , "requirements.txt" ):
324+ return "/test/requirements.txt"
325+ elif args == ("/test" , "build_native_dependencies.sh" ):
326+ return "/test/build_native_dependencies.sh"
327+ else :
328+ return "/" .join (args )
329+
330+ mock_join .side_effect = join_side_effect
331+
255332 with pytest .raises (CalledProcessError , match = "Run failed" ):
256333 prepare_dependency_archive ("/test/dir" , "default" , "script" )
257334
258335 # Verify docker images command was called
259336 mock_cmd_output .assert_any_call (self .EXPECTED_DOCKER_IMAGES_CMD )
260337
261- # Verify files were copied to temp directory
262- mock_copy .assert_any_call ("requirements.txt" , "/tmp/test_dir" )
263- mock_copy .assert_any_call ("build_native_dependencies.sh" , "/tmp/test_dir" )
338+ # Verify files were copied to temp directory from parent directory
339+ mock_copy .assert_any_call ("/test/ requirements.txt" , "/tmp/test_dir" )
340+ mock_copy .assert_any_call ("/test/ build_native_dependencies.sh" , "/tmp/test_dir" )
264341
265342 # Verify docker run command was called
266343 mock_docker_run_cmd .assert_called_once_with ("default" , "/tmp/test_dir" )
@@ -269,6 +346,7 @@ def test_prepare_dependency_archive_docker_run_failure(
269346 @patch ("datacustomcode.deploy.shutil.copy" )
270347 @patch ("datacustomcode.deploy.tempfile.TemporaryDirectory" )
271348 @patch ("datacustomcode.deploy.os.path.join" )
349+ @patch ("datacustomcode.deploy.os.path.dirname" )
272350 @patch ("datacustomcode.deploy.os.makedirs" )
273351 @patch ("datacustomcode.deploy.docker_build_cmd" )
274352 @patch ("datacustomcode.deploy.docker_run_cmd" )
@@ -277,6 +355,7 @@ def test_prepare_dependency_archive_file_copy_failure(
277355 mock_docker_run_cmd ,
278356 mock_docker_build_cmd ,
279357 mock_makedirs ,
358+ mock_dirname ,
280359 mock_join ,
281360 mock_temp_dir ,
282361 mock_copy ,
@@ -292,6 +371,26 @@ def test_prepare_dependency_archive_file_copy_failure(
292371 # Mock cmd_output to return image ID
293372 mock_cmd_output .return_value = "abc123"
294373
374+ # Mock os.path.dirname to handle different calls
375+ def dirname_side_effect (path ):
376+ if path == "/test/dir" :
377+ return "/test"
378+ else :
379+ return path .rsplit ("/" , 1 )[0 ] if "/" in path else ""
380+
381+ mock_dirname .side_effect = dirname_side_effect
382+
383+ # Mock os.path.join to handle different calls
384+ def join_side_effect (* args ):
385+ if args == ("/test" , "requirements.txt" ):
386+ return "/test/requirements.txt"
387+ elif args == ("/test" , "build_native_dependencies.sh" ):
388+ return "/test/build_native_dependencies.sh"
389+ else :
390+ return "/" .join (args )
391+
392+ mock_join .side_effect = join_side_effect
393+
295394 # Mock shutil.copy to raise exception
296395 mock_copy .side_effect = FileNotFoundError ("File not found" )
297396
@@ -301,8 +400,8 @@ def test_prepare_dependency_archive_file_copy_failure(
301400 # Verify docker images command was called
302401 mock_cmd_output .assert_any_call (self .EXPECTED_DOCKER_IMAGES_CMD )
303402
304- # Verify files were attempted to be copied
305- mock_copy .assert_any_call ("requirements.txt" , "/tmp/test_dir" )
403+ # Verify files were attempted to be copied from parent directory
404+ mock_copy .assert_any_call ("/test/ requirements.txt" , "/tmp/test_dir" )
306405
307406 @patch ("datacustomcode.deploy.cmd_output" )
308407 @patch ("datacustomcode.deploy.shutil.copytree" )
@@ -311,6 +410,7 @@ def test_prepare_dependency_archive_file_copy_failure(
311410 @patch ("datacustomcode.deploy.tempfile.TemporaryDirectory" )
312411 @patch ("datacustomcode.deploy.os.path.exists" )
313412 @patch ("datacustomcode.deploy.os.path.join" )
413+ @patch ("datacustomcode.deploy.os.path.dirname" )
314414 @patch ("datacustomcode.deploy.os.makedirs" )
315415 @patch ("datacustomcode.deploy.docker_build_cmd" )
316416 @patch ("datacustomcode.deploy.docker_run_cmd" )
@@ -319,6 +419,7 @@ def test_prepare_dependency_archive_function_type(
319419 mock_docker_run_cmd ,
320420 mock_docker_build_cmd ,
321421 mock_makedirs ,
422+ mock_dirname ,
322423 mock_join ,
323424 mock_exists ,
324425 mock_temp_dir ,
@@ -337,10 +438,27 @@ def test_prepare_dependency_archive_function_type(
337438 # Mock cmd_output to return image ID (indicating image exists)
338439 mock_cmd_output .return_value = "abc123"
339440
340- # Mock os.path.join for py-files paths
441+ # Mock os.path.dirname to handle different calls
442+ def dirname_side_effect (path ):
443+ if path == "/test/dir" :
444+ return "/test"
445+ elif path == "payload/py-files" :
446+ return "payload"
447+ else :
448+ return path .rsplit ("/" , 1 )[0 ] if "/" in path else ""
449+
450+ mock_dirname .side_effect = dirname_side_effect
451+
452+ # Mock os.path.join for all paths
341453 def join_side_effect (* args ):
342- if args == ("/tmp/test_dir" , "py-files" ):
454+ if args == ("/test" , "requirements.txt" ):
455+ return "/test/requirements.txt"
456+ elif args == ("/test" , "build_native_dependencies.sh" ):
457+ return "/test/build_native_dependencies.sh"
458+ elif args == ("/tmp/test_dir" , "py-files" ):
343459 return "/tmp/test_dir/py-files"
460+ elif args == ("payload" , "py-files" ):
461+ return "payload/py-files"
344462 return "/" .join (args )
345463
346464 mock_join .side_effect = join_side_effect
@@ -367,13 +485,13 @@ def exists_side_effect(path):
367485 # Verify docker build command was not called (since image already exists)
368486 mock_docker_build_cmd .assert_not_called ()
369487
370- # Verify files were copied to temp directory
371- mock_copy .assert_any_call ("requirements.txt" , "/tmp/test_dir" )
372- mock_copy .assert_any_call ("build_native_dependencies.sh" , "/tmp/test_dir" )
488+ # Verify files were copied to temp directory from parent directory
489+ mock_copy .assert_any_call ("/test/ requirements.txt" , "/tmp/test_dir" )
490+ mock_copy .assert_any_call ("/test/ build_native_dependencies.sh" , "/tmp/test_dir" )
373491
374492 # Verify docker run command was called
375493 mock_docker_run_cmd .assert_called_once_with ("default" , "/tmp/test_dir" )
376- mock_cmd_output .assert_any_call ("mock run command" , env = ANY )
494+ mock_cmd_output .assert_any_call ("mock run command" , env = ANY , cwd = "/test" )
377495
378496 # Verify payload directory was created
379497 mock_makedirs .assert_called_once_with ("payload" , exist_ok = True )
@@ -391,6 +509,7 @@ def exists_side_effect(path):
391509 @patch ("datacustomcode.deploy.tempfile.TemporaryDirectory" )
392510 @patch ("datacustomcode.deploy.os.path.exists" )
393511 @patch ("datacustomcode.deploy.os.path.join" )
512+ @patch ("datacustomcode.deploy.os.path.dirname" )
394513 @patch ("datacustomcode.deploy.os.makedirs" )
395514 @patch ("datacustomcode.deploy.docker_build_cmd" )
396515 @patch ("datacustomcode.deploy.docker_run_cmd" )
@@ -399,6 +518,7 @@ def test_prepare_dependency_archive_function_type_missing_pyfiles(
399518 mock_docker_run_cmd ,
400519 mock_docker_build_cmd ,
401520 mock_makedirs ,
521+ mock_dirname ,
402522 mock_join ,
403523 mock_exists ,
404524 mock_temp_dir ,
@@ -418,9 +538,22 @@ def test_prepare_dependency_archive_function_type_missing_pyfiles(
418538 # Mock cmd_output to return image ID (indicating image exists)
419539 mock_cmd_output .return_value = "abc123"
420540
421- # Mock os.path.join for py-files path
541+ # Mock os.path.dirname to handle different calls
542+ def dirname_side_effect (path ):
543+ if path == "/test/dir" :
544+ return "/test"
545+ else :
546+ return path .rsplit ("/" , 1 )[0 ] if "/" in path else ""
547+
548+ mock_dirname .side_effect = dirname_side_effect
549+
550+ # Mock os.path.join for all paths
422551 def join_side_effect (* args ):
423- if args == ("/tmp/test_dir" , "py-files" ):
552+ if args == ("/test" , "requirements.txt" ):
553+ return "/test/requirements.txt"
554+ elif args == ("/test" , "build_native_dependencies.sh" ):
555+ return "/test/build_native_dependencies.sh"
556+ elif args == ("/tmp/test_dir" , "py-files" ):
424557 return "/tmp/test_dir/py-files"
425558 return "/" .join (args )
426559
0 commit comments