Skip to content

Commit a1cb08e

Browse files
committed
🧪 integration tests
1 parent a48579c commit a1cb08e

File tree

3 files changed

+171
-0
lines changed

3 files changed

+171
-0
lines changed

tests/conftest.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ def __post_init__(self) -> None:
104104
interactive=False,
105105
enable_color=False,
106106
)
107+
self.application.data_dir = self.isolated_data_dir
108+
self.application.project = self.project
107109
self.default_environment = self.reload_environment("default")
108110
self.test_environment = self.reload_environment("test")
109111

tests/test_integration.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,18 @@
88

99
from typing import Dict, Type
1010

11+
import hatch
1112
import packaging.requirements
1213
import pytest
1314

1415
from hatch_pip_compile.installer import PluginInstaller
1516
from tests.conftest import PipCompileFixture
1617

18+
try:
19+
match_major, hatch_minor, _ = hatch._version.__version__.split(".")
20+
except AttributeError:
21+
match_major, hatch_minor, _ = hatch.__about__.__version__.split(".")
22+
1723

1824
@pytest.mark.parametrize("installer", ["pip", "pip-sync"])
1925
def test_new_dependency(
@@ -34,6 +40,11 @@ def test_new_dependency(
3440
assert updated_environment.lockfile_up_to_date is True
3541
new_lockfile_requirements = pip_compile.default_environment.piptools_lock.read_requirements()
3642
assert new_lockfile_requirements == [packaging.requirements.Requirement("requests")]
43+
result = updated_environment.plugin_check_command(
44+
command=["python", "-m", "pip", "list"],
45+
capture_output=True,
46+
)
47+
assert "requests" in result.stdout.decode()
3748

3849

3950
@pytest.mark.parametrize("installer", ["pip", "pip-sync"])
@@ -69,3 +80,68 @@ def test_create_constraint_environment(pip_compile: PipCompileFixture) -> None:
6980
pip_compile.application.prepare_environment(environment=test_environment)
7081
new_lockfile_requirements = pip_compile.default_environment.piptools_lock.read_requirements()
7182
assert new_lockfile_requirements == [packaging.requirements.Requirement("requests")]
83+
result = test_environment.plugin_check_command(
84+
command=["python", "-m", "pip", "list"],
85+
capture_output=True,
86+
)
87+
assert "pytest" in result.stdout.decode()
88+
89+
90+
def test_dependency_uninstalled(pip_compile: PipCompileFixture) -> None:
91+
"""
92+
An environment is prepared, then a dependency is uninstalled,
93+
the environment should be out of sync even though the lockfile
94+
is good
95+
"""
96+
pip_compile.application.prepare_environment(environment=pip_compile.test_environment)
97+
list_result = pip_compile.test_environment.plugin_check_command(
98+
command=["python", "-m", "pip", "list"],
99+
capture_output=True,
100+
)
101+
assert "pytest" in list_result.stdout.decode()
102+
assert pip_compile.test_environment.dependencies_in_sync() is True
103+
pip_compile.test_environment.plugin_check_command(
104+
command=["python", "-m", "pip", "uninstall", "pytest", "pytest-cov", "-y"],
105+
)
106+
new_list_result = pip_compile.test_environment.plugin_check_command(
107+
command=["python", "-m", "pip", "list"],
108+
capture_output=True,
109+
)
110+
assert "pytest" not in new_list_result.stdout.decode()
111+
assert pip_compile.test_environment.lockfile_up_to_date is True
112+
assert pip_compile.test_environment.dependencies_in_sync() is False
113+
114+
115+
def test_lockfile_missing(pip_compile: PipCompileFixture) -> None:
116+
"""
117+
Lockfile missing on previously prepared environment
118+
"""
119+
# Prepare the test environment, assert it is in sync
120+
pip_compile.application.prepare_environment(environment=pip_compile.test_environment)
121+
assert pip_compile.test_environment.dependencies_in_sync() is True
122+
# Delete the lockfile, assert environment is in sync but lockfile is missing
123+
pip_compile.test_environment.piptools_lock_file.unlink()
124+
updated_environment = pip_compile.reload_environment("test")
125+
list_result = updated_environment.plugin_check_command(
126+
command=["python", "-m", "pip", "list"],
127+
capture_output=True,
128+
)
129+
assert "pytest" in list_result.stdout.decode()
130+
assert updated_environment.dependencies_in_sync() is False
131+
# Prepare the environment again, assert it is in sync
132+
pip_compile.application.prepare_environment(environment=updated_environment)
133+
new_updated_environment = pip_compile.reload_environment("test")
134+
assert new_updated_environment.dependencies_in_sync() is True
135+
assert new_updated_environment.piptools_lock_file.exists() is True
136+
137+
138+
@pytest.mark.skipif(match_major == "1" and hatch_minor == "7", reason="hatch 1.8.0+ required")
139+
def test_check_dependency_hash_creates_lock(pip_compile: PipCompileFixture) -> None:
140+
"""
141+
Calling `dependency_hash` creates a lockfile when one does not exist
142+
"""
143+
pip_compile.application.prepare_environment(environment=pip_compile.default_environment)
144+
pip_compile.default_environment.piptools_lock_file.unlink()
145+
updated_environment = pip_compile.reload_environment("default")
146+
_ = updated_environment.dependency_hash()
147+
assert updated_environment.piptools_lock_file.exists() is True

tests/test_integration_cli.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
"""
2+
Integration Tests using the CLI
3+
"""
4+
5+
import hatch.cli
6+
import hatch.cli.env
7+
from click.testing import CliRunner
8+
9+
from tests.conftest import PipCompileFixture
10+
11+
12+
def test_invoke_environment_creates_env(pip_compile: PipCompileFixture) -> None:
13+
"""
14+
Test using the CLI runner
15+
"""
16+
runner = CliRunner()
17+
environment = pip_compile.test_environment
18+
venv = environment.virtual_env.directory
19+
assert not venv.exists()
20+
with runner.isolated_filesystem(pip_compile.isolation):
21+
result = runner.invoke(
22+
hatch.cli.hatch,
23+
args=["env", "run", "--env", environment.name, "--", "python", "--version"],
24+
)
25+
assert result.exit_code == 0
26+
assert venv.exists()
27+
28+
29+
def test_missing_lockfile_created(pip_compile: PipCompileFixture) -> None:
30+
"""
31+
Running the CLI without a lockfile creates one
32+
"""
33+
runner = CliRunner()
34+
environment = pip_compile.default_environment
35+
venv = environment.virtual_env.directory
36+
environment.piptools_lock_file.unlink()
37+
assert not environment.piptools_lock_file.exists()
38+
with runner.isolated_filesystem(pip_compile.isolation):
39+
result = runner.invoke(
40+
hatch.cli.hatch,
41+
args=["env", "run", "--env", environment.name, "--", "python", "--version"],
42+
)
43+
assert result.exit_code == 0
44+
assert environment.piptools_lock_file.exists()
45+
assert venv.exists()
46+
47+
48+
def test_constraint_env_created(pip_compile: PipCompileFixture) -> None:
49+
"""
50+
Running the CLI with a constraint env creates one
51+
"""
52+
runner = CliRunner()
53+
environment = pip_compile.test_environment
54+
environment.piptools_lock_file.unlink()
55+
environment.constraint_env.piptools_lock_file.unlink()
56+
with runner.isolated_filesystem(pip_compile.isolation):
57+
result = runner.invoke(
58+
hatch.cli.hatch,
59+
args=["env", "run", "--env", environment.name, "--", "python", "--version"],
60+
)
61+
assert result.exit_code == 0
62+
assert environment.piptools_lock_file.exists()
63+
assert environment.constraint_env.piptools_lock_file.exists()
64+
assert environment.virtual_env.directory.exists()
65+
assert environment.constraint_env.virtual_env.directory.exists()
66+
67+
68+
def test_missing_lockfile_after_prepared(pip_compile: PipCompileFixture) -> None:
69+
"""
70+
After an environment is prepared the lockfile is deleted and recreated the next time
71+
"""
72+
runner = CliRunner()
73+
environment = pip_compile.default_environment
74+
# Create the environment the first time
75+
with runner.isolated_filesystem(pip_compile.isolation):
76+
result = runner.invoke(
77+
hatch.cli.hatch,
78+
args=["env", "run", "--env", environment.name, "--", "python", "--version"],
79+
)
80+
assert result.exit_code == 0
81+
# Delete the lockfile
82+
assert environment.piptools_lock_file.exists()
83+
environment.piptools_lock_file.unlink()
84+
assert not environment.piptools_lock_file.exists()
85+
# Run the environment again
86+
with runner.isolated_filesystem(pip_compile.isolation):
87+
result = runner.invoke(
88+
hatch.cli.hatch,
89+
args=["env", "run", "--env", environment.name, "--", "python", "--version"],
90+
)
91+
assert result.exit_code == 0
92+
# Assert the lockfile was recreated
93+
assert environment.piptools_lock_file.exists()

0 commit comments

Comments
 (0)