NewTestCommand creates a new test command.
(ctx context.Context)
| 117 | |
| 118 | // NewTestCommand creates a new test command. |
| 119 | func NewTestCommand(ctx context.Context) *cobra.Command { |
| 120 | cmd := cobra.Command{ |
| 121 | Use: "test <path> [path [...]]", |
| 122 | Short: "Test your configuration files using Open Policy Agent", |
| 123 | Long: testDesc, |
| 124 | PreRunE: func(cmd *cobra.Command, _ []string) error { |
| 125 | flagNames := []string{ |
| 126 | "all-namespaces", |
| 127 | "combine", |
| 128 | "data", |
| 129 | "fail-on-warn", |
| 130 | "ignore", |
| 131 | "namespace", |
| 132 | "no-color", |
| 133 | "no-fail", |
| 134 | "suppress-exceptions", |
| 135 | "output", |
| 136 | "parser", |
| 137 | "policy", |
| 138 | "proto-file-dirs", |
| 139 | "capabilities", |
| 140 | "rego-version", |
| 141 | "trace", |
| 142 | "strict", |
| 143 | "show-builtin-errors", |
| 144 | "update", |
| 145 | "junit-hide-message", |
| 146 | "quiet", |
| 147 | "tls", |
| 148 | } |
| 149 | for _, name := range flagNames { |
| 150 | if err := viper.BindPFlag(name, cmd.Flags().Lookup(name)); err != nil { |
| 151 | return fmt.Errorf("bind flag: %w", err) |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | return nil |
| 156 | }, |
| 157 | |
| 158 | RunE: func(cmd *cobra.Command, fileList []string) error { |
| 159 | if len(fileList) < 1 { |
| 160 | cmd.Usage() //nolint |
| 161 | return fmt.Errorf("missing required arguments") |
| 162 | } |
| 163 | |
| 164 | var runner runner.TestRunner |
| 165 | if err := viper.Unmarshal(&runner); err != nil { |
| 166 | return fmt.Errorf("unmarshal parameters: %w", err) |
| 167 | } |
| 168 | |
| 169 | results, resultsErr := runner.Run(ctx, fileList) |
| 170 | |
| 171 | exitCode := results.ExitCode() |
| 172 | if runner.FailOnWarn { |
| 173 | exitCode = results.ExitCodeFailOnWarn() |
| 174 | } |
| 175 | |
| 176 | // Do this so we can write the appstudio format to a file |
no test coverage detected