Skip to content

Commit 529edbe

Browse files
committed
test: share git config setup helper
1 parent ef72813 commit 529edbe

File tree

5 files changed

+74
-24
lines changed

5 files changed

+74
-24
lines changed

internal/git/repository_remove_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"path/filepath"
77
"strings"
88
"testing"
9+
10+
"github.com/satococoa/wtp/v2/internal/testutil"
911
)
1012

1113
// runGitCommand is a helper to run git commands in tests
@@ -32,9 +34,9 @@ func checkoutMainBranch(t *testing.T, repoDir string) {
3234
func initializeTestRepo(t *testing.T, repoDir string) {
3335
t.Helper()
3436
runGitCommand(t, repoDir, "init")
35-
runGitCommand(t, repoDir, "config", "user.name", "Test User")
36-
runGitCommand(t, repoDir, "config", "user.email", "[email protected]")
37-
runGitCommand(t, repoDir, "config", "commit.gpgsign", "false")
37+
testutil.ConfigureTestRepo(t, repoDir, func(dir string, args ...string) {
38+
runGitCommand(t, dir, args...)
39+
})
3840

3941
// Create initial commit
4042
readmeFile := filepath.Join(repoDir, "README.md")

internal/git/repository_test.go

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,23 @@ import (
66
"path/filepath"
77
"strings"
88
"testing"
9+
10+
"github.com/satococoa/wtp/v2/internal/testutil"
911
)
1012

1113
func setupTestRepo(t *testing.T) string {
1214
tempDir := t.TempDir()
1315

16+
runGitCommand := func(dir string, args ...string) {
17+
t.Helper()
18+
19+
cmd := exec.Command("git", args...)
20+
cmd.Dir = dir
21+
if err := cmd.Run(); err != nil {
22+
t.Fatalf("Failed to run git %v: %v", args, err)
23+
}
24+
}
25+
1426
// Initialize git repository
1527
cmd := exec.Command("git", "init")
1628
cmd.Dir = tempDir
@@ -23,24 +35,7 @@ func setupTestRepo(t *testing.T) string {
2335
cmd.Dir = tempDir
2436
_ = cmd.Run() // Ignore error if git version is too old
2537

26-
// Configure git user
27-
cmd = exec.Command("git", "config", "user.name", "Test User")
28-
cmd.Dir = tempDir
29-
if err := cmd.Run(); err != nil {
30-
t.Fatalf("Failed to configure git user: %v", err)
31-
}
32-
33-
cmd = exec.Command("git", "config", "user.email", "[email protected]")
34-
cmd.Dir = tempDir
35-
if err := cmd.Run(); err != nil {
36-
t.Fatalf("Failed to configure git email: %v", err)
37-
}
38-
39-
cmd = exec.Command("git", "config", "commit.gpgsign", "false")
40-
cmd.Dir = tempDir
41-
if err := cmd.Run(); err != nil {
42-
t.Fatalf("Failed to disable gpgsign: %v", err)
43-
}
38+
testutil.ConfigureTestRepo(t, tempDir, runGitCommand)
4439

4540
// Create initial commit
4641
readmeFile := filepath.Join(tempDir, "README.md")

internal/testutil/git.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package testutil
2+
3+
import "testing"
4+
5+
// ConfigureTestRepo applies common git configuration used in tests.
6+
//
7+
// The runner is responsible for executing git commands within the provided
8+
// repository directory and should handle errors appropriately.
9+
func ConfigureTestRepo(t *testing.T, repoDir string, runner func(dir string, args ...string)) {
10+
t.Helper()
11+
12+
commands := [][]string{
13+
{"config", "user.name", "Test User"},
14+
{"config", "user.email", "[email protected]"},
15+
{"config", "commit.gpgsign", "false"},
16+
}
17+
18+
for _, args := range commands {
19+
runner(repoDir, args...)
20+
}
21+
}

internal/testutil/git_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package testutil
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func TestConfigureTestRepo(t *testing.T) {
9+
repoDir := t.TempDir()
10+
11+
var calls [][]string
12+
runner := func(dir string, args ...string) {
13+
if dir != repoDir {
14+
t.Fatalf("runner dir = %s, want %s", dir, repoDir)
15+
}
16+
calls = append(calls, args)
17+
}
18+
19+
ConfigureTestRepo(t, repoDir, runner)
20+
21+
want := [][]string{
22+
{"config", "user.name", "Test User"},
23+
{"config", "user.email", "[email protected]"},
24+
{"config", "commit.gpgsign", "false"},
25+
}
26+
27+
if !reflect.DeepEqual(calls, want) {
28+
t.Fatalf("runner calls = %#v, want %#v", calls, want)
29+
}
30+
}

test/e2e/framework/framework.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"strings"
1010
"testing"
1111
"time"
12+
13+
"github.com/satococoa/wtp/v2/internal/testutil"
1214
)
1315

1416
const (
@@ -97,9 +99,9 @@ func (e *TestEnvironment) CreateTestRepo(name string) *TestRepo {
9799
repoDir := filepath.Join(e.tmpDir, name)
98100

99101
e.run("git", "init", repoDir)
100-
e.runInDir(repoDir, "git", "config", "user.name", "Test User")
101-
e.runInDir(repoDir, "git", "config", "user.email", "[email protected]")
102-
e.runInDir(repoDir, "git", "config", "commit.gpgsign", "false")
102+
testutil.ConfigureTestRepo(e.t, repoDir, func(dir string, args ...string) {
103+
e.runInDir(dir, "git", args...)
104+
})
103105

104106
// Ensure the default branch is 'main' regardless of global git config
105107
e.runInDir(repoDir, "git", "config", "init.defaultBranch", "main")

0 commit comments

Comments
 (0)