Skip to content

Commit 02fc200

Browse files
authored
Change install_collection to avoid use of -p (#233)
As passing -p (target collection path destination) break ansible-galaxy ability to find existing collection in other location, we avoid it and use the environment variable approach instead. Related: ansible/ansible-lint#3251
1 parent 18ddf94 commit 02fc200

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

src/ansible_compat/runtime.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -253,14 +253,19 @@ def install_collection(
253253
if matches and Version(matches[1]).is_prerelease:
254254
cmd.append("--pre")
255255

256-
if destination:
257-
cmd.extend(["-p", str(destination)])
256+
cpaths: List[str] = self.config.collections_paths
257+
if destination and str(destination) not in cpaths:
258+
# we cannot use '-p' because it breaks galaxy ability to ignore already installed collections, so
259+
# we hack ansible_collections_path instead and inject our own path there.
260+
# pylint: disable=no-member
261+
cpaths.insert(0, str(destination))
258262
cmd.append(f"{collection}")
259263

260264
_logger.info("Running from %s : %s", os.getcwd(), " ".join(cmd))
261265
run = self.exec(
262266
cmd,
263267
retry=True,
268+
env={**self.environ, ansible_collections_path(): ":".join(cpaths)},
264269
)
265270
if run.returncode != 0:
266271
msg = f"Command returned {run.returncode} code:\n{run.stdout}\n{run.stderr}"
@@ -351,15 +356,14 @@ def install_requirements(
351356
)
352357
else:
353358
cmd.extend(["-r", requirement])
354-
cpaths = ansible_collections_path().split(":")
359+
cpaths = self.config.collections_paths
355360
if self.cache_dir:
356361
# we cannot use '-p' because it breaks galaxy ability to ignore already installed collections, so
357362
# we hack ansible_collections_path instead and inject our own path there.
358363
dest_path = f"{self.cache_dir}/collections"
359-
cpaths = ansible_collections_path().split(":")
360364
if dest_path not in cpaths:
365+
# pylint: disable=no-member
361366
cpaths.insert(0, dest_path)
362-
# cmd.extend(["-p", f"{self.cache_dir}/collections"])
363367
_logger.info("Running %s", " ".join(cmd))
364368
result = self.exec(
365369
cmd,

tox.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ commands =
3737
# pytest users to run coverage when they just want to run a single test with `pytest -k test`
3838
coverage run -m pytest {posargs:}
3939
sh -c "coverage xml || true && coverage report"
40+
# We fail if files are modified at the end
41+
git diff --exit-code
4042
commands_pre =
4143
# safety measure to assure we do not accidentally run tests with broken dependencies
4244
{envpython} -m pip check
@@ -63,6 +65,7 @@ setenv =
6365
FORCE_COLOR = 1
6466
allowlist_externals =
6567
ansible
68+
git
6669
sh
6770

6871
[testenv:lint]

0 commit comments

Comments
 (0)