Skip to content

Commit

Permalink
fix: cutpath for ../ paths (#2498)
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Pana <8968914+acpana@users.noreply.github.com>

Signed-off-by: Alex Pana <8968914+acpana@users.noreply.github.com>
Co-authored-by: Sertaç Özercan <852750+sozercan@users.noreply.github.com>
  • Loading branch information
acpana and sozercan authored Jan 6, 2023
1 parent c61db24 commit 923a183
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
5 changes: 4 additions & 1 deletion pkg/gator/verify/read_suites.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,14 @@ func readSuites(f fs.FS, files []string, originalPath string) ([]*Suite, error)
if suite != nil {
suite.AbsolutePath = file

// trim any prefixes like "/" or "./" in order for the
// trim any prefixes like "/", "./" or "../" in order for the
// .Cut call below to actually work with the absolute path
// contained in the file var.
cutPath := strings.TrimPrefix(originalPath, "/")
cutPath = strings.TrimPrefix(cutPath, "./")
for strings.HasPrefix(cutPath, "../") {
cutPath = strings.TrimPrefix(cutPath, "../")
}

_, after, found := strings.Cut(file, cutPath)
if !found {
Expand Down
64 changes: 64 additions & 0 deletions pkg/gator/verify/read_suites_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,41 @@ apiVersion: test.gatekeeper.sh/v1alpha1
want: []*Suite{{AbsolutePath: "test.yaml"}},
wantErr: nil,
},
{
name: "single target absolute path",
target: "test.yaml",
originalPath: "/test.yaml",
recursive: false,
fileSystem: fstest.MapFS{
"test.yaml": &fstest.MapFile{
Data: []byte(`
kind: Suite
apiVersion: test.gatekeeper.sh/v1alpha1
`),
},
},
want: []*Suite{{AbsolutePath: "test.yaml", InputPath: "/test.yaml"}},
wantErr: nil,
},
{
name: "single target relative path",
target: "test.yaml",
originalPath: "test.yaml",
recursive: false,
fileSystem: fstest.MapFS{
"test.yaml": &fstest.MapFile{
Data: []byte(`
kind: Suite
apiVersion: test.gatekeeper.sh/v1alpha1
`),
},
},
want: []*Suite{{AbsolutePath: "test.yaml", InputPath: "test.yaml"}},
wantErr: nil,
},
{
name: "single target relative path ./",
target: "test.yaml",
originalPath: "./test.yaml",
recursive: false,
fileSystem: fstest.MapFS{
Expand All @@ -91,6 +123,38 @@ apiVersion: test.gatekeeper.sh/v1alpha1
want: []*Suite{{AbsolutePath: "test.yaml", InputPath: "./test.yaml"}},
wantErr: nil,
},
{
name: "single target relative path ../",
target: "test.yaml",
originalPath: "../test.yaml",
recursive: false,
fileSystem: fstest.MapFS{
"test.yaml": &fstest.MapFile{
Data: []byte(`
kind: Suite
apiVersion: test.gatekeeper.sh/v1alpha1
`),
},
},
want: []*Suite{{AbsolutePath: "test.yaml", InputPath: "../test.yaml"}},
wantErr: nil,
},
{
name: "single target relative path ../../",
target: "test.yaml",
originalPath: "../../test.yaml",
recursive: false,
fileSystem: fstest.MapFS{
"test.yaml": &fstest.MapFile{
Data: []byte(`
kind: Suite
apiVersion: test.gatekeeper.sh/v1alpha1
`),
},
},
want: []*Suite{{AbsolutePath: "test.yaml", InputPath: "../../test.yaml"}},
wantErr: nil,
},
{
name: "invalid filepath",
target: "test/../test/test.yaml",
Expand Down

0 comments on commit 923a183

Please sign in to comment.