Skip to content

Commit

Permalink
Fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
doshitan committed Dec 26, 2024
1 parent 6bd1853 commit d4090be
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
20 changes: 18 additions & 2 deletions nava/platform/util/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down Expand Up @@ -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 int(result.stdout.strip())


def is_a_git_worktree(dir: Path) -> bool:
result = run_text(
Expand Down
25 changes: 19 additions & 6 deletions tests/projects/test_migrate_from_legacy_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,27 @@ def new_template_dir_with_git(tmp_path: Path) -> GitProject:
return new_dir_with_git(tmp_path / "template")


@pytest.fixture
def new_template_dir_with_git_and_content(new_template_dir_with_git: GitProject) -> GitProject:
template_git = new_template_dir_with_git

(template_git.dir / "foo.txt").write_text("bar")
template_git.commit_all("Initial commit")

return template_git


@pytest.fixture
def legacy_project_dir_with_git(
new_project_dir_with_git: GitProject, new_template_dir_with_git: GitProject
new_project_dir_with_git: GitProject, new_template_dir_with_git_and_content: GitProject
) -> GitProject:
template_git = new_template_dir_with_git_and_content

(new_project_dir_with_git.dir / ".template-version").write_text(
new_template_dir_with_git.get_commit_hash_for_head()
template_git.get_commit_hash_for_head() or "foobar"
)
new_project_dir_with_git.commit_all("Legacy install")

return new_project_dir_with_git


Expand Down Expand Up @@ -64,7 +77,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 = project.git.get_commit_count()

MigrateFromLegacyTemplate(
ctx=cli_context,
Expand All @@ -73,7 +86,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 = project.git.get_commit_count()

# only new file should exist
assert (project.dir / ".template" / "foo.yml").exists()
Expand All @@ -88,7 +101,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 = project.git.get_commit_count() or 0

MigrateFromLegacyTemplate(
ctx=cli_context,
Expand All @@ -97,7 +110,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 = project.git.get_commit_count()

# only new file should exist
assert (project.dir / ".template" / "foo.yml").exists()
Expand Down

0 comments on commit d4090be

Please sign in to comment.