Skip to content

Commit

Permalink
🌱 Fix race condition in output file test. (#3533)
Browse files Browse the repository at this point in the history
Signed-off-by: Spencer Schrock <sschrock@google.com>
  • Loading branch information
spencerschrock authored Oct 4, 2023
1 parent 2c25c46 commit 7ad9de3
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions pkg/scorecard_result_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ func Test_formatResults_outputToFile(t *testing.T) {
args: args{
opts: &options.Options{
Format: options.FormatJSON,
ResultsFile: "result.json",
ShowDetails: true,
LogLevel: log.DebugLevel.String(),
},
Expand All @@ -116,7 +115,6 @@ func Test_formatResults_outputToFile(t *testing.T) {
args: args{
opts: &options.Options{
Format: options.FormatDefault,
ResultsFile: "result.log",
ShowDetails: true,
LogLevel: log.DebugLevel.String(),
},
Expand All @@ -134,6 +132,14 @@ func Test_formatResults_outputToFile(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

// generate a unique result file in a temp directory for every test run to avoid race conditions in the test.
// This can happen when ginkgo runs unit tests in parallel (with -p flag)
resultFile, err := os.CreateTemp("", "result-file")
if err != nil {
t.Fatalf("create temp result file: %v", err)
}
tt.args.opts.ResultsFile = resultFile.Name()

// Format results.
formatErr := FormatResults(tt.args.opts, tt.args.results, tt.args.doc, tt.args.policy)
// Delete generated output file at the end of the test.
Expand Down

0 comments on commit 7ad9de3

Please sign in to comment.