Skip to content

Commit

Permalink
Walker now only returns path that need changes to be applied.
Browse files Browse the repository at this point in the history
  • Loading branch information
Qalthos committed Sep 25, 2024
1 parent fc2590a commit d4715ba
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 12 deletions.
28 changes: 16 additions & 12 deletions src/ansible_creator/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,17 +240,22 @@ def each_obj(
if obj.is_file():
dest_path.set_content(template_data, self.templar)

conflict_msg = dest_path.conflict
if dest_path.needs_write and conflict_msg:
self.output.warning(conflict_msg)
if obj.is_dir() and obj.name not in SKIP_DIRS:
return [
dest_path,
*self._recursive_walk(root=obj, resource=resource, template_data=template_data),
]
if dest_path.needs_write:
# Warn on conflict
conflict_msg = dest_path.conflict
if conflict_msg:
self.output.warning(conflict_msg)

if obj.is_dir() and obj.name not in SKIP_DIRS:
return [
dest_path,
*self._recursive_walk(root=obj, resource=resource, template_data=template_data),
]
if obj.is_file():
return [dest_path]

if obj.is_file():
return [dest_path]
if obj.is_dir() and obj.name not in SKIP_DIRS:
return self._recursive_walk(root=obj, resource=resource, template_data=template_data)

return []

Expand Down Expand Up @@ -345,8 +350,7 @@ def copy_containers(self: Copier, paths: list[DestinationFile]) -> None:
paths: A list of paths to create in the destination.
"""
for path in paths:
if path.conflict:
path.remove_existing()
path.remove_existing()

if path.source.is_dir():
path.dest.mkdir(parents=True, exist_ok=True)
Expand Down
27 changes: 27 additions & 0 deletions tests/units/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,30 @@ def test_overwrite(tmp_path: Path, output: Output) -> None:
assert base_file.read_text() == base_contents
assert podman_dir.is_dir()
assert docker_file.is_file()


def test_skip_repeats(tmp_path: Path, output: Output) -> None:
"""Test Copier skipping existing files.
Args:
tmp_path: Temporary directory path.
output: Output class object.
"""
walker = Walker(
resources=("common.devcontainer",),
resource_id="common.devcontainer",
dest=tmp_path,
output=output,
template_data=TemplateData(),
)
paths = walker.collect_paths()
assert paths

copier = Copier(
output=output,
)
copier.copy_containers(paths)

# Re-walk directory to generate new path list
paths = walker.collect_paths()
assert not paths

0 comments on commit d4715ba

Please sign in to comment.