|
| 1 | +package core |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "os" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/snyk/go-application-framework/pkg/configuration" |
| 9 | + "github.com/snyk/go-application-framework/pkg/workflow" |
| 10 | + "github.com/spf13/cobra" |
| 11 | + "github.com/spf13/pflag" |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | +) |
| 15 | + |
| 16 | +func Test_help_routing(t *testing.T) { |
| 17 | + root := setupHelpRoutingRoot(t) |
| 18 | + |
| 19 | + tests := map[string]struct { |
| 20 | + argv []string |
| 21 | + command func(t *testing.T, root *cobra.Command) (*cobra.Command, *bytes.Buffer) |
| 22 | + wantLegacy bool |
| 23 | + cobraOut []string |
| 24 | + }{ |
| 25 | + "top-level --help uses legacy readme": { |
| 26 | + argv: []string{"snyk", "--help"}, |
| 27 | + command: func(_ *testing.T, root *cobra.Command) (*cobra.Command, *bytes.Buffer) { |
| 28 | + return root, nil |
| 29 | + }, |
| 30 | + wantLegacy: true, |
| 31 | + }, |
| 32 | + "help subcommand uses legacy": { |
| 33 | + argv: []string{"snyk", "help"}, |
| 34 | + command: func(_ *testing.T, _ *cobra.Command) (*cobra.Command, *bytes.Buffer) { |
| 35 | + return helpSubcommand(), nil |
| 36 | + }, |
| 37 | + wantLegacy: true, |
| 38 | + }, |
| 39 | + "help test uses legacy user doc": { |
| 40 | + argv: []string{"snyk", "help", "test"}, |
| 41 | + command: func(_ *testing.T, _ *cobra.Command) (*cobra.Command, *bytes.Buffer) { |
| 42 | + return helpSubcommand(), nil |
| 43 | + }, |
| 44 | + wantLegacy: true, |
| 45 | + }, |
| 46 | + "documented test --help uses legacy": { |
| 47 | + argv: []string{"snyk", "test", "--help"}, |
| 48 | + command: func(_ *testing.T, _ *cobra.Command) (*cobra.Command, *bytes.Buffer) { |
| 49 | + return nil, nil |
| 50 | + }, |
| 51 | + wantLegacy: true, |
| 52 | + }, |
| 53 | + "undocumented secrets test --help uses cobra": { |
| 54 | + argv: []string{"snyk", "secrets", "test", "--help"}, |
| 55 | + command: func(t *testing.T, root *cobra.Command) (*cobra.Command, *bytes.Buffer) { |
| 56 | + cmd := findCommand(t, root, "secrets", "test") |
| 57 | + buf := &bytes.Buffer{} |
| 58 | + cmd.SetOut(buf) |
| 59 | + return cmd, buf |
| 60 | + }, |
| 61 | + cobraOut: []string{"Usage:"}, |
| 62 | + }, |
| 63 | + "unknown command --help falls back to legacy": { |
| 64 | + argv: []string{"snyk", "rainmaker", "--help"}, |
| 65 | + command: func(_ *testing.T, _ *cobra.Command) (*cobra.Command, *bytes.Buffer) { |
| 66 | + return nil, nil |
| 67 | + }, |
| 68 | + wantLegacy: true, |
| 69 | + }, |
| 70 | + "unknown flag on undocumented command uses cobra": { |
| 71 | + argv: []string{"snyk", "secrets", "test", "--bad-flag"}, |
| 72 | + command: func(t *testing.T, root *cobra.Command) (*cobra.Command, *bytes.Buffer) { |
| 73 | + cmd := findCommand(t, root, "secrets", "test") |
| 74 | + buf := &bytes.Buffer{} |
| 75 | + cmd.SetOut(buf) |
| 76 | + return cmd, buf |
| 77 | + }, |
| 78 | + cobraOut: []string{"Usage:"}, |
| 79 | + }, |
| 80 | + } |
| 81 | + |
| 82 | + for name, tc := range tests { |
| 83 | + t.Run(name, func(t *testing.T) { |
| 84 | + defer cleanup() |
| 85 | + |
| 86 | + oldArgs := append([]string{}, os.Args...) |
| 87 | + oldLegacyHelp := runLegacyHelp |
| 88 | + defer func() { |
| 89 | + os.Args = oldArgs |
| 90 | + runLegacyHelp = oldLegacyHelp |
| 91 | + globalRootCommand = nil |
| 92 | + }() |
| 93 | + |
| 94 | + legacyCalled := false |
| 95 | + runLegacyHelp = func() error { |
| 96 | + legacyCalled = true |
| 97 | + return nil |
| 98 | + } |
| 99 | + |
| 100 | + os.Args = tc.argv |
| 101 | + cmd, buf := tc.command(t, root) |
| 102 | + |
| 103 | + err := help(cmd, nil) |
| 104 | + require.NoError(t, err) |
| 105 | + assert.Equal(t, tc.wantLegacy, legacyCalled, "legacy help routing") |
| 106 | + if tc.wantLegacy { |
| 107 | + return |
| 108 | + } |
| 109 | + require.NotNil(t, buf) |
| 110 | + for _, want := range tc.cobraOut { |
| 111 | + assert.Contains(t, buf.String(), want) |
| 112 | + } |
| 113 | + }) |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +func setupHelpRoutingRoot(t *testing.T) *cobra.Command { |
| 118 | + t.Helper() |
| 119 | + |
| 120 | + globalConfiguration = configuration.New() |
| 121 | + globalEngine = workflow.NewWorkFlowEngine(globalConfiguration) |
| 122 | + |
| 123 | + fn := func(invocation workflow.InvocationContext, input []workflow.Data) ([]workflow.Data, error) { |
| 124 | + return []workflow.Data{}, nil |
| 125 | + } |
| 126 | + |
| 127 | + workflowConfig := workflow.ConfigurationOptionsFromFlagset(pflag.NewFlagSet("secrets-test", pflag.ContinueOnError)) |
| 128 | + workflowId := workflow.NewWorkflowIdentifier("secrets test") |
| 129 | + _, err := globalEngine.Register(workflowId, workflowConfig, fn) |
| 130 | + require.NoError(t, err) |
| 131 | + require.NoError(t, globalEngine.Init()) |
| 132 | + |
| 133 | + rootCommand := prepareRootCommand() |
| 134 | + globalRootCommand = rootCommand |
| 135 | + createCommandsForWorkflows(rootCommand, globalEngine) |
| 136 | + |
| 137 | + return rootCommand |
| 138 | +} |
| 139 | + |
| 140 | +func findCommand(t *testing.T, root *cobra.Command, path ...string) *cobra.Command { |
| 141 | + t.Helper() |
| 142 | + |
| 143 | + cmd, _, err := root.Find(path) |
| 144 | + require.NoError(t, err) |
| 145 | + return cmd |
| 146 | +} |
| 147 | + |
| 148 | +func helpSubcommand() *cobra.Command { |
| 149 | + return &cobra.Command{Use: "help"} |
| 150 | +} |
0 commit comments