-
Notifications
You must be signed in to change notification settings - Fork 280
Expand file tree
/
Copy pathtest.go
More file actions
59 lines (52 loc) · 1.76 KB
/
Copy pathtest.go
File metadata and controls
59 lines (52 loc) · 1.76 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
package main
import (
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
)
func main() {
rootDir := "." // Start from the current directory
var testFailures bool
err := filepath.WalkDir(rootDir, func(path string, d os.DirEntry, err error) error {
if err != nil {
// Prevent panic if a directory is not accessible
log.Printf("Warning: Error accessing path %q: %v\n", path, err)
return err
}
// Check if the entry is a go.mod file
if !d.IsDir() && d.Name() == "go.mod" {
modDir := filepath.Dir(path)
fmt.Printf("--> Found go.mod in: %s\n", modDir)
// Skip the root go.mod if it exists and we only want submodules,
// or adjust logic if root tests are also desired.
// For this example, we'll run tests in all directories with go.mod.
fmt.Printf("--> Running tests in: %s\n", modDir)
cmd := exec.Command("go", "test", "./...", "-cover") //nolint:noctx // intentionally runs module-local test command
cmd.Dir = modDir // Set the working directory for the command
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
// Log the error but continue checking other modules
log.Printf("Error running tests in %s: %v\n", modDir, err)
testFailures = true
// Optionally, return the error here if you want to stop the walk on the first failure
// return fmt.Errorf("tests failed in %s: %w", modDir, err)
} else {
fmt.Printf("--> Tests finished successfully in: %s\n", modDir)
}
fmt.Println(strings.Repeat("-", 40)) // Separator
}
return nil // Continue walking
})
if err != nil {
log.Fatalf("Error walking the path %q: %v\n", rootDir, err)
}
fmt.Println("--> Finished running tests for all modules.")
if testFailures {
os.Exit(1)
}
}