-
-
Notifications
You must be signed in to change notification settings - Fork 7
test: Add unit tests for internal/gen command flags #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
test: Add unit tests for internal/gen command flags #27
Conversation
| // 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() |
There was a problem hiding this comment.
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:
| // 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
What did this pull request do?
adds unit tests for
internal/gento verify flags and required-flag behaviorAdd unit tests for flag handling in
internal/gencommandAdds a single test file that exercises the
New()command ininternal/gen, ensuring all expected flags (typed,output,input) are present and that theinputflag is marked as required. Additional coverage verifiescobra.MarkFlagRequirederror behaviour on a standalone command.No production code is modified; only test coverage increases.
Key Changes
• Created
internal/gen/gen_test.gowith 61 new lines of tests• Added
TestNewCommandFlagsto validate flag presence, required-flag enforcement and safe execution paths• Added
TestMarkFlagRequiredBehaviorto assert correct behaviour ofcobra.MarkFlagRequiredwhen the flag existsAffected Areas
•
internal/gen(tests only)This summary was automatically generated by @propel-code-bot