88 "testing"
99)
1010
11- func TestFind (t * testing.T ) {
11+ func TestFindNonLegacy (t * testing.T ) {
1212 tmpdir , err := ioutil .TempDir ("" , "gh-cli" )
1313 if err != nil {
1414 t .Fatal (err )
@@ -25,52 +25,69 @@ func TestFind(t *testing.T) {
2525 want []string
2626 }{
2727 {
28- name : "Template in root " ,
28+ name : "Legacy templates ignored " ,
2929 prepare : []string {
3030 "README.md" ,
3131 "ISSUE_TEMPLATE" ,
3232 "issue_template.md" ,
3333 "issue_template.txt" ,
3434 "pull_request_template.md" ,
35+ ".github/issue_template.md" ,
36+ "docs/issue_template.md" ,
37+ },
38+ args : args {
39+ rootDir : tmpdir ,
40+ name : "ISSUE_TEMPLATE" ,
41+ },
42+ want : []string {},
43+ },
44+ {
45+ name : "Template folder in .github takes precedence" ,
46+ prepare : []string {
47+ "ISSUE_TEMPLATE.md" ,
48+ "docs/ISSUE_TEMPLATE/abc.md" ,
49+ "ISSUE_TEMPLATE/abc.md" ,
50+ ".github/ISSUE_TEMPLATE/abc.md" ,
3551 },
3652 args : args {
3753 rootDir : tmpdir ,
3854 name : "ISSUE_TEMPLATE" ,
3955 },
4056 want : []string {
41- path .Join (tmpdir , "issue_template .md" ),
57+ path .Join (tmpdir , ".github/ISSUE_TEMPLATE/abc .md" ),
4258 },
4359 },
4460 {
45- name : "Template in .github takes precedence " ,
61+ name : "Template folder in root " ,
4662 prepare : []string {
4763 "ISSUE_TEMPLATE.md" ,
48- ".github/issue_template.md" ,
64+ "docs/ISSUE_TEMPLATE/abc.md" ,
65+ "ISSUE_TEMPLATE/abc.md" ,
4966 },
5067 args : args {
5168 rootDir : tmpdir ,
5269 name : "ISSUE_TEMPLATE" ,
5370 },
5471 want : []string {
55- path .Join (tmpdir , ".github/issue_template .md" ),
72+ path .Join (tmpdir , "ISSUE_TEMPLATE/abc .md" ),
5673 },
5774 },
5875 {
59- name : "Template in docs" ,
76+ name : "Template folder in docs" ,
6077 prepare : []string {
61- "README .md" ,
62- "docs/issue_template .md" ,
78+ "ISSUE_TEMPLATE .md" ,
79+ "docs/ISSUE_TEMPLATE/abc .md" ,
6380 },
6481 args : args {
6582 rootDir : tmpdir ,
6683 name : "ISSUE_TEMPLATE" ,
6784 },
6885 want : []string {
69- path .Join (tmpdir , "docs/issue_template .md" ),
86+ path .Join (tmpdir , "docs/ISSUE_TEMPLATE/abc .md" ),
7087 },
7188 },
7289 {
73- name : "Multiple templates" ,
90+ name : "Multiple templates in template folder " ,
7491 prepare : []string {
7592 ".github/ISSUE_TEMPLATE/nope.md" ,
7693 ".github/PULL_REQUEST_TEMPLATE.md" ,
@@ -90,18 +107,109 @@ func TestFind(t *testing.T) {
90107 },
91108 },
92109 {
93- name : "Empty multiple templates directory " ,
110+ name : "Empty template directories " ,
94111 prepare : []string {
112+ ".github/ISSUE_TEMPLATE/.keep" ,
113+ ".docs/ISSUE_TEMPLATE/.keep" ,
114+ "ISSUE_TEMPLATE/.keep" ,
115+ },
116+ args : args {
117+ rootDir : tmpdir ,
118+ name : "ISSUE_TEMPLATE" ,
119+ },
120+ want : []string {},
121+ },
122+ }
123+ for _ , tt := range tests {
124+ t .Run (tt .name , func (t * testing.T ) {
125+ for _ , p := range tt .prepare {
126+ fp := path .Join (tmpdir , p )
127+ _ = os .MkdirAll (path .Dir (fp ), 0700 )
128+ file , err := os .Create (fp )
129+ if err != nil {
130+ t .Fatal (err )
131+ }
132+ file .Close ()
133+ }
134+
135+ if got := FindNonLegacy (tt .args .rootDir , tt .args .name ); ! reflect .DeepEqual (got , tt .want ) {
136+ t .Errorf ("Find() = %v, want %v" , got , tt .want )
137+ }
138+ })
139+ os .RemoveAll (tmpdir )
140+ }
141+ }
142+
143+ func TestFindLegacy (t * testing.T ) {
144+ tmpdir , err := ioutil .TempDir ("" , "gh-cli" )
145+ if err != nil {
146+ t .Fatal (err )
147+ }
148+
149+ type args struct {
150+ rootDir string
151+ name string
152+ }
153+ tests := []struct {
154+ name string
155+ prepare []string
156+ args args
157+ want string
158+ }{
159+ {
160+ name : "Template in root" ,
161+ prepare : []string {
162+ "README.md" ,
163+ "ISSUE_TEMPLATE" ,
164+ "issue_template.md" ,
165+ "issue_template.txt" ,
166+ "pull_request_template.md" ,
167+ "docs/issue_template.md" ,
168+ },
169+ args : args {
170+ rootDir : tmpdir ,
171+ name : "ISSUE_TEMPLATE" ,
172+ },
173+ want : path .Join (tmpdir , "issue_template.md" ),
174+ },
175+ {
176+ name : "Template in .github takes precedence" ,
177+ prepare : []string {
178+ "ISSUE_TEMPLATE.md" ,
95179 ".github/issue_template.md" ,
96- ".github /issue_template/.keep " ,
180+ "docs /issue_template.md " ,
97181 },
98182 args : args {
99183 rootDir : tmpdir ,
100184 name : "ISSUE_TEMPLATE" ,
101185 },
102- want : []string {
103- path .Join (tmpdir , ".github/issue_template.md" ),
186+ want : path .Join (tmpdir , ".github/issue_template.md" ),
187+ },
188+ {
189+ name : "Template in docs" ,
190+ prepare : []string {
191+ "README.md" ,
192+ "docs/issue_template.md" ,
193+ },
194+ args : args {
195+ rootDir : tmpdir ,
196+ name : "ISSUE_TEMPLATE" ,
197+ },
198+ want : path .Join (tmpdir , "docs/issue_template.md" ),
199+ },
200+ {
201+ name : "Non legacy templates ignored" ,
202+ prepare : []string {
203+ ".github/PULL_REQUEST_TEMPLATE/abc.md" ,
204+ "PULL_REQUEST_TEMPLATE/abc.md" ,
205+ "docs/PULL_REQUEST_TEMPLATE/abc.md" ,
206+ ".github/PULL_REQUEST_TEMPLATE.md" ,
207+ },
208+ args : args {
209+ rootDir : tmpdir ,
210+ name : "PuLl_ReQuEsT_TeMpLaTe" ,
104211 },
212+ want : path .Join (tmpdir , ".github/PULL_REQUEST_TEMPLATE.md" ),
105213 },
106214 }
107215 for _ , tt := range tests {
@@ -116,7 +224,7 @@ func TestFind(t *testing.T) {
116224 file .Close ()
117225 }
118226
119- if got := Find (tt .args .rootDir , tt .args .name ); ! reflect . DeepEqual ( got , tt .want ) {
227+ if got := FindLegacy (tt .args .rootDir , tt .args .name ); * got != tt .want {
120228 t .Errorf ("Find() = %v, want %v" , got , tt .want )
121229 }
122230 })
0 commit comments