Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly handle cwd #53

Merged
merged 4 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions src/appenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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__":
Expand Down
6 changes: 3 additions & 3 deletions tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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"))
Expand All @@ -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"))
Expand Down
4 changes: 2 additions & 2 deletions tests/test_prepare.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions tests/test_reset.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@


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)
assert os.path.exists(str(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()
Expand Down
8 changes: 4 additions & 4 deletions tests/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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()

Expand All @@ -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()
Expand Down
6 changes: 3 additions & 3 deletions tests/test_update_lockfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
Expand Down Expand Up @@ -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")
Expand Down
Loading