@@ -20,30 +20,32 @@ const (
2020 GIT_CONFIG_GLOBAL_ENV_VAR = "GIT_CONFIG_GLOBAL"
2121)
2222
23+ type RunTestArgs struct {
24+ Tests []* IntegrationTest
25+ Logf func (format string , formatArgs ... interface {})
26+ RunCmd func (cmd * exec.Cmd ) (int , error )
27+ TestWrapper func (test * IntegrationTest , f func () error )
28+ Sandbox bool
29+ WaitForDebugger bool
30+ RaceDetector bool
31+ CodeCoverageDir string
32+ InputDelay int
33+ MaxAttempts int
34+ }
35+
2336// This function lets you run tests either from within `go test` or from a regular binary.
2437// The reason for having two separate ways of testing is that `go test` isn't great at
2538// showing what's actually happening during the test, but it's still good at running
2639// tests in telling you about their results.
27- func RunTests (
28- tests []* IntegrationTest ,
29- logf func (format string , formatArgs ... interface {}),
30- runCmd func (cmd * exec.Cmd ) (int , error ),
31- testWrapper func (test * IntegrationTest , f func () error ),
32- sandbox bool ,
33- waitForDebugger bool ,
34- raceDetector bool ,
35- inputDelay int ,
36- maxAttempts int ,
37- ) error {
40+ func RunTests (args RunTestArgs ) error {
3841 projectRootDir := lazycoreUtils .GetLazyRootDirectory ()
3942 err := os .Chdir (projectRootDir )
4043 if err != nil {
4144 return err
4245 }
4346
4447 testDir := filepath .Join (projectRootDir , "test" , "_results" )
45-
46- if err := buildLazygit (waitForDebugger , raceDetector ); err != nil {
48+ if err := buildLazygit (args ); err != nil {
4749 return err
4850 }
4951
@@ -52,21 +54,21 @@ func RunTests(
5254 return err
5355 }
5456
55- for _ , test := range tests {
57+ for _ , test := range args . Tests {
5658 test := test
5759
58- testWrapper (test , func () error { //nolint: thelper
60+ args . TestWrapper (test , func () error { //nolint: thelper
5961 paths := NewPaths (
6062 filepath .Join (testDir , test .Name ()),
6163 )
6264
63- for i := 0 ; i < maxAttempts ; i ++ {
64- err := runTest (test , paths , projectRootDir , logf , runCmd , sandbox , waitForDebugger , raceDetector , inputDelay , gitVersion )
65+ for i := 0 ; i < args . MaxAttempts ; i ++ {
66+ err := runTest (test , args , paths , projectRootDir , gitVersion )
6567 if err != nil {
66- if i == maxAttempts - 1 {
68+ if i == args . MaxAttempts - 1 {
6769 return err
6870 }
69- logf ("retrying test %s" , test .Name ())
71+ args . Logf ("retrying test %s" , test .Name ())
7072 } else {
7173 break
7274 }
@@ -81,42 +83,37 @@ func RunTests(
8183
8284func runTest (
8385 test * IntegrationTest ,
86+ args RunTestArgs ,
8487 paths Paths ,
8588 projectRootDir string ,
86- logf func (format string , formatArgs ... interface {}),
87- runCmd func (cmd * exec.Cmd ) (int , error ),
88- sandbox bool ,
89- waitForDebugger bool ,
90- raceDetector bool ,
91- inputDelay int ,
9289 gitVersion * git_commands.GitVersion ,
9390) error {
9491 if test .Skip () {
95- logf ("Skipping test %s" , test .Name ())
92+ args . Logf ("Skipping test %s" , test .Name ())
9693 return nil
9794 }
9895
9996 if ! test .ShouldRunForGitVersion (gitVersion ) {
100- logf ("Skipping test %s for git version %d.%d.%d" , test .Name (), gitVersion .Major , gitVersion .Minor , gitVersion .Patch )
97+ args . Logf ("Skipping test %s for git version %d.%d.%d" , test .Name (), gitVersion .Major , gitVersion .Minor , gitVersion .Patch )
10198 return nil
10299 }
103100
104101 if err := prepareTestDir (test , paths , projectRootDir ); err != nil {
105102 return err
106103 }
107104
108- cmd , err := getLazygitCommand (test , paths , projectRootDir , sandbox , waitForDebugger , inputDelay )
105+ cmd , err := getLazygitCommand (test , args , paths , projectRootDir )
109106 if err != nil {
110107 return err
111108 }
112109
113- pid , err := runCmd (cmd )
110+ pid , err := args . RunCmd (cmd )
114111
115112 // Print race detector log regardless of the command's exit status
116- if raceDetector {
113+ if args . RaceDetector {
117114 logPath := fmt .Sprintf ("%s.%d" , raceDetectorLogsPath (), pid )
118115 if bytes , err := os .ReadFile (logPath ); err == nil {
119- logf ("Race detector log:\n " + string (bytes ))
116+ args . Logf ("Race detector log:\n " + string (bytes ))
120117 }
121118 }
122119
@@ -139,20 +136,19 @@ func prepareTestDir(
139136 return createFixture (test , paths , rootDir )
140137}
141138
142- func buildLazygit (debug bool , raceDetector bool ) error {
143- // // TODO: remove this line!
144- // // skipping this because I'm not making changes to the app code atm.
145- // return nil
146-
139+ func buildLazygit (testArgs RunTestArgs ) error {
147140 args := []string {"go" , "build" }
148- if debug {
141+ if testArgs . WaitForDebugger {
149142 // Disable compiler optimizations (-N) and inlining (-l) because this
150143 // makes debugging work better
151144 args = append (args , "-gcflags=all=-N -l" )
152145 }
153- if raceDetector {
146+ if testArgs . RaceDetector {
154147 args = append (args , "-race" )
155148 }
149+ if testArgs .CodeCoverageDir != "" {
150+ args = append (args , "-cover" )
151+ }
156152 args = append (args , "-o" , tempLazygitPath (), filepath .FromSlash ("pkg/integration/clients/injector/main.go" ))
157153 osCommand := oscommands .NewDummyOSCommand ()
158154 return osCommand .Cmd .New (args ).Run ()
@@ -183,7 +179,7 @@ func getGitVersion() (*git_commands.GitVersion, error) {
183179 return git_commands .ParseGitVersion (versionStr )
184180}
185181
186- func getLazygitCommand (test * IntegrationTest , paths Paths , rootDir string , sandbox bool , waitForDebugger bool , inputDelay int ) (* exec.Cmd , error ) {
182+ func getLazygitCommand (test * IntegrationTest , args RunTestArgs , paths Paths , rootDir string ) (* exec.Cmd , error ) {
187183 osCommand := oscommands .NewDummyOSCommand ()
188184
189185 err := os .RemoveAll (paths .Config ())
@@ -211,11 +207,18 @@ func getLazygitCommand(test *IntegrationTest, paths Paths, rootDir string, sandb
211207
212208 cmdObj := osCommand .Cmd .New (cmdArgs )
213209
210+ if args .CodeCoverageDir != "" {
211+ // We set this explicitly here rather than inherit it from the test runner's
212+ // environment because the test runner has its own coverage directory that
213+ // it writes to and so if we pass GOCOVERDIR to that, it will be overwritten.
214+ cmdObj .AddEnvVars ("GOCOVERDIR=" + args .CodeCoverageDir )
215+ }
216+
214217 cmdObj .AddEnvVars (fmt .Sprintf ("%s=%s" , TEST_NAME_ENV_VAR , test .Name ()))
215- if sandbox {
218+ if args . Sandbox {
216219 cmdObj .AddEnvVars (fmt .Sprintf ("%s=%s" , SANDBOX_ENV_VAR , "true" ))
217220 }
218- if waitForDebugger {
221+ if args . WaitForDebugger {
219222 cmdObj .AddEnvVars (fmt .Sprintf ("%s=true" , WAIT_FOR_DEBUGGER_ENV_VAR ))
220223 }
221224 // Set a race detector log path only to avoid spamming the terminal with the
@@ -227,8 +230,8 @@ func getLazygitCommand(test *IntegrationTest, paths Paths, rootDir string, sandb
227230 }
228231 }
229232
230- if inputDelay > 0 {
231- cmdObj .AddEnvVars (fmt .Sprintf ("INPUT_DELAY=%d" , inputDelay ))
233+ if args . InputDelay > 0 {
234+ cmdObj .AddEnvVars (fmt .Sprintf ("INPUT_DELAY=%d" , args . InputDelay ))
232235 }
233236
234237 cmdObj .AddEnvVars (fmt .Sprintf ("%s=%s" , GIT_CONFIG_GLOBAL_ENV_VAR , globalGitConfigPath (rootDir )))
0 commit comments