-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgotest.go
More file actions
1032 lines (894 loc) · 31 KB
/
gotest.go
File metadata and controls
1032 lines (894 loc) · 31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package devflow
import (
"bytes"
"context"
"fmt"
"os"
"os/exec"
"regexp"
"strconv"
"strings"
"sync"
"time"
)
var semverTagRe = regexp.MustCompile(`^v?\d+\.\d+\.\d+$`)
// Test executes the test suite for the project.
// timeoutSec sets the per-package timeout in seconds (0 = default 30s).
func (g *Go) Test(customArgs []string, skipRace bool, timeoutSec int, noCache bool, runAll bool) (string, error) {
if timeoutSec <= 0 {
timeoutSec = 30
}
hasCustomArgs := len(customArgs) > 0
// Detect Module Name
moduleName, err := getModuleName(".")
if err != nil {
return "", fmt.Errorf("error: %v", err)
}
// Check cache only for full suite runs
if !hasCustomArgs && !noCache {
cache := NewTestCache()
if cache.IsCacheValid() {
return cache.GetCachedMessage(), nil
}
}
// Branch based on whether custom args are provided
if hasCustomArgs {
return g.runCustomTests(customArgs, moduleName, timeoutSec, runAll)
}
// Full test suite (run all phases)
return g.runFullTestSuite(moduleName, skipRace, timeoutSec, noCache, runAll)
}
// runFullTestSuite executes the complete test suite (vet, race, cover, wasm, badges)
func (g *Go) runFullTestSuite(moduleName string, skipRace bool, timeoutSec int, noCache bool, runAll bool) (string, error) {
// Check cache - if code hasn't changed since last successful test, return cached result
if !noCache {
cache := NewTestCache()
if cache.IsCacheValid() {
return cache.GetCachedMessage(), nil
}
}
// Initialize Status
testStatus := "Failed"
coveragePercent := "0"
raceStatus := "Detected"
vetStatus := "Issues"
var msgs []string
addMsg := func(ok bool, msg string) {
symbol := "✅"
if !ok {
symbol = "❌"
}
msgs = append(msgs, fmt.Sprintf("%s %s", msg, symbol))
}
// Parallel Phase 1: Vet + WASM detection
var wg1 sync.WaitGroup
var vetOutput string
var vetErr error
var enableWasmTests bool
wg1.Add(2)
// Go Vet (async)
go func() {
defer wg1.Done()
vetArgs := []string{"vet"}
if runAll {
vetArgs = append(vetArgs, "-tags=integration")
}
vetArgs = append(vetArgs, "./...")
vetOutput, vetErr = RunCommand("go", vetArgs...)
}()
// Check for WASM test files (async)
go func() {
defer wg1.Done()
// Check for WASM test files
// We do NOT return early for runAll anymore, we scan to see if actual WASM files exist.
// 1. Get native test files
nativeArgs := []string{"list", "-f", "{{.ImportPath}} {{.TestGoFiles}} {{.XTestGoFiles}}"}
if runAll {
nativeArgs = append(nativeArgs, "-tags=integration")
}
nativeArgs = append(nativeArgs, "./...")
nativeCmd := exec.Command("go", nativeArgs...)
nativeOut, _ := nativeCmd.CombinedOutput()
// 2. Get WASM test files
wasmArgs := []string{"list", "-f", "{{.ImportPath}} {{.TestGoFiles}} {{.XTestGoFiles}}"}
if runAll {
wasmArgs = append(wasmArgs, "-tags=integration")
}
wasmArgs = append(wasmArgs, "./...")
wasmCmd := exec.Command("go", wasmArgs...)
wasmCmd.Env = os.Environ()
wasmCmd.Env = append(wasmCmd.Env, "GOOS=js", "GOARCH=wasm")
wasmOut, _ := wasmCmd.CombinedOutput()
// 3. Decision logic
enableWasmTests = ShouldEnableWasm(string(nativeOut), string(wasmOut))
}()
wg1.Wait()
// Process vet results
if vetErr != nil {
// Check if it's just "no packages" error (WASM-only projects)
if strings.Contains(vetOutput, "matched no packages") ||
strings.Contains(vetOutput, "no packages to vet") ||
strings.Contains(vetOutput, "build constraints exclude all Go files") {
vetStatus = "OK"
addMsg(true, "vet")
} else {
vetStatus = "Issues"
// Filter unsafe.Pointer warnings
lines := strings.Split(vetOutput, "\n")
var filteredLines []string
for _, line := range lines {
if strings.TrimSpace(line) == "" || strings.HasPrefix(line, "#") { // Ignore comments/empty
continue
}
if !strings.Contains(line, "possible misuse of unsafe.Pointer") {
filteredLines = append(filteredLines, line)
}
}
if len(filteredLines) > 0 {
addMsg(false, "vet")
} else {
vetStatus = "OK"
addMsg(true, "vet")
}
}
} else {
vetStatus = "OK"
addMsg(true, "vet")
}
// Run tests with coverage and optional race detection
// go test ./... automatically discovers all packages with tests
var testErr error
var testOutput string
timeoutFlag := fmt.Sprintf("-timeout=%ds", timeoutSec)
tmpCovDir, _ := os.MkdirTemp("", "gotest-cov")
defer os.RemoveAll(tmpCovDir)
coverProfilePath := fmt.Sprintf("%s/cover.out", tmpCovDir)
testArgs := []string{"test", "-v", "-cover", "-coverpkg=./...", fmt.Sprintf("-coverprofile=%s", coverProfilePath), "-count=1", timeoutFlag}
if runAll {
testArgs = append(testArgs, "-tags=integration")
}
testArgs = append(testArgs, "./...")
if !skipRace {
testArgs = append(testArgs[:1], append([]string{"-race"}, testArgs[1:]...)...)
}
// Safety net: context kills process 10s after Go's -timeout should have fired
// This ensures we get the nice panic output from Go if possible
testCtx, testCancel := context.WithTimeout(context.Background(), time.Duration(timeoutSec+10)*time.Second)
defer testCancel()
testCmd := testCommand(testCtx, "go", testArgs...)
testBuffer := &bytes.Buffer{}
testFilter := NewConsoleFilter(g.consoleOutput)
testPipe := ¶mWriter{
write: func(p []byte) (n int, err error) {
s := string(p)
testBuffer.Write(p)
testFilter.Add(s)
return len(p), nil
},
}
testCmd.Stdout = testPipe
testCmd.Stderr = testPipe
testErr = testCmd.Run()
testFilter.Flush()
testOutput = testBuffer.String()
// Detect process-level timeout (killed by context)
if testCtx.Err() == context.DeadlineExceeded {
timedOut := FindTimedOutTests(testOutput)
if len(timedOut) > 0 {
for _, name := range timedOut {
addMsg(false, fmt.Sprintf("timeout: %s (exceeded %ds)", name, timeoutSec))
}
} else {
addMsg(false, fmt.Sprintf("timeout: tests exceeded %ds", timeoutSec))
}
testStatus = "Failed"
}
// Process test results
var stdTestsRan bool
testStatus, raceStatus, stdTestsRan, msgs = EvaluateTestResults(testErr, testOutput, moduleName, msgs, skipRace)
// If no stdlib tests ran but we see exclusions, consider enabling WASM (if not already enabled)
if !stdTestsRan {
isExclusionError := strings.Contains(testOutput, "matched no packages") ||
strings.Contains(testOutput, "build constraints exclude all Go files")
if isExclusionError {
enableWasmTests = true
g.log("No stdlib tests matched/run (possibly WASM-only module), skipping stdlib tests...")
}
}
// Process coverage results from the profile generated during the test run above
if stdTestsRan {
if cov := exactCoverageFromProfile(coverProfilePath); cov != "" {
coveragePercent = cov
} else {
coveragePercent = calculateAverageCoverage(testOutput)
}
}
// WASM Tests
var wasmTestOutput string
if enableWasmTests {
if err := g.installWasmBrowserTest(); err != nil {
addMsg(false, "WASM tests skipped (setup failed)")
} else {
execArg := "wasmbrowsertest"
// Add -count=1 to force cache bypass for WASM tests, consistent with native run
testArgs := []string{"test", "-exec", execArg, "-v", "-cover", "-coverpkg=./...", "-count=1", "./..."}
// Add cushion for WASM tests too
wasmCtx, wasmCancel := context.WithTimeout(context.Background(), time.Duration(timeoutSec+10)*time.Second)
defer wasmCancel()
wasmCmd := testCommand(wasmCtx, "go", testArgs...)
wasmCmd.Env = os.Environ()
wasmCmd.Env = append(wasmCmd.Env, "GOOS=js", "GOARCH=wasm")
var wasmOut bytes.Buffer
wasmFilter := NewConsoleFilter(g.consoleOutput)
wasmPipe := ¶mWriter{
write: func(p []byte) (n int, err error) {
s := string(p)
wasmOut.Write(p)
wasmFilter.Add(s)
return len(p), nil
},
}
wasmCmd.Stdout = wasmPipe
wasmCmd.Stderr = wasmPipe
err := wasmCmd.Run()
wasmFilter.Flush()
wOutput := wasmOut.String()
wasmTestOutput = wOutput
// Detect process-level timeout for WASM tests
if wasmCtx.Err() == context.DeadlineExceeded {
timedOut := FindTimedOutTests(wOutput)
if len(timedOut) == 0 {
// wasmbrowsertest buffers output: retry individually to find culprit
timedOut = g.findWasmTimeoutCulprit(timeoutSec)
}
if len(timedOut) > 0 {
for _, name := range timedOut {
addMsg(false, fmt.Sprintf("timeout: %s (exceeded %ds)", name, timeoutSec))
}
} else {
addMsg(false, fmt.Sprintf("timeout: wasm tests exceeded %ds", timeoutSec))
}
testStatus = "Failed"
} else if err != nil {
// WASM test failure - ConsoleFilter already filtered the output in quiet mode
addMsg(false, "wasm")
testStatus = "Failed"
} else {
addMsg(true, "wasm")
if testStatus != "Failed" {
testStatus = "Passing"
}
wCov := calculateAverageCoverage(wOutput)
// Try exact coverage for WASM if possible (might need special handling for WASM env)
// WASM tests are tricky because we use -exec wasmbrowsertest.
// getExactCoverage can support it if we pass correct args.
// But getExactCoverage implementation uses 'go test' which should respect GOOS/GOARCH from env.
// Let's rely on calculateAverageCoverage for WASM for now unless we update getExactCoverage to support WASM env injection passed from here.
// Actually, we can try getExactCoverage but we need to set Env.
// For now, let's stick to parsing for WASM as it seems reliable (89.0 vs 89.0 from manual run was parsed correctly from go tool cover output in manual run)
// Wait, manual run output "total: ... 89.0%".
// The parsed output of `go test` usually doesn't show "total:" key unless using -coverprofile?
// The output we parse is "coverage: 80.5% of statements".
// So manual run showed 89.0% because I ran `go tool cover`.
// `gotest` parsing only sees what `go test` emits.
// If we want 89.0% here, we need getExactCoverage for WASM too.
// Let's stick to simple parsing for WASM for now to avoid complexity with wasmbrowsertest + profile generation multiple times.
// The user sees 76.7% vs 89% discrepancy mostly because Native tests were averaging 22 and 80.
if wCov != "0" {
wVal, _ := strconv.ParseFloat(wCov, 64)
nVal, _ := strconv.ParseFloat(coveragePercent, 64)
if wVal > nVal {
coveragePercent = wCov
}
}
}
}
}
// Report consolidated coverage
if coveragePercent != "0" {
msgs = append(msgs, "coverage: "+coveragePercent+"%")
}
// Detect slowest test across stdlib and WASM outputs
allTestOutput := testOutput + "\n" + wasmTestOutput
if name, dur := FindSlowestTest(allTestOutput, 2.0); name != "" {
g.consoleOutput(fmt.Sprintf("⚠️ slow: %s (%.1fs)", name, dur))
}
// Detect timed out tests
if timedOut := FindTimedOutTests(allTestOutput); len(timedOut) > 0 {
for _, name := range timedOut {
addMsg(false, fmt.Sprintf("timeout: %s (exceeded %ds)", name, timeoutSec))
}
}
// Badges
licenseType := "MIT"
if checkFileExists("LICENSE") {
// naive check
}
goVer := GetGoVersion()
bh := NewBadges()
bh.SetLog(g.log)
if err := bh.updateBadges("README.md", licenseType, goVer, testStatus, coveragePercent, raceStatus, vetStatus, true); err != nil {
}
// Return error if tests or vet failed
summary := strings.Join(msgs, ", ")
if testStatus == "Failed" || vetStatus == "Issues" {
return summary, fmt.Errorf("%s", summary)
}
// Save test cache on success (for gopush optimization)
// We save even if noCache=true, because this was a valid run
cache := NewTestCache()
if err := cache.SaveCache(summary); err != nil {
g.log("Warning: failed to save test cache:", err)
}
return summary, nil
}
// runCustomTests executes tests with custom go test flags (fast path)
// Skips vet, badges, and cache, but runs WASM tests if detected
func (g *Go) runCustomTests(customArgs []string, moduleName string, timeoutSec int, runAll bool) (string, error) {
var msgs []string
addMsg := func(ok bool, msg string) {
symbol := "✅"
if !ok {
symbol = "❌"
}
msgs = append(msgs, fmt.Sprintf("%s %s", msg, symbol))
}
// Detect WASM tests in parallel with stdlib tests preparation
var wg sync.WaitGroup
var enableWasmTests bool
wg.Add(1)
go func() {
defer wg.Done()
// Check for WASM test files by comparing native vs WASM test file lists
// if runAll is set, we still check existence but include integration tags in detection
nativeArgs := []string{"list", "-f", "{{.ImportPath}} {{.TestGoFiles}} {{.XTestGoFiles}}"}
if runAll {
nativeArgs = append(nativeArgs, "-tags=integration")
}
nativeArgs = append(nativeArgs, "./...")
nativeCmd := exec.Command("go", nativeArgs...)
nativeOut, _ := nativeCmd.CombinedOutput()
wasmArgs := []string{"list", "-f", "{{.ImportPath}} {{.TestGoFiles}} {{.XTestGoFiles}}"}
if runAll {
wasmArgs = append(wasmArgs, "-tags=integration")
}
wasmArgs = append(wasmArgs, "./...")
wasmCmd := exec.Command("go", wasmArgs...)
wasmCmd.Env = os.Environ()
wasmCmd.Env = append(wasmCmd.Env, "GOOS=js", "GOARCH=wasm")
wasmOut, _ := wasmCmd.CombinedOutput()
enableWasmTests = ShouldEnableWasm(string(nativeOut), string(wasmOut))
}()
// Inject timeout if user didn't already pass -timeout
timeoutFlag := fmt.Sprintf("-timeout=%ds", timeoutSec)
if !HasTimeoutFlag(customArgs) {
customArgs = append(customArgs, timeoutFlag)
}
// Build command: go test <customArgs> ./...
testArgs := append([]string{"test"}, customArgs...)
if runAll {
testArgs = append(testArgs, "-tags=integration")
}
testArgs = append(testArgs, "./...")
customCtx, customCancel := context.WithTimeout(context.Background(), time.Duration(timeoutSec+10)*time.Second)
defer customCancel()
testCmd := testCommand(customCtx, "go", testArgs...)
testBuffer := &bytes.Buffer{}
// CRITICAL: Keep ConsoleFilter for clean output
testFilter := NewConsoleFilter(g.consoleOutput)
testPipe := ¶mWriter{
write: func(p []byte) (n int, err error) {
s := string(p)
testBuffer.Write(p)
testFilter.Add(s)
return len(p), nil
},
}
testCmd.Stdout = testPipe
testCmd.Stderr = testPipe
testErr := testCmd.Run()
testFilter.Flush()
testOutput := testBuffer.String()
// Detect process-level timeout
if customCtx.Err() == context.DeadlineExceeded {
timedOut := FindTimedOutTests(testOutput)
if len(timedOut) > 0 {
for _, name := range timedOut {
addMsg(false, fmt.Sprintf("timeout: %s (exceeded %ds)", name, timeoutSec))
}
} else {
addMsg(false, fmt.Sprintf("timeout: tests exceeded %ds", timeoutSec))
}
// Will be caught by EvaluateTestResults as a failure
}
// Wait for WASM detection to complete
wg.Wait()
// Process stdlib test results (without race detection reporting)
testStatus, _, stdTestsRan, msgs := EvaluateTestResults(testErr, testOutput, moduleName, msgs, false)
// Initialize coveragePercent for custom runs (not calculated for stdlib in fast path usually, but we need it for comparison)
coveragePercent := "0"
// Process coverage if std tests ran (fast path: use average from output)
if stdTestsRan {
coveragePercent = calculateAverageCoverage(testOutput)
}
// Remove "race detection ok" message since we're not forcing -race in custom args
// (user can add -race explicitly if desired)
var filteredMsgs []string
for _, msg := range msgs {
if !strings.Contains(msg, "race detection ok") {
filteredMsgs = append(filteredMsgs, msg)
}
}
msgs = filteredMsgs
// If no stdlib tests ran but we see exclusions, consider enabling WASM
if !stdTestsRan {
isExclusionError := strings.Contains(testOutput, "matched no packages") ||
strings.Contains(testOutput, "build constraints exclude all Go files")
if isExclusionError {
enableWasmTests = true
g.log("No stdlib tests matched/run (possibly WASM-only module), attempting WASM tests...")
}
}
// Run WASM tests with same custom args (excluding -race)
if enableWasmTests {
if err := g.installWasmBrowserTest(); err != nil {
addMsg(false, "WASM tests skipped (setup failed)")
} else {
// Build WASM test command with custom args, filtering out -race (not supported in WASM)
var wasmArgs []string
for _, arg := range customArgs {
if arg != "-race" {
wasmArgs = append(wasmArgs, arg)
}
}
// Inject timeout for WASM tests too
if !HasTimeoutFlag(wasmArgs) {
wasmArgs = append(wasmArgs, timeoutFlag)
}
// Always add -count=1 for WASM to enforce consistent behavior (no caching)
// unless user already specified it.
hasCount := false
for _, arg := range wasmArgs {
if strings.Contains(arg, "-count") {
hasCount = true
break
}
}
if !hasCount {
wasmArgs = append(wasmArgs, "-count=1")
}
wasmTestArgs := append([]string{"test", "-exec", "wasmbrowsertest"}, wasmArgs...)
if runAll {
wasmTestArgs = append(wasmTestArgs, "-tags=integration")
}
wasmTestArgs = append(wasmTestArgs, "./...")
wasmCtx, wasmCancel := context.WithTimeout(context.Background(), time.Duration(timeoutSec+10)*time.Second)
defer wasmCancel()
wasmCmd := testCommand(wasmCtx, "go", wasmTestArgs...)
wasmCmd.Env = os.Environ()
wasmCmd.Env = append(wasmCmd.Env, "GOOS=js", "GOARCH=wasm")
var wasmOut bytes.Buffer
wasmFilter := NewConsoleFilter(g.consoleOutput)
wasmPipe := ¶mWriter{
write: func(p []byte) (n int, err error) {
s := string(p)
wasmOut.Write(p)
wasmFilter.Add(s)
return len(p), nil
},
}
wasmCmd.Stdout = wasmPipe
wasmCmd.Stderr = wasmPipe
err := wasmCmd.Run()
wasmFilter.Flush()
if wasmCtx.Err() == context.DeadlineExceeded {
wOutput := wasmOut.String()
timedOut := FindTimedOutTests(wOutput)
if len(timedOut) == 0 {
timedOut = g.findWasmTimeoutCulprit(timeoutSec)
}
if len(timedOut) > 0 {
for _, name := range timedOut {
addMsg(false, fmt.Sprintf("timeout: %s (exceeded %ds)", name, timeoutSec))
}
} else {
addMsg(false, fmt.Sprintf("timeout: wasm tests exceeded %ds", timeoutSec))
}
testStatus = "Failed"
} else if err != nil {
addMsg(false, "wasm")
testStatus = "Failed"
} else {
wOutput := wasmOut.String()
wCov := calculateAverageCoverage(wOutput)
if wCov != "0" {
wVal, _ := strconv.ParseFloat(wCov, 64)
nVal, _ := strconv.ParseFloat(coveragePercent, 64)
if wVal > nVal {
coveragePercent = wCov
}
}
}
}
}
// Report consolidated coverage if available (and not 0)
if coveragePercent != "0" {
msgs = append(msgs, "coverage: "+coveragePercent+"%")
}
summary := strings.Join(msgs, ", ")
if testStatus == "Failed" {
return summary, fmt.Errorf("%s", summary)
}
// NO cache save, NO badges (as requested)
return summary, nil
}
// testCommand creates an exec.Cmd with graceful timeout handling.
// On timeout: sends SIGINT first (lets the process flush output), then SIGKILL after 5s.
func testCommand(ctx context.Context, name string, args ...string) *exec.Cmd {
cmd := exec.CommandContext(ctx, name, args...)
cmd.Cancel = func() error {
return cmd.Process.Signal(os.Interrupt)
}
cmd.WaitDelay = 5 * time.Second
return cmd
}
type paramWriter struct {
write func(p []byte) (n int, err error)
}
func (p *paramWriter) Write(b []byte) (n int, err error) {
return p.write(b)
}
// FindSlowestTest parses -v test output and returns the name and duration of the slowest individual test
// across all packages if it exceeds the specified threshold.
func FindSlowestTest(output string, threshold float64) (string, float64) {
// Parse individual test timing from -v output: --- PASS: TestName (2.00s)
testRe := regexp.MustCompile(`--- (?:PASS|FAIL): (\S+) \((\d+(?:\.\d+)?)s\)`)
var slowestName string
var slowestTime float64
for _, match := range testRe.FindAllStringSubmatch(output, -1) {
t, err := strconv.ParseFloat(match[2], 64)
if err != nil {
continue
}
if t > slowestTime {
slowestName = match[1]
slowestTime = t
}
}
if slowestTime >= threshold {
return slowestName, slowestTime
}
return "", 0
}
func calculateAverageCoverage(output string) string {
lines := strings.Split(output, "\n")
// Map to store max coverage per package
// If package name is unknown, use unique key to treat as separate
pkgCoverage := make(map[string]float64)
// Regex to parse: ok package_name time coverage: X% of statements [in target_package]
// We want to group by the TARGET package if specified ("in target_package"),
// otherwise by the test package.
// Actually, if we use -coverpkg=./..., many tests cover the SAME target package.
// We want the coverage OF the target package.
// So if "in X" is present, use X. If not, use test package Y.
// Regex for "coverage: X% of statements in PACKAGE"
reWithPkg := regexp.MustCompile(`coverage:\s+(\d+(\.\d+)?)%\s+of\s+statements\s+in\s+(\S+)`)
// Regex for simple "coverage: X%" (fallback)
reSimple := regexp.MustCompile(`coverage:\s+(\d+(\.\d+)?)%`)
for _, line := range lines {
if strings.Contains(line, "[no test files]") {
continue
}
// Try explicit target package first
matchesPkg := reWithPkg.FindStringSubmatch(line)
if len(matchesPkg) > 3 {
val, _ := strconv.ParseFloat(matchesPkg[1], 64)
pkg := matchesPkg[3]
if val > pkgCoverage[pkg] {
pkgCoverage[pkg] = val
}
continue
}
// Fallback to simple coverage (usually implies covering itself)
// We need to find the package name from the "ok" line start if possible
// Line format: "ok package_name time coverage: ..."
matchesSimple := reSimple.FindStringSubmatch(line)
if len(matchesSimple) > 1 {
val, _ := strconv.ParseFloat(matchesSimple[1], 64)
// Try to extract package name from start of line
fields := strings.Fields(line)
pkg := ""
if len(fields) >= 2 && fields[0] == "ok" {
pkg = fields[1]
} else {
// If we can't determine package, use the line itself as unique key to avoid merging
pkg = line
}
if val > pkgCoverage[pkg] {
pkgCoverage[pkg] = val
}
}
}
if len(pkgCoverage) == 0 {
return "0"
}
var total float64
for _, val := range pkgCoverage {
total += val
}
return fmt.Sprintf("%.1f", total/float64(len(pkgCoverage)))
}
// exactCoverageFromProfile reads a coverage profile and returns the total percentage.
func exactCoverageFromProfile(profilePath string) string {
out, err := exec.Command("go", "tool", "cover", fmt.Sprintf("-func=%s", profilePath)).CombinedOutput()
if err != nil {
return ""
}
for _, line := range strings.Split(string(out), "\n") {
if strings.Contains(line, "total:") {
parts := strings.Fields(line)
if len(parts) > 0 {
return strings.TrimSuffix(parts[len(parts)-1], "%")
}
}
}
return ""
}
func (g *Go) installWasmBrowserTest() error {
if _, err := RunCommandSilent("which", "wasmbrowsertest"); err == nil {
return nil
}
_, err := RunCommand("go", "install", "github.com/tinywasm/wasmbrowsertest@latest")
if err != nil {
return fmt.Errorf("go install failed: %w", err)
}
return nil
}
// ShouldEnableWasm decides if WASM tests should be run based on go list output differences
func ShouldEnableWasm(nativeOut, wasmOut string) bool {
// fmt.Printf("DEBUG: ShouldEnableWasm check starting\n")
nativeFiles := parseGoListFiles(nativeOut)
// fmt.Printf("DEBUG: ShouldEnableWasm - Native files found: %d\n", len(nativeFiles))
wasmFiles := parseGoListFiles(wasmOut)
// fmt.Printf("DEBUG: ShouldEnableWasm - WASM files found: %d\n", len(wasmFiles))
// Activation condition: at least one test file in WASM that is NOT in Native
// This means it has a //go:build wasm tag or similar.
for f := range wasmFiles {
if !nativeFiles[f] {
return true
}
}
return false
}
// parseGoListFiles converts the output of go list into a map of unique test files
func parseGoListFiles(output string) map[string]bool {
fileMap := make(map[string]bool)
lines := strings.Split(output, "\n")
for _, line := range lines {
line = strings.TrimSpace(line)
if line == "" {
continue
}
// fmt.Printf("DEBUG: parse line: %q\n", line)
// Legitimate go list lines for this template usually contain '['
// but we must skip error messages that might start with "package" or involve "syscall/js"
if !strings.Contains(line, "[") {
continue
}
// Extract package path and file list: "path [a_test.go b_test.go] []"
parts := strings.SplitN(line, " ", 2)
if len(parts) < 2 {
continue
}
pkgPath := parts[0]
fileList := parts[1]
// Final check: pkgPath shouldn't have spaces if it's a real path from go list
if strings.Contains(pkgPath, " ") {
continue
}
// Normalize file list and add to map: pkgPath/file
fileList = strings.ReplaceAll(fileList, "[", "")
fileList = strings.ReplaceAll(fileList, "]", "")
files := strings.Fields(fileList)
for _, f := range files {
fileMap[pkgPath+"/"+f] = true
}
}
// fmt.Printf("DEBUG: Found %d unique test files\n", len(fileMap))
return fileMap
}
// EvaluateTestResults analyzes the output of go test and decides the outcome
// This function is pure and can be easily tested.
func EvaluateTestResults(err error, output, moduleName string, msgs []string, skipRace bool) (testStatus, raceStatus string, stdTestsRan bool, newMsgs []string) {
testStatus = "Failed"
raceStatus = "Detected"
if skipRace {
raceStatus = "Skipped"
}
newMsgs = msgs
addMsg := func(ok bool, msg string) {
symbol := "✅"
if !ok {
symbol = "❌"
}
newMsgs = append(newMsgs, fmt.Sprintf("%s %s", msg, symbol))
}
// Determine if any stdlib tests actually ran by looking for ok/FAIL markers in output
// Use more robust matching that handles different spacing/tabs
hasStdOk := strings.Contains(output, "ok ") || strings.Contains(output, "ok\t") || strings.Contains(output, "\tok\t")
hasStdFail := strings.Contains(output, "FAIL ") || strings.Contains(output, "FAIL\t") || strings.Contains(output, "\tFAIL\t")
stdTestsRan = hasStdOk || hasStdFail
if err == nil {
testStatus = "Passing"
if !skipRace {
raceStatus = "Clean"
addMsg(true, "race")
} else {
addMsg(true, "race skipped")
}
addMsg(true, "tests")
stdTestsRan = true
return
}
// It failed (exit code != 0). Is it a real test failure or just build constraints?
// Check for real test failures: "--- FAIL"
// Also check for "FAIL\t" but EXCLUDE "[setup failed]" if we have valid tests passing elsewhere
hasRealFailures := strings.Contains(output, "--- FAIL")
if !hasRealFailures {
// Look for FAIL lines that are NOT setup failures
lines := strings.Split(output, "\n")
for _, line := range lines {
if (strings.Contains(line, "FAIL\t") || strings.Contains(line, "FAIL ")) &&
!strings.Contains(line, "[setup failed]") {
hasRealFailures = true
break
}
}
}
// Check for build failures: "[build failed]" or similar
hasBuildFailures := strings.Contains(output, "[build failed]")
// Check for exclusion errors (can be explicit or part of setup failed)
isExclusionError := strings.Contains(output, "matched no packages") ||
strings.Contains(output, "build constraints exclude all Go files")
// Special case: Setup failed due to build constraints but other tests PASSED
if !hasRealFailures && !hasBuildFailures {
if strings.Contains(output, "[setup failed]") && isExclusionError && hasStdOk {
// This is the "Partial Success" scenario (client)
// Treat as success
} else if strings.Contains(output, "[setup failed]") {
// Setup failed for other reasons (and no other success confirmed logic override)
hasRealFailures = true
}
}
if !hasRealFailures && !hasBuildFailures && (isExclusionError || hasStdOk) {
// It's a "Partial Success" or "Exclusion Only"
testStatus = "Passing"
if !skipRace {
raceStatus = "Clean"
if stdTestsRan {
addMsg(true, "race")
}
} else {
if stdTestsRan {
addMsg(true, "race skipped")
}
}
if stdTestsRan {
addMsg(true, "tests")
}
} else {
// Real failure
addMsg(false, fmt.Sprintf("Test errors found in %s", moduleName))
}
return
}
// HasTimeoutFlag checks if -timeout is already present in the args
func HasTimeoutFlag(args []string) bool {
for _, arg := range args {
if arg == "-timeout" || strings.HasPrefix(arg, "-timeout=") ||
arg == "-test.timeout" || strings.HasPrefix(arg, "-test.timeout=") {
return true
}
}
return false
}
// FindTimedOutTests parses go test output and extracts test names that timed out.
// Handles two scenarios:
// 1. Go's native timeout: "panic: test timed out after Ns\n running tests:\n TestName (Ns)"
// 2. Process killed externally (context.WithTimeout): finds the last "=== RUN" without a matching "--- PASS/FAIL"
func FindTimedOutTests(output string) []string {
// Try Go's native timeout format: "running tests:" section
if strings.Contains(output, "running tests:") {
re := regexp.MustCompile(`(?m)^\s+(\S+)\s+\(\d+`)
var names []string
inRunning := false
for _, line := range strings.Split(output, "\n") {
if strings.Contains(line, "running tests:") {
inRunning = true
continue
}
if inRunning {
if matches := re.FindStringSubmatch(line); len(matches) > 1 {
names = append(names, matches[1])
} else if strings.TrimSpace(line) != "" && !strings.HasPrefix(strings.TrimSpace(line), "goroutine") {
continue
} else {
break
}
}
}
if len(names) > 0 {
return names
}
}
// Fallback: find the last "=== RUN" without a matching "--- PASS/FAIL"
// Works when process is killed externally (context timeout, SIGKILL)
var lastRun string
for _, line := range strings.Split(output, "\n") {
if strings.Contains(line, "=== RUN") {
fields := strings.Fields(line)
if len(fields) >= 3 {
lastRun = fields[2]
}
}
if strings.Contains(line, "--- PASS:") || strings.Contains(line, "--- FAIL:") {
lastRun = ""
}
}
if lastRun != "" {
return []string{lastRun}
}
return nil
}
// discoverWasmTestNames scans WASM test source files for func TestXxx declarations.
// Used as fallback when wasmbrowsertest doesn't relay === RUN lines before a timeout kill.
// findWasmTimeoutCulprit retries WASM tests individually to identify which test hangs.
// Called after a bulk WASM run times out (wasmbrowsertest buffers output, so we can't
// determine the culprit from the output buffer).
func (g *Go) findWasmTimeoutCulprit(timeoutSec int) []string {
names := discoverWasmTestNames()
if len(names) <= 1 {
return names
}
g.log("Identifying timed out wasm test...")
for _, name := range names {
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeoutSec)*time.Second)
cmd := testCommand(ctx, "go", "test", "-exec", "wasmbrowsertest", "-run", "^"+name+"$", "-v", "./...")
cmd.Env = append(os.Environ(), "GOOS=js", "GOARCH=wasm")
cmd.Stdout = nil
cmd.Stderr = nil
cmd.Run()
timedOut := ctx.Err() == context.DeadlineExceeded
cancel()
if timedOut {
return []string{name}