@@ -25,6 +25,7 @@ import (
2525 "testing"
2626
2727 "github.com/google/go-github/v29/github"
28+ "github.com/stretchr/testify/assert"
2829)
2930
3031func hash (s string ) uint32 {
@@ -66,6 +67,8 @@ func groupings(g ...Grouping) *[]Grouping {
6667}
6768
6869func 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+ }
0 commit comments