Skip to content

Commit

Permalink
🐛 constraint venv state
Browse files Browse the repository at this point in the history
  • Loading branch information
juftin committed Feb 1, 2024
1 parent 18a8a96 commit b44d9e9
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
32 changes: 28 additions & 4 deletions hatch_pip_compile/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ def run_pip_compile(self) -> None:
"""
Run pip-compile if necessary
"""
self.prepare_environment()
if not self.lockfile_up_to_date:
with self.safe_activation():
self.install_pip_tools()
Expand Down Expand Up @@ -323,10 +324,6 @@ def constraint_env(self) -> "PipCompileEnvironment":
elif not environment.dependencies:
return self
else:
try:
_ = environment.virtual_env.executables_directory
except OSError:
environment.create()
return environment

def validate_constraints_file(
Expand Down Expand Up @@ -378,3 +375,30 @@ def plugin_check_command(
shell=shell,
**kwargs,
)

def virtualenv_exists(self) -> bool:
"""
Check if the virtualenv exists
"""
try:
_ = self.virtual_env.executables_directory
return True
except OSError:
return False

def prepare_environment(self) -> None:
"""
Prepare the environment
Ideally, hatch.cli.Application.prepare_environment would be called
but the `Application` class is not exposed to the environment plugin.
"""
if not self.virtualenv_exists():
self.create()
if not self.dependencies_in_sync():
self.sync_dependencies()
if not self.skip_install:
if self.dev_mode:
self.install_project_dev_mode()
else:
self.install_project()
1 change: 1 addition & 0 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ def test_dependencies_in_sync(pip_compile: PipCompileFixture) -> None:
"""
Test the `dependencies_in_sync` method
"""
pip_compile.default_environment.create()
assert pip_compile.default_environment.lockfile_up_to_date is True
assert pip_compile.default_environment.dependencies_in_sync() is False
pip_compile.application.prepare_environment(pip_compile.default_environment)
Expand Down
20 changes: 20 additions & 0 deletions tests/test_integration_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,23 @@ def test_pip_compile_disable_cli(pip_compile: PipCompileFixture, environment_nam
)
assert result.exit_code == 1
assert isinstance(result.exception, HatchPipCompileError)


def test_prune_removes_all_environments(pip_compile: PipCompileFixture) -> None:
"""
Assert that running `hatch env prune` removes all environments
"""
runner = CliRunner()
pip_compile.default_environment.create()
pip_compile.test_environment.create()
venv_dir = pip_compile.isolation / ".venv"
assert venv_dir.exists()
assert len(list(venv_dir.iterdir())) == 2
with runner.isolated_filesystem(pip_compile.isolation):
result = runner.invoke(
hatch.cli.hatch,
args=["env", "prune"],
)
assert result.exit_code == 0
if venv_dir.exists():
assert len(list(venv_dir.iterdir())) == 0

0 comments on commit b44d9e9

Please sign in to comment.