forked from cli/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub_template_test.go
More file actions
253 lines (242 loc) · 4.68 KB
/
github_template_test.go
File metadata and controls
253 lines (242 loc) · 4.68 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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package githubtemplate
import (
"io/ioutil"
"os"
"path"
"reflect"
"testing"
)
func TestFind(t *testing.T) {
tmpdir, err := ioutil.TempDir("", "gh-cli")
if err != nil {
t.Fatal(err)
}
type args struct {
rootDir string
name string
}
tests := []struct {
name string
prepare []string
args args
want []string
}{
{
name: "Template in root",
prepare: []string{
"README.md",
"ISSUE_TEMPLATE",
"issue_template.md",
"issue_template.txt",
"pull_request_template.md",
},
args: args{
rootDir: tmpdir,
name: "ISSUE_TEMPLATE",
},
want: []string{
path.Join(tmpdir, "issue_template.md"),
},
},
{
name: "Template in .github takes precedence",
prepare: []string{
"ISSUE_TEMPLATE.md",
".github/issue_template.md",
},
args: args{
rootDir: tmpdir,
name: "ISSUE_TEMPLATE",
},
want: []string{
path.Join(tmpdir, ".github/issue_template.md"),
},
},
{
name: "Template in docs",
prepare: []string{
"README.md",
"docs/issue_template.md",
},
args: args{
rootDir: tmpdir,
name: "ISSUE_TEMPLATE",
},
want: []string{
path.Join(tmpdir, "docs/issue_template.md"),
},
},
{
name: "Multiple templates",
prepare: []string{
".github/ISSUE_TEMPLATE/nope.md",
".github/PULL_REQUEST_TEMPLATE.md",
".github/PULL_REQUEST_TEMPLATE/one.md",
".github/PULL_REQUEST_TEMPLATE/two.md",
".github/PULL_REQUEST_TEMPLATE/three.md",
"docs/pull_request_template.md",
},
args: args{
rootDir: tmpdir,
name: "PuLl_ReQuEsT_TeMpLaTe",
},
want: []string{
path.Join(tmpdir, ".github/PULL_REQUEST_TEMPLATE/one.md"),
path.Join(tmpdir, ".github/PULL_REQUEST_TEMPLATE/three.md"),
path.Join(tmpdir, ".github/PULL_REQUEST_TEMPLATE/two.md"),
},
},
{
name: "Empty multiple templates directory",
prepare: []string{
".github/issue_template.md",
".github/issue_template/.keep",
},
args: args{
rootDir: tmpdir,
name: "ISSUE_TEMPLATE",
},
want: []string{
path.Join(tmpdir, ".github/issue_template.md"),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
for _, p := range tt.prepare {
fp := path.Join(tmpdir, p)
os.MkdirAll(path.Dir(fp), 0700)
file, err := os.Create(fp)
if err != nil {
t.Fatal(err)
}
file.Close()
}
if got := Find(tt.args.rootDir, tt.args.name); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Find() = %v, want %v", got, tt.want)
}
})
os.RemoveAll(tmpdir)
}
}
func TestExtractName(t *testing.T) {
tmpfile, err := ioutil.TempFile("", "gh-cli")
if err != nil {
t.Fatal(err)
}
tmpfile.Close()
defer os.Remove(tmpfile.Name())
type args struct {
filePath string
}
tests := []struct {
name string
prepare string
args args
want string
}{
{
name: "Complete front-matter",
prepare: `---
name: Bug Report
about: This is how you report bugs
---
Template contents
---
More of template
`,
args: args{
filePath: tmpfile.Name(),
},
want: "Bug Report",
},
{
name: "Incomplete front-matter",
prepare: `---
about: This is how you report bugs
---
`,
args: args{
filePath: tmpfile.Name(),
},
want: path.Base(tmpfile.Name()),
},
{
name: "No front-matter",
prepare: `name: This is not yaml!`,
args: args{
filePath: tmpfile.Name(),
},
want: path.Base(tmpfile.Name()),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ioutil.WriteFile(tmpfile.Name(), []byte(tt.prepare), 0600)
if got := ExtractName(tt.args.filePath); got != tt.want {
t.Errorf("ExtractName() = %v, want %v", got, tt.want)
}
})
}
}
func TestExtractContents(t *testing.T) {
tmpfile, err := ioutil.TempFile("", "gh-cli")
if err != nil {
t.Fatal(err)
}
tmpfile.Close()
defer os.Remove(tmpfile.Name())
type args struct {
filePath string
}
tests := []struct {
name string
prepare string
args args
want string
}{
{
name: "Has front-matter",
prepare: `---
name: Bug Report
---
Template contents
---
More of template
`,
args: args{
filePath: tmpfile.Name(),
},
want: `Template contents
---
More of template
`,
},
{
name: "No front-matter",
prepare: `Template contents
---
More of template
---
Even more
`,
args: args{
filePath: tmpfile.Name(),
},
want: `Template contents
---
More of template
---
Even more
`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ioutil.WriteFile(tmpfile.Name(), []byte(tt.prepare), 0600)
if got := ExtractContents(tt.args.filePath); string(got) != tt.want {
t.Errorf("ExtractContents() = %v, want %v", string(got), tt.want)
}
})
}
}