Skip to content

Commit 10454d1

Browse files
authored
Allow user specified ansible-core version (#319)
1 parent 64d265e commit 10454d1

File tree

3 files changed

+74
-7
lines changed

3 files changed

+74
-7
lines changed

src/ansible_dev_environment/arg_parser.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
ENVVAR_MAPPING: dict[str, str] = {
3434
"ansi": "NO_COLOR",
35+
"ansible_core_version": "ADE_ANSIBLE_CORE_VERSION",
3536
"isolation_mode": "ADE_ISOLATION_MODE",
3637
"seed": "ADE_SEED",
3738
"uv": "ADE_UV",
@@ -211,6 +212,14 @@ def parse() -> argparse.Namespace:
211212
parents=[level2],
212213
help="Install a collection.",
213214
)
215+
216+
install.add_argument(
217+
"--acv",
218+
"--ansible-core-version",
219+
dest="ansible_core_version",
220+
help="Ansible core version to use. (e.g. --acv 2.19.0)",
221+
)
222+
214223
install.add_argument(
215224
"-e",
216225
"--editable",

src/ansible_dev_environment/subcommands/installer.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,7 @@ def run(self) -> None:
7373
err = "Multiple optional dependencies are not supported at this time."
7474
self._output.critical(err)
7575

76-
if self._config.args.seed:
77-
self._install_dev_tools()
78-
else:
79-
self._install_core()
76+
self._install_ade_deps()
8077

8178
if self._config.args.requirement or self._config.args.cpi:
8279
self._install_galaxy_requirements()
@@ -114,17 +111,34 @@ def run(self) -> None:
114111
)
115112
self._output.note(msg)
116113

114+
def _install_ade_deps(self) -> None:
115+
"""Install our dependencies."""
116+
core_version = getattr(self._config.args, "ansible_core_version", None)
117+
118+
if core_version:
119+
self._install_core()
120+
if self._config.args.seed:
121+
self._install_dev_tools()
122+
elif core_version is None:
123+
self._install_core()
124+
117125
def _install_core(self) -> None:
118126
"""Install ansible-core if not installed already."""
119-
msg = "Installing ansible-core."
120-
self._output.info(msg)
121-
122127
core = self._config.venv_bindir / "ansible"
123128
if core.exists():
129+
msg = "ansible-core is already installed."
130+
self._output.debug(msg)
124131
return
125132
msg = "Installing ansible-core."
126133
self._output.debug(msg)
127134
command = f"{self._config.venv_pip_install_cmd} ansible-core"
135+
136+
core_version = getattr(self._config.args, "ansible_core_version", None)
137+
if core_version:
138+
command += f"=={core_version}"
139+
msg = f"Using user specified ansible-core version: {core_version}"
140+
self._output.debug(msg)
141+
128142
try:
129143
subprocess_run(
130144
command=command,

tests/integration/test_basic.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from __future__ import annotations
44

55
import json
6+
import re
67
import sys
78

89
from pathlib import Path
@@ -244,3 +245,46 @@ def test_system_site_packages(
244245
captured = capsys.readouterr()
245246
assert "with system site packages" in captured.out
246247
assert "Installed collections include: ansible.utils" in captured.out
248+
249+
250+
def test_specified_core_version_pass(
251+
tmp_path: Path,
252+
monkeypatch: pytest.MonkeyPatch,
253+
) -> None:
254+
"""Install a user-specified core version.
255+
256+
Args:
257+
tmp_path: Temporary directory
258+
monkeypatch: Pytest monkeypatch
259+
"""
260+
term_features = TermFeatures(color=False, links=False)
261+
output = Output(
262+
log_file=f"/{tmp_path}/ansible-dev-environment.log",
263+
log_level="INFO",
264+
log_append="false",
265+
term_features=term_features,
266+
verbosity=0,
267+
)
268+
command = "pip index versions ansible-core"
269+
result = subprocess_run(command=command, verbose=True, msg="", output=output)
270+
271+
version_pattern = re.compile(r"\d+\.\d+\.\d+")
272+
versions = version_pattern.findall(result.stdout)
273+
second_latest = versions[2]
274+
275+
venv_path = tmp_path / ".venv"
276+
277+
monkeypatch.setattr(
278+
"sys.argv",
279+
[
280+
"ade",
281+
"install",
282+
f"--venv={venv_path}",
283+
f"--ansible-core-version={second_latest}",
284+
],
285+
)
286+
with pytest.raises(SystemExit):
287+
main()
288+
command = f"{venv_path}/bin/ansible --version"
289+
result = subprocess_run(command=command, verbose=True, msg="", output=output)
290+
assert second_latest in result.stdout

0 commit comments

Comments
 (0)