@@ -4,29 +4,25 @@ import (
44 "encoding/json"
55 "fmt"
66 "os"
7+ "path/filepath"
78 "testing"
89
910 "github.com/smartcontractkit/chainlink-testing-framework/tools/flakeguard/reports"
1011 "github.com/stretchr/testify/assert"
1112 "github.com/stretchr/testify/require"
1213)
1314
14- type MockGoRunner struct {
15- RunFunc func (dir string , args []string ) (string , bool , error )
16- }
17-
18- func (m MockGoRunner ) RunCommand (dir string , args []string ) (string , bool , error ) {
19- return m .RunFunc (dir , args )
20- }
21-
2215type expectedTestResult struct {
2316 TestResult * reports.TestResult
2417 seen bool
2518}
2619
2720var (
28- defaultRuns = 5
29- defaultTestRunner = Runner {
21+ defaultRuns = 5
22+ )
23+
24+ func TestRunDefault (t * testing.T ) {
25+ defaultTestRunner := Runner {
3026 ProjectPath : "./" ,
3127 Verbose : true ,
3228 RunCount : defaultRuns ,
3632 SelectedTestPackages : []string {"./flaky_test_package" },
3733 CollectRawOutput : true ,
3834 }
39- )
4035
41- func TestRunDefault (t * testing.T ) {
4236 expectedResults := map [string ]* expectedTestResult {
4337 "TestFlaky" : {
4438 TestResult : & reports.TestResult {
@@ -77,16 +71,7 @@ func TestRunDefault(t *testing.T) {
7771
7872 testResults , err := defaultTestRunner .RunTests ()
7973 require .NoError (t , err )
80- t .Cleanup (func () {
81- if t .Failed () {
82- resultsFileName := fmt .Sprintf ("flaky_test_results_%s.json" , t .Name ())
83- t .Logf ("Writing test results to %s" , resultsFileName )
84- jsonResults , err := json .Marshal (testResults )
85- require .NoError (t , err )
86- err = os .WriteFile (resultsFileName , jsonResults , 0644 ) //nolint:gosec
87- require .NoError (t , err )
88- }
89- })
74+ cleanup (t , testResults , defaultTestRunner )
9075
9176 for _ , result := range testResults {
9277 t .Run (fmt .Sprintf ("checking results of %s" , result .TestName ), func (t * testing.T ) {
@@ -124,8 +109,16 @@ func TestRunDefault(t *testing.T) {
124109}
125110
126111func TestRunWithPanics (t * testing.T ) {
127- panicRunner := defaultTestRunner
128- panicRunner .SkipTests = []string {}
112+ panicTestRunner := Runner {
113+ ProjectPath : "./" ,
114+ Verbose : true ,
115+ RunCount : defaultRuns ,
116+ UseRace : false ,
117+ SkipTests : []string {},
118+ FailFast : false ,
119+ SelectedTestPackages : []string {"./flaky_test_package" },
120+ CollectRawOutput : true ,
121+ }
129122
130123 expectedResults := map [string ]* expectedTestResult {
131124 "TestFlaky" : {
@@ -170,18 +163,9 @@ func TestRunWithPanics(t *testing.T) {
170163 },
171164 }
172165
173- testResults , err := panicRunner .RunTests ()
166+ testResults , err := panicTestRunner .RunTests ()
174167 require .NoError (t , err )
175- t .Cleanup (func () {
176- if t .Failed () {
177- resultsFileName := fmt .Sprintf ("flaky_test_results_%s.json" , t .Name ())
178- t .Logf ("Writing test results to %s" , resultsFileName )
179- jsonResults , err := json .Marshal (testResults )
180- require .NoError (t , err )
181- err = os .WriteFile (resultsFileName , jsonResults , 0644 ) //nolint:gosec
182- require .NoError (t , err )
183- }
184- })
168+ cleanup (t , testResults , panicTestRunner )
185169
186170 for _ , result := range testResults {
187171 t .Run (fmt .Sprintf ("checking results of %s" , result .TestName ), func (t * testing.T ) {
@@ -218,3 +202,34 @@ func TestRunWithPanics(t *testing.T) {
218202 assert .True (t , expected .seen , "expected test '%s' not found in test runs" , expected .TestResult .TestName )
219203 }
220204}
205+
206+ func cleanup (t * testing.T , testResults []reports.TestResult , runner Runner ) {
207+ t .Helper ()
208+
209+ t .Cleanup (func () {
210+ if t .Failed () {
211+ resultsFileName := fmt .Sprintf ("debug_test_results_%s.json" , t .Name ())
212+ t .Logf ("Writing test results to %s" , resultsFileName )
213+ jsonResults , err := json .Marshal (testResults )
214+ if err != nil {
215+ t .Logf ("error marshalling test results: %v" , err )
216+ }
217+ err = os .WriteFile (resultsFileName , jsonResults , 0644 ) //nolint:gosec
218+ if err != nil {
219+ t .Logf ("error writing test results: %v" , err )
220+ }
221+ if len (runner .RawOutputs ()) == 0 {
222+ t .Logf ("WARN: no raw output to write" )
223+ }
224+ for packageName , rawOutput := range runner .RawOutputs () {
225+ saniPackageName := filepath .Base (packageName )
226+ rawOutputFileName := fmt .Sprintf ("debug_raw_output_%s_%s.json" , t .Name (), saniPackageName )
227+ t .Logf ("Writing raw output to %s" , rawOutputFileName )
228+ err = os .WriteFile (rawOutputFileName , rawOutput .Bytes (), 0644 ) //nolint:gosec
229+ if err != nil {
230+ t .Logf ("error writing raw output: %v" , err )
231+ }
232+ }
233+ }
234+ })
235+ }
0 commit comments