Skip to content

Commit

Permalink
chore: absolute pattern in exclusions
Browse files Browse the repository at this point in the history
  • Loading branch information
gabotechs committed Feb 10, 2024
1 parent 80a193d commit b95ebdb
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 7 deletions.
10 changes: 9 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,15 @@ func loadConfig() (*config.Config, error) {
cfg.Python.IgnoreFromImportsAsExports = true
cfg.Python.IgnoreDirectoryImports = true

// TODO: the excluded list should be relative to the CWD
absExclude := make([]string, len(exclude))
for i, file := range exclude {
if !filepath.IsAbs(file) {
cwd, _ := os.Getwd()
absExclude[i] = filepath.Join(cwd, file)
} else {
absExclude[i] = file
}
}
cfg.Exclude = append(cfg.Exclude, exclude...)
// validate exclusion patterns.
for _, exclusion := range cfg.Exclude {
Expand Down
5 changes: 5 additions & 0 deletions internal/config/.config_test/.excludes.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
exclude:
- '**/foo.js'
- '*/foo.js'
- 'foo/**/foo.js'
- '/**/*.js'
16 changes: 15 additions & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ func ParseConfig(cfgPath string) (*Config, error) {
cfgPath = DefaultConfigPath
}
content, err := os.ReadFile(cfgPath)
// If a specific path was requested, and it does not exist, fail
// If no specific path was requested, and the default config path does not exist, succeed
if os.IsNotExist(err) {
if !isDefault {
return &cfg, err
Expand All @@ -71,14 +73,26 @@ func ParseConfig(cfgPath string) (*Config, error) {
return &cfg, err
}
cfg.Path = filepath.Dir(absCfgPath)
// TODO: The exclusion list should be either absolute glob paths or relative to the config file.

decoder := yaml.NewDecoder(bytes.NewReader(content))
decoder.KnownFields(true)
err = decoder.Decode(&cfg)
if err != nil {
return &cfg, fmt.Errorf(`config file "%s" is not a valid yml file: %w`, cfgPath, err)
}

exclude := make([]string, len(cfg.Exclude))
for i, pattern := range cfg.Exclude {
if !filepath.IsAbs(pattern) {
exclude[i] = filepath.Join(cfg.Path, pattern)
} else {
exclude[i] = pattern
}
}
if len(exclude) > 0 {
cfg.Exclude = exclude
}

cfg.Check.Init(filepath.Dir(absCfgPath))
return &cfg, nil
}
14 changes: 14 additions & 0 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ import (
const testFolder = ".config_test"

func TestParseConfig(t *testing.T) {
absTestFolder, _ := filepath.Abs(testFolder)

tests := []struct {
Name string
File string
ExpectedWhiteList map[string][]string
ExpectedBlackList map[string][]string
ExpectedExclude []string
}{
{
Name: "default file",
Expand Down Expand Up @@ -52,6 +55,16 @@ func TestParseConfig(t *testing.T) {
},
},
},
{
Name: "Exclusion",
File: ".excludes.yml",
ExpectedExclude: []string{
filepath.Join(absTestFolder, "**/foo.js"),
filepath.Join(absTestFolder, "*/foo.js"),
filepath.Join(absTestFolder, "foo/**/foo.js"),
"/**/*.js",
},
},
}

for _, tt := range tests {
Expand All @@ -66,6 +79,7 @@ func TestParseConfig(t *testing.T) {

a.Equal(tt.ExpectedWhiteList, cfg.Check.WhiteList)
a.Equal(tt.ExpectedBlackList, cfg.Check.BlackList)
a.Equal(tt.ExpectedExclude, cfg.Exclude)
})
}
}
Expand Down
5 changes: 0 additions & 5 deletions internal/language/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,6 @@ func NewParser(lang Language) *Parser {

var _ graph.NodeParser[*FileInfo] = &Parser{}

type Config interface {
UnwrapProxyExports() bool
IgnoreFiles() []string
}

func (p *Parser) shouldExclude(path string) bool {
for _, exclusion := range p.Exclude {
if ok, _ := utils.GlobstarMatch(exclusion, path); ok {
Expand Down
54 changes: 54 additions & 0 deletions internal/language/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,60 @@ import (
"github.com/stretchr/testify/require"
)

func TestParser_shouldExclude(t *testing.T) {
tests := []struct {
Name string
Paths []string
Exclude []string
Expected []string
}{
{
Name: "simple",
Paths: []string{"/foo/bar.ts", "/foo/baz.ts"},
Exclude: []string{"/foo/bar.ts"},
Expected: []string{"/foo/baz.ts"},
},
{
Name: "globstar",
Paths: []string{"/foo/bar.ts", "/foo/baz.ts"},
Exclude: []string{"/foo/*.ts"},
Expected: nil,
},
{
Name: "globstar 2",
Paths: []string{"/foo/1/2/3/4/bar.ts", "/foo/baz.ts"},
Exclude: []string{"/foo/**/*.ts"},
Expected: nil,
},
{
Name: "globstar 3",
Paths: []string{"/foo/1/2/3/4/bar.ts", "/foo/baz.ts"},
Exclude: []string{"2/**/*.ts"},
Expected: []string{"/foo/1/2/3/4/bar.ts", "/foo/baz.ts"},
},
{
Name: "globstar 4",
Paths: []string{"/foo/1/2/3/4/bar.ts", "/foo/baz.ts"},
Exclude: []string{"*/2/**/*.ts"},
Expected: []string{"/foo/1/2/3/4/bar.ts", "/foo/baz.ts"},
},
}

for _, tt := range tests {
t.Run(tt.Name, func(t *testing.T) {
a := require.New(t)
parser := Parser{Exclude: tt.Exclude}
var result []string
for _, path := range tt.Paths {
if !parser.shouldExclude(path) {
result = append(result, path)
}
}
a.Equal(tt.Expected, result)
})
}
}

func TestParser_Deps(t *testing.T) {
tests := []struct {
Name string
Expand Down

0 comments on commit b95ebdb

Please sign in to comment.