File tree Expand file tree Collapse file tree 5 files changed +42
-22
lines changed Expand file tree Collapse file tree 5 files changed +42
-22
lines changed Original file line number Diff line number Diff line change 10
10
"source.fixAll" : " explicit" ,
11
11
"source.organizeImports" : " explicit"
12
12
},
13
- "editor.defaultFormatter" : " ms-python.black-formatter " ,
13
+ "editor.defaultFormatter" : " charliermarsh.ruff " ,
14
14
"editor.formatOnSave" : true
15
15
},
16
16
"editor.formatOnSave" : true ,
40
40
"yaml.completion" : true ,
41
41
"yaml.customTags" : [" !encrypted/pkcs1-oaep scalar" , " !vault scalar" ],
42
42
"yaml.format.enable" : false ,
43
- "yaml.validate" : true
43
+ "yaml.validate" : true ,
44
+ "cSpell.words" : [" delenv" ]
44
45
}
Original file line number Diff line number Diff line change 1
1
"""Utilities for configuring ansible runtime environment."""
2
2
3
- import hashlib
4
3
import os
5
4
from pathlib import Path
6
5
7
6
8
- def get_cache_dir (project_dir : Path ) -> Path :
9
- """Compute cache directory to be used based on project path."""
10
- # we only use the basename instead of the full path in order to ensure that
11
- # we would use the same key regardless the location of the user home
12
- # directory or where the project is clones (as long the project folder uses
13
- # the same name).
14
- basename = project_dir .resolve ().name .encode (encoding = "utf-8" )
15
- # 6 chars of entropy should be enough
16
- cache_key = hashlib .sha256 (basename ).hexdigest ()[:6 ]
17
- cache_dir = (
18
- Path (os .getenv ("XDG_CACHE_HOME" , "~/.cache" )).expanduser ()
19
- / "ansible-compat"
20
- / cache_key
21
- )
7
+ def get_cache_dir (project_dir : Path , * , isolated : bool = True ) -> Path :
8
+ """Compute cache directory to be used based on project path.
9
+
10
+ Returns:
11
+ Cache directory path.
12
+ """
13
+ if "VIRTUAL_ENV" in os .environ :
14
+ cache_dir = Path (os .environ ["VIRTUAL_ENV" ]) / ".ansible"
15
+ elif isolated :
16
+ cache_dir = project_dir / ".ansible"
17
+ else :
18
+ cache_dir = Path (os .environ .get ("ANSIBLE_HOME" , "~/.ansible" )).expanduser ()
19
+
20
+ # Ensure basic folder structure exists so `ansible-galaxy list` does not
21
+ # fail with: None of the provided paths were usable. Please specify a valid path with
22
+ for name in ("roles" , "collections" ):
23
+ (cache_dir / name ).mkdir (parents = True , exist_ok = True )
24
+
22
25
return cache_dir
Original file line number Diff line number Diff line change @@ -208,8 +208,8 @@ def __init__(
208
208
if "PYTHONWARNINGS" not in self .environ : # pragma: no cover
209
209
self .environ ["PYTHONWARNINGS" ] = "ignore:Blowfish has been deprecated"
210
210
211
- if isolated :
212
- self . cache_dir = get_cache_dir ( self . project_dir )
211
+ self . cache_dir = get_cache_dir ( self . project_dir , isolated = self . isolated )
212
+
213
213
self .config = AnsibleConfig (cache_dir = self .cache_dir )
214
214
215
215
# Add the sys.path to the collection paths if not isolated
Original file line number Diff line number Diff line change 1
1
"""Tests for ansible_compat.prerun module."""
2
2
3
+ from __future__ import annotations
4
+
3
5
from pathlib import Path
6
+ from typing import TYPE_CHECKING
7
+
8
+ if TYPE_CHECKING :
9
+ from _pytest .monkeypatch import MonkeyPatch
4
10
5
11
from ansible_compat .prerun import get_cache_dir
6
12
@@ -10,3 +16,9 @@ def test_get_cache_dir_relative() -> None:
10
16
relative_path = Path ()
11
17
abs_path = relative_path .resolve ()
12
18
assert get_cache_dir (relative_path ) == get_cache_dir (abs_path )
19
+
20
+
21
+ def test_get_cache_dir_no_isolation_no_venv (monkeypatch : MonkeyPatch ) -> None :
22
+ """Test behaviors of get_cache_dir."""
23
+ monkeypatch .delenv ("VIRTUAL_ENV" , raising = False )
24
+ assert get_cache_dir (Path (), isolated = False ) == Path ("~/.ansible" ).expanduser ()
Original file line number Diff line number Diff line change @@ -652,10 +652,9 @@ def test_upgrade_collection(runtime_tmp: Runtime) -> None:
652
652
runtime_tmp .require_collection ("community.molecule" , "0.1.0" )
653
653
654
654
655
- def test_require_collection_no_cache_dir () -> None :
655
+ def test_require_collection_not_isolated () -> None :
656
656
"""Check require_collection without a cache directory."""
657
- runtime = Runtime ()
658
- assert not runtime .cache_dir
657
+ runtime = Runtime (isolated = False )
659
658
runtime .require_collection ("community.molecule" , "0.1.0" , install = True )
660
659
661
660
@@ -1022,6 +1021,11 @@ def test_runtime_has_playbook() -> None:
1022
1021
"""Tests has_playbook method."""
1023
1022
runtime = Runtime (require_module = True )
1024
1023
1024
+ runtime .prepare_environment (
1025
+ required_collections = {"community.molecule" : "0.1.0" },
1026
+ install_local = True ,
1027
+ )
1028
+
1025
1029
assert not runtime .has_playbook ("this-does-not-exist.yml" )
1026
1030
# call twice to ensure cache is used:
1027
1031
assert not runtime .has_playbook ("this-does-not-exist.yml" )
You can’t perform that action at this time.
0 commit comments