Skip to content

Commit db4b7e4

Browse files
committed
Add test for BatchCommand
1 parent efc4d43 commit db4b7e4

23 files changed

+271
-31
lines changed

modules/git/batch_reader.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"strings"
1414

1515
"code.gitea.io/gitea/modules/git/gitcmd"
16+
"code.gitea.io/gitea/modules/git/internal"
1617
"code.gitea.io/gitea/modules/log"
1718

1819
"github.com/djherbis/buffer"
@@ -69,7 +70,7 @@ func newCatFileBatch(ctx context.Context, repoPath, batchArg string) *batchCatFi
6970
go func() {
7071
stderr := strings.Builder{}
7172
err := gitcmd.NewCommand("cat-file").
72-
AddArguments(gitcmd.ToTrustCmdArg(batchArg)).
73+
AddArguments(internal.CmdArg(batchArg)).
7374
WithDir(repoPath).
7475
WithStdin(batchStdinReader).
7576
WithStdout(batchStdoutWriter).

modules/git/blob_test.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,25 @@ import (
99
"path/filepath"
1010
"testing"
1111

12+
"code.gitea.io/gitea/modules/test"
13+
1214
"github.com/stretchr/testify/assert"
1315
"github.com/stretchr/testify/require"
1416
)
1517

16-
func TestBlob_Data(t *testing.T) {
18+
func TestBlob_Data_Batch(t *testing.T) {
19+
defer test.MockVariableValue(&DefaultFeatures().SupportCatFileBatchCommand, false)()
20+
21+
testBlobData(t)
22+
}
23+
24+
func TestBlob_Data_BatchCommand(t *testing.T) {
25+
defer test.MockVariableValue(&DefaultFeatures().SupportCatFileBatchCommand, true)()
26+
27+
testBlobData(t)
28+
}
29+
30+
func testBlobData(t *testing.T) {
1731
output := "file2\n"
1832
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
1933
repo, err := OpenRepository(t.Context(), bareRepo1Path)

modules/git/commit_info_test.go

Lines changed: 59 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"testing"
99
"time"
1010

11+
"code.gitea.io/gitea/modules/test"
1112
"github.com/stretchr/testify/assert"
1213
"github.com/stretchr/testify/require"
1314
)
@@ -30,28 +31,57 @@ func cloneRepo(tb testing.TB, url string) (string, error) {
3031
}
3132

3233
func testGetCommitsInfo(t *testing.T, repo1 *Repository) {
34+
type expectedEntryInfo struct {
35+
CommitID string
36+
Size int64
37+
}
38+
3339
// these test case are specific to the repo1 test repo
3440
testCases := []struct {
3541
CommitID string
3642
Path string
37-
ExpectedIDs map[string]string
43+
ExpectedIDs map[string]expectedEntryInfo
3844
ExpectedTreeCommit string
3945
}{
40-
{"8d92fc957a4d7cfd98bc375f0b7bb189a0d6c9f2", "", map[string]string{
41-
"file1.txt": "95bb4d39648ee7e325106df01a621c530863a653",
42-
"file2.txt": "8d92fc957a4d7cfd98bc375f0b7bb189a0d6c9f2",
46+
{"8d92fc957a4d7cfd98bc375f0b7bb189a0d6c9f2", "", map[string]expectedEntryInfo{
47+
"file1.txt": {
48+
CommitID: "95bb4d39648ee7e325106df01a621c530863a653",
49+
Size: 6,
50+
},
51+
"file2.txt": {
52+
CommitID: "8d92fc957a4d7cfd98bc375f0b7bb189a0d6c9f2",
53+
Size: 6,
54+
},
4355
}, "8d92fc957a4d7cfd98bc375f0b7bb189a0d6c9f2"},
44-
{"2839944139e0de9737a044f78b0e4b40d989a9e3", "", map[string]string{
45-
"file1.txt": "2839944139e0de9737a044f78b0e4b40d989a9e3",
46-
"branch1.txt": "9c9aef8dd84e02bc7ec12641deb4c930a7c30185",
56+
{"2839944139e0de9737a044f78b0e4b40d989a9e3", "", map[string]expectedEntryInfo{
57+
"file1.txt": {
58+
CommitID: "2839944139e0de9737a044f78b0e4b40d989a9e3",
59+
Size: 15,
60+
},
61+
"branch1.txt": {
62+
CommitID: "9c9aef8dd84e02bc7ec12641deb4c930a7c30185",
63+
Size: 8,
64+
},
4765
}, "2839944139e0de9737a044f78b0e4b40d989a9e3"},
48-
{"5c80b0245c1c6f8343fa418ec374b13b5d4ee658", "branch2", map[string]string{
49-
"branch2.txt": "5c80b0245c1c6f8343fa418ec374b13b5d4ee658",
66+
{"5c80b0245c1c6f8343fa418ec374b13b5d4ee658", "branch2", map[string]expectedEntryInfo{
67+
"branch2.txt": {
68+
CommitID: "5c80b0245c1c6f8343fa418ec374b13b5d4ee658",
69+
Size: 8,
70+
},
5071
}, "5c80b0245c1c6f8343fa418ec374b13b5d4ee658"},
51-
{"feaf4ba6bc635fec442f46ddd4512416ec43c2c2", "", map[string]string{
52-
"file1.txt": "95bb4d39648ee7e325106df01a621c530863a653",
53-
"file2.txt": "8d92fc957a4d7cfd98bc375f0b7bb189a0d6c9f2",
54-
"foo": "37991dec2c8e592043f47155ce4808d4580f9123",
72+
{"feaf4ba6bc635fec442f46ddd4512416ec43c2c2", "", map[string]expectedEntryInfo{
73+
"file1.txt": {
74+
CommitID: "95bb4d39648ee7e325106df01a621c530863a653",
75+
Size: 6,
76+
},
77+
"file2.txt": {
78+
CommitID: "8d92fc957a4d7cfd98bc375f0b7bb189a0d6c9f2",
79+
Size: 6,
80+
},
81+
"foo": {
82+
CommitID: "37991dec2c8e592043f47155ce4808d4580f9123",
83+
Size: 0,
84+
},
5585
}, "feaf4ba6bc635fec442f46ddd4512416ec43c2c2"},
5686
}
5787
for _, testCase := range testCases {
@@ -93,16 +123,29 @@ func testGetCommitsInfo(t *testing.T, repo1 *Repository) {
93123
for _, commitInfo := range commitsInfo {
94124
entry := commitInfo.Entry
95125
commit := commitInfo.Commit
96-
expectedID, ok := testCase.ExpectedIDs[entry.Name()]
126+
expectedInfo, ok := testCase.ExpectedIDs[entry.Name()]
97127
if !assert.True(t, ok) {
98128
continue
99129
}
100-
assert.Equal(t, expectedID, commit.ID.String())
130+
assert.Equal(t, expectedInfo.CommitID, commit.ID.String())
131+
assert.Equal(t, expectedInfo.Size, entry.Size(), entry.Name())
101132
}
102133
}
103134
}
104135

105-
func TestEntries_GetCommitsInfo(t *testing.T) {
136+
func TestEntries_GetCommitsInfo_Batch(t *testing.T) {
137+
defer test.MockVariableValue(&DefaultFeatures().SupportCatFileBatchCommand, false)()
138+
139+
testEntriesGetCommitsInfo(t)
140+
}
141+
142+
func TestEntries_GetCommitsInfo_BatchCommand(t *testing.T) {
143+
defer test.MockVariableValue(&DefaultFeatures().SupportCatFileBatchCommand, true)()
144+
145+
testEntriesGetCommitsInfo(t)
146+
}
147+
148+
func testEntriesGetCommitsInfo(t *testing.T) {
106149
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
107150
bareRepo1, err := OpenRepository(t.Context(), bareRepo1Path)
108151
assert.NoError(t, err)

modules/git/gitcmd/command.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -185,12 +185,6 @@ func (c *Command) AddConfig(key, value string) *Command {
185185
return c
186186
}
187187

188-
// ToTrustCmdArg converts a string (trusted as argument) to CmdArg
189-
// In most cases, it shouldn't be used. Use AddXxx function instead
190-
func ToTrustCmdArg(arg string) internal.CmdArg {
191-
return internal.CmdArg(arg)
192-
}
193-
194188
// ToTrustedCmdArgs converts a list of strings (trusted as argument) to TrustedCmdArgs
195189
// In most cases, it shouldn't be used. Use NewCommand().AddXxx() function instead
196190
func ToTrustedCmdArgs(args []string) TrustedCmdArgs {

modules/git/languagestats/language_stats_test.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,25 @@ import (
1010

1111
"code.gitea.io/gitea/modules/git"
1212
"code.gitea.io/gitea/modules/setting"
13+
"code.gitea.io/gitea/modules/test"
1314

1415
"github.com/stretchr/testify/assert"
1516
"github.com/stretchr/testify/require"
1617
)
1718

18-
func TestRepository_GetLanguageStats(t *testing.T) {
19+
func TestRepository_GetLanguageStats_Batch(t *testing.T) {
20+
defer test.MockVariableValue(&git.DefaultFeatures().SupportCatFileBatchCommand, true)()
21+
22+
testRepositoryGetLanguageStats(t)
23+
}
24+
25+
func TestRepository_GetLanguageStats_BatchCommand(t *testing.T) {
26+
defer test.MockVariableValue(&git.DefaultFeatures().SupportCatFileBatchCommand, true)()
27+
28+
testRepositoryGetLanguageStats(t)
29+
}
30+
31+
func testRepositoryGetLanguageStats(t *testing.T) {
1932
setting.AppDataPath = t.TempDir()
2033
repoPath := "../tests/repos/language_stats_repo"
2134
gitRepo, err := git.OpenRepository(t.Context(), repoPath)

modules/git/pipeline/lfs_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright 2025 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package pipeline
5+
6+
import (
7+
"testing"
8+
"time"
9+
10+
"code.gitea.io/gitea/modules/git"
11+
"code.gitea.io/gitea/modules/test"
12+
13+
"github.com/stretchr/testify/assert"
14+
"github.com/stretchr/testify/require"
15+
)
16+
17+
func TestFindLFSFile_Batch(t *testing.T) {
18+
defer test.MockVariableValue(&git.DefaultFeatures().SupportCatFileBatchCommand, false)()
19+
20+
testFindLFSFile(t)
21+
}
22+
23+
func TestFindLFSFile_BatchCommand(t *testing.T) {
24+
defer test.MockVariableValue(&git.DefaultFeatures().SupportCatFileBatchCommand, true)()
25+
26+
testFindLFSFile(t)
27+
}
28+
29+
func testFindLFSFile(t *testing.T) {
30+
repoPath := "../tests/repos/repo_lfs"
31+
gitRepo, err := git.OpenRepository(t.Context(), repoPath)
32+
require.NoError(t, err)
33+
defer gitRepo.Close()
34+
35+
objectID := git.MustIDFromString("2b6c6c4eaefa24b22f2092c3d54b263ff26feb58")
36+
37+
stats, err := FindLFSFile(gitRepo, objectID)
38+
require.NoError(t, err)
39+
40+
tm, err := time.Parse(time.RFC3339, "2022-12-21T17:56:42-05:00")
41+
require.NoError(t, err)
42+
43+
assert.Equal(t, []*LFSResult{
44+
{
45+
Name: "CONTRIBUTING.md",
46+
SHA: "73cf03db6ece34e12bf91e8853dc58f678f2f82d",
47+
Summary: "Initial commit",
48+
When: tm,
49+
BranchName: "master",
50+
FullCommitName: "master",
51+
},
52+
}, stats)
53+
}

modules/git/pipeline/main_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright 2025 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package pipeline
5+
6+
import (
7+
"fmt"
8+
"os"
9+
"testing"
10+
11+
"code.gitea.io/gitea/modules/git"
12+
"code.gitea.io/gitea/modules/setting"
13+
"code.gitea.io/gitea/modules/util"
14+
)
15+
16+
func testRun(m *testing.M) error {
17+
gitHomePath, err := os.MkdirTemp(os.TempDir(), "git-home")
18+
if err != nil {
19+
return fmt.Errorf("unable to create temp dir: %w", err)
20+
}
21+
defer util.RemoveAll(gitHomePath)
22+
setting.Git.HomePath = gitHomePath
23+
24+
if err = git.InitFull(); err != nil {
25+
return fmt.Errorf("failed to call Init: %w", err)
26+
}
27+
28+
exitCode := m.Run()
29+
if exitCode != 0 {
30+
return fmt.Errorf("run test failed, ExitCode=%d", exitCode)
31+
}
32+
return nil
33+
}
34+
35+
func TestMain(m *testing.M) {
36+
if err := testRun(m); err != nil {
37+
_, _ = fmt.Fprintf(os.Stderr, "Test failed: %v", err)
38+
os.Exit(1)
39+
}
40+
}

modules/git/repo_branch_test.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"path/filepath"
88
"testing"
99

10+
"code.gitea.io/gitea/modules/test"
1011
"github.com/stretchr/testify/assert"
1112
"github.com/stretchr/testify/require"
1213
)
@@ -96,7 +97,19 @@ func BenchmarkGetRefsBySha(b *testing.B) {
9697
_, _ = bareRepo5.GetRefsBySha("58a4bcc53ac13e7ff76127e0fb518b5262bf09af", "")
9798
}
9899

99-
func TestRepository_IsObjectExist(t *testing.T) {
100+
func TestRepository_IsObjectExist_Batch(t *testing.T) {
101+
defer test.MockVariableValue(&DefaultFeatures().SupportCatFileBatchCommand, false)()
102+
103+
testRepositoryIsObjectExist(t)
104+
}
105+
106+
func TestRepository_IsObjectExist_BatchCommand(t *testing.T) {
107+
defer test.MockVariableValue(&DefaultFeatures().SupportCatFileBatchCommand, true)()
108+
109+
testRepositoryIsObjectExist(t)
110+
}
111+
112+
func testRepositoryIsObjectExist(t *testing.T) {
100113
repo, err := OpenRepository(t.Context(), filepath.Join(testReposDir, "repo1_bare"))
101114
require.NoError(t, err)
102115
defer repo.Close()

0 commit comments

Comments
 (0)