Skip to content

Commit

Permalink
fix(#34): support git init on Windows (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleKing authored Nov 19, 2024
1 parent 7840360 commit c647247
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 5 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/usage_test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
name: CTT Usage Test

"on":
push:
branches: [main]
pull_request:
branches: [main]
paths:
- .github/workflows/usage_test.yml
- copier_template_tester/**
- tests/**
- poetry.lock
- pyproject.toml

jobs:
usage-test:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [windows-latest]
python-version: ["3.10"]
steps:
- uses: actions/checkout@v4
- uses: ./.github/actions/setup
with:
os: ${{ matrix.os }}
python-version: ${{ matrix.python-version }}

- name: "Run Usage Test (Specifically addresses #34 for Windows)"
run: |
poetry run ctt --base-dir="tests/data/ci_usage_test"
cat "tests/data/ci_usage_test/.ctt/README.md"
23 changes: 22 additions & 1 deletion copier_template_tester/_write_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import re
import shutil
import stat
import sys
from contextlib import contextmanager, suppress
from functools import lru_cache
from pathlib import Path
Expand Down Expand Up @@ -120,6 +122,22 @@ def _output_dir(*, src_path: Path, dst_path: Path): # noqa: ANN202
answers_path.unlink()


def _remove_readonly(func, path: str, _excinfo) -> None: # noqa: ANN001
"""Clear the readonly bit for `shutil.rmtree(..., onexc=_remove_readonly)`.
Adapted from: https://docs.python.org/3/library/shutil.html#rmtree-example
Resolves: https://github.com/KyleKing/copier-template-tester/issues/34
The first parameter, function, is the function which raised the exception; it depends on the platform and
implementation. The second parameter, path, will be the path name passed to function. The third parameter,
excinfo, is the exception that was raised. Exceptions raised by onexc will not be caught.
"""
Path.chmod(Path(path), stat.S_IWRITE)
func(path)


def write_output(*, src_path: Path, dst_path: Path, data: dict[str, bool | int | float | str | None], **kwargs) -> None:
"""Copy the specified directory to the target location with provided data.
Expand All @@ -141,4 +159,7 @@ def write_output(*, src_path: Path, dst_path: Path, data: dict[str, bool | int |
git_path = dst_path / '.git'
if git_path.is_dir(): # pragma: no cover
logger.info('Removing git created by copier', git_path=git_path)
shutil.rmtree(git_path)
if sys.version_info >= (3, 12, 0):
shutil.rmtree(git_path, onexc=_remove_readonly) # type: ignore[call-arg]
else:
shutil.rmtree(git_path, onerror=_remove_readonly)
2 changes: 1 addition & 1 deletion docs/docs/CODE_TAG_SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

| Type | Comment | Last Edit | Source File |
|---------|-----------------------------------------------------------------------|------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| PLANNED | In python 3.10, there is a Beartype error for this return annotation: | 2023-10-14 | [copier_template_tester/_write_output.py:96](https://github.com/KyleKing/copier-template-tester/blame/57f881822440c37e163312269c0d5893da21cd55/copier_template_tester/_write_output.py#L92) |
| PLANNED | In python 3.10, there is a Beartype error for this return annotation: | 2023-10-14 | [copier_template_tester/_write_output.py:98](https://github.com/KyleKing/copier-template-tester/blame/57f881822440c37e163312269c0d5893da21cd55/copier_template_tester/_write_output.py#L92) |
| PLANNED | document | 2024-11-18 | [pyproject.toml:87](https://github.com/KyleKing/copier-template-tester/blame/d0e5d3c674c533d47cdec43aa4b18fce7ec0cb89/pyproject.toml#L87) |
| PLANNED | document | 2024-11-18 | [pyproject.toml:88](https://github.com/KyleKing/copier-template-tester/blame/d0e5d3c674c533d47cdec43aa4b18fce7ec0cb89/pyproject.toml#L88) |
| PLANNED | document | 2024-11-18 | [pyproject.toml:89](https://github.com/KyleKing/copier-template-tester/blame/d0e5d3c674c533d47cdec43aa4b18fce7ec0cb89/pyproject.toml#L89) |
Expand Down
6 changes: 3 additions & 3 deletions docs/docs/DEVELOPER_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ poetry config pypi-token.pypi ...
| `copier_template_tester/_config.py` | 14 | 0 | 3 | 100.0% |
| `copier_template_tester/_pre_commit_support.py` | 13 | 0 | 0 | 93.3% |
| `copier_template_tester/_runtime_type_check_setup.py` | 13 | 0 | 37 | 100.0% |
| `copier_template_tester/_write_output.py` | 76 | 0 | 13 | 100.0% |
| `copier_template_tester/_write_output.py` | 81 | 1 | 16 | 98.9% |
| `copier_template_tester/main.py` | 30 | 4 | 20 | 86.7% |
| **Totals** | 150 | 4 | 73 | 97.0% |
| **Totals** | 155 | 5 | 76 | 96.4% |

Generated on: 2024-11-17
Generated on: 2024-11-19
<!-- {cte} -->
5 changes: 5 additions & 0 deletions tests/data/ci_usage_test/copier.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
_subdirectory: template
# questionnaire here...
_tasks:
- git init
- git add .
5 changes: 5 additions & 0 deletions tests/data/ci_usage_test/ctt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Minimal Configuration

[defaults]

[output.".ctt"]
3 changes: 3 additions & 0 deletions tests/data/ci_usage_test/template/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# CI Usage Test

Test usage as part of CI. See associated Github Action

0 comments on commit c647247

Please sign in to comment.