Add GeneratePRTemplate method to github/template

The function of this method is to defer the logic of generating a commit
message to template.go, and eventually remove that logic from
commands/pull_request.go

Also adds tests for this new method, which also modifies the
fixtures/test_repo.go file to accomidate a test repo with a `.github` PR
dir.
This commit is contained in:
Nick LaMuro 2016-04-11 11:10:18 -05:00 коммит произвёл Jake Zimmerman
Родитель 085821f05e
Коммит a1e49b89a9
3 изменённых файлов: 57 добавлений и 1 удалений

Просмотреть файл

@ -59,6 +59,31 @@ 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"))
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)
}
func (r *TestRepo) clone(repo, dir string) error {
cmd := cmd.New("git").WithArgs("clone", repo, dir)
output, err := cmd.CombinedOutput()

Просмотреть файл

@ -20,7 +20,11 @@ func GetPullRequestTemplate() string {
}
func GetIssueTemplate() string {
return getGithubTemplate(issueTempalte)
return getGithubTemplate(issueTemplate)
}
func GeneratePRTemplate(defaultMsg string) string {
return "\n\n" + GetPullRequestTemplate()
}
func getGithubTemplate(pat string) (body string) {

27
github/template_test.go Normal file
Просмотреть файл

@ -0,0 +1,27 @@
package github
import (
"testing"
"github.com/bmizerany/assert"
"github.com/github/hub/fixtures"
)
// 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()
defaultMessage := ""
expectedOutput := `
Description
-----------
[Enter your pull request description here]
`
assert.Equal(t, expectedOutput, GeneratePRTemplate(defaultMessage))
}