From 7ad9de35bb38c5654e211117934a4ba6b80ca48d Mon Sep 17 00:00:00 2001 From: Spencer Schrock Date: Tue, 3 Oct 2023 17:21:09 -0700 Subject: [PATCH] :seedling: Fix race condition in output file test. (#3533) Signed-off-by: Spencer Schrock --- pkg/scorecard_result_test.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/pkg/scorecard_result_test.go b/pkg/scorecard_result_test.go index e2176ce99ce..7d78aafa29c 100644 --- a/pkg/scorecard_result_test.go +++ b/pkg/scorecard_result_test.go @@ -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(), }, @@ -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(), }, @@ -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.