Skip to content

Commit

Permalink
Merge pull request #562 from projectsyn/fix/template-update-commit-me…
Browse files Browse the repository at this point in the history
…ssage

Fix `package update` commit message to use correct template commit SHA
  • Loading branch information
simu authored Jul 20, 2022
2 parents ae70c2a + fa35a6a commit 3110140
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 4 deletions.
2 changes: 1 addition & 1 deletion commodore/dependency_templater.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,6 @@ def commit(self, msg: str, amend=False, init=True) -> bool:
click.echo(message)

if changed:
# Only create a new commmit if there are any changes.
# Only create a new commit if there are any changes.
repo.commit(msg, amend=amend)
return changed
9 changes: 6 additions & 3 deletions commodore/package/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
class PackageTemplater(Templater):
template_url: str
template_version: str
template_commit: str
_test_cases: list[str] = ["defaults"]
copyright_year: Optional[str] = None
_target_dir: Optional[Path] = None
Expand All @@ -43,8 +42,6 @@ def from_existing(cls, config: Config, package_path: Path):
t.template_url = cruft_json["template"]
if cruft_json["checkout"]:
t.template_version = cruft_json["checkout"]
if cruft_json["commit"]:
t.template_commit = cruft_json["commit"]

if "test_cases" in cookiecutter_args:
t.test_cases = cookiecutter_args["test_cases"].split(" ")
Expand All @@ -54,6 +51,12 @@ def from_existing(cls, config: Config, package_path: Path):
t.copyright_year = cookiecutter_args["copyright_year"]
return t

@property
def template_commit(self) -> str:
with open(self.target_dir / ".cruft.json", "r", encoding="utf-8") as f:
cruft_json = json.load(f)
return cruft_json["commit"]

@property
def test_cases(self) -> list[str]:
"""Return list of test cases.
Expand Down
49 changes: 49 additions & 0 deletions tests/test_package_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,55 @@ def test_package_update_test_cases(
assert r.repo.head.commit.message.startswith("Update from template\n\n")


def test_package_update_commit_message(
tmp_path: Path, config: Config, cli_runner: RunnerFunc
):
pkg_dir = tmp_path / "test-package"

# Intentionally create package from old version
result = cli_runner(
[
"-d",
str(tmp_path),
"package",
"new",
"test-package",
"--output-dir",
str(tmp_path),
"--template-version",
"main^",
]
)
assert result.exit_code == 0

# Adjust cruft config to use "main" branch of template
with open(pkg_dir / ".cruft.json", "r", encoding="utf-8") as f:
cruft_json = json.load(f)
cruft_json["checkout"] = "main"
with open(pkg_dir / ".cruft.json", "w", encoding="utf-8") as f:
json.dump(cruft_json, f)

r = GitRepo(None, pkg_dir)
r.stage_files([".cruft.json"])
r.commit("Initial commit", amend=True)

# Update package
result = cli_runner(["-d", str(tmp_path), "package", "update", str(pkg_dir)])
print(result.stdout)
assert result.exit_code == 0

with open(pkg_dir / ".cruft.json", "r") as f:
cruft_json = json.load(f)
template_version = cruft_json["checkout"]
template_sha = cruft_json["commit"]

assert (
r.repo.head.commit.message
== "Update from template\n\n"
+ f"Template version: {template_version} ({template_sha[:7]})"
)


def test_package_templater_from_existing_nonexistent(tmp_path: Path, config: Config):
with pytest.raises(click.ClickException) as e:
_ = PackageTemplater.from_existing(config, tmp_path / "test-package")
Expand Down

0 comments on commit 3110140

Please sign in to comment.