@@ -7,32 +7,42 @@ import (
77 "testing"
88)
99
10- func TestLoadConfigEmpty (t * testing.T ) {
11- path := filepath .Join ("testdata" , "config" , "empty.json" )
12- _ , err := c .LoadConfig (path )
13- expectError (t , err )
14- }
15-
16- func TestLoadConfigDefaultValues (t * testing.T ) {
17- path := filepath .Join ("testdata" , "config" , "default-values.json" )
18- config , err := c .LoadConfig (path )
19- ok (t , err )
20- expected := c.Config {
21- PackageFile : []string {"package.json" },
22- Match : []string {"*" },
10+ func TestLoadConfig (t * testing.T ) {
11+ tests := []struct {
12+ filename string
13+ config * c.Config
14+ fails bool
15+ }{
16+ {
17+ filename : "empty.json" ,
18+ fails : true ,
19+ },
20+ {
21+ filename : "default-values.json" ,
22+ config : & c.Config {
23+ PackageFile : []string {"package.json" },
24+ Match : []string {"*" },
25+ },
26+ },
27+ {
28+ filename : "comments.jsonc" ,
29+ config : & c.Config {
30+ PackageFile : []string {"package.json" },
31+ Match : []string {"*" },
32+ },
33+ },
2334 }
24- equals (t , expected , config )
25- }
2635
27- func TestLoadConfigComments (t * testing.T ) {
28- path := filepath .Join ("testdata" , "config" , "comments.jsonc" )
29- config , err := c .LoadConfig (path )
30- ok (t , err )
31- expected := c.Config {
32- PackageFile : []string {"package.json" },
33- Match : []string {"*" },
36+ for _ , test := range tests {
37+ path := filepath .Join ("testdata" , "config" , test .filename )
38+ got , err := c .LoadConfig (path )
39+ if test .fails {
40+ expectError (t , err )
41+ continue
42+ }
43+ ok (t , err )
44+ equals (t , test .config , got )
3445 }
35- equals (t , expected , config )
3646}
3747
3848func TestSaveLoadConfig (t * testing.T ) {
@@ -52,83 +62,112 @@ func TestSaveLoadConfig(t *testing.T) {
5262 loadedConfig , err := c .LoadConfig (file .Name ())
5363 ok (t , err )
5464
55- equals (t , config , loadedConfig )
56- }
57-
58- func TestMatchNoPatterns (t * testing.T ) {
59- patterns := []string {}
60- path := "path/to/file.js"
61- assert (t , ! c .Match (patterns , path ), "match(%v, %v) = false" )
65+ equals (t , & config , loadedConfig )
6266}
6367
64- func TestMatchFilePattern (t * testing.T ) {
65- patterns := []string {"*.js" }
66- path := "path/to/file.js"
67- assert (t , c .Match (patterns , path ), "match(%v, %v) = true" )
68- }
69-
70- func TestMatchFilePath (t * testing.T ) {
71- patterns := []string {"path/to/" }
72- path := "path/to/file.js"
73- assert (t , c .Match (patterns , path ), "match(%v, %v) = true" )
74- }
68+ func TestMatch (t * testing.T ) {
69+ tests := []struct {
70+ patterns []string
71+ path string
72+ expected bool
73+ }{
74+ {
75+ patterns : []string {},
76+ path : "path/to/file.js" ,
77+ expected : false ,
78+ },
79+ {
80+ patterns : []string {"*.js" },
81+ path : "path/to/file.js" ,
82+ expected : true ,
83+ },
84+ {
85+ patterns : []string {"path/to/" },
86+ path : "path/to/file.js" ,
87+ expected : true ,
88+ },
89+ }
7590
76- func TestIsPackageDirNotExists ( t * testing. T ) {
77- config := c.Config { PackageFile : [] string { "package.json" }}
78- dir := "path-does-not-exist"
79- assert ( t , ! config . IsPackageDir ( dir ), "config.IsPackageDir(%v) = false" )
91+ for _ , test := range tests {
92+ got := c .Match ( test . patterns , test . path )
93+ equals ( t , test . expected , got )
94+ }
8095}
8196
82- func TestIsPackageDirExists (t * testing.T ) {
97+ func TestIsPackage (t * testing.T ) {
8398 config := c.Config {PackageFile : []string {"package.json" }}
84- dir := filepath .Join ("testdata" , "my-package" )
85- assert (t , config .IsPackageDir (dir ), "config.IsPackageDir(%v) = true" )
86- }
99+ tests := []struct {
100+ path string
101+ expected bool
102+ }{
103+ {
104+ path : filepath .Join ("testdata" , "path-does-not-exist" ),
105+ expected : false ,
106+ },
107+ {
108+ path : filepath .Join ("testdata" , "my-package" ),
109+ expected : true ,
110+ },
111+ }
87112
88- func TestFindPackageRoot ( t * testing. T ) {
89- config := c. Config { PackageFile : [] string { "package.json" }}
90- path := filepath . Join ( "testdata" , "my-file.txt" )
91- equals ( t , "." , config . FindPackage ( path ))
113+ for _ , test := range tests {
114+ got := config . IsPackageDir ( test . path )
115+ equals ( t , test . expected , got )
116+ }
92117}
93118
94- func TestFindPackageSubpackage (t * testing.T ) {
119+ func TestFindPackage (t * testing.T ) {
95120 config := c.Config {PackageFile : []string {"package.json" }}
96- path := filepath .Join ("testdata" , "my-package" , "subpackage" , "my-file.txt" )
97- equals (t , "testdata/my-package/subpackage" , config .FindPackage (path ))
98- }
121+ tests := []struct {
122+ path string
123+ expected string
124+ }{
125+ {
126+ path : filepath .Join ("testdata" , "my-file.txt" ),
127+ expected : "." ,
128+ },
129+ {
130+ path : filepath .Join ("testdata" , "my-package" , "my-file.txt" ),
131+ expected : filepath .Join ("testdata" , "my-package" ),
132+ },
133+ {
134+ path : filepath .Join ("testdata" , "my-package" , "subpackage" , "my-file.txt" ),
135+ expected : filepath .Join ("testdata" , "my-package" , "subpackage" ),
136+ },
137+ }
99138
100- func TestChangedRoot (t * testing.T ) {
101- config := c.Config {PackageFile : []string {"package.json" }}
102- diffs := []string {
103- filepath .Join ("testdata" , "file.txt" ),
139+ for _ , test := range tests {
140+ got := config .FindPackage (test .path )
141+ equals (t , test .expected , got )
104142 }
105- got := config .Changed (diffs )
106- expected := []string {"." }
107- equals (t , expected , got )
108143}
109144
110- func TestChangedPackage (t * testing.T ) {
145+ func TestChanged (t * testing.T ) {
111146 config := c.Config {
112147 PackageFile : []string {"package.json" },
113148 Match : []string {"*" },
114149 }
115- diffs := []string {
116- filepath .Join ("testdata" , "my-package" , "file.txt" ),
117- }
118- got := config .Changed (diffs )
119- expected := []string {"testdata/my-package" }
120- equals (t , expected , got )
121- }
122150
123- func TestChangedSubpackage (t * testing.T ) {
124- config := c.Config {
125- PackageFile : []string {"package.json" },
126- Match : []string {"*" },
151+ tests := []struct {
152+ diffs []string
153+ expected []string
154+ }{
155+ {
156+ diffs : []string {filepath .Join ("testdata" , "file.txt" )},
157+ expected : []string {"." },
158+ },
159+ {
160+ diffs : []string {filepath .Join ("testdata" , "my-package" , "file.txt" )},
161+ expected : []string {filepath .Join ("testdata" , "my-package" )},
162+ },
163+ {
164+ diffs : []string {filepath .Join ("testdata" , "my-package" , "subpackage" , "file.txt" )},
165+ expected : []string {filepath .Join ("testdata" , "my-package" , "subpackage" )},
166+ },
127167 }
128- diffs := []string {
129- filepath .Join ("testdata" , "my-package" , "subpackage" , "file.txt" ),
168+
169+ for _ , test := range tests {
170+ got := config .Changed (test .diffs )
171+ equals (t , test .expected , got )
130172 }
131- got := config .Changed (diffs )
132- expected := []string {"testdata/my-package/subpackage" }
133- equals (t , expected , got )
134173}
0 commit comments