Merge pull request #9746 from neoaggelos/fix/config-glob

Fix config import relative path glob
This commit is contained in:
Derek McGowan 2024-02-16 06:08:17 +00:00 committed by GitHub
commit d346aeebdf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 5 deletions

View File

@ -373,6 +373,11 @@ func resolveImports(parent string, imports []string) ([]string, error) {
var out []string var out []string
for _, path := range imports { for _, path := range imports {
path = filepath.Clean(path)
if !filepath.IsAbs(path) {
path = filepath.Join(filepath.Dir(parent), path)
}
if strings.Contains(path, "*") { if strings.Contains(path, "*") {
matches, err := filepath.Glob(path) matches, err := filepath.Glob(path)
if err != nil { if err != nil {
@ -381,11 +386,6 @@ func resolveImports(parent string, imports []string) ([]string, error) {
out = append(out, matches...) out = append(out, matches...)
} else { } else {
path = filepath.Clean(path)
if !filepath.IsAbs(path) {
path = filepath.Join(filepath.Dir(parent), path)
}
out = append(out, path) out = append(out, path)
} }
} }

View File

@ -105,6 +105,17 @@ func TestResolveImports(t *testing.T) {
filepath.Join(tempDir, "test.toml"), filepath.Join(tempDir, "test.toml"),
filepath.Join(tempDir, "current.toml"), filepath.Join(tempDir, "current.toml"),
}) })
t.Run("GlobRelativePath", func(t *testing.T) {
imports, err := resolveImports(filepath.Join(tempDir, "root.toml"), []string{
"config_*.toml", // Glob files from working dir
})
assert.NoError(t, err)
assert.Equal(t, imports, []string{
filepath.Join(tempDir, "config_1.toml"),
filepath.Join(tempDir, "config_2.toml"),
})
})
} }
func TestLoadSingleConfig(t *testing.T) { func TestLoadSingleConfig(t *testing.T) {