From 9acecd07907bd20ea498c40c89c6427c081ea415 Mon Sep 17 00:00:00 2001 From: ANGkeith Date: Wed, 10 Apr 2024 15:19:52 +0800 Subject: [PATCH] fix: :zip:embedded source directory should read from sh_work_dir Signed-off-by: ANGkeith --- package.py | 8 +++++++ tests/test_package_toml.py | 47 +++++++++++++++++++++++++++++++++++++- 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/package.py b/package.py index d5ae6703..1e2c87d5 100644 --- a/package.py +++ b/package.py @@ -916,16 +916,24 @@ def execute(self, build_plan, zip_stream, query): # XXX: timestamp=0 - what actually do with it? zs.write_dirs(rd, prefix=prefix, timestamp=0) elif cmd == "sh": + r, w = os.pipe() + side_ch = os.fdopen(r) path, script = action[1:] + script = "{} && pwd >&{}".format(script, w) p = subprocess.Popen( script, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=path, + pass_fds=(w,), ) p.wait() + os.close(w) + sh_work_dir = side_ch.read().strip() + log.info("WD: %s", sh_work_dir) + side_ch.close() call_stdout, call_stderr = p.communicate() exit_code = p.returncode log.info("exit_code: %s", exit_code) diff --git a/tests/test_package_toml.py b/tests/test_package_toml.py index 4861c6f4..6bf57762 100644 --- a/tests/test_package_toml.py +++ b/tests/test_package_toml.py @@ -1,6 +1,7 @@ +import os from package import get_build_system_from_pyproject_toml, BuildPlanManager from pytest import raises -from unittest.mock import Mock +from unittest.mock import MagicMock, Mock def test_get_build_system_from_pyproject_toml_inexistent(): @@ -39,3 +40,47 @@ def test_get_build_system_from_pyproject_toml_poetry(): ) == "poetry" ) + + +def test_zip_source_path_sh_work_dir(): + zs = Mock() + zs.write_dirs = MagicMock() + + bpm = BuildPlanManager(args=Mock()) + bpm._zip_write_with_filter = MagicMock() + + bpm.execute( + build_plan=[ + ["sh", ".", "cd $(mktemp -d)\n echo pip install"], + ["zip:embedded", ".", "./python"], + ], + zip_stream=zs, + query=None, + ) + + zs.write_dirs.assert_called_once() + + zip_source_path = zs.write_dirs.call_args_list[0][0][0] + assert zip_source_path != f"{os.getcwd()}" + + +def test_zip_source_path(): + zs = Mock() + zs.write_dirs = MagicMock() + + bpm = BuildPlanManager(args=Mock()) + bpm._zip_write_with_filter = MagicMock() + + bpm.execute( + build_plan=[ + ["sh", ".", "echo pip install"], + ["zip:embedded", ".", "./python"], + ], + zip_stream=zs, + query=None, + ) + + zs.write_dirs.assert_called_once() + + zip_source_path = zs.write_dirs.call_args_list[0][0][0] + assert zip_source_path == f"{os.getcwd()}"