-
Notifications
You must be signed in to change notification settings - Fork 158
/
project_test.go
151 lines (140 loc) · 3.51 KB
/
project_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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package actionlint
import (
"os"
"path/filepath"
"strings"
"testing"
)
// Create `.git` directory since actionlint finds the directory to detect the repository root.
// Without creating this directory, this test case will fail when `actionlint/.git` directory
// doesn't exist. When cloning actionlint repository with Git, it never happens. However, when
// downloading sources tarball from github.com, it doesn't contain `.git` directory so it
// happens. Please see #307 for more details.
func testEnsureDotGitDir(dir string) {
d := filepath.Join(dir, ".git")
if err := os.MkdirAll(d, 0750); err != nil {
panic(err)
}
}
func TestProjectsFindProjectFromPath(t *testing.T) {
d := filepath.Join("testdata", "find_project")
abs, err := filepath.Abs(d)
if err != nil {
panic(err)
}
testEnsureDotGitDir(d)
ps := NewProjects()
for _, tc := range []struct {
what string
path string
}{
{
what: "project root",
path: d,
},
{
what: "workflows directory",
path: filepath.Join(d, ".github", "workflows"),
},
{
what: "workflow file",
path: filepath.Join(d, ".github", "workflows", "test.yaml"),
},
{
what: "outside workflows directory",
path: filepath.Join(d, ".github", "reusable", "broken.yaml"),
},
{
what: "directory outside .github",
path: filepath.Join(d, "foo"),
},
{
what: "file outside .github",
path: filepath.Join(d, "foo", "test.txt"),
},
} {
t.Run(tc.what, func(t *testing.T) {
p, err := ps.At(tc.path)
if err != nil {
t.Fatal(err)
}
r := p.RootDir()
if r != abs {
t.Fatalf("root directory of project %v should be %q but got %q", p, abs, r)
}
// Result should be cached
p2, err := ps.At(tc.path)
if err != nil {
t.Fatal(err)
}
if p != p2 {
t.Fatalf("project %v is not cached. New project is %v. %p v.s. %p", p, p2, p, p2)
}
})
}
}
func TestProjectsDoesNotFindProjectFromOutside(t *testing.T) {
d := filepath.Join("testdata", "find_project")
abs, err := filepath.Abs(d)
if err != nil {
panic(err)
}
testEnsureDotGitDir(d)
outside := filepath.Join(d, "..")
ps := NewProjects()
p, err := ps.At(outside)
if err != nil {
t.Fatal(err)
}
if p != nil && p.RootDir() == abs {
t.Fatalf("project %v is detected from outside of the project %q", p, outside)
}
}
func TestProjectsLoadProjectConfig(t *testing.T) {
for _, n := range []string{"ok", "yml"} {
d := filepath.Join("testdata", "config", "projects", n)
testEnsureDotGitDir(d)
ps := NewProjects()
p, err := ps.At(d)
if err != nil {
t.Fatal(err)
}
if p == nil {
t.Fatal("project was not found at", d)
}
if c := p.Config(); c == nil {
t.Fatal("config was not found for directory", d)
}
}
}
func TestProjectsLoadingNoProjectConfig(t *testing.T) {
d := filepath.Join("testdata", "config", "projects", "none")
testEnsureDotGitDir(d)
ps := NewProjects()
p, err := ps.At(d)
if err != nil {
t.Fatal(err)
}
if p == nil {
t.Fatal("project was not found at", d)
}
if c := p.Config(); c != nil {
t.Fatal("config was found for directory", d)
}
}
func TestProjectsLoadingBrokenProjectConfig(t *testing.T) {
want := "could not parse config file"
d := filepath.Join("testdata", "config", "projects", "err")
testEnsureDotGitDir(d)
ps := NewProjects()
p, err := ps.At(d)
if err == nil {
t.Fatalf("wanted error %q but have no error", want)
}
if p != nil {
t.Fatal("project was returned though getting config failed", p)
}
if msg := err.Error(); !strings.Contains(msg, want) {
t.Fatalf("wanted error %q but have error %q", want, msg)
}
}