3
3
from __future__ import annotations
4
4
5
5
import argparse
6
+ import shutil
6
7
8
+ from pathlib import Path
7
9
from typing import TYPE_CHECKING
8
10
9
11
import pytest
10
12
11
13
from ansible_dev_environment .config import Config
12
- from ansible_dev_environment .output import Output
13
- from ansible_dev_environment .utils import TermFeatures
14
14
15
15
16
16
if TYPE_CHECKING :
17
- from pathlib import Path
17
+ from ansible_dev_environment .output import Output
18
+
19
+
20
+ def gen_args (
21
+ venv : str ,
22
+ system_site_packages : bool = False , # noqa: FBT001, FBT002
23
+ ) -> argparse .Namespace :
24
+ """Generate the arguments.
25
+
26
+ Args:
27
+ venv: The virtual environment.
28
+ system_site_packages: Whether to include system site packages.
29
+
30
+ Returns:
31
+ The arguments.
32
+ """
33
+ return argparse .Namespace (
34
+ verbose = 0 ,
35
+ venv = venv ,
36
+ system_site_packages = system_site_packages ,
37
+ )
18
38
19
39
20
40
@pytest .mark .parametrize (
21
41
"system_site_packages" ,
22
42
((True , False )),
23
43
ids = ["ssp_true" , "ssp_false" ],
24
44
)
25
- def test_paths (tmpdir : Path , system_site_packages : bool ) -> None : # noqa: FBT001
45
+ def test_paths (
46
+ tmpdir : Path ,
47
+ system_site_packages : bool , # noqa: FBT001
48
+ output : Output ,
49
+ ) -> None :
26
50
"""Test the paths.
27
51
28
52
Several of the found directories should have a parent of the tmpdir / test_venv
29
53
30
54
Args:
31
55
tmpdir: A temporary directory.
32
56
system_site_packages: Whether to include system site packages.
57
+ output: The output fixture.
33
58
"""
34
59
venv = tmpdir / "test_venv"
35
- args = argparse . Namespace (
60
+ args = gen_args (
36
61
venv = str (venv ),
37
62
system_site_packages = system_site_packages ,
38
- verbose = 0 ,
39
- )
40
- term_features = TermFeatures (color = False , links = False )
41
-
42
- output = Output (
43
- log_file = str (tmpdir / "test_log.log" ),
44
- log_level = "debug" ,
45
- log_append = "false" ,
46
- term_features = term_features ,
47
- verbosity = 0 ,
48
63
)
49
64
50
- config = Config (args = args , output = output , term_features = term_features )
65
+ config = Config (args = args , output = output , term_features = output . term_features )
51
66
config .init ()
52
67
53
68
assert config .venv == venv
@@ -59,3 +74,172 @@ def test_paths(tmpdir: Path, system_site_packages: bool) -> None: # noqa: FBT00
59
74
"venv_interpreter" ,
60
75
):
61
76
assert venv in getattr (config , attr ).parents
77
+
78
+
79
+ def test_galaxy_bin_venv (
80
+ tmpdir : Path ,
81
+ monkeypatch : pytest .MonkeyPatch ,
82
+ output : Output ,
83
+ ) -> None :
84
+ """Test the galaxy_bin property found in venv.
85
+
86
+ Args:
87
+ tmpdir: A temporary directory.
88
+ monkeypatch: A pytest fixture for monkey patching.
89
+ output: The output fixture.
90
+ """
91
+ venv = tmpdir / "test_venv"
92
+ args = gen_args (venv = str (venv ))
93
+
94
+ config = Config (args = args , output = output , term_features = output .term_features )
95
+ config .init ()
96
+
97
+ orig_exists = Path .exists
98
+ exists_called = False
99
+
100
+ def _exists (path : Path ) -> bool :
101
+ if path .name != "ansible-galaxy" :
102
+ return orig_exists (path )
103
+ if path .parent == config .venv_bindir :
104
+ nonlocal exists_called
105
+ exists_called = True
106
+ return True
107
+ return False
108
+
109
+ monkeypatch .setattr (Path , "exists" , _exists )
110
+
111
+ assert config .galaxy_bin == venv / "bin" / "ansible-galaxy"
112
+ assert exists_called
113
+
114
+
115
+ def test_galaxy_bin_site (
116
+ tmpdir : Path ,
117
+ monkeypatch : pytest .MonkeyPatch ,
118
+ output : Output ,
119
+ ) -> None :
120
+ """Test the galaxy_bin property found in site.
121
+
122
+ Args:
123
+ tmpdir: A temporary directory.
124
+ monkeypatch: A pytest fixture for monkey patching.
125
+ output: The output fixture.
126
+ """
127
+ venv = tmpdir / "test_venv"
128
+ args = gen_args (venv = str (venv ))
129
+
130
+ config = Config (args = args , output = output , term_features = output .term_features )
131
+ config .init ()
132
+
133
+ orig_exists = Path .exists
134
+ exists_called = False
135
+
136
+ def _exists (path : Path ) -> bool :
137
+ if path .name != "ansible-galaxy" :
138
+ return orig_exists (path )
139
+ if path .parent == config .site_pkg_path / "bin" :
140
+ nonlocal exists_called
141
+ exists_called = True
142
+ return True
143
+ return False
144
+
145
+ monkeypatch .setattr (Path , "exists" , _exists )
146
+
147
+ assert config .galaxy_bin == config .site_pkg_path / "bin" / "ansible-galaxy"
148
+ assert exists_called
149
+
150
+
151
+ def test_galaxy_bin_path (
152
+ tmpdir : Path ,
153
+ monkeypatch : pytest .MonkeyPatch ,
154
+ output : Output ,
155
+ ) -> None :
156
+ """Test the galaxy_bin property found in path.
157
+
158
+ Args:
159
+ tmpdir: A temporary directory.
160
+ monkeypatch: A pytest fixture for monkey patching.
161
+ output: The output fixture.
162
+ """
163
+ venv = tmpdir / "test_venv"
164
+ args = gen_args (venv = str (venv ))
165
+
166
+ config = Config (args = args , output = output , term_features = output .term_features )
167
+ config .init ()
168
+
169
+ orig_exists = Path .exists
170
+ exists_called = False
171
+
172
+ def _exists (path : Path ) -> bool :
173
+ if path .name != "ansible-galaxy" :
174
+ return orig_exists (path )
175
+ nonlocal exists_called
176
+ exists_called = True
177
+ return False
178
+
179
+ monkeypatch .setattr (Path , "exists" , _exists )
180
+
181
+ orig_which = shutil .which
182
+ which_called = False
183
+
184
+ def _which (name : str ) -> str | None :
185
+ if not name .endswith ("ansible-galaxy" ):
186
+ return orig_which (name )
187
+ nonlocal which_called
188
+ which_called = True
189
+ return "patched"
190
+
191
+ monkeypatch .setattr (shutil , "which" , _which )
192
+
193
+ assert config .galaxy_bin == Path ("patched" )
194
+ assert exists_called
195
+ assert which_called
196
+
197
+
198
+ def test_galaxy_bin_not_found (
199
+ tmpdir : Path ,
200
+ monkeypatch : pytest .MonkeyPatch ,
201
+ output : Output ,
202
+ ) -> None :
203
+ """Test the galaxy_bin property found in venv.
204
+
205
+ Args:
206
+ tmpdir: A temporary directory.
207
+ monkeypatch: A pytest fixture for monkey patching.
208
+ output: The output fixture.
209
+ """
210
+ venv = tmpdir / "test_venv"
211
+ args = gen_args (venv = str (venv ))
212
+
213
+ config = Config (args = args , output = output , term_features = output .term_features )
214
+ config .init ()
215
+
216
+ orig_exists = Path .exists
217
+ exist_called = False
218
+
219
+ def _exists (path : Path ) -> bool :
220
+ if path .name == "ansible-galaxy" :
221
+ nonlocal exist_called
222
+ exist_called = True
223
+ return False
224
+ return orig_exists (path )
225
+
226
+ monkeypatch .setattr (Path , "exists" , _exists )
227
+
228
+ orig_which = shutil .which
229
+ which_called = False
230
+
231
+ def _which (name : str ) -> str | None :
232
+ if name .endswith ("ansible-galaxy" ):
233
+ nonlocal which_called
234
+ which_called = True
235
+ return None
236
+ return orig_which (name )
237
+
238
+ monkeypatch .setattr (shutil , "which" , _which )
239
+
240
+ with pytest .raises (SystemExit ) as exc :
241
+ assert config .galaxy_bin is None
242
+
243
+ assert exc .value .code == 1
244
+ assert exist_called
245
+ assert which_called
0 commit comments