Skip to content

Commit d466e81

Browse files
committed
fix: use goccy/yaml to fix yaml parsing
I'd originally intended to use goccy/yaml to simplify json+yaml parsing configuration. I tested both, but lost the work I did with goccy/yaml and didn't realize. This updates to use that library and also fixes the tests around config parsing to better represent the intended test cases.
1 parent bf79269 commit d466e81

File tree

11 files changed

+168
-45
lines changed

11 files changed

+168
-45
lines changed

cmd/main.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,11 @@ func main() {
7878

7979
config := model.LoadOrNewConfig(opts.Config, opts.Owner, opts.Repo)
8080
config.MaxCommits = opts.MaxCommits
81-
config.PreferLocal = opts.Local
81+
if opts.Local != nil {
82+
config.PreferLocal = opts.Local
83+
}
84+
85+
log.WithFields(log.Fields{"config": config}).Debug("Loaded config.")
8286

8387
changes := changelog.Changelog{
8488
Config: config,

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ go 1.14
44

55
require (
66
github.com/go-git/go-git/v5 v5.0.0
7+
github.com/goccy/go-yaml v1.4.3
78
github.com/google/go-github/v29 v29.0.3
89
github.com/jessevdk/go-flags v1.4.0
910
github.com/sirupsen/logrus v1.4.2
11+
github.com/stretchr/testify v1.5.1
1012
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be
11-
gopkg.in/yaml.v2 v2.2.4
1213
)

go.sum

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
1010
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1111
github.com/emirpasic/gods v1.12.0 h1:QAUIPSaCu4G+POclxeqb3F+WPpdKqFGlw36+yOzGlrg=
1212
github.com/emirpasic/gods v1.12.0/go.mod h1:YfzfFFoVP/catgzJb4IKIqXjX78Ha8FMSDh3ymbK86o=
13+
github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
14+
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
1315
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568 h1:BHsljHzVlRcyQhjrss6TZTdY2VfCqZPbv5k3iBFa2ZQ=
1416
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI435gkrCt3MPfRiAkVrwSbHsst4LCFVfpJc=
1517
github.com/gliderlabs/ssh v0.2.2 h1:6zsha5zo/TWhRhwqCD3+EarCAgZ2yN28ipRnGPnwkI0=
@@ -22,6 +24,12 @@ github.com/go-git/go-git-fixtures/v4 v4.0.1 h1:q+IFMfLx200Q3scvt2hN79JsEzy4AmBTp
2224
github.com/go-git/go-git-fixtures/v4 v4.0.1/go.mod h1:m+ICp2rF3jDhFgEZ/8yziagdT1C+ZpZcrJjappBCDSw=
2325
github.com/go-git/go-git/v5 v5.0.0 h1:k5RWPm4iJwYtfWoxIJy4wJX9ON7ihPeZZYC1fLYDnpg=
2426
github.com/go-git/go-git/v5 v5.0.0/go.mod h1:oYD8y9kWsGINPFJoLdaScGCN6dlKg23blmClfZwtUVA=
27+
github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q=
28+
github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8=
29+
github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD876Lmtgy7VtROAbHHXk8no=
30+
github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA=
31+
github.com/goccy/go-yaml v1.4.3 h1:+1jK1ost1TBEfWjciIMU8rciBq0poxurgS7XvLgQInM=
32+
github.com/goccy/go-yaml v1.4.3/go.mod h1:PsEEJ29nIFZL07P/c8dv4P6rQkVFFXafQee85U+ERHA=
2533
github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs=
2634
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
2735
github.com/google/go-cmp v0.3.0 h1:crn/baboCvb5fXaQ0IJ1SGTsTVrWpDsCWC8EGETZijY=
@@ -43,6 +51,13 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
4351
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
4452
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
4553
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
54+
github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=
55+
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
56+
github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA=
57+
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
58+
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
59+
github.com/mattn/go-isatty v0.0.10 h1:qxFzApOv4WsAL965uUPIsXzAKCZxN2p9UqdhFS4ZW10=
60+
github.com/mattn/go-isatty v0.0.10/go.mod h1:qgIWMr58cqv1PHHyhnkY9lrL7etaEgOFcMEpPG5Rm84=
4661
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
4762
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
4863
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
@@ -61,6 +76,8 @@ github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1
6176
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
6277
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
6378
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
79+
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
80+
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
6481
github.com/xanzy/ssh-agent v0.2.1 h1:TCbipTQL2JiiCprBWx9frJ2eJlCYT00NmctrHxVAr70=
6582
github.com/xanzy/ssh-agent v0.2.1/go.mod h1:mLlQY/MoOhWBj+gOGMQkOeiEvkx+8pJSI+0Bx9h2kr4=
6683
golang.org/x/crypto v0.0.0-20190219172222-a4c6cb3142f2/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
@@ -77,25 +94,36 @@ golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be h1:vEDujvNQGv4jgYKudGeI/+
7794
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
7895
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
7996
golang.org/x/sys v0.0.0-20190221075227-b4e8571b14e0/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
97+
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
8098
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
8199
golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc=
82100
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
101+
golang.org/x/sys v0.0.0-20191008105621-543471e840be/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
102+
golang.org/x/sys v0.0.0-20191010194322-b09406accb47/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
83103
golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527 h1:uYVVQ9WP/Ds2ROhcaGPeIdVq0RIXVLwsHlnvJ+cT1So=
84104
golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
85105
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
86106
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
87107
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
88108
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
109+
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898 h1:/atklqdjdhuosWIl6AIbOeHJjicWYPqR9bpxqxYG2pA=
110+
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
89111
google.golang.org/appengine v1.1.0 h1:igQkv0AAhEIvTEpD5LIpAfav2eeVO9HBTjvKHVJPRSs=
90112
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
91113
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
92114
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
93115
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
94116
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU=
95117
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
118+
gopkg.in/go-playground/assert.v1 v1.2.1 h1:xoYuJVE7KT85PYWrN730RguIQO0ePzVRfFMXadIrXTM=
119+
gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE=
120+
gopkg.in/go-playground/validator.v9 v9.30.0 h1:Wk0Z37oBmKj9/n+tPyBHZmeL19LaCoK3Qq48VwYENss=
121+
gopkg.in/go-playground/validator.v9 v9.30.0/go.mod h1:+c9/zcJMFNgbLvly1L1V+PpxWdVbfP1avr/N00E2vyQ=
96122
gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME=
97123
gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI=
98124
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
99125
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
100126
gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I=
101127
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
128+
gopkg.in/yaml.v3 v3.0.0-20191120175047-4206685974f2 h1:XZx7nhd5GMaZpmDaEHFVafUZC7ya0fuo7cSJ3UCKYmM=
129+
gopkg.in/yaml.v3 v3.0.0-20191120175047-4206685974f2/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

model/config.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ import (
2323
"regexp"
2424
"strings"
2525

26+
"github.com/goccy/go-yaml"
2627
log "github.com/sirupsen/logrus"
27-
"gopkg.in/yaml.v2"
2828
)
2929

3030
// Grouping allows assigning a grouping name with a set of regex patterns or texts.
@@ -40,7 +40,7 @@ type Grouping struct {
4040
// Config provides a user with more robust options for Changelog configuration
4141
type Config struct {
4242
// Defines whether we resolve commits only or query additional information from pull requests
43-
ResolveType *ResolveType `json:"resolve"`
43+
ResolveType *ResolveType `json:"resolve" yaml:"resolve"`
4444

4545
// The Owner (user or org) of the target repository
4646
Owner string `json:"owner"`
@@ -50,28 +50,28 @@ type Config struct {
5050

5151
// A set of Grouping objects which allow to define groupings for changelog output.
5252
// Commits are associated with the first matching group.
53-
Groupings *[]Grouping `json:"groupings"`
53+
Groupings *[]Grouping `json:"groupings,omitempty"`
5454

5555
// As set of square-bracket regex patterns, wrapped texts and/or labels to be excluded from output.
5656
// If the commit message or pr labels reference any text in this Exclude set, that commit
5757
// will be ignored./**/
58-
Exclude *[]string `json:"exclude"`
58+
Exclude *[]string `json:"exclude,omitempty"`
5959

6060
// Optional base url when targeting GitHub Enterprise
61-
Enterprise *string `json:"enterprise"`
61+
Enterprise *string `json:"enterprise,omitempty"`
6262

6363
// Custom template following Go text/template syntax
6464
// For more details, see https://golang.org/pkg/text/template/
65-
Template *string `json:"template"`
65+
Template *string `json:"template,omitempty"`
6666

6767
// SortDirection defines the order of commits within the changelog
6868
SortDirection *SortDirection `json:"sort"`
6969

7070
// PreferLocal defines whether commits may be queried locally. Requires executing from within a Git repository.
71-
PreferLocal *bool `json:"local"`
71+
PreferLocal *bool `json:"local,omitempty"`
7272

7373
// MaxCommits defines the maximum number of commits to be processed.
74-
MaxCommits *int `json:"max_commits"`
74+
MaxCommits *int `json:"max_commits,omitempty"`
7575
}
7676

7777
// Load a Config from path
@@ -120,7 +120,7 @@ func (c *Config) ShouldExcludeByText(text *string) bool {
120120
for _, pattern := range *c.Exclude {
121121
re := regexp.MustCompile(pattern)
122122
if re.Match([]byte(*text)) {
123-
log.WithFields(log.Fields{"text": *text,"pattern":pattern}).Debug("exclude via pattern")
123+
log.WithFields(log.Fields{"text": *text, "pattern": pattern}).Debug("exclude via pattern")
124124
return true
125125
}
126126
}
@@ -136,7 +136,7 @@ func (c *Config) FindGroup(commitMessage string) *string {
136136
re := regexp.MustCompile(pattern)
137137
if re.Match([]byte(title)) {
138138
grouping = &g.Name
139-
log.WithFields(log.Fields{"grouping":*grouping,"title":title}).Debug("found group name for commit")
139+
log.WithFields(log.Fields{"grouping": *grouping, "title": title}).Debug("found group name for commit")
140140
return grouping
141141
}
142142
}
@@ -177,7 +177,7 @@ func LoadOrNewConfig(path *string, owner string, repo string) *Config {
177177
defaultSortDirection := Descending
178178
config := Config{
179179
SortDirection: &defaultSortDirection,
180-
ResolveType: &defaultResolveType,
180+
ResolveType: &defaultResolveType,
181181
}
182182
if path != nil {
183183
err := config.Load(*path)

model/config_test.go

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"testing"
2626

2727
"github.com/google/go-github/v29/github"
28+
"github.com/stretchr/testify/assert"
2829
)
2930

3031
func hash(s string) uint32 {
@@ -66,6 +67,8 @@ func groupings(g ...Grouping) *[]Grouping {
6667
}
6768

6869
func TestConfig_Load(t *testing.T) {
70+
bt := true
71+
pint := func(i int) *int { return &i }
6972
type fields struct {
7073
JSONData string
7174
ResolveType *ResolveType
@@ -77,6 +80,8 @@ func TestConfig_Load(t *testing.T) {
7780
Template *string
7881
SortDirection *SortDirection
7982
TempFileExt *string
83+
PreferLocal *bool
84+
MaxCommits *int
8085
}
8186
tests := []struct {
8287
name string
@@ -85,45 +90,45 @@ func TestConfig_Load(t *testing.T) {
8590
}{
8691
{"Loads valid empty json", fields{JSONData: "{}"}, false},
8792
{"Loads valid json resolve-only", fields{JSONData: `{"resolve": "commits"}`, ResolveType: Commits.Ptr()}, false},
88-
{"Fail on valid json with invalid data type resolve-only", fields{JSONData: `{"resolve": 1.0}`}, true}, // note that 1 would resolve since enum is an int
93+
{"Fail on valid json with invalid data type resolve-only", fields{JSONData: `{"resolve": 1.0}`, ResolveType: ResolveType(0).Ptr()}, true}, // note that 1 would resolve since enum is an int
8994
{"Loads valid json owner-only", fields{JSONData: `{"owner": "jimschubert"}`, Owner: "jimschubert"}, false},
9095
{"Fail on valid json with invalid data type owner-only", fields{JSONData: `{"owner": []}`}, true},
91-
{"Loads valid json repo-only", fields{JSONData: `{"repo": "changelog"}`, Owner: "changelog"}, false},
96+
{"Loads valid json repo-only", fields{JSONData: `{"repo": "changelog"}`, Repo: "changelog"}, false},
9297
{"Fail on valid json with invalid data type repo-only", fields{JSONData: `{"repo": []}`}, true},
93-
{"Loads valid json groupings-only", fields{JSONData: `{"groupings": []}`, Groupings: groupings(Grouping { Name: "g", Patterns: make([]string, 0)})}, false},
94-
{"Fail on valid json with invalid data type groupings-only", fields{JSONData: `{"groupings": 4}`}, true},
98+
{"Loads valid json groupings-only", fields{JSONData: `{"groupings":[{"name":"g","patterns":[]}]}`, Groupings: groupings(Grouping{Name: "g", Patterns: make([]string, 0)})}, false},
9599
{"Loads valid json exclude-only", fields{JSONData: `{"exclude": []}`, Exclude: ptrStringArray()}, false},
96-
{"Fail on valid json with invalid data type exclude-only", fields{JSONData: `{"exclude": 1}`}, true},
97100
{"Loads valid json enterprise-only", fields{JSONData: `{"enterprise": "https://ghe.example.com"}`, Enterprise: p("https://ghe.example.com")}, false},
98-
{"Fail on valid json with invalid data type enterprise-only", fields{JSONData: `{"enterprise": 0}`}, true},
99101
{"Loads valid json template-only", fields{JSONData: `{"template": "/path/to/template"}`, Template: p("/path/to/template")}, false},
100102
{"Loads valid json ascending sort-only", fields{JSONData: `{"sort": "asc"}`, SortDirection: Ascending.Ptr()}, false},
101103
{"Loads valid json descending sort-only", fields{JSONData: `{"sort": "desc"}`, SortDirection: Descending.Ptr()}, false},
102-
{"Fail on valid json with invalid data type template-only", fields{JSONData: `{"template": []}`}, true},
103104
{"Loads valid config_full.json",
104105
fields{
105106
JSONData: string(helperTestData(t, "config_full.json")),
106107
ResolveType: Commits.Ptr(),
107108
Owner: "jimschubert",
108109
Repo: "ossify",
109-
Groupings: groupings(Grouping{Name: "feature", Patterns: []string{}}, Grouping{Name: "bug", Patterns: []string{} }),
110+
Groupings: groupings(Grouping{Name: "feature", Patterns: []string{}}, Grouping{Name: "bug", Patterns: []string{}}),
110111
Exclude: ptrStringArray("wip", "help wanted"),
111112
Enterprise: p("https://ghe.example.com"),
112113
Template: p("/path/to/template"),
113114
SortDirection: Ascending.Ptr(),
115+
PreferLocal: &bt,
116+
MaxCommits: pint(150),
114117
}, false},
115118
{"Loads valid config_full.yaml",
116119
fields{
117120
JSONData: string(helperTestData(t, "config_full.yaml")),
118121
ResolveType: Commits.Ptr(),
119122
Owner: "jimschubert",
120123
Repo: "ossify",
121-
Groupings: groupings(Grouping{Name: "feature", Patterns: []string{}}, Grouping{Name: "bug", Patterns: []string{} }),
124+
Groupings: groupings(Grouping{Name: "feature", Patterns: []string{"^a", "\\bb$"}}, Grouping{Name: "bug", Patterns: []string{"cba", "\\b\\[f\\]\\b"}}),
122125
Exclude: ptrStringArray("wip", "help wanted"),
123126
Enterprise: p("https://ghe.example.com"),
124127
Template: p("/path/to/template"),
125128
SortDirection: Ascending.Ptr(),
126-
TempFileExt : p("yaml"),
129+
TempFileExt: p("yaml"),
130+
PreferLocal: &bt,
131+
MaxCommits: pint(199),
127132
}, false},
128133
{"Fails on invalid json",
129134
fields{
@@ -140,20 +145,28 @@ func TestConfig_Load(t *testing.T) {
140145
}
141146
jsonLocation, cleanup := createTempConfig(t, tt.fields.JSONData, tmpExt)
142147
defer cleanup()
143-
t.Run(tt.name, func(t *testing.T) {
144-
c := &Config{
145-
ResolveType: tt.fields.ResolveType,
146-
Owner: tt.fields.Owner,
147-
Repo: tt.fields.Repo,
148-
Groupings: tt.fields.Groupings,
149-
Exclude: tt.fields.Exclude,
150-
Enterprise: tt.fields.Enterprise,
151-
Template: tt.fields.Template,
152-
}
153-
if err := c.Load(jsonLocation); (err != nil) != tt.wantErr {
154-
t.Errorf("Load() error = %v, wantErr %v", err, tt.wantErr)
148+
149+
c := &Config{}
150+
err := c.Load(jsonLocation)
151+
if tt.wantErr {
152+
assert.Error(t, err, "Load() error = %v, wantErr %v", err, tt.wantErr)
153+
} else {
154+
assert.NoError(t, err)
155+
156+
if tt.fields.ResolveType == nil {
157+
assert.Nil(t, c.ResolveType)
158+
} else {
159+
assert.Equal(t, tt.fields.ResolveType, c.ResolveType)
155160
}
156-
})
161+
assert.Equal(t, tt.fields.Owner, c.Owner)
162+
assert.Equal(t, tt.fields.Repo, c.Repo)
163+
assert.Equal(t, tt.fields.Groupings, c.Groupings)
164+
assert.Equal(t, tt.fields.Exclude, c.Exclude)
165+
assert.Equal(t, tt.fields.Enterprise, c.Enterprise)
166+
assert.Equal(t, tt.fields.Template, c.Template)
167+
assert.Equal(t, tt.fields.PreferLocal, c.PreferLocal)
168+
assert.Equal(t, tt.fields.MaxCommits, c.MaxCommits)
169+
}
157170
})
158171
}
159172
}
@@ -179,7 +192,7 @@ func TestConfig_String(t *testing.T) {
179192
ResolveType: Commits.Ptr(),
180193
Owner: "jimschubert",
181194
Repo: "ossify",
182-
Groupings: groupings(Grouping{Name: "feature", Patterns: []string{}}, Grouping{Name: "bug", Patterns: []string{} }),
195+
Groupings: groupings(Grouping{Name: "feature", Patterns: []string{}}, Grouping{Name: "bug", Patterns: []string{}}),
183196
Exclude: ptrStringArray("wip", "help wanted"),
184197
Enterprise: p("https://ghe.example.com"),
185198
Template: p("/path/to/template"),
@@ -308,4 +321,4 @@ func TestConfig_FindGroup(t *testing.T) {
308321
}
309322
})
310323
}
311-
}
324+
}

model/resolve_type.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,24 @@ func (r *ResolveType) UnmarshalJSON(b []byte) error {
5353

5454
s := string(b)
5555
switch s {
56-
case `"commits"`:
56+
case "commits", `"commits"`:
5757
*r = Commits
58-
case `"pulls"`, `"pullrequest"`, `"prs"`:
58+
case "pulls", "pullrequest", "prs", `"pulls"`, `"pullrequest"`, `"prs"`:
5959
*r = PullRequests
6060
default:
6161
return fmt.Errorf("unknown resolve type %q", s)
6262
}
6363
return nil
6464
}
6565

66+
func (r *ResolveType) UnmarshalYAML(b []byte) error {
67+
return r.UnmarshalJSON(b)
68+
}
69+
70+
func (r *ResolveType) MarshalYAML() ([]byte, error) {
71+
return r.MarshalJSON()
72+
}
73+
6674
// String displays a human readable representation of the ResolveType values
6775
func (r ResolveType) String() string {
6876
switch r {

model/resolve_type_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,31 @@ func TestResolveType_MarshalJSON1(t *testing.T) {
111111
})
112112
}
113113
}
114+
115+
func TestResolveType_UnmarshalYAML(t *testing.T) {
116+
type args struct {
117+
b []byte
118+
}
119+
tests := []struct {
120+
name string
121+
r ResolveType
122+
args args
123+
wantErr bool
124+
}{
125+
{"unmarshal commits", Commits, args{b: []byte("commits")}, false},
126+
{"unmarshal prs", PullRequests, args{b: []byte("prs")}, false},
127+
{"unmarshal pulls", PullRequests, args{b: []byte("pulls")}, false},
128+
{"unmarshal pullrequest", PullRequests, args{b: []byte("pullrequest")}, false},
129+
{"unmarshal commits (bad single character)", Commits, args{b: []byte("\"a\"")}, true},
130+
{"unmarshal prs (bad single character)", PullRequests, args{b: []byte("\"1\"")}, true},
131+
{"unmarshal prs (bad empty character)", PullRequests, args{b: []byte("")}, true},
132+
{"unmarshal prs (nil for null terminating character)", PullRequests, args{b: []byte("\x00")}, false},
133+
}
134+
for _, tt := range tests {
135+
t.Run(tt.name, func(t *testing.T) {
136+
if err := tt.r.UnmarshalYAML(tt.args.b); (err != nil) != tt.wantErr {
137+
t.Errorf("UnmarshalYAML() error = %v, wantErr %v", err, tt.wantErr)
138+
}
139+
})
140+
}
141+
}

0 commit comments

Comments
 (0)