(t *testing.T)
| 39 | } |
| 40 | |
| 41 | func TestConfigFile_ProjectLevel(t *testing.T) { |
| 42 | resetViper() |
| 43 | resetFlags() |
| 44 | defer resetViper() // Clean up after test |
| 45 | defer resetFlags() |
| 46 | |
| 47 | // Create a temporary project directory with config |
| 48 | projectDir := t.TempDir() |
| 49 | createConfigFile(t, projectDir, ` |
| 50 | dir: ./my-tasks |
| 51 | verbose: true |
| 52 | `) |
| 53 | |
| 54 | // Change to project directory |
| 55 | origDir, _ := os.Getwd() |
| 56 | defer os.Chdir(origDir) |
| 57 | if err := os.Chdir(projectDir); err != nil { |
| 58 | t.Fatalf("failed to change directory: %v", err) |
| 59 | } |
| 60 | // Use os.Getwd to get the canonical cwd path (matches what initConfig sees via filepath.Abs) |
| 61 | projectDir, _ = os.Getwd() |
| 62 | |
| 63 | // Re-initialize cobra command to pick up new config |
| 64 | rootCmd = &cobra.Command{Use: "taskmd"} |
| 65 | cobra.OnInitialize(initConfig) |
| 66 | rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file") |
| 67 | rootCmd.PersistentFlags().BoolVar(&stdin, "stdin", false, "read from stdin") |
| 68 | rootCmd.PersistentFlags().BoolVarP(&quiet, "quiet", "q", false, "quiet mode") |
| 69 | rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose logging") |
| 70 | rootCmd.PersistentFlags().StringVarP(&taskDir, "task-dir", "d", ".", "task directory") |
| 71 | |
| 72 | // Bind flags to viper |
| 73 | viper.BindPFlag("stdin", rootCmd.PersistentFlags().Lookup("stdin")) |
| 74 | viper.BindPFlag("quiet", rootCmd.PersistentFlags().Lookup("quiet")) |
| 75 | viper.BindPFlag("verbose", rootCmd.PersistentFlags().Lookup("verbose")) |
| 76 | viper.BindPFlag("task-dir", rootCmd.PersistentFlags().Lookup("task-dir")) |
| 77 | |
| 78 | // Initialize config |
| 79 | initConfig() |
| 80 | |
| 81 | // Test that config values are loaded |
| 82 | flags := GetGlobalFlags() |
| 83 | expectedDir := filepath.Join(projectDir, "my-tasks") |
| 84 | if flags.TaskDir != expectedDir { |
| 85 | t.Errorf("expected dir to be %q, got %q", expectedDir, flags.TaskDir) |
| 86 | } |
| 87 | if !flags.Verbose { |
| 88 | t.Error("expected verbose to be true from config") |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | func TestConfigFile_GlobalLevel(t *testing.T) { |
| 93 | resetViper() |
nothing calls this directly
no test coverage detected