git-chglog/commit_extractor_test.go
Derek Smith ae3382b7c8
chore(ci): add golangci-lint action and apply linting changes (#120)
BREAKING CHANGE: `JiraIssueId` has been renamed to `JiraIssueID`. This impacts the value for `pattern_maps` in `config.yml`.

* chore(ci): add golangci-lint action

* chore(lint): address errcheck lint failures

* chore(lint): address misspell lint failures

* chore(lint): address gocritic lint failures

* chore(lint): address golint lint failures

* chore(lint): address structcheck lint failures

* chore(lint): address gosimple lint failures

* chore(lint): address gofmt lint failures

* chore(ci): port to official golangci-lint github action

* Update golangci configuration for better coverage

Signed-off-by: Khosrow Moossavi <khos2ow@gmail.com>

* fix: file is not goimports-ed

Signed-off-by: Khosrow Moossavi <khos2ow@gmail.com>

* fix: golint and exported functions comments

Signed-off-by: Khosrow Moossavi <khos2ow@gmail.com>

* chore(lint): address gosec G304 warning

* chore(lint): address uparam warnings

* chore(lint): address scopelint lint failures

* fix: cyclomatic complexity

Signed-off-by: Khosrow Moossavi <khos2ow@gmail.com>

* chore(lint): address prealloc warning, noting that we are warning for now

* chore(lint): address govet and errorlint failures

* chore: clean up defer logic when checking errors

Co-authored-by: Khosrow Moossavi <khos2ow@gmail.com>
2021-03-16 21:24:36 -05:00

268 lines
4.1 KiB
Go

package chglog
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestCommitExtractor(t *testing.T) {
assert := assert.New(t)
extractor := newCommitExtractor(&Options{
CommitSortBy: "Scope",
CommitGroupBy: "Type",
CommitGroupSortBy: "Title",
CommitGroupTitleMaps: map[string]string{
"bar": "BAR",
},
})
fixtures := []*Commit{
// [0]
{
Type: "foo",
Scope: "c",
Header: "1",
Notes: []*Note{},
},
// [1]
{
Type: "foo",
Scope: "b",
Header: "2",
Notes: []*Note{
{"note1-title", "note1-body"},
{"note2-title", "note2-body"},
},
},
// [2]
{
Type: "bar",
Scope: "d",
Header: "3",
Notes: []*Note{
{"note1-title", "note1-body"},
{"note3-title", "note3-body"},
},
},
// [3]
{
Type: "foo",
Scope: "a",
Header: "4",
Notes: []*Note{
{"note4-title", "note4-body"},
},
},
// [4]
{
Type: "",
Scope: "",
Header: "Merge1",
Notes: []*Note{},
Merge: &Merge{
Ref: "123",
Source: "merges/merge1",
},
},
// [5]
{
Type: "",
Scope: "",
Header: "Revert1",
Notes: []*Note{},
Revert: &Revert{
Header: "REVERT1",
},
},
}
commitGroups, mergeCommits, revertCommits, noteGroups := extractor.Extract(fixtures)
assert.Equal([]*CommitGroup{
{
RawTitle: "bar",
Title: "BAR",
Commits: []*Commit{
fixtures[2],
},
},
{
RawTitle: "foo",
Title: "Foo",
Commits: []*Commit{
fixtures[3],
fixtures[1],
fixtures[0],
},
},
}, commitGroups)
assert.Equal([]*Commit{
fixtures[4],
}, mergeCommits)
assert.Equal([]*Commit{
fixtures[5],
}, revertCommits)
assert.Equal([]*NoteGroup{
{
Title: "note1-title",
Notes: []*Note{
fixtures[1].Notes[0],
fixtures[2].Notes[0],
},
},
{
Title: "note2-title",
Notes: []*Note{
fixtures[1].Notes[1],
},
},
{
Title: "note3-title",
Notes: []*Note{
fixtures[2].Notes[1],
},
},
{
Title: "note4-title",
Notes: []*Note{
fixtures[3].Notes[0],
},
},
}, noteGroups)
}
func TestCommitOrderExtractor(t *testing.T) {
assert := assert.New(t)
extractor := newCommitExtractor(&Options{
CommitSortBy: "Scope",
CommitGroupBy: "Type",
CommitGroupSortBy: "Custom",
CommitGroupTitleOrder: []string{"foo", "bar"},
CommitGroupTitleMaps: map[string]string{
"bar": "BAR",
},
})
fixtures := []*Commit{
// [0]
{
Type: "foo",
Scope: "c",
Header: "1",
Notes: []*Note{},
},
// [1]
{
Type: "foo",
Scope: "b",
Header: "2",
Notes: []*Note{
{"note1-title", "note1-body"},
{"note2-title", "note2-body"},
},
},
// [2]
{
Type: "bar",
Scope: "d",
Header: "3",
Notes: []*Note{
{"note1-title", "note1-body"},
{"note3-title", "note3-body"},
},
},
// [3]
{
Type: "foo",
Scope: "a",
Header: "4",
Notes: []*Note{
{"note4-title", "note4-body"},
},
},
// [4]
{
Type: "",
Scope: "",
Header: "Merge1",
Notes: []*Note{},
Merge: &Merge{
Ref: "123",
Source: "merges/merge1",
},
},
// [5]
{
Type: "",
Scope: "",
Header: "Revert1",
Notes: []*Note{},
Revert: &Revert{
Header: "REVERT1",
},
},
}
commitGroups, mergeCommits, revertCommits, noteGroups := extractor.Extract(fixtures)
assert.Equal([]*CommitGroup{
{
RawTitle: "foo",
Title: "Foo",
Commits: []*Commit{
fixtures[3],
fixtures[1],
fixtures[0],
},
},
{
RawTitle: "bar",
Title: "BAR",
Commits: []*Commit{
fixtures[2],
},
},
}, commitGroups)
assert.Equal([]*Commit{
fixtures[4],
}, mergeCommits)
assert.Equal([]*Commit{
fixtures[5],
}, revertCommits)
assert.Equal([]*NoteGroup{
{
Title: "note1-title",
Notes: []*Note{
fixtures[1].Notes[0],
fixtures[2].Notes[0],
},
},
{
Title: "note2-title",
Notes: []*Note{
fixtures[1].Notes[1],
},
},
{
Title: "note3-title",
Notes: []*Note{
fixtures[2].Notes[1],
},
},
{
Title: "note4-title",
Notes: []*Note{
fixtures[3].Notes[0],
},
},
}, noteGroups)
}