Skip to content

Commit 872918f

Browse files
committed
refactor
1 parent 4c6d91a commit 872918f

File tree

15 files changed

+40
-262
lines changed

15 files changed

+40
-262
lines changed

modules/git/attribute/main_test.go

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,11 @@
44
package attribute
55

66
import (
7-
"fmt"
8-
"os"
97
"testing"
108

119
"code.gitea.io/gitea/modules/git"
12-
"code.gitea.io/gitea/modules/setting"
13-
"code.gitea.io/gitea/modules/util"
1410
)
1511

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-
3512
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-
}
13+
git.RunGitTests(m)
4014
}

modules/git/batch_reader.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ func (b *catFileBatchCommunicator) Close() {
4343
// ensureValidGitRepository runs git rev-parse in the repository path - thus ensuring that the repository is a valid repository.
4444
// Run before opening git cat-file.
4545
// This is needed otherwise the git cat-file will hang for invalid repositories.
46+
// FIXME: the comment is from https://github.com/go-gitea/gitea/pull/17991 but it doesn't seem to be true, there must be some bugs in code, not git's problem
4647
func ensureValidGitRepository(ctx context.Context, repoPath string) error {
4748
stderr := strings.Builder{}
4849
err := gitcmd.NewCommand("rev-parse").

modules/git/blob_test.go

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

12-
"code.gitea.io/gitea/modules/test"
13-
1412
"github.com/stretchr/testify/assert"
1513
"github.com/stretchr/testify/require"
1614
)
1715

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) {
16+
func TestBlob_Data(t *testing.T) {
3117
output := "file2\n"
3218
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
3319
repo, err := OpenRepository(t.Context(), bareRepo1Path)

modules/git/commit_info_test.go

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import (
88
"testing"
99
"time"
1010

11-
"code.gitea.io/gitea/modules/test"
12-
1311
"github.com/stretchr/testify/assert"
1412
"github.com/stretchr/testify/require"
1513
)
@@ -134,19 +132,7 @@ func testGetCommitsInfo(t *testing.T, repo1 *Repository) {
134132
}
135133
}
136134

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

modules/git/git.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"code.gitea.io/gitea/modules/git/gitcmd"
1818
"code.gitea.io/gitea/modules/log"
1919
"code.gitea.io/gitea/modules/setting"
20+
"code.gitea.io/gitea/modules/tempdir"
2021

2122
"github.com/hashicorp/go-version"
2223
)
@@ -178,3 +179,23 @@ func InitFull() (err error) {
178179

179180
return syncGitConfig(context.Background())
180181
}
182+
183+
func RunGitTests(m interface{ Run() int }) {
184+
fatalf := func(format string, args ...any) {
185+
_, _ = fmt.Fprintf(os.Stderr, format, args...)
186+
os.Exit(1)
187+
}
188+
gitHomePath, cleanup, err := tempdir.OsTempDir("gitea-test").MkdirTempRandom("git-home")
189+
if err != nil {
190+
fatalf("unable to create temp dir: %w", err)
191+
}
192+
defer cleanup()
193+
194+
setting.Git.HomePath = gitHomePath
195+
if err = InitFull(); err != nil {
196+
fatalf("failed to call Init: %w", err)
197+
}
198+
if exitCode := m.Run(); exitCode != 0 {
199+
fatalf("run test failed, ExitCode=%d", exitCode)
200+
}
201+
}

modules/git/git_test.go

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,14 @@
44
package git
55

66
import (
7-
"fmt"
8-
"os"
97
"testing"
108

11-
"code.gitea.io/gitea/modules/setting"
12-
"code.gitea.io/gitea/modules/tempdir"
13-
149
"github.com/hashicorp/go-version"
1510
"github.com/stretchr/testify/assert"
1611
)
1712

18-
func testRun(m *testing.M) error {
19-
gitHomePath, cleanup, err := tempdir.OsTempDir("gitea-test").MkdirTempRandom("git-home")
20-
if err != nil {
21-
return fmt.Errorf("unable to create temp dir: %w", err)
22-
}
23-
defer cleanup()
24-
25-
setting.Git.HomePath = gitHomePath
26-
27-
if err = InitFull(); err != nil {
28-
return fmt.Errorf("failed to call Init: %w", err)
29-
}
30-
31-
exitCode := m.Run()
32-
if exitCode != 0 {
33-
return fmt.Errorf("run test failed, ExitCode=%d", exitCode)
34-
}
35-
return nil
36-
}
37-
3813
func TestMain(m *testing.M) {
39-
if err := testRun(m); err != nil {
40-
_, _ = fmt.Fprintf(os.Stderr, "Test failed: %v", err)
41-
os.Exit(1)
42-
}
14+
RunGitTests(m)
4315
}
4416

4517
func TestParseGitVersion(t *testing.T) {

modules/git/gitcmd/command_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import (
1515
)
1616

1717
func TestMain(m *testing.M) {
18+
// FIXME: the package dependency is not right
19+
// "setting.Git.HomePath" is initialized in "git" package but really used in "gitcmd" package
1820
gitHomePath, cleanup, err := tempdir.OsTempDir("gitea-test").MkdirTempRandom("git-home")
1921
if err != nil {
2022
_, _ = fmt.Fprintf(os.Stderr, "unable to create temp dir: %v", err)

modules/git/languagestats/language_stats_test.go

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

1111
"code.gitea.io/gitea/modules/git"
1212
"code.gitea.io/gitea/modules/setting"
13-
"code.gitea.io/gitea/modules/test"
1413

1514
"github.com/stretchr/testify/assert"
1615
"github.com/stretchr/testify/require"
1716
)
1817

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) {
18+
func TestRepository_GetLanguageStats(t *testing.T) {
3219
setting.AppDataPath = t.TempDir()
3320
repoPath := "../tests/repos/language_stats_repo"
3421
gitRepo, err := git.OpenRepository(t.Context(), repoPath)

modules/git/languagestats/main_test.go

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,11 @@
44
package languagestats
55

66
import (
7-
"fmt"
8-
"os"
97
"testing"
108

119
"code.gitea.io/gitea/modules/git"
12-
"code.gitea.io/gitea/modules/setting"
13-
"code.gitea.io/gitea/modules/util"
1410
)
1511

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-
3512
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-
}
13+
git.RunGitTests(m)
4014
}

modules/git/pipeline/lfs_test.go

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,12 @@ import (
88
"time"
99

1010
"code.gitea.io/gitea/modules/git"
11-
"code.gitea.io/gitea/modules/test"
1211

1312
"github.com/stretchr/testify/assert"
1413
"github.com/stretchr/testify/require"
1514
)
1615

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) {
16+
func TestFindLFSFile(t *testing.T) {
3017
repoPath := "../../../tests/gitea-repositories-meta/user2/lfs.git"
3118
gitRepo, err := git.OpenRepository(t.Context(), repoPath)
3219
require.NoError(t, err)

0 commit comments

Comments
 (0)