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

Add cleanup step for empty mocked role folders #2235

Merged
merged 2 commits into from
Jul 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/ansiblelint/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
from enrich.console import should_do_markup

from ansiblelint import cli
from ansiblelint._mockings import _perform_mockings_cleanup
from ansiblelint.app import get_app
from ansiblelint.color import console, console_options, reconfigure, render_yaml
from ansiblelint.config import options
Expand Down Expand Up @@ -215,6 +216,8 @@ def main(argv: Optional[List[str]] = None) -> int:

app.render_matches(result.matches)

_perform_mockings_cleanup()

return app.report_outcome(result, mark_as_success=mark_as_success)


Expand Down
14 changes: 14 additions & 0 deletions src/ansiblelint/_mockings.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,17 @@ def _perform_mockings() -> None: # noqa: C901
pass
if not link_path.exists():
link_path.symlink_to(target, target_is_directory=True)


def _perform_mockings_cleanup() -> None: # noqa: C901
"""Clean up mocked modules and roles."""
for role_name in options.mock_roles:
if re.match(r"\w+\.\w+\.\w+$", role_name):
namespace, collection, role_dir = role_name.split(".")
path = f"{options.cache_dir}/collections/ansible_collections/{ namespace }/{ collection }/roles/{ role_dir }/"
else:
path = f"{options.cache_dir}/roles/{role_name}"
try:
os.rmdir(path)
except OSError:
pass