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__": 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_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_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..9ea38a8 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") @@ -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.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')) + env = appenv.AppEnv(os.path.join(workdir, 'ppytest'), os.getcwd()) env.init() requirements_file = os.path.join(workdir, "ppytest", "requirements.txt")