tests: remove panics from test fixtures (#7522)

This commit is contained in:
Sam Kleinman
2022-01-06 17:34:32 -05:00
committed by GitHub
parent a91d3cb894
commit 569629486b
27 changed files with 815 additions and 858 deletions
+11 -15
View File
@@ -18,17 +18,12 @@ import (
)
// clearConfig clears env vars, the given root dir, and resets viper.
func clearConfig(dir string) {
if err := os.Unsetenv("TMHOME"); err != nil {
panic(err)
}
if err := os.Unsetenv("TM_HOME"); err != nil {
panic(err)
}
func clearConfig(t *testing.T, dir string) {
t.Helper()
require.NoError(t, os.Unsetenv("TMHOME"))
require.NoError(t, os.Unsetenv("TM_HOME"))
require.NoError(t, os.RemoveAll(dir))
if err := os.RemoveAll(dir); err != nil {
panic(err)
}
viper.Reset()
config = cfg.DefaultConfig()
}
@@ -46,8 +41,9 @@ func testRootCmd() *cobra.Command {
return rootCmd
}
func testSetup(rootDir string, args []string, env map[string]string) error {
clearConfig(rootDir)
func testSetup(t *testing.T, rootDir string, args []string, env map[string]string) error {
t.Helper()
clearConfig(t, rootDir)
rootCmd := testRootCmd()
cmd := cli.PrepareBaseCmd(rootCmd, "TM", rootDir)
@@ -73,7 +69,7 @@ func TestRootHome(t *testing.T) {
for i, tc := range cases {
idxString := strconv.Itoa(i)
err := testSetup(defaultRoot, tc.args, tc.env)
err := testSetup(t, defaultRoot, tc.args, tc.env)
require.NoError(t, err, idxString)
assert.Equal(t, tc.root, config.RootDir, idxString)
@@ -105,7 +101,7 @@ func TestRootFlagsEnv(t *testing.T) {
for i, tc := range cases {
idxString := strconv.Itoa(i)
err := testSetup(defaultRoot, tc.args, tc.env)
err := testSetup(t, defaultRoot, tc.args, tc.env)
require.NoError(t, err, idxString)
assert.Equal(t, tc.logLevel, config.LogLevel, idxString)
@@ -134,7 +130,7 @@ func TestRootConfig(t *testing.T) {
for i, tc := range cases {
defaultRoot := t.TempDir()
idxString := strconv.Itoa(i)
clearConfig(defaultRoot)
clearConfig(t, defaultRoot)
// XXX: path must match cfg.defaultConfigPath
configFilePath := filepath.Join(defaultRoot, "config")