-
-
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
Open
miladev95
wants to merge
1
commit into
go-gorm:master
Choose a base branch
from
miladev95:test/handle-markflagrequired-gen
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() | ||
| 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) | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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. IfExecuteC()modifies internal cobra command state (parsed flags, etc.), the secondExecuteC()call on line 46 may not execute cleanly. Consider creating a fresh command instance for the second test case:Context for Agents