From 22cfb5112444e4aa38d2ab53af3c62e5a2ac5f78 Mon Sep 17 00:00:00 2001 From: tsuyoshiwada Date: Sat, 17 Feb 2018 16:38:20 +0900 Subject: [PATCH] refactor: Change to return an error if corresponding commit is empty --- chglog.go | 9 ++++ chglog_test.go | 76 ++++++++++++++++++++++++++++++---- testdata/not_found.md | 0 testdata/type_scope_subject.md | 2 +- 4 files changed, 77 insertions(+), 10 deletions(-) create mode 100644 testdata/not_found.md diff --git a/chglog.go b/chglog.go index ccd6e4bc..a4a0a5a9 100644 --- a/chglog.go +++ b/chglog.go @@ -1,6 +1,7 @@ package chglog import ( + "fmt" "io" "os" "path/filepath" @@ -164,6 +165,10 @@ func (gen *Generator) readVersions(query string) ([]*Version, error) { }) } + if len(versions) == 0 { + return nil, fmt.Errorf("a commits corresponding to \"%s\" was not found", query) + } + return versions, nil } @@ -201,6 +206,10 @@ func (gen *Generator) workdir() (func() error, error) { } func (gen *Generator) render(w io.Writer, versions []*Version) error { + if _, err := os.Stat(gen.config.Template); err != nil { + return err + } + fmap := template.FuncMap{ "datetime": func(layout string, input time.Time) string { return input.Format(layout) diff --git a/chglog_test.go b/chglog_test.go index 6c4e2552..43821a72 100644 --- a/chglog_test.go +++ b/chglog_test.go @@ -11,20 +11,22 @@ import ( gitcmd "github.com/tsuyoshiwada/go-gitcmd" ) -const ( +var ( + cwd string testRepoRoot = ".tmp" ) func TestMain(m *testing.M) { + cwd, _ = os.Getwd() + cleanup() code := m.Run() cleanup() os.Exit(code) } func setup(dir string, setupRepo func(gitcmd.Client)) { - cwd, _ := os.Getwd() + testDir := filepath.Join(cwd, testRepoRoot, dir) - testDir := filepath.Join(testRepoRoot, dir) os.MkdirAll(testDir, os.ModePerm) os.Chdir(testDir) @@ -39,12 +41,64 @@ func setup(dir string, setupRepo func(gitcmd.Client)) { } func cleanup() { - os.RemoveAll(testRepoRoot) + os.Chdir(cwd) + os.RemoveAll(filepath.Join(cwd, testRepoRoot)) +} + +func TestGeneratorNotFoundCommits(t *testing.T) { + assert := assert.New(t) + testName := "not_found" + + setup(testName, func(git gitcmd.Client) { + }) + + gen := NewGenerator(&Config{ + Bin: "git", + WorkingDir: filepath.Join(testRepoRoot, testName), + Template: filepath.Join("testdata", testName+".md"), + Info: &Info{ + RepositoryURL: "https://github.com/git-chglog/git-chglog", + }, + Options: &Options{ + CommitFilters: map[string][]string{}, + CommitSortBy: "Scope", + CommitGroupBy: "Type", + CommitGroupSortBy: "Title", + CommitGroupTitleMaps: map[string]string{}, + HeaderPattern: "^(\\w*)(?:\\(([\\w\\$\\.\\-\\*\\s]*)\\))?\\:\\s(.*)$", + HeaderPatternMaps: []string{ + "Type", + "Scope", + "Subject", + }, + IssuePrefix: []string{ + "#", + "gh-", + }, + RefActions: []string{}, + MergePattern: "^Merge pull request #(\\d+) from (.*)$", + MergePatternMaps: []string{ + "Ref", + "Source", + }, + RevertPattern: "^Revert \"([\\s\\S]*)\"$", + RevertPatternMaps: []string{ + "Header", + }, + NoteKeywords: []string{ + "BREAKING CHANGE", + }, + }, + }) + + buf := &bytes.Buffer{} + err := gen.Generate(buf, "foo") + assert.Contains(err.Error(), "\"foo\" was not found") + assert.Equal("", buf.String()) } func TestGeneratorWithTypeScopeSubject(t *testing.T) { assert := assert.New(t) - testName := "type_scope_subject" setup(testName, func(git gitcmd.Client) { @@ -105,10 +159,9 @@ func TestGeneratorWithTypeScopeSubject(t *testing.T) { "Ref", "Source", }, - RevertPattern: "^Revert\\s\"([\\s\\S]*)\"\\s*This reverts commit (\\w*)\\.", + RevertPattern: "^Revert \"([\\s\\S]*)\"$", RevertPatternMaps: []string{ - "Subject", - "Hash", + "Header", }, NoteKeywords: []string{ "BREAKING CHANGE", @@ -117,8 +170,9 @@ func TestGeneratorWithTypeScopeSubject(t *testing.T) { }) buf := &bytes.Buffer{} - gen.Generate(buf, "") + err := gen.Generate(buf, "") + assert.Nil(err) assert.Equal(` ## 2.0.0-beta.0 (2018-01-03) @@ -144,6 +198,10 @@ Online breaking change message. * **parser:** New some super options #333 +### Reverts + +* feat(core): Add foo bar @mention and issue #987 + ### Pull Requests * Merge pull request #1000 from tsuyoshiwada/patch-1 diff --git a/testdata/not_found.md b/testdata/not_found.md new file mode 100644 index 00000000..e69de29b diff --git a/testdata/type_scope_subject.md b/testdata/type_scope_subject.md index 7407717a..918ea9d9 100644 --- a/testdata/type_scope_subject.md +++ b/testdata/type_scope_subject.md @@ -8,7 +8,7 @@ {{end}}{{if .RevertCommits}} ### Reverts {{range .RevertCommits}} -* {{.Header}}{{end}} +* {{.Revert.Header}}{{end}} {{end}}{{if .MergeCommits}} ### Pull Requests {{range .MergeCommits}}