RunWithInput executes the CLI with the given input and arguments and captures the output.
(t *testing.T, input string, args ...string)
| 52 | |
| 53 | // RunWithInput executes the CLI with the given input and arguments and captures the output. |
| 54 | func (f *Fixture) RunWithInput(t *testing.T, input string, args ...string) Result { |
| 55 | // Buffer for capturing output |
| 56 | var out bytes.Buffer |
| 57 | |
| 58 | // Create a new command helper configured for testing |
| 59 | ch, err := cmdutil.NewHelper(version.Version{}, f.HomeDir) |
| 60 | require.NoError(t, err) |
| 61 | ch.Printer.OverrideDataOutput(&out) |
| 62 | ch.Printer.OverrideHumanOutput(&out) |
| 63 | |
| 64 | // Configure the root command for testing |
| 65 | root := cmd.RootCmd(ch) |
| 66 | root.SetOut(&out) |
| 67 | root.SetErr(&out) |
| 68 | if input != "" { |
| 69 | root.SetIn(bytes.NewBufferString(input + "\n")) |
| 70 | } |
| 71 | root.SetArgs(args) |
| 72 | |
| 73 | // Execute the command (mirrors the logic in cli/cmd.Run) |
| 74 | err = root.ExecuteContext(t.Context()) |
| 75 | code := cmd.HandleExecuteError(ch, err) |
| 76 | |
| 77 | return Result{ |
| 78 | ExitCode: code, |
| 79 | Output: out.String(), |
| 80 | } |
| 81 | } |
no test coverage detected