Skip to content

Commit

Permalink
Tests should update test-local git configuration files (#3262)
Browse files Browse the repository at this point in the history
The tests would mutate $HOME/.gitconfig and $HOME/.git-credentials.
Set the HOME environment variable to a suitable temporary
directory before running git commands that mutate the global config.

Fixes #2604
  • Loading branch information
finnag authored Mar 24, 2023
1 parent 8b90c8b commit ff5894c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
1 change: 1 addition & 0 deletions server/events/vcs/gh_app_creds_rotator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func Test_githubAppTokenRotator_GenerateJob(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tmpDir := t.TempDir()
t.Setenv("HOME", tmpDir)
r := vcs.NewGithubAppTokenRotator(logging.NewNoopLogger(t), tt.fields.githubCredentials, testServer, tmpDir)
got, err := r.GenerateJob()
if (err != nil) != tt.wantErr {
Expand Down
8 changes: 8 additions & 0 deletions server/events/vcs/git_cred_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
func TestWriteGitCreds_WriteFile(t *testing.T) {
logger := logging.NewNoopLogger(t)
tmp := t.TempDir()
t.Setenv("HOME", tmp)

err := vcs.WriteGitCreds("user", "token", "hostname", tmp, logger, false)
Ok(t, err)
Expand All @@ -32,6 +33,7 @@ func TestWriteGitCreds_WriteFile(t *testing.T) {
func TestWriteGitCreds_Appends(t *testing.T) {
logger := logging.NewNoopLogger(t)
tmp := t.TempDir()
t.Setenv("HOME", tmp)

credsFile := filepath.Join(tmp, ".git-credentials")
err := os.WriteFile(credsFile, []byte("contents"), 0600)
Expand All @@ -51,6 +53,7 @@ func TestWriteGitCreds_Appends(t *testing.T) {
func TestWriteGitCreds_NoModification(t *testing.T) {
logger := logging.NewNoopLogger(t)
tmp := t.TempDir()
t.Setenv("HOME", tmp)

credsFile := filepath.Join(tmp, ".git-credentials")
contents := "line1\nhttps://user:token@hostname\nline2"
Expand All @@ -68,6 +71,7 @@ func TestWriteGitCreds_NoModification(t *testing.T) {
func TestWriteGitCreds_ReplaceApp(t *testing.T) {
logger := logging.NewNoopLogger(t)
tmp := t.TempDir()
t.Setenv("HOME", tmp)

credsFile := filepath.Join(tmp, ".git-credentials")
contents := "line1\nhttps://x-access-token:v1.87dddddddddddddddd@github.com\nline2"
Expand All @@ -86,6 +90,7 @@ func TestWriteGitCreds_ReplaceApp(t *testing.T) {
func TestWriteGitCreds_AppendApp(t *testing.T) {
logger := logging.NewNoopLogger(t)
tmp := t.TempDir()
t.Setenv("HOME", tmp)

credsFile := filepath.Join(tmp, ".git-credentials")
contents := ""
Expand All @@ -105,6 +110,7 @@ func TestWriteGitCreds_AppendApp(t *testing.T) {
func TestWriteGitCreds_ErrIfCannotRead(t *testing.T) {
logger := logging.NewNoopLogger(t)
tmp := t.TempDir()
t.Setenv("HOME", tmp)

credsFile := filepath.Join(tmp, ".git-credentials")
err := os.WriteFile(credsFile, []byte("can't see me!"), 0000)
Expand All @@ -128,6 +134,7 @@ func TestWriteGitCreds_ErrIfCannotWrite(t *testing.T) {
func TestWriteGitCreds_ConfigureGitCredentialHelper(t *testing.T) {
logger := logging.NewNoopLogger(t)
tmp := t.TempDir()
t.Setenv("HOME", tmp)

err := vcs.WriteGitCreds("user", "token", "hostname", tmp, logger, false)
Ok(t, err)
Expand All @@ -142,6 +149,7 @@ func TestWriteGitCreds_ConfigureGitCredentialHelper(t *testing.T) {
func TestWriteGitCreds_ConfigureGitUrlOverride(t *testing.T) {
logger := logging.NewNoopLogger(t)
tmp := t.TempDir()
t.Setenv("HOME", tmp)

err := vcs.WriteGitCreds("user", "token", "hostname", tmp, logger, false)
Ok(t, err)
Expand Down

0 comments on commit ff5894c

Please sign in to comment.