diff --git a/.vscode/settings.json b/.vscode/settings.json index fb13c49..6740457 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -7,7 +7,7 @@ "source.fixAll": "explicit", "source.organizeImports": "explicit" }, - "editor.defaultFormatter": "ms-python.black-formatter", + "editor.defaultFormatter": "charliermarsh.ruff", "editor.formatOnSave": true }, "flake8.importStrategy": "fromEnvironment", diff --git a/docs/index.md b/docs/index.md index 810fb61..acf5574 100644 --- a/docs/index.md +++ b/docs/index.md @@ -16,7 +16,8 @@ For more information about communication, see the [Ansible communication guide]( ## Features - Promotes an "ephemeral" development approach -- Ensures the current development environment is isolated +- Ensures the current development environment is isolated by using python virtual + environments and redefining ANSIBLE_HOME to point to inside them. - Install all collection python requirements - Install all collection test requirements - Checks for missing system packages diff --git a/src/ansible_dev_environment/cli.py b/src/ansible_dev_environment/cli.py index 47223af..58f1681 100644 --- a/src/ansible_dev_environment/cli.py +++ b/src/ansible_dev_environment/cli.py @@ -109,21 +109,26 @@ def ensure_isolated(self) -> None: self.output.hint(hint) errored = True - home_coll = Path.home() / ".ansible/collections/ansible_collections" - if home_coll.exists() and tuple(home_coll.iterdir()): - err = f"Collections found in {home_coll}" - self.output.error(err) - hint = "Run `rm -rf ~/.ansible/collections` to remove them." - self.output.hint(hint) - errored = True + ansible_home: Path + if "ANSIBLE_HOME" not in os.environ: + ansible_home = Path(os.environ.get("VIRTUAL_ENV", "~/.ansible")) + self.output.warning( + f"Declaring ANSIBLE_HOME={ansible_home} to ensure isolation from user environment.", + ) + os.environ["ANSIBLE_HOME"] = str(ansible_home) + else: + ansible_home = Path(os.environ.get("ANSIBLE_HOME", "~/.ansible")) + self.output.info( + f"Using ANSIBLE_HOME={ansible_home} to ensure isolation from user environment.", + ) - usr_coll = Path("/usr/share/ansible/collections") - if usr_coll.exists() and tuple(usr_coll.iterdir()): - err = f"Collections found in {usr_coll}" - self.output.error(err) - hint = "Run `sudo rm -rf /usr/share/ansible/collections` to remove them." - self.output.hint(hint) - errored = True + if "ANSIBLE_COLLECTIONS_SCAN_SYS_PATH" not in os.environ: + usr_coll = Path("/usr/share/ansible/collections") + if usr_coll.exists() and tuple(usr_coll.iterdir()): + err = f"Collections found in {usr_coll}, declaring ANSIBLE_COLLECTIONS_SCAN_SYS_PATH=False to ensure isolation." + self.output.error(err) + hint = "You can also `sudo rm -rf /usr/share/ansible/collections` to remove them or explicitly define ANSIBLE_COLLECTIONS_SCAN_SYS_PATH variable to avoid this message." + self.output.hint(hint) if errored: err = "The development environment is not isolated, please resolve the above errors." diff --git a/tests/unit/test_cli.py b/tests/unit/test_cli.py index 523728a..0567d18 100644 --- a/tests/unit/test_cli.py +++ b/tests/unit/test_cli.py @@ -163,11 +163,10 @@ def test_collections_in_home( monkeypatch.setenv("HOME", str(tmp_path)) collection_root = tmp_path / ".ansible" / "collections" / "ansible_collections" (collection_root / "ansible" / "utils").mkdir(parents=True) - with pytest.raises(SystemExit): - main(dry=True) + main(dry=True) captured = capsys.readouterr() - msg = f"Collections found in {collection_root}" - assert msg in captured.err + msg = "Declaring ANSIBLE_HOME=" + assert msg in captured.out def test_collections_in_user( @@ -219,8 +218,7 @@ def _iterdir(path: Path) -> list[Path] | Generator[Path, None, None]: "sys.argv", ["ansible-dev-environment", "install", "--venv", "venv"], ) - with pytest.raises(SystemExit): - main(dry=True) + main(dry=True) captured = capsys.readouterr() msg = f"Collections found in {usr_path}" assert msg in captured.err