Skip to content

Commit 15f9baa

Browse files
tac0turtletac0turtlegraphite-app[bot]
authored
chore: run all tests in ci and coverage (#2302)
<!-- Please read and fill out this form before submitting your PR. Please make sure you have reviewed our contributors guide before submitting your first PR. NOTE: PR titles should follow semantic commits: https://www.conventionalcommits.org/en/v1.0.0/ --> ## Overview this pr creates a test-cover.go file allowing it to run in ci and produce a large coverage reports ref #2256 <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Added a streamlined command to run unit tests with coverage across multiple modules. - **Chores** - Updated test workflow to use the new coverage command. - Excluded specific mock directories from code coverage metrics. - Removed redundant test jobs from the workflow for improved efficiency. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: tac0turtle <you@example.com> Co-authored-by: graphite-app[bot] <96075541+graphite-app[bot]@users.noreply.github.com>
1 parent 9eb9e59 commit 15f9baa

4 files changed

Lines changed: 133 additions & 25 deletions

File tree

.github/workflows/test.yml

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -39,37 +39,13 @@ jobs:
3939
with:
4040
go-version-file: ./go.mod
4141
- name: Run unit test
42-
run: go test -race -covermode=atomic -coverprofile=coverage.txt ./...
42+
run: make test-cover
4343
- name: upload coverage report
4444
uses: codecov/codecov-action@v5.4.3
4545
with:
4646
token: ${{ secrets.CODECOV_TOKEN }}
4747
file: ./coverage.txt
4848

49-
test_single_sequencer:
50-
name: Run Single Sequencer Tests
51-
runs-on: ubuntu-latest
52-
steps:
53-
- uses: actions/checkout@v4
54-
- name: set up go
55-
uses: actions/setup-go@v5
56-
with:
57-
go-version-file: ./go.mod
58-
- name: Run unit test
59-
run: cd sequencers/single && go test ./...
60-
61-
test_da:
62-
name: Run DA Tests
63-
runs-on: ubuntu-latest
64-
steps:
65-
- uses: actions/checkout@v4
66-
- name: set up go
67-
uses: actions/setup-go@v5
68-
with:
69-
go-version-file: ./go.mod
70-
- name: Run unit test
71-
run: cd da && go test ./...
72-
7349
e2e-tests:
7450
name: Run E2E System Tests
7551
runs-on: ubuntu-latest

codecov.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ ignore:
1111
- "types/pb"
1212
- "scripts/"
1313
- "pkg/rpc/example/"
14+
- "da/internal/mocks"

scripts/test.mk

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,9 @@ cover:
2222
@go install github.com/ory/go-acc@latest
2323
@go-acc -o coverage.txt ./...
2424
.PHONY: cover
25+
26+
## test-cover: generate code coverage report.
27+
test-cover:
28+
@echo "--> Running unit tests"
29+
@go run -tags=cover scripts/test_cover.go
30+
.PHONY: test-cover

scripts/test_cover.go

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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

Comments
 (0)