From a752ea53b1bb6f7563556e51402b7bdbdb7fc214 Mon Sep 17 00:00:00 2001 From: Simon Gerber Date: Wed, 20 Jul 2022 14:51:16 +0200 Subject: [PATCH 1/2] Fix typo in comment --- commodore/dependency_templater.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commodore/dependency_templater.py b/commodore/dependency_templater.py index 3553c236..49f2bad9 100644 --- a/commodore/dependency_templater.py +++ b/commodore/dependency_templater.py @@ -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 From fa35a6a821d9ee85b8726a37a9a1bd97cad23d78 Mon Sep 17 00:00:00 2001 From: Simon Gerber Date: Wed, 20 Jul 2022 14:51:30 +0200 Subject: [PATCH 2/2] Fix `package update` commit message to use correct template commit SHA We previously read the template commit SHA from the package's `.cruft.json` before actually performing the update. However, this commit SHA wouldn't match the actually commit SHA of the actual template commit when updating a package to a new template version. This commit changes the field `PackageTemplater.template_commit` to a property and reads the value from `.cruft.json` at the time the field is read. This ensures that we always read the correct commit SHA when we commit template changes in `package update`. --- commodore/package/template.py | 9 ++++--- tests/test_package_template.py | 49 ++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 3 deletions(-) diff --git a/commodore/package/template.py b/commodore/package/template.py index 23dd17dd..add04fea 100644 --- a/commodore/package/template.py +++ b/commodore/package/template.py @@ -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 @@ -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(" ") @@ -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. diff --git a/tests/test_package_template.py b/tests/test_package_template.py index c1589108..84f34753 100644 --- a/tests/test_package_template.py +++ b/tests/test_package_template.py @@ -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")