(t *testing.T)
| 359 | } |
| 360 | |
| 361 | func TestConfigFile_ExplicitConfigFile(t *testing.T) { |
| 362 | resetViper() |
| 363 | resetFlags() |
| 364 | defer resetViper() |
| 365 | defer resetFlags() |
| 366 | |
| 367 | // Create a custom config file in a specific location |
| 368 | configDir := t.TempDir() |
| 369 | customConfigPath := filepath.Join(configDir, "custom-config.yaml") |
| 370 | if err := os.WriteFile(customConfigPath, []byte(` |
| 371 | dir: ./custom-tasks |
| 372 | verbose: true |
| 373 | `), 0644); err != nil { |
| 374 | t.Fatalf("failed to create custom config file: %v", err) |
| 375 | } |
| 376 | |
| 377 | // Re-initialize cobra command |
| 378 | rootCmd = &cobra.Command{Use: "taskmd"} |
| 379 | cobra.OnInitialize(initConfig) |
| 380 | rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file") |
| 381 | rootCmd.PersistentFlags().BoolVar(&stdin, "stdin", false, "read from stdin") |
| 382 | rootCmd.PersistentFlags().BoolVarP(&quiet, "quiet", "q", false, "quiet mode") |
| 383 | rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose logging") |
| 384 | rootCmd.PersistentFlags().StringVarP(&taskDir, "task-dir", "d", ".", "task directory") |
| 385 | |
| 386 | // Bind flags to viper |
| 387 | viper.BindPFlag("stdin", rootCmd.PersistentFlags().Lookup("stdin")) |
| 388 | viper.BindPFlag("quiet", rootCmd.PersistentFlags().Lookup("quiet")) |
| 389 | viper.BindPFlag("verbose", rootCmd.PersistentFlags().Lookup("verbose")) |
| 390 | viper.BindPFlag("task-dir", rootCmd.PersistentFlags().Lookup("task-dir")) |
| 391 | |
| 392 | // Set the config flag using cobra |
| 393 | rootCmd.PersistentFlags().Set("config", customConfigPath) |
| 394 | |
| 395 | // Initialize config |
| 396 | initConfig() |
| 397 | |
| 398 | // Test that custom config file is loaded |
| 399 | flags := GetGlobalFlags() |
| 400 | expectedDir := filepath.Join(configDir, "custom-tasks") |
| 401 | if flags.TaskDir != expectedDir { |
| 402 | t.Errorf("expected dir to be %q, got %q", expectedDir, flags.TaskDir) |
| 403 | } |
| 404 | if !flags.Verbose { |
| 405 | t.Error("expected verbose to be true from custom config") |
| 406 | } |
| 407 | } |
nothing calls this directly
no test coverage detected