Skip to content

Commit

Permalink
Make pkg/git tests not modifying global git config
Browse files Browse the repository at this point in the history
Before this patch, running unit tests on `pkg/git` would modify the
content of the machine's global git configuration (usually
`$HOME/.git/config`).

This makes sure we setup a proper isolated git config, per test.

Signed-off-by: Vincent Demeester <vdemeest@redhat.com>
  • Loading branch information
vdemeester authored and tekton-robot committed May 4, 2022
1 parent 2ffa9a5 commit d5ae54a
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions pkg/git/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,31 @@ import (

const fileMode = 0755 // rwxr-xr-x

func withTemporaryGitConfig(t *testing.T) func() {
gitConfigDir := t.TempDir()
key := "GIT_CONFIG_GLOBAL"
t.Helper()
oldValue, envVarExists := os.LookupEnv(key)
if err := os.Setenv(key, filepath.Join(gitConfigDir, "config")); err != nil {
t.Fatal(err)
}
clean := func() {
t.Helper()
if !envVarExists {
if err := os.Unsetenv(key); err != nil {
t.Fatal(err)
}
return
}
if err := os.Setenv(key, oldValue); err != nil {
t.Fatal(err)
}
}
return clean
}

func TestValidateGitSSHURLFormat(t *testing.T) {
withTemporaryGitConfig(t)
tests := []struct {
url string
want bool
Expand Down Expand Up @@ -117,6 +141,7 @@ func TestValidateGitSSHURLFormat(t *testing.T) {
}

func TestValidateGitAuth(t *testing.T) {
withTemporaryGitConfig(t)
tests := []struct {
name string
url string
Expand Down Expand Up @@ -167,6 +192,7 @@ func TestValidateGitAuth(t *testing.T) {
}

func TestUserHasKnownHostsFile(t *testing.T) {
withTemporaryGitConfig(t)
tests := []struct {
name string
want bool
Expand Down Expand Up @@ -204,6 +230,7 @@ func TestUserHasKnownHostsFile(t *testing.T) {
}

func TestEnsureHomeEnv(t *testing.T) {
withTemporaryGitConfig(t)
tests := []struct {
name string
homeenvSet bool
Expand Down Expand Up @@ -257,6 +284,7 @@ func TestEnsureHomeEnv(t *testing.T) {
}

func TestFetch(t *testing.T) {
withTemporaryGitConfig(t)
tests := []struct {
name string
logMessage string
Expand Down

0 comments on commit d5ae54a

Please sign in to comment.