|
| 1 | +//go:build cover |
| 2 | +// +build cover |
| 3 | + |
| 4 | +package main |
| 5 | + |
| 6 | +import ( |
| 7 | + "bufio" |
| 8 | + "fmt" |
| 9 | + "log" |
| 10 | + "os" |
| 11 | + "os/exec" |
| 12 | + "path/filepath" |
| 13 | + "strings" |
| 14 | +) |
| 15 | + |
| 16 | +func main() { |
| 17 | + rootDir := "." |
| 18 | + var coverFiles []string |
| 19 | + var testFailures bool |
| 20 | + |
| 21 | + // Phase 1: Find go.mod files, run tests, and collect cover.out file paths |
| 22 | + err := filepath.WalkDir(rootDir, func(path string, d os.DirEntry, err error) error { |
| 23 | + if err != nil { |
| 24 | + log.Printf("Warning: Error accessing path %q: %v\n", path, err) |
| 25 | + return err // Skip this path |
| 26 | + } |
| 27 | + |
| 28 | + if d.IsDir() && (d.Name() == "vendor" || d.Name() == ".git" || strings.Contains(path, "testdata")) { |
| 29 | + log.Printf("Skipping directory: %s\n", path) |
| 30 | + return filepath.SkipDir |
| 31 | + } |
| 32 | + |
| 33 | + if !d.IsDir() && d.Name() == "go.mod" { |
| 34 | + modDir := filepath.Dir(path) |
| 35 | + fmt.Printf("--> Found go.mod in: %s\n", modDir) |
| 36 | + |
| 37 | + fullCoverProfilePath := filepath.Join(modDir, "cover.out") |
| 38 | + relativeCoverProfileArg := "cover.out" |
| 39 | + |
| 40 | + fmt.Printf("--> Running tests with coverage in: %s (profile: %s)\n", modDir, relativeCoverProfileArg) |
| 41 | + cmd := exec.Command("go", "test", "./...", "-race", "-coverprofile="+relativeCoverProfileArg, "-covermode=atomic") |
| 42 | + cmd.Dir = modDir |
| 43 | + cmd.Stdout = os.Stdout |
| 44 | + cmd.Stderr = os.Stderr |
| 45 | + |
| 46 | + runErr := cmd.Run() |
| 47 | + if runErr != nil { |
| 48 | + log.Printf("Error running tests in %s: %v\n", modDir, runErr) |
| 49 | + testFailures = true |
| 50 | + } else { |
| 51 | + fmt.Printf("--> Tests finished successfully in: %s\n", modDir) |
| 52 | + // Check for the existence of the cover file using its full path |
| 53 | + if _, statErr := os.Stat(fullCoverProfilePath); statErr == nil { |
| 54 | + coverFiles = append(coverFiles, fullCoverProfilePath) |
| 55 | + } else { |
| 56 | + log.Printf("Warning: %s not found in %s after tests ran. This module might not have any tests or testable code.\n", relativeCoverProfileArg, modDir) |
| 57 | + } |
| 58 | + } |
| 59 | + fmt.Println(strings.Repeat("-", 40)) |
| 60 | + } |
| 61 | + return nil |
| 62 | + }) |
| 63 | + |
| 64 | + if err != nil { |
| 65 | + log.Fatalf("Error walking the path %q: %v\n", rootDir, err) |
| 66 | + } |
| 67 | + |
| 68 | + // Phase 2: Merge coverage files |
| 69 | + if len(coverFiles) > 0 { |
| 70 | + mergedCoverageFile := filepath.Join(rootDir, "coverage.txt") // Place in root directory |
| 71 | + fmt.Printf("--> Merging %d coverage files into %s\n", len(coverFiles), mergedCoverageFile) |
| 72 | + |
| 73 | + out, err := os.Create(mergedCoverageFile) |
| 74 | + if err != nil { |
| 75 | + log.Fatalf("Failed to create merged coverage file %s: %v\n", mergedCoverageFile, err) |
| 76 | + } |
| 77 | + defer out.Close() |
| 78 | + |
| 79 | + firstFile := true |
| 80 | + for _, coverFile := range coverFiles { |
| 81 | + fmt.Printf(" Processing %s\n", coverFile) |
| 82 | + f, err := os.Open(coverFile) |
| 83 | + if err != nil { |
| 84 | + log.Printf("Warning: Failed to open coverage file %s: %v. Skipping.\n", coverFile, err) |
| 85 | + continue |
| 86 | + } |
| 87 | + |
| 88 | + scanner := bufio.NewScanner(f) |
| 89 | + for scanner.Scan() { |
| 90 | + line := scanner.Text() |
| 91 | + if strings.HasPrefix(line, "mode: ") { |
| 92 | + if firstFile { |
| 93 | + if _, err := out.WriteString(line + "\n"); err != nil { |
| 94 | + log.Fatalf("Failed to write mode line to merged coverage file: %v", err) |
| 95 | + } |
| 96 | + firstFile = false |
| 97 | + } |
| 98 | + } else { |
| 99 | + if _, err := out.WriteString(line + "\n"); err != nil { |
| 100 | + log.Fatalf("Failed to write to merged coverage file: %v", err) |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + if err := scanner.Err(); err != nil { |
| 105 | + log.Printf("Warning: Error reading coverage file %s: %v\n", coverFile, err) |
| 106 | + } |
| 107 | + f.Close() |
| 108 | + |
| 109 | + // Phase 3: Clean up individual cover.out files |
| 110 | + fmt.Printf(" Deleting %s\n", coverFile) |
| 111 | + if err := os.Remove(coverFile); err != nil { |
| 112 | + log.Printf("Warning: Failed to delete coverage file %s: %v\n", coverFile, err) |
| 113 | + } |
| 114 | + } |
| 115 | + fmt.Printf("--> Merged coverage data written to %s\n", mergedCoverageFile) |
| 116 | + } else { |
| 117 | + fmt.Println("--> No coverage files found to merge. This could be because no tests were found or all tests failed before producing coverage.") |
| 118 | + } |
| 119 | + |
| 120 | + fmt.Println("--> Finished processing coverage for all modules.") |
| 121 | + if testFailures { |
| 122 | + fmt.Println("Error: Some tests failed. Please check the logs above.") |
| 123 | + os.Exit(1) |
| 124 | + } |
| 125 | +} |
0 commit comments