Skip to content

Commit

Permalink
test: Add initial tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Mar 14, 2024
1 parent 38246cd commit 0fcc06e
Show file tree
Hide file tree
Showing 9 changed files with 342 additions and 2 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/test-action.yaml
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
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ repos:
- id: go-vet-mod
- id: golangci-lint-mod
args: [--fix]
- id: go-test-mod

- repo: local
hooks:
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/knadh/koanf/providers/structs v0.1.0
github.com/knadh/koanf/v2 v2.1.0
github.com/spf13/cobra v1.8.0
github.com/stretchr/testify v1.9.0
)

require (
Expand All @@ -19,6 +20,7 @@ require (
github.com/cloudflare/circl v1.3.7 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.3 // indirect
github.com/cyphar/filepath-securejoin v0.2.4 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/fatih/structs v1.1.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
Expand All @@ -33,6 +35,7 @@ require (
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/pjbgf/sha1cd v0.3.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/sergi/go-diff v1.3.1 // indirect
github.com/skeema/knownhosts v1.2.1 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/xanzy/ssh-agent v0.3.3 h1:+/15pJfg/RsTxqYcX6fHqOXZwwMP+2VyYWJeWM2qQFM=
github.com/xanzy/ssh-agent v0.3.3/go.mod h1:6dzNDKs0J9rVPHPhaGCukekBHKqfl+L3KghI1Bc68Uw=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
Expand Down
11 changes: 11 additions & 0 deletions internal/config/config_test.go
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())
}
65 changes: 65 additions & 0 deletions internal/config/filter_test.go
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)
}
})
}
}
86 changes: 86 additions & 0 deletions internal/config/group_test.go
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())
})
}
}
121 changes: 121 additions & 0 deletions internal/config/load_test.go
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)
}
})
}
}
27 changes: 27 additions & 0 deletions internal/util/commit_test.go
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))
})
}
}

0 comments on commit 0fcc06e

Please sign in to comment.