test: isolate tests from system/global git config · Entire

test: isolate tests from system/global git config

2f730b7→main·

pjbgf·4d ago·5 files·+35 added/-11 removed

go-git's default NewAuto ConfigLoader and the git binary both read the host's global/system config, leaking settings like commit.gpgSign=true into tests. Add syncertest.IsolateGitConfig, which points GIT_CONFIG_GLOBAL/SYSTEM at os.DevNull, and call it from each affected TestMain. This replaces the syncer package's go-git-only plugin override, which left the SSH integration test still reading the host config.

Assisted-by: Claude Opus 4.8 noreply@anthropic.com Signed-off-by: Paulo Gomes paulo@entire.io

Sessions

01KXDSREBG8S07KQ15HHD21NHFView transcript

Changes

5

7 unmodified lines

8
9
10
11
12
13
14
5 unmodified lines

20
21
22
23
24
25
26
27
28
29
30

7 unmodified lines

"io"
    "net/http"
    "net/http/httptest"
    "os"
    "testing"

git "github.com/go-git/go-git/v6"
5 unmodified lines

"entire.io/entire/git-sync/internal/syncertest"
)

func TestMain(m *testing.M) {
    syncertest.IsolateGitConfig()
    os.Exit(m.Run())
}

type errAuthProvider struct{}

func (errAuthProvider) AuthFor(_ context.Context, _ Endpoint, _ EndpointRole) (EndpointAuth, error) {

Mclient_test.go+6

46 unmodified lines

47
48
49
50
51
52
53

46 unmodified lines

// Tests that need to exercise helper behaviour explicitly should
// restore auth.GitCredentialCommand in their own setup.
func TestMain(m *testing.M) {
    syncertest.IsolateGitConfig()
    auth.GitCredentialCommand = func(_ context.Context, _ auth.CredentialOp, _ string) ([]byte, error) {
        return nil, errors.New("no helper configured (test default)")
    }
}

Mcmd/git-sync/main_test.go+1

1 unmodified line

2 3 4 5 6 7 8 5 unmodified lines

14 15 16 17 18 19 20 21 22 23 24

1 unmodified line

import ( "context" "os" "testing"

"github.com/go-git/go-git/v6/plumbing" 5 unmodified lines

"entire.io/entire/git-sync/internal/syncertest" )

func TestMain(m *testing.M) { syncertest.IsolateGitConfig() os.Exit(m.Run()) }

func TestDefaultMaxMaterializedObjectsExported(t *testing.T) { // Verify the constant is exported and has a reasonable positive value. if DefaultMaxMaterializedObjects <= 0 {


Minternal/strategy/materialized/materialized_test.go+6

7 unmodified lines

8 9 10 11 12 13 14 17 unmodified lines

32 33 34 34 35 35 36 37 4251 unmodified lines

4289 4290 4291 4293 4294 4295 4296 4297 4298 4299 4300 4301 4292 4293 4294 4295 4296 4297 4298

7 unmodified lines

"io" "net/http" "net/http/httptest" "os" "sort" "strings" "sync" 17 unmodified lines

"github.com/go-git/go-git/v6/plumbing/revlist" "github.com/go-git/go-git/v6/plumbing/transport" "github.com/go-git/go-git/v6/storage/memory" "github.com/go-git/go-git/v6/x/plugin" "github.com/go-git/go-git/v6/x/plugin/config" )

const ( 4251 unmodified lines

}

func TestMain(m *testing.M) { // Ensures empty config files for system/global so that test execution // is not affected by environmental settings (e.g. commit.gpgSign=true). if err := plugin.Register(plugin.ConfigLoader(), func() plugin.ConfigSource { return config.NewEmpty() }); err != nil { panic("register go-git empty config loader: " + err.Error()) }

m.Run() // Isolate tests from the host's system/global git config (e.g. // commit.gpgSign=true). This covers both go-git and the git binary the // SSH integration tests shell out to, which the go-git-only plugin // override did not. syncertest.IsolateGitConfig() os.Exit(m.Run()) }


Minternal/syncer/integration_test.go+7/-11

2 unmodified lines

3 4 5 6 7 8 9 7 unmodified lines

17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36

2 unmodified lines

import ( "fmt" "io" "os" "strings" "testing" "time" 7 unmodified lines

"github.com/go-git/go-git/v6/storage/memory" )

// IsolateGitConfig makes the current process ignore the host's global and // system git configuration for the lifetime of the test binary, so developer // settings such as commit.gpgSign=true do not leak into tests. // // Both go-git (via its default NewAuto ConfigLoader plugin) and the git binary // honour GIT_CONFIG_GLOBAL and GIT_CONFIG_SYSTEM; pointing them at os.DevNull // yields empty global/system config. Call this from TestMain before m.Run(). // It uses os.Setenv rather than testing.T.Setenv because the suites run tests // in parallel, which forbids per-test Setenv. func IsolateGitConfig() { _ = os.Setenv("GIT_CONFIG_GLOBAL", os.DevNull) _ = os.Setenv("GIT_CONFIG_SYSTEM", os.DevNull) }

// SetRefAtBranch points an arbitrary ref (e.g. refs/notes/commits) at the // current tip of branch and returns the resolved hash. Used by --all-refs // integration tests to seed non-branch/non-tag refs.