-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRunTests.go
92 lines (78 loc) · 2.11 KB
/
RunTests.go
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
package main
import (
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
)
func main() {
if len(os.Args) > 2 {
fmt.Println("Script supports only one optional argument - path to the file you want to test.")
return
}
root := "."
directories := []string{"algorithms", "structures"}
if len(os.Args) == 2 {
// Run a specific test file if path is provided
testFilePath := os.Args[1]
runTestFile(testFilePath)
} else {
// Traverse directories and run tests
for _, dir := range directories {
fullPath := filepath.Join(root, dir)
err := filepath.Walk(fullPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
runTestsInDir(path)
}
return nil
})
if err != nil {
fmt.Printf("Error walking the path %q: %v\n", fullPath, err)
}
}
}
}
func runTestsInDir(dir string) {
cmd := exec.Command("go", "test", "-v")
cmd.Dir = dir
output, err := cmd.CombinedOutput()
if err != nil {
fmt.Printf("Error running tests in directory %s: %v\n", dir, err)
}
fmt.Printf("Output of tests in %s:\n%s\n", dir, string(output))
}
func runTestFile(testPath string) {
separator := string(filepath.Separator)
dirPath := testPath + separator
dirPathFull := "." + separator + dirPath
// Ensure the provided path is a directory
dir, err := os.Open(dirPath)
if err != nil {
log.Fatalf("Error opening directory: %v", err)
}
defer dir.Close()
entries, err := dir.Readdir(-1) // Read all directory entries
if err != nil {
log.Fatalf("Error reading directory: %v", err)
}
for _, e := range entries {
if strings.HasSuffix(e.Name(), "_test.go") && !e.IsDir() {
fileName := filepath.Join(testPath, e.Name())
// Run go test with the package path
cmd := exec.Command("go", "test", "-v", dirPathFull)
cmd.Dir = "." // Set the working directory to the module root
output, err := cmd.CombinedOutput()
if err != nil {
fmt.Printf("Error running test file %s: %v\n", fileName, err)
fmt.Printf("Output: %s\n", output)
continue
}
fmt.Printf("Output of test file %s:\n%s\n", fileName, string(output))
}
}
}