From 407d6af94372de79720e5587a9c37189adeee04d Mon Sep 17 00:00:00 2001 From: Tanner Doshier Date: Thu, 26 Dec 2024 11:40:42 -0500 Subject: [PATCH] Fix test --- nava/platform/util/git.py | 20 +++++++++++++++++-- .../test_migrate_from_legacy_template.py | 8 ++++---- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/nava/platform/util/git.py b/nava/platform/util/git.py index 5554099..a99d020 100644 --- a/nava/platform/util/git.py +++ b/nava/platform/util/git.py @@ -92,8 +92,16 @@ def tag(self, tag: str) -> None: def rename_branch(self, new_branch_name: str) -> None: self._run_cmd(["git", "branch", "-m", new_branch_name]) - def get_commit_hash_for_head(self) -> str: - return self._run_cmd(["git", "rev-parse", "HEAD"]).stdout.strip() + def get_commit_hash_for_head(self) -> str | None: + result = self._run_cmd(["git", "rev-parse", "HEAD"]) + + # say you run this against an empty repo, you'll get a return code of + # 128 and message "fatal: ambiguous argument 'HEAD': unknown revision or + # path not in the working tree." + if result.returncode != 0: + return None + + return result.stdout.strip() def is_path_ignored(self, path: str) -> bool: result = self._run_cmd(["git", "check-ignore", "-q", path]) @@ -135,6 +143,14 @@ def get_commit_description(self, commit_ish: str = "HEAD") -> str | None: return result.stdout.strip() + def get_commit_count(self, ref: str = "HEAD") -> int | None: + result = self._run_cmd(["git", "rev-list", "--count", ref]) + + if result.returncode != 0: + return None + + return result.stdout.strip() + def is_a_git_worktree(dir: Path) -> bool: result = run_text( diff --git a/tests/projects/test_migrate_from_legacy_template.py b/tests/projects/test_migrate_from_legacy_template.py index f55dd76..e54452f 100644 --- a/tests/projects/test_migrate_from_legacy_template.py +++ b/tests/projects/test_migrate_from_legacy_template.py @@ -64,7 +64,7 @@ def test_migrate_from_legacy_no_commit( cli_context: CliContext, ): project = Project(legacy_project_dir_with_git.dir) - commit_count_before = len(project.git.log().stdout.splitlines()) + commit_count_before = len(project.git.log("--oneline ").stdout.splitlines()) MigrateFromLegacyTemplate( ctx=cli_context, @@ -73,7 +73,7 @@ def test_migrate_from_legacy_no_commit( new_version_answers_file_name="foo.yml", ).migrate_from_legacy(commit=False) - commit_count_after = len(project.git.log().stdout.splitlines()) + commit_count_after = len(project.git.log("--oneline").stdout.splitlines()) # only new file should exist assert (project.dir / ".template" / "foo.yml").exists() @@ -88,7 +88,7 @@ def test_migrate_from_legacy_commit( cli_context: CliContext, ): project = Project(legacy_project_dir_with_git.dir) - commit_count_before = len(project.git.log().stdout.splitlines()) + commit_count_before = len(project.git.log("--oneline ").stdout.splitlines()) MigrateFromLegacyTemplate( ctx=cli_context, @@ -97,7 +97,7 @@ def test_migrate_from_legacy_commit( new_version_answers_file_name="foo.yml", ).migrate_from_legacy(commit=True) - commit_count_after = len(project.git.log().stdout.splitlines()) + commit_count_after = len(project.git.log("--oneline").stdout.splitlines()) # only new file should exist assert (project.dir / ".template" / "foo.yml").exists()