Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change cache_dir location to prefer or and project_directory #283

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 19 additions & 14 deletions src/ansible_dev_environment/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down
10 changes: 4 additions & 6 deletions tests/unit/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down
Loading