Skip to content

Commit

Permalink
Make config filenames constant
Browse files Browse the repository at this point in the history
  • Loading branch information
mostafa committed Sep 18, 2023
1 parent 4394807 commit 90e40cb
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 18 deletions.
5 changes: 4 additions & 1 deletion cmd/cmd_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import (
"github.com/spf13/cobra"
)

var pluginTestConfigFile = "./test.yaml"
var (
globalTestConfigFile = "./test_global.yaml"
pluginTestConfigFile = "./test_plugins.yaml"
)

// executeCommandC executes a cobra command and returns the command, output, and error.
// Taken from https://github.com/spf13/cobra/blob/0c72800b8dba637092b57a955ecee75949e79a73/command_test.go#L48.
Expand Down
13 changes: 5 additions & 8 deletions cmd/config_init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,25 @@ import (
)

func Test_configInitCmd(t *testing.T) {
// Reset globalConfigFile to avoid conflicts with other tests.
globalConfigFile = "./test_config.yaml"

// Test configInitCmd.
output, err := executeCommandC(rootCmd, "config", "init", "-c", globalConfigFile)
output, err := executeCommandC(rootCmd, "config", "init", "-c", globalTestConfigFile)
assert.NoError(t, err, "configInitCmd should not return an error")
assert.Equal(t,
fmt.Sprintf("Config file '%s' was created successfully.", globalConfigFile),
fmt.Sprintf("Config file '%s' was created successfully.", globalTestConfigFile),
output,
"configInitCmd should print the correct output")
// Check that the config file was created.
assert.FileExists(t, globalConfigFile, "configInitCmd should create a config file")
assert.FileExists(t, globalTestConfigFile, "configInitCmd should create a config file")

// Test configInitCmd with the --force flag to overwrite the config file.
output, err = executeCommandC(rootCmd, "config", "init", "--force")
assert.NoError(t, err, "configInitCmd should not return an error")
assert.Equal(t,
fmt.Sprintf("Config file '%s' was overwritten successfully.", globalConfigFile),
fmt.Sprintf("Config file '%s' was overwritten successfully.", globalTestConfigFile),
output,
"configInitCmd should print the correct output")

// Clean up.
err = os.Remove(globalConfigFile)
err = os.Remove(globalTestConfigFile)
assert.NoError(t, err)
}
13 changes: 5 additions & 8 deletions cmd/config_lint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,25 @@ import (
)

func Test_configLintCmd(t *testing.T) {
// Reset globalConfigFile to avoid conflicts with other tests.
globalConfigFile = "./test_config.yaml"

// Test configInitCmd.
output, err := executeCommandC(rootCmd, "config", "init", "-c", globalConfigFile)
output, err := executeCommandC(rootCmd, "config", "init", "-c", globalTestConfigFile)
assert.NoError(t, err, "configInitCmd should not return an error")
assert.Equal(t,
fmt.Sprintf("Config file '%s' was created successfully.", globalConfigFile),
fmt.Sprintf("Config file '%s' was created successfully.", globalTestConfigFile),
output,
"configInitCmd should print the correct output")
// Check that the config file was created.
assert.FileExists(t, globalConfigFile, "configInitCmd should create a config file")
assert.FileExists(t, globalTestConfigFile, "configInitCmd should create a config file")

// Test configLintCmd.
output, err = executeCommandC(rootCmd, "config", "lint", "-c", globalConfigFile)
output, err = executeCommandC(rootCmd, "config", "lint", "-c", globalTestConfigFile)
assert.NoError(t, err, "configLintCmd should not return an error")
assert.Equal(t,
"global config is valid\n",
output,
"configLintCmd should print the correct output")

// Clean up.
err = os.Remove(globalConfigFile)
err = os.Remove(globalTestConfigFile)
assert.NoError(t, err)
}
2 changes: 1 addition & 1 deletion cmd/plugin_install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func Test_pluginInstallCmd(t *testing.T) {
// Create a test config file.
// Create a test plugin config file.
output, err := executeCommandC(rootCmd, "plugin", "init", "-p", pluginTestConfigFile)
assert.NoError(t, err, "plugin init should not return an error")
assert.Equal(t,
Expand Down

0 comments on commit 90e40cb

Please sign in to comment.