Skip to content

Commit

Permalink
Added test that example updates ok
Browse files Browse the repository at this point in the history
  • Loading branch information
coretl committed Jan 25, 2024
1 parent fa6f5c0 commit 3e5f726
Showing 1 changed file with 42 additions and 15 deletions.
57 changes: 42 additions & 15 deletions tests/test_example.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import functools
import shlex
import subprocess
from pathlib import Path

import pytest
import yaml
from copier import run_copy
from prompt_toolkit.validation import ValidationError

Expand Down Expand Up @@ -34,28 +36,53 @@ def copy_project(
)


def test_template(tmp_path: Path):
project = tmp_path / "project"
venv = tmp_path / "venv"
copy_project(project)
def run_pipe(cmd: str, cwd=None):
sp = subprocess.run(
shlex.split(cmd),
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
cwd=cwd,
)
output = sp.stdout.decode()
assert sp.returncode == 0, output
return output

def run(cmd: str):
sp = subprocess.run(
shlex.split(cmd),
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
cwd=str(project),
)
assert sp.returncode == 0, sp.stdout.decode()

run(f"python -m venv {venv}")
run(f"{venv}/bin/python -m pip install -e .[dev]")
run(f"{venv}/bin/tox -p")
def test_template(tmp_path: Path):
project_path = tmp_path / "project"
venv_path = tmp_path / "venv"
copy_project(project_path)
run = functools.partial(run_pipe, cwd=str(project_path))
run(f"python -m venv {venv_path}")
run(f"{venv_path}/bin/python -m pip install -e .[dev]")
run(f"{venv_path}/bin/tox -p")


def test_bad_repo_name(tmp_path: Path):
with pytest.raises(ValidationError, match="bad:thing is not a valid repo name"):
copy_project(tmp_path, repo_name="bad:thing")


def test_example_repo_updates(tmp_path: Path):
generated_path = tmp_path / "generated"
example_url = (
"https://github.com/DiamondLightSource/python-copier-template-example.git"
)
example_path = tmp_path / "example"
copy_project(generated_path)
run_pipe(f"git clone {example_url} {example_path}")
with open(example_path / ".copier-answers.yml") as f:
d = yaml.safe_load(f)
d["_src_path"] = str(TOP)
with open(example_path / ".copier-answers.yml", "w") as f:
yaml.dump(d, f)
run_pipe(f"git -C {example_path} commit -am 'Update src'")
run_pipe(f"copier update --trust --vcs-ref=HEAD -A {example_path}")
output = run_pipe(
"diff -ur --exclude=.git --ignore-matching-lines='_commit: '"
f" {generated_path} {example_path}"
)
assert not output, output


# TODO: check that copier update python-copier-template-example matches generated

0 comments on commit 3e5f726

Please sign in to comment.