summaryrefslogtreecommitdiff
path: root/internal/cli/root_test.go
diff options
context:
space:
mode:
authorClaudomator Agent <agent@claudomator>2026-03-10 04:36:19 +0000
committerClaudomator Agent <agent@claudomator>2026-03-10 04:36:19 +0000
commit8873187921c55d94be56364bf0b9d6b2d12127c2 (patch)
treecc277db491104ebec5645e1a0422be9d5d4cafd2 /internal/cli/root_test.go
parent7d6943c5f9f4124c652377148a35bea5f61be4bf (diff)
cli: implement --config flag to load TOML config file
The --config flag was registered but silently ignored. Now: - config.LoadFile loads a TOML file on top of defaults - PersistentPreRunE applies the file when --config is set - Explicit CLI flags (--data-dir, --claude-bin) take precedence over the file Tests: TestLoadFile_OverridesDefaults, TestLoadFile_MissingFile_ReturnsError, TestRootCmd_ConfigFile_Loaded, TestRootCmd_ConfigFile_CLIFlagOverrides, TestRootCmd_ConfigFile_Missing_ReturnsError Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Diffstat (limited to 'internal/cli/root_test.go')
-rw-r--r--internal/cli/root_test.go69
1 files changed, 69 insertions, 0 deletions
diff --git a/internal/cli/root_test.go b/internal/cli/root_test.go
new file mode 100644
index 0000000..4603d3b
--- /dev/null
+++ b/internal/cli/root_test.go
@@ -0,0 +1,69 @@
+package cli
+
+import (
+ "os"
+ "path/filepath"
+ "testing"
+
+ "github.com/spf13/cobra"
+)
+
+// noopCmd is a harmless subcommand used to trigger PersistentPreRunE.
+func noopCmd() *cobra.Command {
+ return &cobra.Command{
+ Use: "noop",
+ RunE: func(cmd *cobra.Command, args []string) error { return nil },
+ }
+}
+
+func TestRootCmd_ConfigFile_Loaded(t *testing.T) {
+ dir := t.TempDir()
+ cfgPath := filepath.Join(dir, "config.toml")
+ if err := os.WriteFile(cfgPath, []byte("server_addr = \":9191\"\n"), 0600); err != nil {
+ t.Fatal(err)
+ }
+ t.Setenv("HOME", dir)
+
+ cmd := NewRootCmd()
+ cmd.AddCommand(noopCmd())
+ cmd.SetArgs([]string{"--config", cfgPath, "noop"})
+ if err := cmd.Execute(); err != nil {
+ t.Fatalf("Execute: %v", err)
+ }
+
+ if cfg.ServerAddr != ":9191" {
+ t.Errorf("ServerAddr = %q, want :9191", cfg.ServerAddr)
+ }
+}
+
+func TestRootCmd_ConfigFile_CLIFlagOverrides(t *testing.T) {
+ dir := t.TempDir()
+ cfgPath := filepath.Join(dir, "config.toml")
+ // Config file sets data_dir, but --data-dir flag should win.
+ if err := os.WriteFile(cfgPath, []byte("data_dir = \"/from/file\"\n"), 0600); err != nil {
+ t.Fatal(err)
+ }
+ t.Setenv("HOME", dir)
+
+ cmd := NewRootCmd()
+ cmd.AddCommand(noopCmd())
+ cmd.SetArgs([]string{"--config", cfgPath, "--data-dir", "/from/flag", "noop"})
+ if err := cmd.Execute(); err != nil {
+ t.Fatalf("Execute: %v", err)
+ }
+
+ if cfg.DataDir != "/from/flag" {
+ t.Errorf("DataDir = %q, want /from/flag", cfg.DataDir)
+ }
+}
+
+func TestRootCmd_ConfigFile_Missing_ReturnsError(t *testing.T) {
+ t.Setenv("HOME", t.TempDir())
+
+ cmd := NewRootCmd()
+ cmd.AddCommand(noopCmd())
+ cmd.SetArgs([]string{"--config", "/nonexistent/config.toml", "noop"})
+ if err := cmd.Execute(); err == nil {
+ t.Fatal("expected error for missing config file, got nil")
+ }
+}