Skip to content

Commit

Permalink
fix: testing-isolation excluded packages (#3935)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidcavazos authored Dec 12, 2024
1 parent 5a95a53 commit 22a46bd
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 14 deletions.
6 changes: 4 additions & 2 deletions .github/cloud-samples-tools/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,11 @@ func affectedCmd(configFile string, diffsFile string) {
if err != nil {
log.Fatalln("❌ error getting the diffs: ", diffsFile, "\n", err)
}
diffs := strings.Split(strings.TrimSpace(string(diffsBytes)), "\n") // Trim whitespace to remove extra newline from diff output.
// Trim whitespace to remove extra newline from diff output.
diffs := strings.Split(strings.TrimSpace(string(diffsBytes)), "\n")

packages, err := config.Affected(diffs)
// Log to stderr since GitHub Actions expects the output on stdout.
packages, err := config.Affected(os.Stderr, diffs)
if err != nil {
log.Fatalln("❌ error finding the affected packages.\n", err)
}
Expand Down
15 changes: 9 additions & 6 deletions .github/cloud-samples-tools/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package config
import (
"encoding/json"
"errors"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
Expand Down Expand Up @@ -154,8 +156,8 @@ func (c *Config) FindAllPackages(root string) ([]string, error) {
// Affected returns the packages that have been affected from diffs.
// If there are diffs on at leat one global file affecting all packages,
// then this returns all packages matched by the config.
func (c *Config) Affected(diffs []string) ([]string, error) {
changed := c.Changed(diffs)
func (c *Config) Affected(log io.Writer, diffs []string) ([]string, error) {
changed := c.Changed(log, diffs)
if slices.Contains(changed, ".") {
return c.FindAllPackages(".")
}
Expand All @@ -165,16 +167,13 @@ func (c *Config) Affected(diffs []string) ([]string, error) {
// Changed returns the packages that have changed.
// It only returns packages that are matched by the config,
// and are not excluded by the config.
func (c *Config) Changed(diffs []string) []string {
func (c *Config) Changed(log io.Writer, diffs []string) []string {
changedUnique := make(map[string]bool)
for _, diff := range diffs {
if !c.Matches(diff) {
continue
}
pkg := c.FindPackage(diff)
if slices.Contains(c.ExcludePackages, pkg) {
continue
}
changedUnique[pkg] = true
}

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

changed := make([]string, 0, len(changedUnique))
for pkg := range changedUnique {
if slices.Contains(c.ExcludePackages, pkg) {
fmt.Fprintf(log, "ℹ️ Excluded package %q, skipping.\n", pkg)
continue
}
changed = append(changed, pkg)
}
return changed
Expand Down
17 changes: 11 additions & 6 deletions .github/cloud-samples-tools/pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,30 +167,35 @@ func TestFindPackage(t *testing.T) {

func TestChanged(t *testing.T) {
config := c.Config{
PackageFile: []string{"package.json"},
Match: []string{"*"},
PackageFile: []string{"package.json"},
Match: []string{"*"},
ExcludePackages: []string{filepath.Join("testdata", "excluded")},
}

tests := []struct {
diffs []string
expected []string
}{
{
{ // Global change, everything is affected.
diffs: []string{filepath.Join("testdata", "file.txt")},
expected: []string{"."},
},
{
{ // Single affected package.
diffs: []string{filepath.Join("testdata", "my-package", "file.txt")},
expected: []string{filepath.Join("testdata", "my-package")},
},
{
{ // Single affected nested package.
diffs: []string{filepath.Join("testdata", "my-package", "subpackage", "file.txt")},
expected: []string{filepath.Join("testdata", "my-package", "subpackage")},
},
{ // Excluded package.
diffs: []string{filepath.Join("testdata", "excluded", "file.txt")},
expected: []string{},
},
}

for _, test := range tests {
got := config.Changed(test.diffs)
got := config.Changed(os.Stderr, test.diffs)
if !reflect.DeepEqual(test.expected, got) {
t.Fatal("expected equal\n", test.expected, got)
}
Expand Down
Empty file.

0 comments on commit 22a46bd

Please sign in to comment.