From c263878340b7a88a8f7c3b3775b85addf16c237d Mon Sep 17 00:00:00 2001 From: Zhao Chen Date: Tue, 3 Feb 2026 11:31:45 +0800 Subject: [PATCH 1/2] test(git): Enhance Git parser tests with temporary repository setup Added a temporary Git repository setup in the test for the Git parser. This includes initializing a repository, creating a remote, adding a test file, and committing it before parsing. Updated assertions to verify the correct URL and commit SHA from the newly created repository. Signed-off-by: Zhao Chen Signed-off-by: Zhao Chen --- pkg/source/git_test.go | 51 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 3 deletions(-) diff --git a/pkg/source/git_test.go b/pkg/source/git_test.go index 954864c5..9f7611dc 100644 --- a/pkg/source/git_test.go +++ b/pkg/source/git_test.go @@ -17,16 +17,61 @@ package source import ( + "os" + "path/filepath" "testing" + "time" + gogit "github.com/go-git/go-git/v5" + "github.com/go-git/go-git/v5/config" + "github.com/go-git/go-git/v5/plumbing/object" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestGit(t *testing.T) { + // Create a temporary directory for the test git repository + tempDir, err := os.MkdirTemp("", "git-test-repo") + require.NoError(t, err) + defer os.RemoveAll(tempDir) + + // Initialize a new git repository + repo, err := gogit.PlainInit(tempDir, false) + require.NoError(t, err) + + // Create a remote "origin" with a test URL + expectedURL := "https://github.com/octocat/Hello-World.git" + _, err = repo.CreateRemote(&config.RemoteConfig{ + Name: "origin", + URLs: []string{expectedURL}, + }) + require.NoError(t, err) + + // Create a test file and commit it + testFile := filepath.Join(tempDir, "README.md") + err = os.WriteFile(testFile, []byte("# Hello World"), 0644) + require.NoError(t, err) + + worktree, err := repo.Worktree() + require.NoError(t, err) + + _, err = worktree.Add("README.md") + require.NoError(t, err) + + commit, err := worktree.Commit("Initial commit", &gogit.CommitOptions{ + Author: &object.Signature{ + Name: "Test User", + Email: "test@example.com", + When: time.Now(), + }, + }) + require.NoError(t, err) + + // Now test the git parser parser := &git{} - info, err := parser.Parse("testdata/git-repo") + info, err := parser.Parse(tempDir) assert.NoError(t, err) - assert.Equal(t, "https://github.com/octocat/Hello-World.git", info.URL, "source url should be equal to expected") - assert.Equal(t, "7fd1a60b01f91b314f59955a4e4d4e80d8edf11d", info.Commit, "commit should be equal to expected") + assert.Equal(t, expectedURL, info.URL, "source url should be equal to expected") + assert.Equal(t, commit.String(), info.Commit, "commit should be equal to expected") assert.Equal(t, false, info.Dirty, "dirty should be equal to expected") } From fd54b4f83b8275f8160f1a60d406ebb3202e5933 Mon Sep 17 00:00:00 2001 From: Zhao Chen Date: Mon, 23 Mar 2026 11:02:58 +0800 Subject: [PATCH 2/2] fix(test): address PR review comments - Use fixed time (time.Date) instead of time.Now() for deterministic commit hashes in tests - Remove testdata/git-repo submodule since tests now use temporary repositories Co-Authored-By: Claude Opus 4.6 Signed-off-by: Zhao Chen --- .gitmodules | 3 --- pkg/source/git_test.go | 2 +- pkg/source/testdata/git-repo | 1 - 3 files changed, 1 insertion(+), 5 deletions(-) delete mode 100644 .gitmodules delete mode 160000 pkg/source/testdata/git-repo diff --git a/.gitmodules b/.gitmodules deleted file mode 100644 index 7c195a4f..00000000 --- a/.gitmodules +++ /dev/null @@ -1,3 +0,0 @@ -[submodule "pkg/source/testdata/git-repo"] - path = pkg/source/testdata/git-repo - url = https://github.com/octocat/Hello-World.git diff --git a/pkg/source/git_test.go b/pkg/source/git_test.go index 9f7611dc..4e528164 100644 --- a/pkg/source/git_test.go +++ b/pkg/source/git_test.go @@ -62,7 +62,7 @@ func TestGit(t *testing.T) { Author: &object.Signature{ Name: "Test User", Email: "test@example.com", - When: time.Now(), + When: time.Date(2024, 1, 1, 12, 0, 0, 0, time.UTC), }, }) require.NoError(t, err) diff --git a/pkg/source/testdata/git-repo b/pkg/source/testdata/git-repo deleted file mode 160000 index 7fd1a60b..00000000 --- a/pkg/source/testdata/git-repo +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 7fd1a60b01f91b314f59955a4e4d4e80d8edf11d