提交 9144e1df 编写于 作者: G ganmacs 提交者: Jake Zimmerman

Add template tests

上级 ad6ac945
......@@ -278,7 +278,7 @@ func createPullRequestMessage(base, head, fullBase, fullHead string) (string, er
}
}
if tmplate := github.GetPullRequestTemplate(); tmplate != "" {
if template := github.GetPullRequestTemplate(); template != "" {
defaultMsg = github.GeneratePRTemplate(defaultMsg)
}
......
......@@ -59,29 +59,14 @@ func (r *TestRepo) AddRemote(name, url, pushURL string) {
}
}
func (r *TestRepo) AddGithubTemplatesDir() {
github_dir := filepath.Join(r.dir, "test.git", ".github")
pr_template_path := filepath.Join(github_dir, "PULL_REQUEST_TEMPLATE.md")
issue_template_path := filepath.Join(github_dir, "ISSUE_TEMPLATE")
// Make `.github` dir in root of test repo
err := os.MkdirAll(filepath.Dir(issue_template_path), 0771)
if err != nil {
panic(err)
}
// Switch to the root of the project dir
err = os.Chdir(filepath.Join(r.dir, "test.git"))
func (r *TestRepo) AddFile(filePath string, content string) {
path := filepath.Join(r.dir, filePath)
err := os.MkdirAll(filepath.Dir(path), 0771)
if err != nil {
panic(err)
}
content := `Description
-----------
[Enter your pull request description here]
`
ioutil.WriteFile(pr_template_path, []byte(content), os.ModePerm)
ioutil.WriteFile(issue_template_path, []byte(content), os.ModePerm)
ioutil.WriteFile(path, []byte(content), os.ModePerm)
}
func (r *TestRepo) clone(repo, dir string) error {
......
......@@ -10,13 +10,13 @@ import (
)
const (
pullRequestTempalte = "PULL_REQUEST_TEMPLATE"
pullRequestTemplate = "PULL_REQUEST_TEMPLATE"
issueTemplate = "ISSUE_TEMPLATE"
githubTemplateDir = ".github"
)
func GetPullRequestTemplate() string {
return getGithubTemplate(pullRequestTempalte)
return getGithubTemplate(pullRequestTemplate)
}
func GetIssueTemplate() string {
......
package github
import (
"path/filepath"
"testing"
"github.com/bmizerany/assert"
"github.com/github/hub/fixtures"
)
var prContent = `Description
-----------
[Enter your pull request description here]
`
var issueContent = `Description
-----------
[Enter your issue description here]
`
func TestGithubTemplate_withoutTemplate(t *testing.T) {
repo := fixtures.SetupTestRepo()
defer repo.TearDown()
assert.Equal(t, "", GetPullRequestTemplate())
assert.Equal(t, "", GetIssueTemplate())
}
func TestGithubTemplate_withInvalidTemplate(t *testing.T) {
repo := fixtures.SetupTestRepo()
defer repo.TearDown()
addGithubTemplates(repo, map[string]string{"dir": "invalidPath"})
assert.Equal(t, "", GetPullRequestTemplate())
assert.Equal(t, "", GetIssueTemplate())
}
func TestGithubTemplate_WithMarkdown(t *testing.T) {
repo := fixtures.SetupTestRepo()
defer repo.TearDown()
addGithubTemplates(repo,
map[string]string{
"prTempalte": pullRequestTemplate + ".md",
"issueTempalte": issueTemplate + ".md",
})
assert.Equal(t, prContent, GetPullRequestTemplate())
assert.Equal(t, issueContent, GetIssueTemplate())
}
func TestGithubTemplate_WithTemplateInHome(t *testing.T) {
repo := fixtures.SetupTestRepo()
defer repo.TearDown()
addGithubTemplates(repo, map[string]string{})
assert.Equal(t, prContent, GetPullRequestTemplate())
assert.Equal(t, issueContent, GetIssueTemplate())
}
func TestGithubTemplate_WithTemplateInGithubDir(t *testing.T) {
repo := fixtures.SetupTestRepo()
defer repo.TearDown()
addGithubTemplates(repo, map[string]string{"dir": githubTemplateDir})
assert.Equal(t, prContent, GetPullRequestTemplate())
assert.Equal(t, issueContent, GetIssueTemplate())
}
func TestGithubTemplate_WithTemplateInGithubDirAndMarkdown(t *testing.T) {
repo := fixtures.SetupTestRepo()
defer repo.TearDown()
addGithubTemplates(repo,
map[string]string{
"prTempalte": pullRequestTemplate + ".md",
"issueTempalte": issueTemplate + ".md",
"dir": githubTemplateDir,
})
assert.Equal(t, prContent, GetPullRequestTemplate())
assert.Equal(t, issueContent, GetIssueTemplate())
}
// When no default message is provided, two blank lines should be added
// (representing the pull request title), and the left should be template.
func TestGeneratePRTemplate_NoDefaultMessage(t *testing.T) {
repo := fixtures.SetupTestRepo()
defer repo.TearDown()
repo.AddGithubTemplatesDir()
addGithubTemplates(repo, map[string]string{})
defaultMessage := ""
expectedOutput := `
......@@ -32,7 +110,7 @@ func TestGeneratePRTemplate_SingleLineDefaultMessage(t *testing.T) {
repo := fixtures.SetupTestRepo()
defer repo.TearDown()
repo.AddGithubTemplatesDir()
addGithubTemplates(repo, map[string]string{})
defaultMessage := "Add Pull Request Templates to Hub"
expectedOutput := `Add Pull Request Templates to Hub
......@@ -54,7 +132,7 @@ func TestGeneratePRTemplate_MultiLineDefaultMessage(t *testing.T) {
repo := fixtures.SetupTestRepo()
defer repo.TearDown()
repo.AddGithubTemplatesDir()
addGithubTemplates(repo, map[string]string{})
defaultMessage := `Add Pull Request Templates to Hub
......@@ -70,3 +148,23 @@ Description
assert.Equal(t, expectedOutput, GeneratePRTemplate(defaultMessage))
}
func addGithubTemplates(r *fixtures.TestRepo, config map[string]string) {
repoDir := "test.git"
if dir := config["dir"]; dir != "" {
repoDir = filepath.Join(repoDir, dir)
}
prTemplatePath := filepath.Join(repoDir, pullRequestTemplate)
if prTmplPath := config["prTemplate"]; prTmplPath != "" {
prTemplatePath = prTmplPath
}
issueTemplatePath := filepath.Join(repoDir, issueTemplate)
if issueTmplPath := config["issueTemplate"]; issueTmplPath != "" {
issueTemplatePath = issueTmplPath
}
r.AddFile(prTemplatePath, prContent)
r.AddFile(issueTemplatePath, issueContent)
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册