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

fix(#3): return error in pre-commit on new output directories #5

Merged
merged 7 commits into from
Apr 22, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions copier_template_tester/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

"""

import shlex
import shutil
import subprocess
import sys
from pathlib import Path

import copier
Expand Down Expand Up @@ -55,6 +58,22 @@ def _render( # type: ignore[no-untyped-def]
shutil.rmtree(git_path)


@beartype
def _ls_untracked_dir() -> set[Path]:
cmd = 'git ls-files --directory --exclude-standard --no-empty-dir --others'
process = subprocess.Popen(shlex.split(cmd), stdout=subprocess.PIPE) # noqa: S603
stdout, _stderr = process.communicate()
return {Path.cwd() / _d.strip() for _d in stdout.decode().split('\n') if _d}


@beartype
def _check_for_untracked(output_paths: set[Path]) -> None:
"""Resolves the edge case in #3 by raising when pre-commit won't error."""
if new_dirs := output_paths.intersection(_ls_untracked_dir()):
print(f'pre-commit error: untracked files from {new_dirs} must be added') # noqa: T201
sys.exit(1)


@beartype
def run(base_dir: Path | None = None) -> None:
"""Main class to run ctt."""
Expand All @@ -63,11 +82,14 @@ def run(base_dir: Path | None = None) -> None:
defaults = config.get('defaults', {})

input_path = base_dir
paths = set()
for key, data in config['output'].items():
output_path = base_dir / key
output_path.mkdir(parents=True, exist_ok=True)
paths.add(output_path)
print(f'Creating: {output_path}') # noqa: T201
_render(input_path, base_dir / output_path, data=defaults | data)
_check_for_untracked(paths)


if __name__ == '__main__': # pragma: no cover
Expand Down
Loading