Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement option to write output to a file in ec test #1244

Merged
merged 1 commit into from
Jan 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion cmd/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
"no-fail",
"suppress-exceptions",
"output",
"file",
"parser",
"policy",
"proto-file-dirs",
Expand Down Expand Up @@ -150,6 +151,11 @@
return fmt.Errorf("unmarshal parameters: %w", err)
}

outputFilePath, err := cmd.Flags().GetString("file")
if err != nil {
return fmt.Errorf("reading flag: %w", err)
}

Check warning on line 157 in cmd/test/test.go

View check run for this annotation

Codecov / codecov/patch

cmd/test/test.go#L156-L157

Added lines #L156 - L157 were not covered by tests

results, resultsErr := runner.Run(ctx, fileList)
var exitCode int
if runner.FailOnWarn {
Expand All @@ -171,7 +177,14 @@
if err != nil {
return appstudioErrorHandler(runner.NoFail, "output results", err)
}
fmt.Printf("%s\n", reportOutput)

if outputFilePath != "" {
err := os.WriteFile(outputFilePath, reportOutput, 0600)
if err != nil {
return fmt.Errorf("creating output file: %w", err)
}

Check warning on line 185 in cmd/test/test.go

View check run for this annotation

Codecov / codecov/patch

cmd/test/test.go#L181-L185

Added lines #L181 - L185 were not covered by tests
}
fmt.Fprintln(cmd.OutOrStdout(), string(reportOutput)+"\n")

Check warning on line 187 in cmd/test/test.go

View check run for this annotation

Codecov / codecov/patch

cmd/test/test.go#L187

Added line #L187 was not covered by tests

} else {
// Conftest handles the output
Expand All @@ -180,15 +193,33 @@
return fmt.Errorf("running test: %w", resultsErr)
}

var outputFile *os.File
if outputFilePath != "" {
outputFile, err = os.Create(outputFilePath)
if err != nil {
return fmt.Errorf("creating output file: %w", err)
}
defer outputFile.Close()

Check warning on line 202 in cmd/test/test.go

View check run for this annotation

Codecov / codecov/patch

cmd/test/test.go#L198-L202

Added lines #L198 - L202 were not covered by tests
}

outputter := output.Get(runner.Output, output.Options{
NoColor: runner.NoColor,
SuppressExceptions: runner.SuppressExceptions,
Tracing: runner.Trace,
JUnitHideMessage: viper.GetBool("junit-hide-message"),
File: outputFile,
})
if err := outputter.Output(results); err != nil {
return fmt.Errorf("output results: %w", err)
}

if outputFilePath != "" {
lcarva marked this conversation as resolved.
Show resolved Hide resolved
contents, err := os.ReadFile(outputFile.Name())
if err != nil {
return fmt.Errorf("copying output file to stdout: %w", err)
}
fmt.Fprintln(cmd.OutOrStdout(), string(contents))

Check warning on line 221 in cmd/test/test.go

View check run for this annotation

Codecov / codecov/patch

cmd/test/test.go#L217-L221

Added lines #L217 - L221 were not covered by tests
}
}

// When the no-fail parameter is set, there is no need to figure out the error code
Expand Down Expand Up @@ -219,6 +250,7 @@
cmd.Flags().String("capabilities", "", "Path to JSON file that can restrict opa functionality against a given policy. Default: all operations allowed")

cmd.Flags().StringP("output", "o", output.OutputStandard, fmt.Sprintf("Output format for conftest results - valid options are: %s", append(output.Outputs(), OutputAppstudio)))
cmd.Flags().String("file", "", "File path to write output to")
cmd.Flags().Bool("junit-hide-message", false, "Do not include the violation message in the JUnit test name")

cmd.Flags().StringSliceP("policy", "p", []string{"policy"}, "Path to the Rego policy files directory")
Expand Down
Loading