diff --git a/loader/loader.go b/loader/loader.go index 461639ed19..8a838afb62 100644 --- a/loader/loader.go +++ b/loader/loader.go @@ -582,7 +582,9 @@ func SplitPrefix(path string) ([]string, string) { } parts := strings.SplitN(path, ":", 2) if len(parts) == 2 && len(parts[0]) > 0 { - return strings.Split(parts[0], "."), parts[1] + if strings.HasPrefix(parts[1], "file:///") || len(parts[0]) > 1 { + return strings.Split(parts[0], "."), parts[1] + } } return nil, path } diff --git a/loader/loader_test.go b/loader/loader_test.go index 01c4d2e21e..952690f87e 100644 --- a/loader/loader_test.go +++ b/loader/loader_test.go @@ -15,6 +15,7 @@ import ( "path" "path/filepath" "reflect" + "runtime" "sort" "strings" "testing" @@ -808,15 +809,19 @@ func TestLoadFileURL(t *testing.T) { files := map[string]string{ "/a/a/1.json": `1`, // this will load as a directory (e.g., file://a/a) "b.json": `{"b": 2}`, // this will load as a normal file - "c.json": `3`, // this will loas as rooted file + "c.json": `3`, // this will load as rooted file + "d.json": `{"d": 4}`, // this will load as a normal file without the prefix file:/// } test.WithTempFS(files, func(rootDir string) { paths := mustListPaths(rootDir, false)[1:] sort.Strings(paths) - - for i := range paths { - paths[i] = "file://" + paths[i] + fileURLPrefix := "file://" + if IsWindows() { + fileURLPrefix = "file:///" + } + for i := range paths[:3] { + paths[i] = fileURLPrefix + paths[i] } paths[2] = "c:" + paths[2] @@ -826,13 +831,18 @@ func TestLoadFileURL(t *testing.T) { t.Fatal(err) } - exp := parseJSON(`{"a": 1, "b": 2, "c": 3}`) + exp := parseJSON(`{"a": 1, "b": 2, "c": 3, "d":4}`) if !reflect.DeepEqual(exp, result.Documents) { t.Fatalf("Expected %v but got %v", exp, result.Documents) } }) } +// IsWindows: checks if the user's OS is Windows +func IsWindows() bool { + return runtime.GOOS == "windows" +} + func TestUnsupportedURLScheme(t *testing.T) { _, err := NewFileLoader().All([]string{"http://openpolicyagent.org"}) if err == nil || !strings.Contains(err.Error(), "unsupported URL scheme: http://openpolicyagent.org") { @@ -879,6 +889,15 @@ func TestSplitPrefix(t *testing.T) { wantParts: []string{"x", "y"}, wantPath: "file:///c:/a/b/c", }, + { + input: "c:/a/b/c", + wantPath: "c:/a/b/c", + }, + { + input: "c:file:///c:/a/b/c", + wantParts: []string{"c"}, + wantPath: "file:///c:/a/b/c", + }, } for _, tc := range tests {