From 375ff97d5e8d51265dc05180dd2b31e61af4b330 Mon Sep 17 00:00:00 2001 From: Debanjan Date: Thu, 17 Oct 2024 18:28:47 +0530 Subject: [PATCH] feat: allow retrieval commits for tags --- testhelper/gittest/gittest.go | 13 +++++++++++-- testhelper/gittest/gittest_test.go | 12 ++++++++++-- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/testhelper/gittest/gittest.go b/testhelper/gittest/gittest.go index 323db29c..229f6b3c 100644 --- a/testhelper/gittest/gittest.go +++ b/testhelper/gittest/gittest.go @@ -109,8 +109,17 @@ func (s *Server) GetServerCA() []byte { return getServerCA(s.Server) } -func (s *Server) GetLatestCommitHash(t testing.TB, branch string) string { - cmd := exec.Command("git", "-c", "http.sslVerify=false", "ls-remote", s.URL, fmt.Sprintf("refs/heads/%s", branch)) +func (s *Server) GetLatestCommitHash(t testing.TB, referenceType, ref string) string { + var refType string + switch referenceType { + case "branch": + refType = "heads" + case "tag": + refType = "tags" + default: + require.Fail(t, "invalid refType") + } + cmd := exec.Command("git", "-c", "http.sslVerify=false", "ls-remote", s.URL, fmt.Sprintf("refs/%s/%s", refType, ref)) out, err := cmd.Output() require.NoError(t, err, "should be able to run the ls-remote command") commitHash := strings.Split(string(out), "\t")[0] diff --git a/testhelper/gittest/gittest_test.go b/testhelper/gittest/gittest_test.go index 18ce52a7..6457acb2 100644 --- a/testhelper/gittest/gittest_test.go +++ b/testhelper/gittest/gittest_test.go @@ -63,7 +63,7 @@ func TestGitServer(t *testing.T) { require.NoError(t, err, "should be able to get the HEAD commit") require.NotEmpty(t, out, "HEAD commit should not be empty") - latestCommit := s.GetLatestCommitHash(t, "develop") + latestCommit := s.GetLatestCommitHash(t, "branch", "develop") require.Equal(t, strings.TrimSpace(out), latestCommit, "HEAD commit should match the latest commit") }) @@ -79,7 +79,15 @@ func TestGitServer(t *testing.T) { require.NoError(t, err, "should be able to get the HEAD commit") require.NotEmpty(t, out, "HEAD commit should not be empty") - latestCommit := s.GetLatestCommitHash(t, "main") + out, err = execCmd("git", "-C", tempDir, "tag", "-a", "v1.0.0", "-m", "tag v1.0.0") + require.NoErrorf(t, err, "should be able to create a new tag: %s", out) + out, err = execCmd("git", "-c", "http.sslVerify=false", "-C", tempDir, "push", "origin", "v1.0.0") + require.NoErrorf(t, err, "should be able to push the new tag: %s", out) + + out, err = execCmd("git", "-C", tempDir, "rev-parse", "v1.0.0") + require.NoError(t, err, "should be able to get the HEAD commit") + latestCommit := s.GetLatestCommitHash(t, "tag", "v1.0.0") + require.Equal(t, strings.TrimSpace(out), latestCommit, "HEAD commit should match the latest commit") }) }