-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkspace_test.go
More file actions
69 lines (59 loc) · 1.39 KB
/
workspace_test.go
File metadata and controls
69 lines (59 loc) · 1.39 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
package workspace
import (
"encoding/json"
"fmt"
"os"
"os/exec"
"path/filepath"
"sort"
"testing"
)
type workFile struct {
Use []struct {
DiskPath string
} `json:"Use"`
}
func TestWorkspaceModules(t *testing.T) {
rootDir, err := os.Getwd()
if err != nil {
t.Fatalf("get working directory: %v", err)
}
work, err := loadWorkFile(rootDir)
if err != nil {
t.Fatalf("load go.work: %v", err)
}
moduleDirs := make([]string, 0, len(work.Use))
for _, use := range work.Use {
dir := filepath.Clean(use.DiskPath)
if dir == "." {
continue
}
moduleDirs = append(moduleDirs, dir)
}
sort.Strings(moduleDirs)
for _, moduleDir := range moduleDirs {
moduleDir := moduleDir
t.Run(moduleDir, func(t *testing.T) {
cmd := exec.Command("go", "test", "./...")
cmd.Dir = filepath.Join(rootDir, moduleDir)
cmd.Env = os.Environ()
output, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("go test ./... in %s failed: %v\n%s", moduleDir, err, output)
}
})
}
}
func loadWorkFile(rootDir string) (workFile, error) {
cmd := exec.Command("go", "work", "edit", "-json")
cmd.Dir = rootDir
output, err := cmd.CombinedOutput()
if err != nil {
return workFile{}, fmt.Errorf("go work edit -json: %w\n%s", err, output)
}
var work workFile
if err := json.Unmarshal(output, &work); err != nil {
return workFile{}, fmt.Errorf("decode go.work json: %w", err)
}
return work, nil
}