Skip to content

Commit 906e2e0

Browse files
authored
fix(sidekick): ignored directories are configurable (#1573)
1 parent 6ac562e commit 906e2e0

File tree

4 files changed

+43
-19
lines changed

4 files changed

+43
-19
lines changed

internal/sidekick/internal/config/config.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,11 @@ type Config struct {
6666
// GeneralConfig contains configuration parameters that affect Parsers and Codecs, including the
6767
// selection of parser and codec.
6868
type GeneralConfig struct {
69-
Language string `toml:"language,omitempty"`
70-
SpecificationFormat string `toml:"specification-format,omitempty"`
71-
SpecificationSource string `toml:"specification-source,omitempty"`
72-
ServiceConfig string `toml:"service-config,omitempty"`
69+
Language string `toml:"language,omitempty"`
70+
SpecificationFormat string `toml:"specification-format,omitempty"`
71+
SpecificationSource string `toml:"specification-source,omitempty"`
72+
ServiceConfig string `toml:"service-config,omitempty"`
73+
IgnoredDirectories []string `toml:"ignored-directories,omitempty"`
7374
}
7475

7576
// LoadConfig loads the top-level configuration file and validates its contents.
@@ -130,6 +131,7 @@ func mergeConfigs(rootConfig, local *Config) (*Config, error) {
130131
General: GeneralConfig{
131132
Language: rootConfig.General.Language,
132133
SpecificationFormat: rootConfig.General.SpecificationFormat,
134+
IgnoredDirectories: rootConfig.General.IgnoredDirectories,
133135
},
134136
Source: map[string]string{},
135137
Codec: map[string]string{},

internal/sidekick/internal/config/config_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ func TestLoadRootConfig(t *testing.T) {
6969
General: GeneralConfig{
7070
Language: "root-language",
7171
SpecificationFormat: "root-specification-format",
72+
IgnoredDirectories: []string{"a", "b"},
7273
},
7374
Source: map[string]string{
7475
"s1": "v1",
@@ -101,6 +102,7 @@ func TestMergeLocalForGeneral(t *testing.T) {
101102
General: GeneralConfig{
102103
Language: "root-language",
103104
SpecificationFormat: "root-specification-format",
105+
IgnoredDirectories: []string{"a", "b"},
104106
},
105107
}
106108

@@ -123,6 +125,7 @@ func TestMergeLocalForGeneral(t *testing.T) {
123125
SpecificationFormat: "local-specification-format",
124126
SpecificationSource: "local-specification-source",
125127
ServiceConfig: "local-service-config",
128+
IgnoredDirectories: []string{"a", "b"},
126129
},
127130
Codec: map[string]string{},
128131
Source: map[string]string{},

internal/sidekick/refreshall.go

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"os"
2323
"path"
2424
"path/filepath"
25+
"slices"
2526
"strings"
2627
"sync"
2728

@@ -68,7 +69,7 @@ func refreshAll(rootConfig *config.Config, cmdLine *CommandLine) error {
6869
if err != nil {
6970
return err
7071
}
71-
directories, err := findAllDirectories()
72+
directories, err := findAllDirectories(override)
7273
if err != nil {
7374
return err
7475
}
@@ -104,28 +105,18 @@ func refreshAll(rootConfig *config.Config, cmdLine *CommandLine) error {
104105
return errors.Join(failures...)
105106
}
106107

107-
func findAllDirectories() ([]string, error) {
108+
func findAllDirectories(config *config.Config) ([]string, error) {
108109
var result []string
109110
err := fs.WalkDir(os.DirFS("."), ".", func(path string, d fs.DirEntry, _ error) error {
110111
if d.IsDir() {
111112
return nil
112113
}
113-
dir := filepath.Dir(path)
114-
115-
// TODO(https://github.com/googleapis/librarian/issues/1563): do not
116-
// harcode
117-
if dir == "dart" {
118-
return nil
119-
}
120-
ignored := []string{
121-
"dart/", // Testing
122-
"target/package/", // The output from `cargo package`
123-
}
124-
for _, candidate := range ignored {
125-
if strings.Contains(dir, candidate) {
114+
for _, ignore := range config.General.IgnoredDirectories {
115+
if isInPath(ignore, path) {
126116
return nil
127117
}
128118
}
119+
dir := filepath.Dir(path)
129120
if d.Name() == ".sidekick.toml" && dir != "." {
130121
result = append(result, dir)
131122
}
@@ -136,3 +127,8 @@ func findAllDirectories() ([]string, error) {
136127
}
137128
return result, nil
138129
}
130+
131+
func isInPath(dir, path string) bool {
132+
components := strings.Split(path, string(filepath.Separator))
133+
return slices.Contains(components, dir)
134+
}

internal/sidekick/refreshall_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,26 @@ func TestRefreshAll(t *testing.T) {
2121
t.Fatal(err)
2222
}
2323
}
24+
25+
func TestIsInPath(t *testing.T) {
26+
type TestCase struct {
27+
Dir string
28+
Path string
29+
Want bool
30+
}
31+
testCases := []TestCase{
32+
{"dart", "dart", true},
33+
{"dart", "dartboard", false},
34+
{"dart", "dart/v2", true},
35+
{"dart", "dart/v2/d2/d4", true},
36+
{"dart", "a/b/c/d/dart/v2/d2/d4", true},
37+
{"generator", "dart/v2", false},
38+
{"generator", "generator/", true},
39+
}
40+
for _, test := range testCases {
41+
got := isInPath(test.Dir, test.Path)
42+
if got != test.Want {
43+
t.Errorf("got (%v) != want (%v) for %q in %q", got, test.Want, test.Dir, test.Path)
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)