-
Notifications
You must be signed in to change notification settings - Fork 7.3k
Closed
Labels
tech-debtA chore that addresses technical debtA chore that addresses technical debt
Description
In both runtime and tests, we only ever initialize one instance of every cobra.Command. When repeatedly running tests, we reuse the same Command instance.
The problem is that a Command seems to keep state and that it's hard to reset some of that state. For example, its FlagSet remembers the values of flags that were passed, which forced us to maintain some ad-hoc resetting code to improve isolation between tests
Lines 55 to 67 in c0c2862
| // Reset flag values so they don't leak between tests | |
| // FIXME: change how we initialize Cobra commands to render this hack unnecessary | |
| cmd.Flags().VisitAll(func(f *pflag.Flag) { | |
| switch v := f.Value.(type) { | |
| case pflag.SliceValue: | |
| _ = v.Replace([]string{}) | |
| default: | |
| switch v.Type() { | |
| case "bool", "string", "int": | |
| _ = v.Set(f.DefValue) | |
| } | |
| } | |
| }) |
A better approach would be to instantiate a new Command every time we run a test. A prerequisite for that would be to define a factory method for each currently available Command. We would also use these factories to assemble the command tree at runtime.
Metadata
Metadata
Assignees
Labels
tech-debtA chore that addresses technical debtA chore that addresses technical debt