Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions internal/gen/gen_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package gen

import (
"testing"

"github.com/spf13/cobra"
)

// TestNewCommandFlags verifies the command returned by New has the expected flags
// and that running it without the required input flag returns an error.
func TestNewCommandFlags(t *testing.T) {
cmd := New()
if cmd == nil {
t.Fatal("New() returned nil command")
}

// Ensure flags exist
if cmd.Flags().Lookup("typed") == nil {
t.Fatalf("expected 'typed' flag to be present")
}
if cmd.Flags().Lookup("output") == nil {
t.Fatalf("expected 'output' flag to be present")
}
if cmd.Flags().Lookup("input") == nil {
t.Fatalf("expected 'input' flag to be present")
}

// Save original RunE and restore later.
origRunE := cmd.RunE
defer func() { cmd.RunE = origRunE }()

// Replace RunE with a no-op to avoid side effects from file operations.
cmd.RunE = func(cmd *cobra.Command, args []string) error {
return nil
}

// Execute without setting input flag - cobra will check required flags during ParseFlags
// Use cobra's Flag error handling by calling PreRunE if present then ExecuteC.
_, err := cmd.ExecuteC()
if err == nil {
t.Fatalf("expected error when executing command without required 'input' flag")
}

// Now set the required flag and ensure no flag parsing error occurs.
cmd.SetArgs([]string{"--input", "dummy.go"})
_, err = cmd.ExecuteC()
Comment on lines +28 to +46

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[BestPractice]

Test isolation issue: Lines 29-30 save and restore cmd.RunE, but this doesn't fully reset the command state. If ExecuteC() modifies internal cobra command state (parsed flags, etc.), the second ExecuteC() call on line 46 may not execute cleanly. Consider creating a fresh command instance for the second test case:

Suggested change
// Save original RunE and restore later.
origRunE := cmd.RunE
defer func() { cmd.RunE = origRunE }()
// Replace RunE with a no-op to avoid side effects from file operations.
cmd.RunE = func(cmd *cobra.Command, args []string) error {
return nil
}
// Execute without setting input flag - cobra will check required flags during ParseFlags
// Use cobra's Flag error handling by calling PreRunE if present then ExecuteC.
_, err := cmd.ExecuteC()
if err == nil {
t.Fatalf("expected error when executing command without required 'input' flag")
}
// Now set the required flag and ensure no flag parsing error occurs.
cmd.SetArgs([]string{"--input", "dummy.go"})
_, err = cmd.ExecuteC()
// Test without required flag
cmd1 := New()
cmd1.RunE = func(cmd *cobra.Command, args []string) error {
return nil
}
_, err := cmd1.ExecuteC()
if err == nil {
t.Fatalf("expected error when executing command without required 'input' flag")
}
// Test with required flag using fresh command
cmd2 := New()
cmd2.RunE = func(cmd *cobra.Command, args []string) error {
return nil
}
cmd2.SetArgs([]string{"--input", "dummy.go"})
_, err = cmd2.ExecuteC()
Context for Agents
Test isolation issue: Lines 29-30 save and restore `cmd.RunE`, but this doesn't fully reset the command state. If `ExecuteC()` modifies internal cobra command state (parsed flags, etc.), the second `ExecuteC()` call on line 46 may not execute cleanly. Consider creating a fresh command instance for the second test case:

```suggestion
	// Test without required flag
	cmd1 := New()
	cmd1.RunE = func(cmd *cobra.Command, args []string) error {
		return nil
	}
	_, err := cmd1.ExecuteC()
	if err == nil {
		t.Fatalf("expected error when executing command without required 'input' flag")
	}

	// Test with required flag using fresh command
	cmd2 := New()
	cmd2.RunE = func(cmd *cobra.Command, args []string) error {
		return nil
	}
	cmd2.SetArgs([]string{"--input", "dummy.go"})
	_, err = cmd2.ExecuteC()
```

File: internal/gen/gen_test.go
Line: 46

if err != nil {
t.Fatalf("unexpected error executing command with input set: %v", err)
}
}

// TestMarkFlagRequiredBehavior ensures cobra.MarkFlagRequired behaves as expected
func TestMarkFlagRequiredBehavior(t *testing.T) {
cmd := &cobra.Command{Use: "x"}
cmd.Flags().String("input", "", "input file")
// MarkFlagRequired returns an error when flag not found; here it should not.
err := cmd.MarkFlagRequired("input")
if err != nil {
t.Fatalf("expected no error from MarkFlagRequired, got: %v", err)
}
}