-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule_test.go
80 lines (73 loc) · 1.53 KB
/
module_test.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
package main
import (
"go/build"
"os"
"path/filepath"
"testing"
)
func TestFindModuleInfo(t *testing.T) {
moduleInfo, err := findModuleInfo()
if err != nil {
t.Fatal(err)
}
got := moduleInfo.modulePath
want := "github.com/konradreiche/coverdiff"
if got != want {
t.Errorf("got %s, want: %s", got, want)
}
}
func TestFindModuleDir(t *testing.T) {
t.Run("success", func(t *testing.T) {
_, err := findModuleDir(".")
if err != nil {
t.Fatal(err)
}
wd := changeDir(t, "testdata")
_, err = findModuleDir(wd)
if err != nil {
t.Fatal(err)
}
})
t.Run("empty-dir-param", func(t *testing.T) {
_, err := findModuleDir("")
if err == nil {
t.Fatal("expected error but got nil")
}
got := err.Error()
want := "dir not set"
if got != want {
t.Errorf("got %s, want: %s", got, want)
}
})
t.Run("goroot-boundary", func(t *testing.T) {
wd := changeDir(t, "..")
build.Default.GOROOT = filepath.Join(wd, "..")
_, err := findModuleDir(wd)
if err == nil {
t.Fatal("expected error but got nil")
}
got := err.Error()
want := "no go.mod file found"
if got != want {
t.Errorf("got %s, want: %s", got, want)
}
})
}
func changeDir(tb testing.TB, path string) string {
tb.Helper()
// reset to current working directory afterwards to avoid breaking
// subsequent tests
cwd, err := os.Getwd()
if err != nil {
tb.Fatal(err)
}
tb.Cleanup(func() {
if err := os.Chdir(cwd); err != nil {
tb.Error(err)
}
})
if err := os.Chdir(path); err != nil {
tb.Fatal(err)
}
return filepath.Join(cwd, path)
}