Skip to content

Commit

Permalink
Merge pull request #341 from mykso/normalize-paths
Browse files Browse the repository at this point in the history
fix: clean paths after reading config
  • Loading branch information
kumaritanushree authored Jan 30, 2024
2 parents d1df25f + 52843a3 commit 727be59
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
10 changes: 10 additions & 0 deletions pkg/vendir/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func NewConfigFromFiles(paths []string) (Config, []Secret, []ConfigMap, error) {

case res.APIVersion == knownAPIVersion && res.Kind == knownKind:
config, err := NewConfigFromBytes(docBytes)
config.cleanPaths()
if err != nil {
return fmt.Errorf("Unmarshaling config: %s", err)
}
Expand Down Expand Up @@ -278,3 +279,12 @@ func (c Config) checkOverlappingPaths() error {

return nil
}

func (c *Config) cleanPaths() {
for i, dir := range c.Directories {
c.Directories[i].Path = filepath.Clean(dir.Path)
for j, con := range dir.Contents {
c.Directories[i].Contents[j].Path = filepath.Clean(con.Path)
}
}
}
68 changes: 68 additions & 0 deletions pkg/vendir/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,74 @@ kind: Config`)
})
}

func TestCleanPaths(t *testing.T) {
cases := []struct {
name string
input string
checkFn func(config.Config)
}{
{
name: "single directory and single contents",
input: `
apiVersion: vendir.k14s.io/v1alpha1
kind: Config
directories:
- path: vendor/foo/..//
contents:
- path: bar//baz///../
inline:
paths:
file.txt: File contents
`,
checkFn: func(cfg config.Config) {
require.Equal(t, "vendor", cfg.Directories[0].Path)
require.Equal(t, "bar", cfg.Directories[0].Contents[0].Path)
},
},
{
name: "multiple directories and multiple contents",
input: `
apiVersion: vendir.k14s.io/v1alpha1
kind: Config
directories:
- path: vendor/foo/
contents:
- path: bar///baz/.
inline:
paths:
file.txt: File contents
- path: lorem/ipsum
inline:
paths:
file.txt: File contents
- path: vendor//../vendor/bar
contents:
- path: baz
inline:
paths:
file.txt: File contents
`,
checkFn: func(cfg config.Config) {
require.Equal(t, "vendor/foo", cfg.Directories[0].Path)
require.Equal(t, "bar/baz", cfg.Directories[0].Contents[0].Path)
require.Equal(t, "lorem/ipsum", cfg.Directories[0].Contents[1].Path)
require.Equal(t, "vendor/bar", cfg.Directories[1].Path)
require.Equal(t, "baz", cfg.Directories[1].Contents[0].Path)
},
},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
tmpFile := filepath.Join(t.TempDir(), "config.yml")
require.NoError(t, os.WriteFile(tmpFile, []byte(tc.input), 0600))
cfg, _, _, err := config.NewConfigFromFiles([]string{tmpFile})
require.NoError(t, err)
tc.checkFn(cfg)
})
}
}

func TestSecretsForNewConfigFromFiles(t *testing.T) {
t.Run("Config with single secret", func(t *testing.T) {
tempConfigPath := filepath.Join(t.TempDir(), "config.yml")
Expand Down

0 comments on commit 727be59

Please sign in to comment.