Skip to content

Commit ab420b8

Browse files
authored
Merge pull request #16 from arduino/per1234/valid-windows-file-uri
Make schema.pathURI() produce specification-compliant URIs from Windows paths
2 parents a8fd4ac + c7241eb commit ab420b8

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

check/checkdata/schema/schema.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,15 @@ func ValidationErrorMatch(typeQuery string, fieldQuery string, descriptionQueryR
9090

9191
// pathURI returns the URI representation of the path argument.
9292
func pathURI(path *paths.Path) string {
93-
uriFriendlyPath := filepath.ToSlash(path.String())
93+
absolutePath, err := path.Abs()
94+
if err != nil {
95+
panic(err.Error())
96+
}
97+
uriFriendlyPath := filepath.ToSlash(absolutePath.String())
98+
// In order to be valid, the path in the URI must start with `/`, but Windows paths do not.
99+
if uriFriendlyPath[0] != '/' {
100+
uriFriendlyPath = "/" + uriFriendlyPath
101+
}
94102
pathURI := url.URL{
95103
Scheme: "file",
96104
Path: uriFriendlyPath,

check/checkdata/schema/schema_test.go

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package schema
2+
3+
import (
4+
"runtime"
5+
"testing"
6+
7+
"github.com/arduino/go-paths-helper"
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func TestPathURI(t *testing.T) {
12+
switch runtime.GOOS {
13+
case "windows":
14+
require.Equal(t, "file:///c:/foo%20bar", pathURI(paths.New("c:/foo bar")))
15+
default:
16+
require.Equal(t, "file:///foo%20bar", pathURI(paths.New("/foo bar")))
17+
}
18+
}

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ require (
77
github.com/arduino/go-paths-helper v1.3.2
88
github.com/arduino/go-properties-orderedmap v1.4.0
99
github.com/sirupsen/logrus v1.6.0
10+
github.com/stretchr/testify v1.6.1
1011
github.com/xeipuuv/gojsonschema v1.2.0
1112
)

0 commit comments

Comments
 (0)