From a81d6df64f676d5215ac350ca6b0bcb3eaf8c529 Mon Sep 17 00:00:00 2001 From: Eli Kogan-Wang Date: Mon, 3 Jun 2024 10:15:44 +0200 Subject: [PATCH 1/4] Correctly handle cwd --- src/appenv.py | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/appenv.py b/src/appenv.py index 7e943e7..24cb396 100755 --- a/src/appenv.py +++ b/src/appenv.py @@ -307,7 +307,7 @@ class AppEnv(object): env_dir = None # The current specific venv that we're working with. appenv_dir = None # The directory where to place specific venvs. - def __init__(self, base): + def __init__(self, base, original_cwd): self.base = base # This used to be computed based on the application name but @@ -318,7 +318,7 @@ def __init__(self, base): # Allow simplifying a lot of code by assuming that all the # meta-operations happen in the base directory. Store the original # working directory here so we switch back at the appropriate time. - self.original_cwd = os.path.abspath(os.curdir) + self.original_cwd = original_cwd def meta(self): # Parse the appenv arguments @@ -565,6 +565,7 @@ def update_lockfile(self, args=None, remaining=None): def main(): base = os.path.dirname(__file__) + original_cwd = os.getcwd() ensure_best_python(base) # clear PYTHONPATH variable to get a defined environment @@ -576,14 +577,11 @@ def main(): # Determine whether we're being called as appenv or as an application name application_name = os.path.splitext(os.path.basename(__file__))[0] - appenv = AppEnv(base) - try: - if application_name == 'appenv': - appenv.meta() - else: - appenv.run(application_name, sys.argv[1:]) - finally: - os.chdir(appenv.original_cwd) + appenv = AppEnv(base, original_cwd) + if application_name == 'appenv': + appenv.meta() + else: + appenv.run(application_name, sys.argv[1:]) if __name__ == "__main__": From 86ffd9cb3d9d722f96979ece2fdbeade8f380dcb Mon Sep 17 00:00:00 2001 From: Eli Kogan-Wang Date: Mon, 3 Jun 2024 10:18:56 +0200 Subject: [PATCH 2/4] augment Appenv class constructor call with os.getcwd() in tests --- tests/test_init.py | 6 +++--- tests/test_prepare.py | 4 ++-- tests/test_run.py | 8 ++++---- tests/test_update_lockfile.py | 2 +- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/tests/test_init.py b/tests/test_init.py index c900c94..704d87b 100644 --- a/tests/test_init.py +++ b/tests/test_init.py @@ -9,7 +9,7 @@ def test_init(workdir, monkeypatch): assert not os.path.exists(os.path.join(workdir, "ducker")) - env = appenv.AppEnv(os.path.join(workdir, 'ducker')) + env = appenv.AppEnv(os.path.join(workdir, 'ducker'), os.getcwd()) env.init() assert os.readlink(os.path.join(workdir, "ducker", "ducker")) == 'appenv' @@ -46,7 +46,7 @@ def test_init(workdir, monkeypatch): def test_init_explicit_target(workdir, monkeypatch): monkeypatch.setattr("sys.stdin", io.StringIO("ducker\n\nbaz\n")) - env = appenv.AppEnv(os.path.join(workdir, 'ducker')) + env = appenv.AppEnv(os.path.join(workdir, 'ducker'), os.getcwd()) env.init() assert os.path.exists(os.path.join(workdir, "baz")) @@ -65,7 +65,7 @@ def test_init_explicit_target(workdir, monkeypatch): def test_init_explicit_package_and_target(workdir, monkeypatch): monkeypatch.setattr("sys.stdin", io.StringIO("foo\nbar\nbaz\n")) - env = appenv.AppEnv(os.path.join(workdir, 'ducker')) + env = appenv.AppEnv(os.path.join(workdir, 'ducker'), os.getcwd()) env.init() assert os.path.exists(os.path.join(workdir, "baz")) diff --git a/tests/test_prepare.py b/tests/test_prepare.py index e8b37c1..e199950 100644 --- a/tests/test_prepare.py +++ b/tests/test_prepare.py @@ -7,7 +7,7 @@ def test_prepare_creates_envdir(workdir, monkeypatch): monkeypatch.setattr('sys.stdin', io.StringIO('ducker\nducker<2.0.2\n\n')) os.makedirs(os.path.join(workdir, 'ducker')) - env = appenv.AppEnv(os.path.join(workdir, 'ducker')) + env = appenv.AppEnv(os.path.join(workdir, 'ducker'), os.getcwd()) env.init() assert not os.path.exists(env.appenv_dir) env.update_lockfile() @@ -20,7 +20,7 @@ def test_prepare_creates_venv_symlink(workdir, monkeypatch): monkeypatch.setattr('sys.stdin', io.StringIO('ducker\nducker<2.0.2\n\n')) os.makedirs(os.path.join(workdir, 'ducker')) - env = appenv.AppEnv(os.path.join(workdir, 'ducker')) + env = appenv.AppEnv(os.path.join(workdir, 'ducker'), os.getcwd()) env.init() env.update_lockfile() env.prepare() diff --git a/tests/test_run.py b/tests/test_run.py index 3615d11..8607220 100644 --- a/tests/test_run.py +++ b/tests/test_run.py @@ -18,7 +18,7 @@ def test_bootstrap_lockfile_missing_dependency(): def test_bootstrap_and_run_with_lockfile(workdir, monkeypatch): monkeypatch.setattr("sys.stdin", io.StringIO("ducker\nducker==2.0.1\n\n")) - env = appenv.AppEnv(os.path.join(workdir, 'ducker')) + env = appenv.AppEnv(os.path.join(workdir, 'ducker'), os.getcwd()) env.init() env.update_lockfile() @@ -37,7 +37,7 @@ def test_bootstrap_and_run_with_lockfile(workdir, monkeypatch): def test_bootstrap_and_run_python_with_lockfile(workdir, monkeypatch): monkeypatch.setattr("sys.stdin", io.StringIO("ducker\nducker==2.0.1\n\n")) - env = appenv.AppEnv(os.path.join(workdir, 'ducker')) + env = appenv.AppEnv(os.path.join(workdir, 'ducker'), os.getcwd()) env.init() env.update_lockfile() @@ -57,7 +57,7 @@ def test_bootstrap_and_run_without_lockfile(workdir, monkeypatch): """It raises as error if no requirements.lock is present.""" monkeypatch.setattr("sys.stdin", io.StringIO("ducker\nducker==2.0.1\n\n")) - env = appenv.AppEnv(os.path.join(workdir, 'ducker')) + env = appenv.AppEnv(os.path.join(workdir, 'ducker'), os.getcwd()) env.init() @@ -78,7 +78,7 @@ def test_bootstrap_and_run_without_lockfile(workdir, monkeypatch): def test_bootstrap_and_run_with_outdated_lockfile(workdir, monkeypatch): monkeypatch.setattr("sys.stdin", io.StringIO("ducker\nducker==2.0.1\n\n")) - env = appenv.AppEnv(os.path.join(workdir, 'ducker')) + env = appenv.AppEnv(os.path.join(workdir, 'ducker'), os.getcwd()) env.init() env.update_lockfile() diff --git a/tests/test_update_lockfile.py b/tests/test_update_lockfile.py index 9c732c7..4320c85 100644 --- a/tests/test_update_lockfile.py +++ b/tests/test_update_lockfile.py @@ -10,7 +10,7 @@ def test_init_and_create_lockfile(workdir, monkeypatch): monkeypatch.setattr('sys.stdin', io.StringIO('ducker\nducker<2.0.2\n\n')) - env = appenv.AppEnv(os.path.join(workdir, 'ducker')) + env = appenv.AppEnv(os.path.join(workdir, 'ducker'), os.getcwd()) env.init() lockfile = os.path.join(workdir, "ducker", "requirements.lock") From 4a1351669b0afe3893b458df27f7d4801f5cb6a3 Mon Sep 17 00:00:00 2001 From: Eli Kogan-Wang Date: Mon, 3 Jun 2024 10:39:32 +0200 Subject: [PATCH 3/4] Fix AppEnv initialization in test_reset.py and test_update_lockfile.py --- tests/test_reset.py | 4 ++-- tests/test_update_lockfile.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test_reset.py b/tests/test_reset.py index 34f6e80..a93d5a9 100644 --- a/tests/test_reset.py +++ b/tests/test_reset.py @@ -4,7 +4,7 @@ def test_reset_nonexisting_envdir_silent(tmpdir): - env = appenv.AppEnv(os.path.join(tmpdir, 'ducker')) + env = appenv.AppEnv(os.path.join(tmpdir, 'ducker'), os.getcwd()) assert not os.path.exists(env.appenv_dir) env.reset() assert not os.path.exists(env.appenv_dir) @@ -12,7 +12,7 @@ def test_reset_nonexisting_envdir_silent(tmpdir): def test_reset_removes_envdir_with_subdirs(tmpdir): - env = appenv.AppEnv(os.path.join(tmpdir, 'ducker')) + env = appenv.AppEnv(os.path.join(tmpdir, 'ducker'), os.getcwd()) os.makedirs(env.appenv_dir) assert os.path.exists(env.appenv_dir) env.reset() diff --git a/tests/test_update_lockfile.py b/tests/test_update_lockfile.py index 4320c85..00ae116 100644 --- a/tests/test_update_lockfile.py +++ b/tests/test_update_lockfile.py @@ -34,7 +34,7 @@ def test_update_lockfile_minimal_python(workdir, monkeypatch): monkeypatch.setattr('sys.stdin', io.StringIO('pytest\npytest==6.1.2\nppytest\n')) - env = appenv.AppEnv(os.path.join(workdir, 'ppytest')) + env = appenv.AppEnv(os.path.join(workdir, 'ppytest'), os.getwd()) env.init() lockfile = os.path.join(workdir, "ppytest", "requirements.lock") @@ -66,7 +66,7 @@ def test_update_lockfile_missing_minimal_python(workdir, monkeypatch): monkeypatch.setattr('sys.stdin', io.StringIO('pytest\npytest==6.1.2\nppytest\n')) - env = appenv.AppEnv(os.path.join(workdir, 'ppytest')) + env = appenv.AppEnv(os.path.join(workdir, 'ppytest'), os.getwd()) env.init() requirements_file = os.path.join(workdir, "ppytest", "requirements.txt") From 02494eff20810e2281a67b8b795eb57a16781f80 Mon Sep 17 00:00:00 2001 From: Eli Kogan-Wang Date: Mon, 3 Jun 2024 10:43:22 +0200 Subject: [PATCH 4/4] Fix typo: getwd -> getcwd --- tests/test_update_lockfile.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_update_lockfile.py b/tests/test_update_lockfile.py index 00ae116..9ea38a8 100644 --- a/tests/test_update_lockfile.py +++ b/tests/test_update_lockfile.py @@ -34,7 +34,7 @@ def test_update_lockfile_minimal_python(workdir, monkeypatch): monkeypatch.setattr('sys.stdin', io.StringIO('pytest\npytest==6.1.2\nppytest\n')) - env = appenv.AppEnv(os.path.join(workdir, 'ppytest'), os.getwd()) + env = appenv.AppEnv(os.path.join(workdir, 'ppytest'), os.getcwd()) env.init() lockfile = os.path.join(workdir, "ppytest", "requirements.lock") @@ -66,7 +66,7 @@ def test_update_lockfile_missing_minimal_python(workdir, monkeypatch): monkeypatch.setattr('sys.stdin', io.StringIO('pytest\npytest==6.1.2\nppytest\n')) - env = appenv.AppEnv(os.path.join(workdir, 'ppytest'), os.getwd()) + env = appenv.AppEnv(os.path.join(workdir, 'ppytest'), os.getcwd()) env.init() requirements_file = os.path.join(workdir, "ppytest", "requirements.txt")