Skip to content

Commit

Permalink
test: FIx some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
haijima committed Dec 29, 2023
1 parent 4ba97c2 commit 9391124
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 32 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ stool genconf path/to/main.go --format yaml >> .stool.yaml
#### Global Options

- `--config string` : Config file (default is `$XDG_CONFIG_HOME/.stool.yaml`)
- `--no_color`: Disable colorized output
- `--no-color`: Disable colorized output
- `-q, --quiet`: Quiet output
- `--verbosity int`: Verbosity level (default `0`)

Expand Down
4 changes: 4 additions & 0 deletions cmd/aa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
func TestNewAaCommand(t *testing.T) {
v := viper.New()
fs := afero.NewMemMapFs()
v.SetFs(fs)
cmd := NewAaCmd(v, fs)

assert.Equal(t, "aa", cmd.Name())
Expand All @@ -20,6 +21,7 @@ func TestNewAaCommand(t *testing.T) {
func TestNewAaCommand_Run(t *testing.T) {
v := viper.New()
fs := afero.NewMemMapFs()
v.SetFs(fs)
cmd := NewAaCmd(v, fs)
stdout := new(bytes.Buffer)
cmd.SetOut(stdout)
Expand All @@ -32,6 +34,7 @@ func TestNewAaCommand_Run(t *testing.T) {
func TestNewAaCommand_Flag_Big(t *testing.T) {
v := viper.New()
fs := afero.NewMemMapFs()
v.SetFs(fs)
cmd := NewAaCmd(v, fs)
v.Set("big", true)
stdout := new(bytes.Buffer)
Expand All @@ -45,6 +48,7 @@ func TestNewAaCommand_Flag_Big(t *testing.T) {
func TestNewAaCommand_Flag_Text(t *testing.T) {
v := viper.New()
fs := afero.NewMemMapFs()
v.SetFs(fs)
cmd := NewAaCmd(v, fs)
v.Set("text", true)
stdout := new(bytes.Buffer)
Expand Down
6 changes: 6 additions & 0 deletions cmd/genconf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
func TestNewGenConfCmd(t *testing.T) {
v := viper.New()
fs := afero.NewMemMapFs()
v.SetFs(fs)
cmd := NewGenConfCmd(v, fs)

assert.Equal(t, "genconf", cmd.Name(), "NewGenConfCmd() should return command named \"genconf\". but: \"%s\"", cmd.Name())
Expand All @@ -23,6 +24,7 @@ func TestNewGenConfCmd(t *testing.T) {
func TestNewGenConfCmd_Flag(t *testing.T) {
v := viper.New()
fs := afero.NewMemMapFs()
v.SetFs(fs)
cmd := NewGenConfCmd(v, fs)
formatFlag := cmd.Flags().Lookup("format")

Expand All @@ -34,6 +36,7 @@ func TestNewGenConfCmd_Flag(t *testing.T) {
func Test_printMatchingGroupInToml(t *testing.T) {
v := viper.New()
fs := afero.NewMemMapFs()
v.SetFs(fs)
cmd := NewGenConfCmd(v, fs)

stdout := new(bytes.Buffer)
Expand All @@ -48,6 +51,7 @@ func Test_printMatchingGroupInToml(t *testing.T) {
func Test_printMatchingGroupInYaml(t *testing.T) {
v := viper.New()
fs := afero.NewMemMapFs()
v.SetFs(fs)
cmd := NewGenConfCmd(v, fs)

stdout := new(bytes.Buffer)
Expand All @@ -62,6 +66,7 @@ func Test_printMatchingGroupInYaml(t *testing.T) {
func Test_printMatchingGroupInJson(t *testing.T) {
v := viper.New()
fs := afero.NewMemMapFs()
v.SetFs(fs)
cmd := NewGenConfCmd(v, fs)

stdout := new(bytes.Buffer)
Expand All @@ -76,6 +81,7 @@ func Test_printMatchingGroupInJson(t *testing.T) {
func Test_printArgNotBasicLitError(t *testing.T) {
v := viper.New()
fs := afero.NewMemMapFs()
v.SetFs(fs)
cmd := NewGenConfCmd(v, fs)

stderr := new(bytes.Buffer)
Expand Down
2 changes: 2 additions & 0 deletions cmd/param_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func TestNewParamCmd(t *testing.T) {
p := internal.NewParamProfiler()
v := viper.New()
fs := afero.NewMemMapFs()
v.SetFs(fs)
cmd := NewParamCmd(p, v, fs)

assert.Equal(t, "param", cmd.Name(), "NewParamCmd() should return command named \"param\". but: \"%s\"", cmd.Name())
Expand All @@ -25,6 +26,7 @@ func TestNewParamCmd_Flag(t *testing.T) {
p := internal.NewParamProfiler()
v := viper.New()
fs := afero.NewMemMapFs()
v.SetFs(fs)
cmd := NewParamCmd(p, v, fs)
fileFlag := cmd.Flags().Lookup("file")
filterFlag := cmd.Flags().Lookup("filter")
Expand Down
68 changes: 37 additions & 31 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import (
)

func TestNewRootCmd(t *testing.T) {
fs := afero.NewMemMapFs()
v := viper.New()
fs := afero.NewMemMapFs()
v.SetFs(fs)
cmd := NewRootCmd(v, fs)

assert.Equal(t, "stool", cmd.Name(), "NewRootCommand() should return command named \"stool\". but: \"%s\"", cmd.Name())
Expand All @@ -21,36 +22,38 @@ func TestNewRootCmd(t *testing.T) {
}

func TestNewRootCmd_Flag(t *testing.T) {
fs := afero.NewMemMapFs()
v := viper.New()
fs := afero.NewMemMapFs()
v.SetFs(fs)
cmd := NewRootCmd(v, fs)
//fileFlag := cmd.PersistentFlags().Lookup("file")
//matchingGroupsFlag := cmd.PersistentFlags().Lookup("matching_groups")
//timeFormatFlag := cmd.PersistentFlags().Lookup("time_format")
//logLabelsFlag := cmd.PersistentFlags().Lookup("log_labels")
//filterFlag := cmd.PersistentFlags().Lookup("filter")
noColorFlag := cmd.PersistentFlags().Lookup("no_color")
versionFlag := cmd.PersistentFlags().Lookup("version")
configFlag := cmd.PersistentFlags().Lookup("config")
noColorFlag := cmd.PersistentFlags().Lookup("no-color")
verboseFlag := cmd.PersistentFlags().Lookup("verbose")
quietFlag := cmd.PersistentFlags().Lookup("quiet")

assert.True(t, cmd.HasAvailablePersistentFlags(), "root command should have available flag")
assert.NotNil(t, noColorFlag, "root command should have \"no_color\" flag")
assert.Equal(t, "", noColorFlag.Shorthand, "\"no_color\" flag doesn't have shorthand")
assert.Equal(t, "bool", noColorFlag.Value.Type(), "\"no_color\" flag is string")
//assert.NotNil(t, fileFlag, "root command should have \"file\" flag")
//assert.Equal(t, "f", fileFlag.Shorthand, "\"file\" flag's shorthand is \"f\"")
//assert.Equal(t, "string", fileFlag.Value.Type(), "\"file\" flag is string")
//assert.NotNil(t, matchingGroupsFlag, "root command should have \"matching_groups\" flag")
//assert.Equal(t, "m", matchingGroupsFlag.Shorthand, "\"matching_groups\" flag's shorthand is \"m\"")
//assert.Equal(t, "stringSlice", matchingGroupsFlag.Value.Type(), "\"matching_groups\" flag is string slice")
//assert.NotNil(t, timeFormatFlag, "root command should have \"time_format\" flag")
//assert.Equal(t, "string", timeFormatFlag.Value.Type(), "\"time_format\" flag is string")
//assert.NotNil(t, logLabelsFlag, "root command should have \"log_labels\" flag")
//assert.Equal(t, "stringToString", logLabelsFlag.Value.Type(), "\"log_labels\" flag is stringToString")
//assert.NotNil(t, filterFlag, "root command should have \"filter\" flag")
//assert.Equal(t, "string", filterFlag.Value.Type(), "\"filter\" flag is string")

assert.NotNil(t, versionFlag, "root command should have \"version\" flag")
assert.Equal(t, "V", versionFlag.Shorthand, "\"version\" flag doesn't have shorthand")
assert.Equal(t, "bool", versionFlag.Value.Type(), "\"version\" flag is bool")
assert.NotNil(t, configFlag, "root command should have \"config\" flag")
assert.Equal(t, "", configFlag.Shorthand, "\"config\" flag doesn't have shorthand")
assert.Equal(t, "string", configFlag.Value.Type(), "\"config\" flag is string")
assert.NotNil(t, noColorFlag, "root command should have \"no-color\" flag")
assert.Equal(t, "", noColorFlag.Shorthand, "\"no-color\" flag doesn't have shorthand")
assert.Equal(t, "bool", noColorFlag.Value.Type(), "\"no-color\" flag is bool")
assert.NotNil(t, verboseFlag, "root command should have \"verbose\" flag")
assert.Equal(t, "v", verboseFlag.Shorthand, "\"verbose\" flag doesn't have shorthand")
assert.Equal(t, "count", verboseFlag.Value.Type(), "\"verbose\" flag is count")
assert.NotNil(t, quietFlag, "root command should have \"quiet\" flag")
assert.Equal(t, "q", quietFlag.Shorthand, "\"quiet\" flag doesn't have shorthand")
assert.Equal(t, "bool", quietFlag.Value.Type(), "\"quiet\" flag is bool")
}
func TestNewRootCmd_Flag_Verbose(t *testing.T) {
fs := afero.NewMemMapFs()
v := viper.New()
fs := afero.NewMemMapFs()
v.SetFs(fs)
cmd := NewRootCmd(v, fs)

v.Set("verbose", true)
Expand All @@ -59,12 +62,13 @@ func TestNewRootCmd_Flag_Verbose(t *testing.T) {
cmd.SetArgs([]string{}) // dummy not to use os.Args[1:]
err := cmd.Execute()

assert.Nil(t, err)
assert.NoError(t, err)
}

func TestNewRootCmd_Flag_Debug(t *testing.T) {
fs := afero.NewMemMapFs()
v := viper.New()
fs := afero.NewMemMapFs()
v.SetFs(fs)
cmd := NewRootCmd(v, fs)

v.Set("debug", true)
Expand All @@ -73,12 +77,13 @@ func TestNewRootCmd_Flag_Debug(t *testing.T) {
cmd.SetArgs([]string{}) // dummy not to use os.Args[1:]
err := cmd.Execute()

assert.Nil(t, err)
assert.NoError(t, err)
}

func TestNewRootCmd_ConfigFile(t *testing.T) {
fs := afero.NewMemMapFs()
v := viper.New()
fs := afero.NewMemMapFs()
v.SetFs(fs)
cmd := NewRootCmd(v, fs)

_, _ = fs.Create(".stool.yaml")
Expand All @@ -87,15 +92,16 @@ func TestNewRootCmd_ConfigFile(t *testing.T) {
cmd.SetArgs([]string{"--config", ".stool.yaml"})
err := cmd.Execute()

assert.Nil(t, err)
assert.NoError(t, err)
}

func TestExecute(t *testing.T) {
fs := afero.NewMemMapFs()
v := viper.New()
fs := afero.NewMemMapFs()
v.SetFs(fs)
cmd := NewRootCmd(v, fs)

err := cmd.Execute()

assert.Nil(t, err)
assert.NoError(t, err)
}
5 changes: 5 additions & 0 deletions cmd/scenario_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func TestNewScenarioCmd(t *testing.T) {
p := internal.NewScenarioProfiler()
v := viper.New()
fs := afero.NewMemMapFs()
v.SetFs(fs)
cmd := NewScenarioCmd(p, v, fs)

assert.Equal(t, "scenario", cmd.Name(), "NewScenarioCmd() should return command named \"scenario\". but: \"%s\"", cmd.Name())
Expand All @@ -24,6 +25,7 @@ func TestNewScenarioCmd_Flag(t *testing.T) {
p := internal.NewScenarioProfiler()
v := viper.New()
fs := afero.NewMemMapFs()
v.SetFs(fs)
cmd := NewScenarioCmd(p, v, fs)
fileFlag := cmd.Flags().Lookup("file")
matchingGroupsFlag := cmd.Flags().Lookup("matching_groups")
Expand Down Expand Up @@ -56,6 +58,7 @@ func Test_ScenarioCmd_RunE(t *testing.T) {
p := internal.NewScenarioProfiler()
v := viper.New()
fs := afero.NewMemMapFs()
v.SetFs(fs)
cmd := NewScenarioCmd(p, v, fs)

fileName := "./access.log"
Expand All @@ -81,6 +84,7 @@ func Test_ScenarioCmd_RunE_format_csv(t *testing.T) {
p := internal.NewScenarioProfiler()
v := viper.New()
fs := afero.NewMemMapFs()
v.SetFs(fs)
cmd := NewScenarioCmd(p, v, fs)

fileName := "./access.log"
Expand All @@ -103,6 +107,7 @@ func Test_ScenarioCmd_RunE_palette(t *testing.T) {
p := internal.NewScenarioProfiler()
v := viper.New()
fs := afero.NewMemMapFs()
v.SetFs(fs)
cmd := NewScenarioCmd(p, v, fs)

fileName := "./access.log"
Expand Down
7 changes: 7 additions & 0 deletions cmd/transition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func TestNewTransitionCmd(t *testing.T) {
p := internal.NewTransitionProfiler()
v := viper.New()
fs := afero.NewMemMapFs()
v.SetFs(fs)
cmd := NewTransitionCmd(p, v, fs)

assert.Equal(t, "transition", cmd.Name(), "NewTransitionCmd() should return command named \"transition\". but: \"%s\"", cmd.Name())
Expand All @@ -25,6 +26,7 @@ func TestNewTransitionCmd_Flag(t *testing.T) {
p := internal.NewTransitionProfiler()
v := viper.New()
fs := afero.NewMemMapFs()
v.SetFs(fs)
cmd := NewTransitionCmd(p, v, fs)
fileFlag := cmd.Flags().Lookup("file")
matchingGroupsFlag := cmd.Flags().Lookup("matching_groups")
Expand Down Expand Up @@ -54,6 +56,7 @@ func Test_TransitionCmd_RunE(t *testing.T) {
p := internal.NewTransitionProfiler()
v := viper.New()
fs := afero.NewMemMapFs()
v.SetFs(fs)
cmd := NewTransitionCmd(p, v, fs)

fileName := "./access.log"
Expand All @@ -79,6 +82,7 @@ func Test_TransitionCmd_RunE_file_not_exists(t *testing.T) {
p := internal.NewTransitionProfiler()
v := viper.New()
fs := afero.NewMemMapFs()
v.SetFs(fs)
cmd := NewTransitionCmd(p, v, fs)

fileName := "./not_exists.log"
Expand All @@ -94,6 +98,7 @@ func Test_TransitionCmd_RunE_format_csv(t *testing.T) {
p := internal.NewTransitionProfiler()
v := viper.New()
fs := afero.NewMemMapFs()
v.SetFs(fs)
cmd := NewTransitionCmd(p, v, fs)

fileName := "./access.log"
Expand All @@ -116,6 +121,7 @@ func Test_TransitionCmd_RunE_format_mermaid(t *testing.T) {
p := internal.NewTransitionProfiler()
v := viper.New()
fs := afero.NewMemMapFs()
v.SetFs(fs)
cmd := NewTransitionCmd(p, v, fs)

fileName := "./access.log"
Expand Down Expand Up @@ -147,6 +153,7 @@ func Test_TransitionCmd_RunE_invalid_format(t *testing.T) {
p := internal.NewTransitionProfiler()
v := viper.New()
fs := afero.NewMemMapFs()
v.SetFs(fs)
cmd := NewTransitionCmd(p, v, fs)

fileName := "./access.log"
Expand Down
6 changes: 6 additions & 0 deletions cmd/trend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func TestNewTrendCmd(t *testing.T) {
p := internal.NewTrendProfiler()
v := viper.New()
fs := afero.NewMemMapFs()
v.SetFs(fs)
cmd := NewTrendCmd(p, v, fs)

assert.Equal(t, "trend", cmd.Name(), "NewTrendCmd() should return command named \"trend\". but: \"%s\"", cmd.Name())
Expand All @@ -25,6 +26,7 @@ func TestNewTrendCmd_Flag(t *testing.T) {
p := internal.NewTrendProfiler()
v := viper.New()
fs := afero.NewMemMapFs()
v.SetFs(fs)
cmd := NewTrendCmd(p, v, fs)
fileFlag := cmd.Flags().Lookup("file")
matchingGroupsFlag := cmd.Flags().Lookup("matching_groups")
Expand Down Expand Up @@ -55,6 +57,7 @@ func Test_Trend_RunE(t *testing.T) {
p := internal.NewTrendProfiler()
v := viper.New()
fs := afero.NewMemMapFs()
v.SetFs(fs)
cmd := NewTrendCmd(p, v, fs)

fileName := "./access.log"
Expand All @@ -77,6 +80,7 @@ func Test_TrendCmd_RunE_Flag_interval_not_positive(t *testing.T) {
p := internal.NewTrendProfiler()
v := viper.New()
fs := afero.NewMemMapFs()
v.SetFs(fs)
cmd := NewTrendCmd(p, v, fs)

v.Set("interval", "0")
Expand All @@ -90,6 +94,7 @@ func Test_TrendCmd_RunE_file_not_exists(t *testing.T) {
p := internal.NewTrendProfiler()
v := viper.New()
fs := afero.NewMemMapFs()
v.SetFs(fs)
cmd := NewTrendCmd(p, v, fs)

fileName := "./not_exists.log"
Expand All @@ -105,6 +110,7 @@ func Test_TrendCmd_RunE_file_profiler_error(t *testing.T) {
p := internal.NewTrendProfiler()
v := viper.New()
fs := afero.NewMemMapFs()
v.SetFs(fs)
cmd := NewTrendCmd(p, v, fs)

fileName := "./access.log"
Expand Down

0 comments on commit 9391124

Please sign in to comment.