diff --git a/cmd/cheatmd/config_test.go b/cmd/cheatmd/config_test.go new file mode 100644 index 0000000..f99bd10 --- /dev/null +++ b/cmd/cheatmd/config_test.go @@ -0,0 +1,82 @@ +package main + +import ( + "bytes" + "os" + "path/filepath" + "strings" + "testing" + + "github.com/cheatmd-dev/cheatmd/pkg/config" + "github.com/spf13/viper" +) + +func TestCLIConfigurationErrors(t *testing.T) { + oldConfig := *config.Get() + benchmark := rootCmd.PersistentFlags().Lookup("benchmark") + oldBenchmark, oldChanged := benchmark.Value.String(), benchmark.Changed + t.Cleanup(func() { + *config.Get() = oldConfig + viper.Reset() + rootCmd.SetArgs(nil) + rootCmd.SetOut(nil) + rootCmd.SetErr(nil) + _ = benchmark.Value.Set(oldBenchmark) + benchmark.Changed = oldChanged + }) + + dir := t.TempDir() + configPath := filepath.Join(dir, "cheatmd.yaml") + if err := os.WriteFile(configPath, []byte("{}\n"), 0o600); err != nil { + t.Fatal(err) + } + path := filepath.Join(dir, "greeting.md") + if err := os.WriteFile(path, []byte("# Greeting\n```sh\necho hello\n```\n"), 0o600); err != nil { + t.Fatal(err) + } + + for _, command := range []struct { + name string + args []string + want string + }{ + {"root", []string{"--benchmark", path}, "Loaded 1 cheats"}, + {"dump", []string{"dump", path}, `"command": "echo hello"`}, + {"widget", []string{"widget", "bash"}, "_cheatmd_widget"}, + } { + t.Run(command.name, func(t *testing.T) { + for _, historyMax := range []string{"not-an-integer", "42"} { + t.Run(historyMax, func(t *testing.T) { + viper.Reset() + _ = benchmark.Value.Set("false") + benchmark.Changed = false + viper.SetConfigFile(configPath) + t.Setenv("CHEATMD_HISTORY_MAX", historyMax) + var out, stderr bytes.Buffer + rootCmd.SetOut(&out) + rootCmd.SetErr(&stderr) + rootCmd.SetArgs(command.args) + err := rootCmd.Execute() + if historyMax == "not-an-integer" { + if err == nil || !strings.Contains(err.Error(), "history_max") { + t.Errorf("expected history_max config error, got %v", err) + } + if out.Len() != 0 { + t.Errorf("command ran despite invalid configuration: %s", out.String()) + } + return + } + if err != nil { + t.Fatal(err) + } + if !strings.Contains(out.String(), command.want) { + t.Errorf("output %q does not contain %q", out.String(), command.want) + } + if got := config.Get().HistoryMax; got != 42 { + t.Errorf("history_max = %d, want 42", got) + } + }) + } + }) + } +} diff --git a/cmd/cheatmd/root.go b/cmd/cheatmd/root.go index 153e25a..a2898c6 100644 --- a/cmd/cheatmd/root.go +++ b/cmd/cheatmd/root.go @@ -25,12 +25,16 @@ var rootCmd = &cobra.Command{ Browse your cheatsheets interactively, select commands, fill in variables, and execute or copy the result.`, Args: cobra.MaximumNArgs(1), + PersistentPreRunE: func(cmd *cobra.Command, args []string) error { + if err := config.Init(); err != nil { + return fmt.Errorf("loading config: %w", err) + } + return nil + }, RunE: runCheats, } func init() { - cobra.OnInitialize(initConfig) - rootCmd.AddCommand(widgetCmd) rootCmd.AddCommand(chainCmd) rootCmd.AddCommand(dumpCmd) @@ -53,12 +57,6 @@ func init() { rootCmd.PersistentFlags().BoolP("strict", "s", false, "Treat lint warnings as errors") } -func initConfig() { - if err := config.Init(); err != nil { - fmt.Fprintf(os.Stderr, "Error loading config: %v\n", err) - } -} - func runCheats(cmd *cobra.Command, args []string) error { absPath, err := resolveCheatPath(args) if err != nil {