Skip to content

Commit

Permalink
Add cleanup step for empty mocked role folders (#2235)
Browse files Browse the repository at this point in the history
* remove empty mocked role dirs

* chore: auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
zhan9san and pre-commit-ci[bot] authored Jul 12, 2022
1 parent 6bf6371 commit 3aa0b27
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
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

0 comments on commit 3aa0b27

Please sign in to comment.