-
Notifications
You must be signed in to change notification settings - Fork 73
/
generator_test.go
149 lines (126 loc) · 4.25 KB
/
generator_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
package main
import (
"io/ioutil"
"os"
"path"
"runtime"
"strings"
"testing"
)
func createAndChdirTo(dir string, issue_and_pr string, contributing string) {
if err := os.MkdirAll(path.Join(dir, ".github"), os.ModeDir|os.ModePerm); err != nil {
panic("Failed to make directory for testing")
}
if err := os.MkdirAll(path.Join(dir, "repo"), os.ModeDir|os.ModePerm); err != nil {
panic("Failed to make repo directory for testing")
}
os.Chdir(dir)
f1, err := os.Create(".github/ISSUE_AND_PULL_REQUEST_TEMPLATE.md")
if err != nil {
panic("Failed to make template file for issue and pull request")
}
defer f1.Close()
f1.WriteString(issue_and_pr)
f2, err := os.Create(path.Join(".github", "CONTRIBUTING.md"))
if err != nil {
panic("Failed to make template file for contributing")
}
defer f2.Close()
f2.WriteString(contributing)
}
func chdirBackAndSweep(dir string) {
os.Chdir("..")
if err := os.RemoveAll(dir); err != nil {
panic(err)
}
}
func newGeneratorForTest(workdir string) *Generator {
r := NewRepositoryFromURL("https://github.com/rhysd/dot-github.git")
r.Path = path.Join(workdir, "repo")
return NewGenerator(path.Join(workdir, ".github"), r)
}
func TestDotGithubDir(t *testing.T) {
g := NewGenerator(TemplateDir(), NewRepositoryFromURL("https://github.com/rhysd/dot-github.git"))
if !strings.HasSuffix(g.dotGithubDir, "dot-github/.github") {
t.Fatalf("g.dotGithubDir must point to repository local .github directory: %v", g.dotGithubDir)
}
}
func TestInvalidRepositoryPath(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("because I don't know how to specify 'invalid' directory path on Windows")
}
r := NewRepositoryFromURL("https://github.com/rhysd/dot-github.git")
r.Path = "/"
defer func() {
if r := recover(); r == nil {
t.Errorf("Invalid repository path must cause panic")
}
}()
NewGenerator(TemplateDir(), r)
}
func TestGeneratingAllTemplates(t *testing.T) {
d := "test-generate-all-templates"
createAndChdirTo(d, "### This is test for {{.RepoUser}}/{{.RepoName}}\n{{if .IsPullRequest}}pull request!{{else if .IsIssue}}issue!{{else if .IsContributing}}contributing!{{end}}", "This is contributing guide for {{.RepoUser}}/{{.RepoName}}")
defer chdirBackAndSweep(d)
w, _ := os.Getwd()
g := newGeneratorForTest(w)
g.GenerateAllTemplates()
var (
content string
bytes []byte
err error
)
bytes, err = ioutil.ReadFile(path.Join(w, "repo", ".github", "ISSUE_TEMPLATE.md"))
if err != nil {
t.Fatalf("ISSUE_TEMPLATE.md was not created")
}
content = string(bytes[:])
if content != "### This is test for rhysd/dot-github\nissue!" {
t.Errorf("ISSUE_TEMPLATE.md is invalid: %v", content)
}
bytes, err = ioutil.ReadFile(path.Join(w, "repo", ".github", "PULL_REQUEST_TEMPLATE.md"))
if err != nil {
t.Fatalf("PULL_REQUEST_TEMPLATE.md was not created")
}
content = string(bytes[:])
if content != "### This is test for rhysd/dot-github\npull request!" {
t.Errorf("PULL_REQUEST_TEMPLATE.md is invalid: %v", content)
}
bytes, err = ioutil.ReadFile(path.Join(w, "repo", ".github", "CONTRIBUTING.md"))
if err != nil {
t.Fatalf("CONTRIBUTING.md was not created")
}
content = string(bytes[:])
if content != "This is contributing guide for rhysd/dot-github" {
t.Errorf("CONTRIBUTING.md is invalid: %v", content)
}
if !g.FileCreated {
t.Errorf("FileCreated flag was not set")
}
}
func TestIgnoreNotExsistingTemplates(t *testing.T) {
d := "test-not-existing"
createAndChdirTo(d, "### This is test for {{.RepoUser}}/{{.RepoName}}\n{{if .IsPullRequest}}pull request!{{else if .IsIssue}}issue!{{else if .IsContributing}}contributing!{{end}}", "This is contributing guide for {{.RepoUser}}/{{.RepoName}}")
defer chdirBackAndSweep(d)
w, _ := os.Getwd()
g := newGeneratorForTest(w)
g.templateDir = path.Join(w, "unknown", ".github")
// Template dir does not exist so it generates nothing
g.GenerateAllTemplates()
if g.FileCreated {
t.Errorf("File is generated although template dir does not exist")
}
}
func TestFailToParseTemplate(t *testing.T) {
d := "test-not-existing"
createAndChdirTo(d, "This causes error: {{", "")
defer chdirBackAndSweep(d)
defer func() {
if r := recover(); r == nil {
t.Errorf("Parse error did not occur")
}
}()
w, _ := os.Getwd()
g := newGeneratorForTest(w)
g.GenerateIssueTemplate()
}