Skip to content

Conversation

@miladev95
Copy link
Contributor

@miladev95 miladev95 commented Nov 29, 2025

  • Do only one thing
  • Non breaking API changes
  • Tested

What did this pull request do?

adds unit tests for internal/gen to verify flags and required-flag behavior


Add unit tests for flag handling in internal/gen command

Adds a single test file that exercises the New() command in internal/gen, ensuring all expected flags (typed, output, input) are present and that the input flag is marked as required. Additional coverage verifies cobra.MarkFlagRequired error behaviour on a standalone command.
No production code is modified; only test coverage increases.

Key Changes

• Created internal/gen/gen_test.go with 61 new lines of tests
• Added TestNewCommandFlags to validate flag presence, required-flag enforcement and safe execution paths
• Added TestMarkFlagRequiredBehavior to assert correct behaviour of cobra.MarkFlagRequired when the flag exists

Affected Areas

internal/gen (tests only)


This summary was automatically generated by @propel-code-bot

@propel-code-bot propel-code-bot bot changed the title test: add tests for internal/gen test: Add unit tests for internal/gen command flags Nov 29, 2025
Comment on lines +28 to +46
// 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()

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant