8
8
9
9
from typing import Dict , Type
10
10
11
+ import hatch
11
12
import packaging .requirements
12
13
import pytest
13
14
14
15
from hatch_pip_compile .installer import PluginInstaller
15
16
from tests .conftest import PipCompileFixture
16
17
18
+ try :
19
+ match_major , hatch_minor , _ = hatch ._version .__version__ .split ("." )
20
+ except AttributeError :
21
+ match_major , hatch_minor , _ = hatch .__about__ .__version__ .split ("." )
22
+
17
23
18
24
@pytest .mark .parametrize ("installer" , ["pip" , "pip-sync" ])
19
25
def test_new_dependency (
@@ -34,6 +40,11 @@ def test_new_dependency(
34
40
assert updated_environment .lockfile_up_to_date is True
35
41
new_lockfile_requirements = pip_compile .default_environment .piptools_lock .read_requirements ()
36
42
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 ()
37
48
38
49
39
50
@pytest .mark .parametrize ("installer" , ["pip" , "pip-sync" ])
@@ -69,3 +80,68 @@ def test_create_constraint_environment(pip_compile: PipCompileFixture) -> None:
69
80
pip_compile .application .prepare_environment (environment = test_environment )
70
81
new_lockfile_requirements = pip_compile .default_environment .piptools_lock .read_requirements ()
71
82
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
0 commit comments