diff --git a/pkg/gator/verify/read_suites.go b/pkg/gator/verify/read_suites.go index 4cbae24d931..bdbaf8a1c6d 100644 --- a/pkg/gator/verify/read_suites.go +++ b/pkg/gator/verify/read_suites.go @@ -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 { diff --git a/pkg/gator/verify/read_suites_test.go b/pkg/gator/verify/read_suites_test.go index a02bcaccb39..e0943932034 100644 --- a/pkg/gator/verify/read_suites_test.go +++ b/pkg/gator/verify/read_suites_test.go @@ -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{ @@ -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",