Skip to content

Commit

Permalink
Override home directory to avoid tests failing because ~/.woke.yaml e…
Browse files Browse the repository at this point in the history
…xists (#164)
  • Loading branch information
caitlinelfring authored Nov 22, 2021
1 parent ec1b9ae commit 9db4e4c
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/get-woke/woke/pkg/output"
"github.com/get-woke/woke/pkg/parser"

"github.com/mitchellh/go-homedir"
"github.com/rs/zerolog"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand All @@ -31,14 +32,6 @@ func BenchmarkRootRunE(b *testing.B) {
}
}

func setTestConfigFile(t *testing.T, filename string) {
origConfigFile := viper.ConfigFileUsed()
t.Cleanup(func() {
viper.SetConfigFile(origConfigFile)
})
viper.SetConfigFile(filename)
}

func TestInitConfig(t *testing.T) {
tests := []struct {
desc string
Expand All @@ -57,6 +50,9 @@ func TestInitConfig(t *testing.T) {
cfgFile: "../testdata/invalid.yml",
},
}

overrideHomeDir(t)

for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
t.Cleanup(func() {
Expand Down Expand Up @@ -150,3 +146,25 @@ func TestRunE(t *testing.T) {
assert.Error(t, err)
})
}

// helper functions

func setTestConfigFile(t *testing.T, filename string) {
origConfigFile := viper.ConfigFileUsed()
t.Cleanup(func() {
viper.SetConfigFile(origConfigFile)
})
viper.SetConfigFile(filename)
}

// overrideHomeDir to avoid pulling in a config file in the home directory
// while running tests
func overrideHomeDir(t *testing.T) {
origHome := os.Getenv("HOME")
t.Cleanup(func() {
os.Setenv("HOME", origHome)
homedir.Reset()
})
os.Setenv("HOME", "foo")
homedir.Reset()
}

0 comments on commit 9db4e4c

Please sign in to comment.