Skip to content

Commit 25b6929

Browse files
author
David Cavazos
committed
use table based testing
1 parent c74db78 commit 25b6929

File tree

2 files changed

+131
-88
lines changed

2 files changed

+131
-88
lines changed

.github/cloud-samples-tools/pkg/config/config.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,32 +58,32 @@ func (c *Config) Save(file *os.File) error {
5858
}
5959

6060
// LoadConfig loads the config from the given path.
61-
func LoadConfig(path string) (Config, error) {
62-
config := Config{}
63-
61+
func LoadConfig(path string) (*Config, error) {
6462
// Read the JSONC file.
6563
sourceJsonc, err := os.ReadFile(path)
6664
if err != nil {
67-
return config, err
65+
return nil, err
6866
}
6967

7068
// Strip the comments and load the JSON.
7169
sourceJson := multiLineCommentsRegex.ReplaceAll(sourceJsonc, []byte{})
7270
sourceJson = singleLineCommentsRegex.ReplaceAll(sourceJson, []byte{})
71+
72+
var config Config
7373
err = json.Unmarshal(sourceJson, &config)
7474
if err != nil {
75-
return config, err
75+
return &config, err
7676
}
7777

7878
// Set default values if they are not set.
7979
if config.PackageFile == nil {
80-
return config, errors.New("package-file is required")
80+
return &config, errors.New("package-file is required")
8181
}
8282
if config.Match == nil {
8383
config.Match = []string{"*"}
8484
}
8585

86-
return config, nil
86+
return &config, nil
8787
}
8888

8989
// Match returns true if the path matches any of the patterns.
@@ -152,6 +152,8 @@ func (c *Config) FindAllPackages(root string) ([]string, error) {
152152
}
153153

154154
// Affected returns the packages that have been affected from diffs.
155+
// If there are diffs on at leat one global file affecting all packages,
156+
// then this returns all packages matched by the config.
155157
func (c *Config) Affected(diffs []string) ([]string, error) {
156158
changed := c.Changed(diffs)
157159
if slices.Contains(changed, ".") {
@@ -161,6 +163,8 @@ func (c *Config) Affected(diffs []string) ([]string, error) {
161163
}
162164

163165
// Changed returns the packages that have changed.
166+
// It only returns packages that are matched by the config,
167+
// and are not excluded by the config.
164168
func (c *Config) Changed(diffs []string) []string {
165169
changedUnique := make(map[string]bool)
166170
for _, diff := range diffs {

.github/cloud-samples-tools/test/config_test.go

Lines changed: 120 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -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

3848
func 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

Comments
 (0)