-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
342 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
name: Test Action | ||
|
||
on: push | ||
|
||
jobs: | ||
test: | ||
name: Test | ||
runs-on: ${{ matrix.os }} | ||
strategy: | ||
matrix: | ||
os: [ubuntu-latest, macos-latest, windows-latest] | ||
steps: | ||
- uses: actions/checkout@v4 | ||
with: | ||
fetch-depth: 0 | ||
- uses: ./ | ||
id: changelog | ||
with: | ||
path: test | ||
install-only: "true" | ||
- name: Binary exists | ||
shell: bash | ||
run: command -v changelog-generator | ||
- name: Binary runs | ||
shell: bash | ||
run: changelog-generator --help |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ repos: | |
- id: go-vet-mod | ||
- id: golangci-lint-mod | ||
args: [--fix] | ||
- id: go-test-mod | ||
|
||
- repo: local | ||
hooks: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package config | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestNewDefault(t *testing.T) { | ||
assert.NotNil(t, NewDefault()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package config | ||
|
||
import ( | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/go-git/go-git/v5/plumbing/object" | ||
) | ||
|
||
func TestFilters_Match(t *testing.T) { | ||
type fields struct { | ||
Exclude []string | ||
excludeRe []*regexp.Regexp | ||
Include []string | ||
includeRe []*regexp.Regexp | ||
} | ||
type args struct { | ||
c *object.Commit | ||
} | ||
tests := []struct { | ||
name string | ||
fields fields | ||
args args | ||
want bool | ||
}{ | ||
{"no filters", fields{}, args{&object.Commit{Message: "test"}}, true}, | ||
{ | ||
"include filter match", | ||
fields{includeRe: []*regexp.Regexp{regexp.MustCompile("test")}}, | ||
args{&object.Commit{Message: "test"}}, | ||
true, | ||
}, | ||
{ | ||
"include filter no match", | ||
fields{includeRe: []*regexp.Regexp{regexp.MustCompile("example")}}, | ||
args{&object.Commit{Message: "test"}}, | ||
false, | ||
}, | ||
{ | ||
"exclude filter match", | ||
fields{excludeRe: []*regexp.Regexp{regexp.MustCompile("test")}}, | ||
args{&object.Commit{Message: "test"}}, | ||
false, | ||
}, | ||
{ | ||
"exclude filter no match", | ||
fields{excludeRe: []*regexp.Regexp{regexp.MustCompile("example")}}, | ||
args{&object.Commit{Message: "test"}}, | ||
true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
f := &Filters{ | ||
Exclude: tt.fields.Exclude, | ||
excludeRe: tt.fields.excludeRe, | ||
Include: tt.fields.Include, | ||
includeRe: tt.fields.includeRe, | ||
} | ||
if got := f.Match(tt.args.c); got != tt.want { | ||
t.Errorf("Match() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package config | ||
|
||
import ( | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/go-git/go-git/v5/plumbing" | ||
"github.com/go-git/go-git/v5/plumbing/object" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGroup_Matches(t *testing.T) { | ||
type fields struct { | ||
Title string | ||
Order int | ||
Regexp string | ||
re *regexp.Regexp | ||
Commits []*object.Commit | ||
} | ||
type args struct { | ||
c *object.Commit | ||
} | ||
tests := []struct { | ||
name string | ||
fields fields | ||
args args | ||
want bool | ||
}{ | ||
{"null regexp", fields{}, args{&object.Commit{Message: "test"}}, true}, | ||
{"has regexp match", fields{re: regexp.MustCompile("test")}, args{&object.Commit{Message: "test"}}, true}, | ||
{"no regexp match", fields{re: regexp.MustCompile("example")}, args{&object.Commit{Message: "test"}}, false}, | ||
{"only match first line", fields{re: regexp.MustCompile("test")}, args{&object.Commit{Message: "example\ntest"}}, false}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
g := &Group{ | ||
Title: tt.fields.Title, | ||
Order: tt.fields.Order, | ||
Regexp: tt.fields.Regexp, | ||
re: tt.fields.re, | ||
Commits: tt.fields.Commits, | ||
} | ||
assert.Equal(t, tt.want, g.Matches(tt.args.c)) | ||
}) | ||
} | ||
} | ||
|
||
func TestGroup_String(t *testing.T) { | ||
testCommit := &object.Commit{Message: "test", Hash: plumbing.NewHash("DEADBEEF")} | ||
|
||
type fields struct { | ||
Title string | ||
Order int | ||
Regexp string | ||
re *regexp.Regexp | ||
Commits []*object.Commit | ||
} | ||
tests := []struct { | ||
name string | ||
abbrev int | ||
fields fields | ||
want string | ||
}{ | ||
{"no commits", 8, fields{Title: "Test"}, ""}, | ||
{"no title", 8, fields{Commits: []*object.Commit{testCommit}}, "- deadbeef test\n"}, | ||
{"title and commits", 8, fields{Title: "Test", Commits: []*object.Commit{testCommit}}, "### Test\n- deadbeef test\n"}, | ||
{"skip commit hash", -1, fields{Commits: []*object.Commit{testCommit}}, "- test\n"}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
defer func(abbrev int) { | ||
Default.Abbrev = abbrev | ||
}(Default.Abbrev) | ||
Default.Abbrev = tt.abbrev | ||
|
||
g := &Group{ | ||
Title: tt.fields.Title, | ||
Order: tt.fields.Order, | ||
Regexp: tt.fields.Regexp, | ||
re: tt.fields.re, | ||
Commits: tt.fields.Commits, | ||
} | ||
assert.Equal(t, tt.want, g.String()) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package config | ||
|
||
import ( | ||
"bufio" | ||
"os" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type stubCmd struct { | ||
*cobra.Command | ||
prevWd string | ||
tempPath string | ||
} | ||
|
||
func newStubCmd() *stubCmd { | ||
temp, err := os.MkdirTemp("", "changelog-generator-") | ||
if err != nil { | ||
panic(err) | ||
} | ||
wd, err := os.Getwd() | ||
if err != nil { | ||
panic(err) | ||
} | ||
if err := os.Chdir(temp); err != nil { | ||
panic(err) | ||
} | ||
cmd := &stubCmd{Command: &cobra.Command{}, prevWd: wd, tempPath: temp} | ||
cmd.Flags().String("config", "", "") | ||
if err := cmd.ParseFlags(os.Args); err != nil { | ||
panic(err) | ||
} | ||
return cmd | ||
} | ||
|
||
func (s *stubCmd) close() { | ||
if err := os.Chdir(s.prevWd); err != nil { | ||
panic(err) | ||
} | ||
if err := os.RemoveAll(s.tempPath); err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
func TestLoad(t *testing.T) { | ||
t.Run("no config file", func(t *testing.T) { | ||
cmd := newStubCmd() | ||
defer cmd.close() | ||
|
||
conf, err := Load(cmd.Command) | ||
if !assert.NoError(t, err) { | ||
return | ||
} | ||
assert.Equal(t, Default, conf) | ||
assert.Len(t, conf.Filters.Include, 0) | ||
assert.Len(t, conf.Filters.Exclude, 0) | ||
if assert.Len(t, conf.Groups, 1) { | ||
assert.Nil(t, conf.Groups[0].re) | ||
} | ||
}) | ||
|
||
cfgFileTests := []struct { | ||
path string | ||
isGoReleaser bool | ||
}{ | ||
{".changelog-generator.yaml", false}, | ||
{".changelog-generator.yml", false}, | ||
{".goreleaser.yaml", true}, | ||
{".goreleaser.yml", true}, | ||
} | ||
for _, tt := range cfgFileTests { | ||
t.Run("loads config at "+tt.path, func(t *testing.T) { | ||
defer func() { | ||
Default = NewDefault() | ||
}() | ||
cmd := newStubCmd() | ||
defer cmd.close() | ||
|
||
data := `filters: | ||
exclude: | ||
- "^docs" | ||
- "^test" | ||
groups: | ||
- title: Features | ||
order: 0 | ||
regexp: "^(feat)" | ||
- title: Fixes | ||
order: 1 | ||
regexp: "^(fix|perf)" | ||
- title: Others | ||
order: 999` | ||
if tt.isGoReleaser { | ||
orig := data | ||
data = "changelog:\n" | ||
scanner := bufio.NewScanner(strings.NewReader(orig)) | ||
for scanner.Scan() { | ||
data += " " + scanner.Text() + "\n" | ||
} | ||
} | ||
|
||
if err := os.WriteFile(tt.path, []byte(data), 0o644); !assert.NoError(t, err) { | ||
return | ||
} | ||
|
||
conf, err := Load(cmd.Command) | ||
if !assert.NoError(t, err) { | ||
return | ||
} | ||
assert.Equal(t, Default, conf) | ||
assert.Len(t, conf.Filters.Include, 0) | ||
assert.Len(t, conf.Filters.Exclude, 2) | ||
assert.Len(t, conf.Groups, 3) | ||
for _, g := range conf.Groups { | ||
assert.NotNil(t, g.re) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package util | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/go-git/go-git/v5/plumbing/object" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestShortMessage(t *testing.T) { | ||
type args struct { | ||
c *object.Commit | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want string | ||
}{ | ||
{"already short", args{&object.Commit{Message: "test"}}, "test"}, | ||
{"long", args{&object.Commit{Message: "test\n\ntest"}}, "test"}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
assert.Equal(t, tt.want, ShortMessage(tt.args.c)) | ||
}) | ||
} | ||
} |