Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, "../") {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'm not too concerned about an infinite loop here since specifying an arbitrarily large number of prev directories has to happen for that

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