Skip to content

Commit

Permalink
Fix package update commit message to use correct template commit SHA
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
simu committed Jul 20, 2022
1 parent a752ea5 commit fa35a6a
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
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 fa35a6a

Please sign in to comment.