Skip to content

Commit

Permalink
ec test: implement option to write output to a file
Browse files Browse the repository at this point in the history
Signed-off-by: Mark Bestavros <mbestavr@redhat.com>
  • Loading branch information
mbestavros committed Dec 19, 2023
1 parent d7a0017 commit aaa1887
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions cmd/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ package test
import (
"encoding/json"
"fmt"
"io"
"os"

"github.com/open-policy-agent/conftest/output"
Expand Down Expand Up @@ -118,6 +119,7 @@ func newTestCommand() *cobra.Command {
"no-fail",
"suppress-exceptions",
"output",
"file",
"parser",
"policy",
"proto-file-dirs",
Expand Down Expand Up @@ -150,6 +152,11 @@ func newTestCommand() *cobra.Command {
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 158 in cmd/test/test.go

View check run for this annotation

Codecov / codecov/patch

cmd/test/test.go#L157-L158

Added lines #L157 - L158 were not covered by tests

results, resultsErr := runner.Run(ctx, fileList)
var exitCode int
if runner.FailOnWarn {
Expand All @@ -171,6 +178,10 @@ func newTestCommand() *cobra.Command {
if err != nil {
return appstudioErrorHandler(runner.NoFail, "output results", err)
}

if outputFilePath != "" {
os.WriteFile(outputFilePath, reportOutput, 0644)

Check failure on line 183 in cmd/test/test.go

View workflow job for this annotation

GitHub Actions / Lint

Error return value of `os.WriteFile` is not checked (errcheck)
}

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

View check run for this annotation

Codecov / codecov/patch

cmd/test/test.go#L182-L184

Added lines #L182 - L184 were not covered by tests
fmt.Printf("%s\n", reportOutput)

} else {
Expand All @@ -180,15 +191,29 @@ func newTestCommand() *cobra.Command {
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 200 in cmd/test/test.go

View check run for this annotation

Codecov / codecov/patch

cmd/test/test.go#L196-L200

Added lines #L196 - L200 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 != "" {
io.Copy(outputFile, os.Stdout)

Check failure on line 215 in cmd/test/test.go

View workflow job for this annotation

GitHub Actions / Lint

Error return value of `io.Copy` is not checked (errcheck)
}

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

View check run for this annotation

Codecov / codecov/patch

cmd/test/test.go#L215-L216

Added lines #L215 - L216 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 +244,7 @@ func newTestCommand() *cobra.Command {
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

0 comments on commit aaa1887

Please sign in to comment.