(t *testing.T)
| 857 | } |
| 858 | |
| 859 | func TestJSONHelpDoesNotChangeCommandExecution(t *testing.T) { |
| 860 | var stdout bytes.Buffer |
| 861 | called := false |
| 862 | root := newJSONHelpTestRoot(t) |
| 863 | ls, _, _ := root.Find([]string{"ls"}) |
| 864 | ls.RunE = func(cmd *cobra.Command, args []string) error { |
| 865 | called = true |
| 866 | _, _ = cmd.OutOrStdout().Write([]byte("executed\n")) |
| 867 | return nil |
| 868 | } |
| 869 | root.PersistentPreRunE = nil |
| 870 | root.SetOut(&stdout) |
| 871 | root.SetArgs([]string{"ls", "--output=json", "/"}) |
| 872 | |
| 873 | if err := root.Execute(); err != nil { |
| 874 | t.Fatalf("Execute returned error: %v", err) |
| 875 | } |
| 876 | if !called { |
| 877 | t.Fatal("ls command did not execute") |
| 878 | } |
| 879 | if got := stdout.String(); got != "executed\n" { |
| 880 | t.Fatalf("stdout = %q, want command output", got) |
| 881 | } |
| 882 | } |
| 883 | |
| 884 | func newJSONHelpTestRoot(t *testing.T) *cobra.Command { |
| 885 | t.Helper() |
nothing calls this directly
no test coverage detected