github.com/darelread/dasdsadasddas@v1.3.0/generator_test.go (about) 1 package main 2 3 import ( 4 "io/ioutil" 5 "os" 6 "path" 7 "runtime" 8 "strings" 9 "testing" 10 ) 11 12 func createAndChdirTo(dir string, issue_and_pr string, contributing string) { 13 if err := os.MkdirAll(path.Join(dir, ".github"), os.ModeDir|os.ModePerm); err != nil { 14 panic("Failed to make directory for testing") 15 } 16 if err := os.MkdirAll(path.Join(dir, "repo"), os.ModeDir|os.ModePerm); err != nil { 17 panic("Failed to make repo directory for testing") 18 } 19 os.Chdir(dir) 20 21 f1, err := os.Create(".github/ISSUE_AND_PULL_REQUEST_TEMPLATE.md") 22 if err != nil { 23 panic("Failed to make template file for issue and pull request") 24 } 25 defer f1.Close() 26 f1.WriteString(issue_and_pr) 27 28 f2, err := os.Create(path.Join(".github", "CONTRIBUTING.md")) 29 if err != nil { 30 panic("Failed to make template file for contributing") 31 } 32 defer f2.Close() 33 f2.WriteString(contributing) 34 } 35 36 func chdirBackAndSweep(dir string) { 37 os.Chdir("..") 38 if err := os.RemoveAll(dir); err != nil { 39 panic(err) 40 } 41 } 42 43 func newGeneratorForTest(workdir string) *Generator { 44 r := NewRepositoryFromURL("https://github.com/rhysd/dot-github.git") 45 r.Path = path.Join(workdir, "repo") 46 return NewGenerator(path.Join(workdir, ".github"), r) 47 } 48 49 func TestDotGithubDir(t *testing.T) { 50 g := NewGenerator(TemplateDir(), NewRepositoryFromURL("https://github.com/rhysd/dot-github.git")) 51 if !strings.HasSuffix(g.dotGithubDir, "dot-github/.github") { 52 t.Fatalf("g.dotGithubDir must point to repository local .github directory: %v", g.dotGithubDir) 53 } 54 } 55 56 func TestInvalidRepositoryPath(t *testing.T) { 57 if runtime.GOOS == "windows" { 58 t.Skip("because I don't know how to specify 'invalid' directory path on Windows") 59 } 60 r := NewRepositoryFromURL("https://github.com/rhysd/dot-github.git") 61 r.Path = "/" 62 defer func() { 63 if r := recover(); r == nil { 64 t.Errorf("Invalid repository path must cause panic") 65 } 66 }() 67 NewGenerator(TemplateDir(), r) 68 } 69 70 func TestGeneratingAllTemplates(t *testing.T) { 71 d := "test-generate-all-templates" 72 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}}") 73 defer chdirBackAndSweep(d) 74 75 w, _ := os.Getwd() 76 g := newGeneratorForTest(w) 77 g.GenerateAllTemplates() 78 79 var ( 80 content string 81 bytes []byte 82 err error 83 ) 84 85 bytes, err = ioutil.ReadFile(path.Join(w, "repo", ".github", "ISSUE_TEMPLATE.md")) 86 if err != nil { 87 t.Fatalf("ISSUE_TEMPLATE.md was not created") 88 } 89 content = string(bytes[:]) 90 if content != "### This is test for rhysd/dot-github\nissue!" { 91 t.Errorf("ISSUE_TEMPLATE.md is invalid: %v", content) 92 } 93 94 bytes, err = ioutil.ReadFile(path.Join(w, "repo", ".github", "PULL_REQUEST_TEMPLATE.md")) 95 if err != nil { 96 t.Fatalf("PULL_REQUEST_TEMPLATE.md was not created") 97 } 98 content = string(bytes[:]) 99 if content != "### This is test for rhysd/dot-github\npull request!" { 100 t.Errorf("PULL_REQUEST_TEMPLATE.md is invalid: %v", content) 101 } 102 103 bytes, err = ioutil.ReadFile(path.Join(w, "repo", ".github", "CONTRIBUTING.md")) 104 if err != nil { 105 t.Fatalf("CONTRIBUTING.md was not created") 106 } 107 content = string(bytes[:]) 108 if content != "This is contributing guide for rhysd/dot-github" { 109 t.Errorf("CONTRIBUTING.md is invalid: %v", content) 110 } 111 112 if !g.FileCreated { 113 t.Errorf("FileCreated flag was not set") 114 } 115 } 116 117 func TestIgnoreNotExsistingTemplates(t *testing.T) { 118 d := "test-not-existing" 119 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}}") 120 defer chdirBackAndSweep(d) 121 122 w, _ := os.Getwd() 123 g := newGeneratorForTest(w) 124 125 g.templateDir = path.Join(w, "unknown", ".github") 126 127 // Template dir does not exist so it generates nothing 128 g.GenerateAllTemplates() 129 130 if g.FileCreated { 131 t.Errorf("File is generated although template dir does not exist") 132 } 133 } 134 135 func TestFailToParseTemplate(t *testing.T) { 136 d := "test-not-existing" 137 createAndChdirTo(d, "This causes error: {{", "") 138 defer chdirBackAndSweep(d) 139 140 defer func() { 141 if r := recover(); r == nil { 142 t.Errorf("Parse error did not occur") 143 } 144 }() 145 146 w, _ := os.Getwd() 147 g := newGeneratorForTest(w) 148 g.GenerateIssueTemplate() 149 }