package cmd import ( "fmt" "io" "os" "strings" "testing" "github.com/spf13/cobra" "github.com/iwasforcedtobehere/git-automation-cli/internal/config" ) // MockVersionService is a mock implementation of the version service type MockVersionService struct { getVersionFunc func() string } func (m *MockVersionService) GetVersion() string { if m.getVersionFunc != nil { return m.getVersionFunc() } return "1.0.0" } // MockConfigService is a mock implementation of the config service type MockConfigService struct { initFunc func() error } func (m *MockConfigService) Init() error { if m.initFunc != nil { return m.initFunc() } return nil } func TestExecute(t *testing.T) { // Save original stdout originalStdout := os.Stdout defer func() { os.Stdout = originalStdout }() // Temporarily redirect stdout to capture output r, w, _ := os.Pipe() os.Stdout = w // Execute root command Execute() // Restore stdout w.Close() os.Stdout = originalStdout out, _ := io.ReadAll(r) // Check that help was displayed output := string(out) if !strings.Contains(output, "Git Automation CLI is a fast, practical tool to automate frequent Git workflows") { t.Errorf("Expected help to be displayed, got %q", output) } } func TestVersionCmd(t *testing.T) { // Create mock version service mockVersionService := &MockVersionService{ getVersionFunc: func() string { return "1.0.0-test" }, } // Temporarily redirect stdout to capture output oldStdout := os.Stdout r, w, _ := os.Pipe() os.Stdout = w // Create a command cmd := &cobra.Command{ Use: "test", Run: func(cmd *cobra.Command, args []string) { // Use mock version service instead of real one versionStr := mockVersionService.GetVersion() fmt.Printf("gitauto version %s\n", versionStr) }, } // Execute command err := cmd.Execute() // Restore stdout w.Close() os.Stdout = oldStdout out, _ := io.ReadAll(r) // Check error if err != nil { t.Errorf("Execute() error = %v", err) return } // Check output output := string(out) expected := "gitauto version 1.0.0-test\n" if output != expected { t.Errorf("Execute() output = %q, want %q", output, expected) } } func TestRootCmdPersistentPreRun(t *testing.T) { // Save original config originalConfig := config.GlobalConfig defer func() { config.GlobalConfig = originalConfig }() // Create mock config service mockConfigService := &MockConfigService{ initFunc: func() error { return nil }, } // Set up test config config.GlobalConfig = &config.Config{ Verbose: false, DryRun: false, } // Create a command with PersistentPreRun cmd := &cobra.Command{ Use: "test", PersistentPreRun: func(cmd *cobra.Command, args []string) { // Initialize configuration if err := mockConfigService.Init(); err != nil { panic(err) } // Override config with command line flags if verbose { config.Set("verbose", true) } if dryRun { config.Set("dry_run", true) } }, Run: func(cmd *cobra.Command, args []string) { // Do nothing }, } // Set verbose flag verbose = true // Execute command err := cmd.Execute() // Check error if err != nil { t.Errorf("Execute() error = %v", err) return } // Check that config was updated if !config.GlobalConfig.Verbose { t.Errorf("Expected Verbose to be true, got false") } } func TestRootCmdFlags(t *testing.T) { // Test that verbose and dry-run flags are properly set cmd := &cobra.Command{ Use: "test", Run: func(cmd *cobra.Command, args []string) { // Do nothing }, } // Add flags like in rootCmd cmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose output") cmd.PersistentFlags().BoolVar(&dryRun, "dry-run", false, "dry run mode (no changes made)") // Set verbose flag err := cmd.ParseFlags([]string{"--verbose"}) if err != nil { t.Errorf("ParseFlags() error = %v", err) return } if !verbose { t.Errorf("Expected verbose to be true, got false") } // Reset flags verbose = false dryRun = false // Set dry-run flag err = cmd.ParseFlags([]string{"--dry-run"}) if err != nil { t.Errorf("ParseFlags() error = %v", err) return } if !dryRun { t.Errorf("Expected dryRun to be true, got false") } // Reset flags verbose = false dryRun = false // Set both flags err = cmd.ParseFlags([]string{"--verbose", "--dry-run"}) if err != nil { t.Errorf("ParseFlags() error = %v", err) return } if !verbose { t.Errorf("Expected verbose to be true, got false") } if !dryRun { t.Errorf("Expected dryRun to be true, got false") } }