Skip to content

Commit 5da6da2

Browse files
aftersnowclaude
andauthored
test(git): Enhance Git parser tests with temporary repository setup (#406)
* 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 <zhaochen.zju@gmail.com> Signed-off-by: Zhao Chen <winters.zc@antgroup.com> * 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 <noreply@anthropic.com> Signed-off-by: Zhao Chen <winters.zc@antgroup.com> --------- Signed-off-by: Zhao Chen <zhaochen.zju@gmail.com> Signed-off-by: Zhao Chen <winters.zc@antgroup.com> Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 22a5e49 commit 5da6da2

File tree

3 files changed

+48
-7
lines changed

3 files changed

+48
-7
lines changed

.gitmodules

Lines changed: 0 additions & 3 deletions
This file was deleted.

pkg/source/git_test.go

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,61 @@
1717
package source
1818

1919
import (
20+
"os"
21+
"path/filepath"
2022
"testing"
23+
"time"
2124

25+
gogit "github.com/go-git/go-git/v5"
26+
"github.com/go-git/go-git/v5/config"
27+
"github.com/go-git/go-git/v5/plumbing/object"
2228
"github.com/stretchr/testify/assert"
29+
"github.com/stretchr/testify/require"
2330
)
2431

2532
func TestGit(t *testing.T) {
33+
// Create a temporary directory for the test git repository
34+
tempDir, err := os.MkdirTemp("", "git-test-repo")
35+
require.NoError(t, err)
36+
defer os.RemoveAll(tempDir)
37+
38+
// Initialize a new git repository
39+
repo, err := gogit.PlainInit(tempDir, false)
40+
require.NoError(t, err)
41+
42+
// Create a remote "origin" with a test URL
43+
expectedURL := "https://github.com/octocat/Hello-World.git"
44+
_, err = repo.CreateRemote(&config.RemoteConfig{
45+
Name: "origin",
46+
URLs: []string{expectedURL},
47+
})
48+
require.NoError(t, err)
49+
50+
// Create a test file and commit it
51+
testFile := filepath.Join(tempDir, "README.md")
52+
err = os.WriteFile(testFile, []byte("# Hello World"), 0644)
53+
require.NoError(t, err)
54+
55+
worktree, err := repo.Worktree()
56+
require.NoError(t, err)
57+
58+
_, err = worktree.Add("README.md")
59+
require.NoError(t, err)
60+
61+
commit, err := worktree.Commit("Initial commit", &gogit.CommitOptions{
62+
Author: &object.Signature{
63+
Name: "Test User",
64+
Email: "test@example.com",
65+
When: time.Date(2024, 1, 1, 12, 0, 0, 0, time.UTC),
66+
},
67+
})
68+
require.NoError(t, err)
69+
70+
// Now test the git parser
2671
parser := &git{}
27-
info, err := parser.Parse("testdata/git-repo")
72+
info, err := parser.Parse(tempDir)
2873
assert.NoError(t, err)
29-
assert.Equal(t, "https://github.com/octocat/Hello-World.git", info.URL, "source url should be equal to expected")
30-
assert.Equal(t, "7fd1a60b01f91b314f59955a4e4d4e80d8edf11d", info.Commit, "commit should be equal to expected")
74+
assert.Equal(t, expectedURL, info.URL, "source url should be equal to expected")
75+
assert.Equal(t, commit.String(), info.Commit, "commit should be equal to expected")
3176
assert.Equal(t, false, info.Dirty, "dirty should be equal to expected")
3277
}

pkg/source/testdata/git-repo

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)