Skip to content

Commit 22a46bd

Browse files
authored
fix: testing-isolation excluded packages (#3935)
1 parent 5a95a53 commit 22a46bd

File tree

4 files changed

+24
-14
lines changed

4 files changed

+24
-14
lines changed

.github/cloud-samples-tools/cmd/main.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,11 @@ func affectedCmd(configFile string, diffsFile string) {
7272
if err != nil {
7373
log.Fatalln("❌ error getting the diffs: ", diffsFile, "\n", err)
7474
}
75-
diffs := strings.Split(strings.TrimSpace(string(diffsBytes)), "\n") // Trim whitespace to remove extra newline from diff output.
75+
// Trim whitespace to remove extra newline from diff output.
76+
diffs := strings.Split(strings.TrimSpace(string(diffsBytes)), "\n")
7677

77-
packages, err := config.Affected(diffs)
78+
// Log to stderr since GitHub Actions expects the output on stdout.
79+
packages, err := config.Affected(os.Stderr, diffs)
7880
if err != nil {
7981
log.Fatalln("❌ error finding the affected packages.\n", err)
8082
}

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

+9-6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ package config
1919
import (
2020
"encoding/json"
2121
"errors"
22+
"fmt"
23+
"io"
2224
"io/fs"
2325
"os"
2426
"path/filepath"
@@ -154,8 +156,8 @@ func (c *Config) FindAllPackages(root string) ([]string, error) {
154156
// Affected returns the packages that have been affected from diffs.
155157
// If there are diffs on at leat one global file affecting all packages,
156158
// then this returns all packages matched by the config.
157-
func (c *Config) Affected(diffs []string) ([]string, error) {
158-
changed := c.Changed(diffs)
159+
func (c *Config) Affected(log io.Writer, diffs []string) ([]string, error) {
160+
changed := c.Changed(log, diffs)
159161
if slices.Contains(changed, ".") {
160162
return c.FindAllPackages(".")
161163
}
@@ -165,16 +167,13 @@ func (c *Config) Affected(diffs []string) ([]string, error) {
165167
// Changed returns the packages that have changed.
166168
// It only returns packages that are matched by the config,
167169
// and are not excluded by the config.
168-
func (c *Config) Changed(diffs []string) []string {
170+
func (c *Config) Changed(log io.Writer, diffs []string) []string {
169171
changedUnique := make(map[string]bool)
170172
for _, diff := range diffs {
171173
if !c.Matches(diff) {
172174
continue
173175
}
174176
pkg := c.FindPackage(diff)
175-
if slices.Contains(c.ExcludePackages, pkg) {
176-
continue
177-
}
178177
changedUnique[pkg] = true
179178
}
180179

@@ -184,6 +183,10 @@ func (c *Config) Changed(diffs []string) []string {
184183

185184
changed := make([]string, 0, len(changedUnique))
186185
for pkg := range changedUnique {
186+
if slices.Contains(c.ExcludePackages, pkg) {
187+
fmt.Fprintf(log, "ℹ️ Excluded package %q, skipping.\n", pkg)
188+
continue
189+
}
187190
changed = append(changed, pkg)
188191
}
189192
return changed

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

+11-6
Original file line numberDiff line numberDiff line change
@@ -167,30 +167,35 @@ func TestFindPackage(t *testing.T) {
167167

168168
func TestChanged(t *testing.T) {
169169
config := c.Config{
170-
PackageFile: []string{"package.json"},
171-
Match: []string{"*"},
170+
PackageFile: []string{"package.json"},
171+
Match: []string{"*"},
172+
ExcludePackages: []string{filepath.Join("testdata", "excluded")},
172173
}
173174

174175
tests := []struct {
175176
diffs []string
176177
expected []string
177178
}{
178-
{
179+
{ // Global change, everything is affected.
179180
diffs: []string{filepath.Join("testdata", "file.txt")},
180181
expected: []string{"."},
181182
},
182-
{
183+
{ // Single affected package.
183184
diffs: []string{filepath.Join("testdata", "my-package", "file.txt")},
184185
expected: []string{filepath.Join("testdata", "my-package")},
185186
},
186-
{
187+
{ // Single affected nested package.
187188
diffs: []string{filepath.Join("testdata", "my-package", "subpackage", "file.txt")},
188189
expected: []string{filepath.Join("testdata", "my-package", "subpackage")},
189190
},
191+
{ // Excluded package.
192+
diffs: []string{filepath.Join("testdata", "excluded", "file.txt")},
193+
expected: []string{},
194+
},
190195
}
191196

192197
for _, test := range tests {
193-
got := config.Changed(test.diffs)
198+
got := config.Changed(os.Stderr, test.diffs)
194199
if !reflect.DeepEqual(test.expected, got) {
195200
t.Fatal("expected equal\n", test.expected, got)
196201
}

.github/cloud-samples-tools/pkg/config/testdata/excluded/package.json

Whitespace-only changes.

0 commit comments

Comments
 (0)