Skip to content

Commit

Permalink
Windows fixes
Browse files Browse the repository at this point in the history
- Ignore errors when executing `shutil.rmtree()`, because it seems like it's common to fail when deleting git repos on Windows, and since these are temp files, we don't really care that much there's garbage left. Any good OS should clean the temp folders automatically.
- Always find Jinja templates in `PosixPath` mode.
- Ignore `OSError` when trying to enter a possibly git root directory. This is yielded by Windows when the path is a URL and we don't really care about it.
- Fix some tests with non-windows haradcoded stuff.
- Fix a test that was using a Bash script. Modified to be Python, which should work fine cross-system.
  • Loading branch information
Jairo Llopis committed Jul 24, 2020
1 parent 7da0d74 commit 2c78fbd
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 22 deletions.
2 changes: 1 addition & 1 deletion copier/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def copy(
raise
finally:
if is_update:
shutil.rmtree(conf.src_path)
shutil.rmtree(conf.src_path, ignore_errors=True)


def copy_local(conf: ConfigData) -> None:
Expand Down
6 changes: 2 additions & 4 deletions copier/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,8 @@ def __init__(self, conf: ConfigData) -> None:
)

def __call__(self, fullpath: StrOrPath) -> str:
relpath = (
str(fullpath).replace(str(self.conf.src_path), "", 1).lstrip(os.path.sep)
)
tmpl = self.env.get_template(relpath)
relpath = Path(fullpath).relative_to(self.conf.src_path).as_posix()
tmpl = self.env.get_template(str(relpath))
return tmpl.render(**self.data)

def string(self, string: StrOrPath) -> str:
Expand Down
6 changes: 2 additions & 4 deletions copier/vcs.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import re
import shutil
import tempfile
from pathlib import Path

Expand All @@ -25,8 +24,8 @@ def is_git_repo_root(path: Path) -> bool:
"""Indicate if a given path is a git repo root directory."""
try:
with local.cwd(path / ".git"):
return bool(git("rev-parse", "--is-inside-git-dir") == "true\n")
except (FileNotFoundError, NotADirectoryError):
return bool(git("rev-parse", "--is-inside-git-dir").strip() == "true")
except OSError:
return False


Expand Down Expand Up @@ -72,7 +71,6 @@ def checkout_latest_tag(local_repo: StrOrPath) -> str:

def clone(url: str, ref: str = "HEAD") -> str:
location = tempfile.mkdtemp(prefix=f"{__name__}.clone.")
shutil.rmtree(location) # Path must not exist
git("clone", "--no-checkout", url, location)
with local.cwd(location):
git("checkout", ref)
Expand Down
6 changes: 3 additions & 3 deletions tests/demo_migrations/copier.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
_exclude:
- tasks.sh
- tasks.py
- migrations.py
- .git

_tasks:
- "[[ _copier_conf.src_path / 'tasks.sh' ]] 1"
- ["[[ _copier_conf.src_path / 'tasks.sh' ]]", 2]
- "python3 [[ _copier_conf.src_path / 'tasks.py' ]] 1"
- [python3, "[[ _copier_conf.src_path / 'tasks.py' ]]", 2]

_migrations:
# This migration is never executed because it's the 1st version copied, and
Expand Down
13 changes: 13 additions & 0 deletions tests/demo_migrations/tasks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env python3
import os
import os.path
import sys
from contextlib import suppress

from plumbum.cmd import git

with open("created-with-tasks.txt", "a", newline="\n") as cwt:
cwt.write(" ".join([os.environ["STAGE"]] + sys.argv[1:]) + "\n")
git("init")
with suppress(FileNotFoundError):
os.unlink("delete-in-tasks.txt")
4 changes: 0 additions & 4 deletions tests/demo_migrations/tasks.sh

This file was deleted.

6 changes: 3 additions & 3 deletions tests/test_config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import re
from pathlib import Path

import pytest
Expand Down Expand Up @@ -45,11 +44,12 @@ def test_read_data(tmp_path, template):


def test_invalid_yaml(capsys):
conf_path = Path("tests/demo_invalid/copier.yml")
conf_path = Path("tests", "demo_invalid", "copier.yml")
with pytest.raises(InvalidConfigFileError):
load_yaml_data(conf_path)
out, _ = capsys.readouterr()
assert re.search(r"INVALID.*tests/demo_invalid/copier\.yml", out)
assert "INVALID CONFIG FILE" in out
assert str(conf_path) in out


@pytest.mark.parametrize(
Expand Down
4 changes: 2 additions & 2 deletions tests/test_migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_migrations_and_tasks(tmpdir: py.path.local):
assert not (tmp_path / "delete-in-tasks.txt").exists()
assert (tmp_path / "delete-in-migration-v2.txt").isfile()
assert not (tmp_path / "migrations.py").exists()
assert not (tmp_path / "tasks.sh").exists()
assert not (tmp_path / "tasks.py").exists()
assert not glob(str(tmp_path / "*-before.txt"))
assert not glob(str(tmp_path / "*-after.txt"))
answers = yaml.safe_load((tmp_path / ".copier-answers.yml").read())
Expand All @@ -53,7 +53,7 @@ def test_migrations_and_tasks(tmpdir: py.path.local):
assert not (tmp_path / "delete-in-tasks.txt").exists()
assert not (tmp_path / "delete-in-migration-v2.txt").exists()
assert not (tmp_path / "migrations.py").exists()
assert not (tmp_path / "tasks.sh").exists()
assert not (tmp_path / "tasks.py").exists()
assert (tmp_path / "v1.0.0-v2-v2.0-before.json").isfile()
assert (tmp_path / "v1.0.0-v2-v2.0-after.json").isfile()
answers = yaml.safe_load((tmp_path / ".copier-answers.yml").read())
Expand Down
2 changes: 1 addition & 1 deletion tests/test_vcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ def test_clone():
tmp = vcs.clone("https://github.com/copier-org/copier.git")
assert tmp
assert exists(join(tmp, "README.md"))
shutil.rmtree(tmp)
shutil.rmtree(tmp, ignore_errors=True)

0 comments on commit 2c78fbd

Please sign in to comment.