@@ -8,40 +8,47 @@ import (
88 "github.com/stretchr/testify/assert"
99)
1010
11+ // writeTemplate is a test helper that creates a file with the given content,
12+ // creating parent directories as needed. It calls t.Fatal on any error so
13+ // that setup failures are clearly distinguished from feature failures.
14+ func writeTemplate (t * testing.T , path string , content []byte ) {
15+ t .Helper ()
16+ if err := os .MkdirAll (filepath .Dir (path ), 0o755 ); err != nil {
17+ t .Fatalf ("test setup: MkdirAll: %v" , err )
18+ }
19+ if err := os .WriteFile (path , content , 0o644 ); err != nil {
20+ t .Fatalf ("test setup: WriteFile: %v" , err )
21+ }
22+ }
23+
1124func TestFindTemplate_GitHubDir (t * testing.T ) {
1225 root := t .TempDir ()
13- dir := filepath .Join (root , ".github" )
14- os .MkdirAll (dir , 0o755 )
15- os .WriteFile (filepath .Join (dir , "pull_request_template.md" ), []byte ("## Description\n \n Fill in details." ), 0o644 )
26+ writeTemplate (t , filepath .Join (root , ".github" , "pull_request_template.md" ), []byte ("## Description\n \n Fill in details." ))
1627
1728 got := FindTemplate (root )
1829 assert .Equal (t , "## Description\n \n Fill in details." , got )
1930}
2031
2132func TestFindTemplate_RootDir (t * testing.T ) {
2233 root := t .TempDir ()
23- os . WriteFile ( filepath .Join (root , "pull_request_template.md" ), []byte ("Root template" ), 0o644 )
34+ writeTemplate ( t , filepath .Join (root , "pull_request_template.md" ), []byte ("Root template" ))
2435
2536 got := FindTemplate (root )
2637 assert .Equal (t , "Root template" , got )
2738}
2839
2940func TestFindTemplate_DocsDir (t * testing.T ) {
3041 root := t .TempDir ()
31- dir := filepath .Join (root , "docs" )
32- os .MkdirAll (dir , 0o755 )
33- os .WriteFile (filepath .Join (dir , "PULL_REQUEST_TEMPLATE.md" ), []byte ("Docs template" ), 0o644 )
42+ writeTemplate (t , filepath .Join (root , "docs" , "PULL_REQUEST_TEMPLATE.md" ), []byte ("Docs template" ))
3443
3544 got := FindTemplate (root )
3645 assert .Equal (t , "Docs template" , got )
3746}
3847
3948func TestFindTemplate_PriorityOrder (t * testing.T ) {
4049 root := t .TempDir ()
41- ghDir := filepath .Join (root , ".github" )
42- os .MkdirAll (ghDir , 0o755 )
43- os .WriteFile (filepath .Join (ghDir , "pull_request_template.md" ), []byte ("github template" ), 0o644 )
44- os .WriteFile (filepath .Join (root , "pull_request_template.md" ), []byte ("root template" ), 0o644 )
50+ writeTemplate (t , filepath .Join (root , ".github" , "pull_request_template.md" ), []byte ("github template" ))
51+ writeTemplate (t , filepath .Join (root , "pull_request_template.md" ), []byte ("root template" ))
4552
4653 got := FindTemplate (root )
4754 assert .Equal (t , "github template" , got )
@@ -56,15 +63,15 @@ func TestFindTemplate_NoTemplate(t *testing.T) {
5663
5764func TestFindTemplate_EmptyFile (t * testing.T ) {
5865 root := t .TempDir ()
59- os . WriteFile ( filepath .Join (root , "pull_request_template.md" ), []byte (" \n " ), 0o644 )
66+ writeTemplate ( t , filepath .Join (root , "pull_request_template.md" ), []byte (" \n " ))
6067
6168 got := FindTemplate (root )
6269 assert .Equal (t , "" , got , "empty/whitespace-only template should be treated as no template" )
6370}
6471
6572func TestFindTemplate_UpperCase (t * testing.T ) {
6673 root := t .TempDir ()
67- os . WriteFile ( filepath .Join (root , "PULL_REQUEST_TEMPLATE.md" ), []byte ("UPPER template" ), 0o644 )
74+ writeTemplate ( t , filepath .Join (root , "PULL_REQUEST_TEMPLATE.md" ), []byte ("UPPER template" ))
6875
6976 got := FindTemplate (root )
7077 assert .Equal (t , "UPPER template" , got )
0 commit comments