diff --git a/commands/pull_request.go b/commands/pull_request.go index 6f4d147bf8840763145cac5d1881e80f38b42848..ce70e6d94983872429258c6972cf629b23f6dbc9 100644 --- a/commands/pull_request.go +++ b/commands/pull_request.go @@ -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) } diff --git a/fixtures/test_repo.go b/fixtures/test_repo.go index 16cb0df242dd36ce86d76b59f267c61f74624afb..66cccf46f86aef497f37a76a4d51d04f418e03b7 100644 --- a/fixtures/test_repo.go +++ b/fixtures/test_repo.go @@ -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 { diff --git a/github/template.go b/github/template.go index 184cee7c4e646fa1918d2b1d3d6b0372a6936146..8f051159932ea1d76f8818988d2b1752b5af1c29 100644 --- a/github/template.go +++ b/github/template.go @@ -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 { diff --git a/github/template_test.go b/github/template_test.go index 42897d78d36440821c5a90a06882bf1dadc08660..65086f685cbac3e509ca6cbbefd974af67b12b8e 100644 --- a/github/template_test.go +++ b/github/template_test.go @@ -1,19 +1,97 @@ 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) +}