Skip to content

Make schema.pathURI() produce specification-compliant URIs from Windows paths #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 6, 2020
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
10 changes: 9 additions & 1 deletion check/checkdata/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,15 @@ func ValidationErrorMatch(typeQuery string, fieldQuery string, descriptionQueryR

// pathURI returns the URI representation of the path argument.
func pathURI(path *paths.Path) string {
uriFriendlyPath := filepath.ToSlash(path.String())
absolutePath, err := path.Abs()
if err != nil {
panic(err.Error())
}
uriFriendlyPath := filepath.ToSlash(absolutePath.String())
// In order to be valid, the path in the URI must start with `/`, but Windows paths do not.
if uriFriendlyPath[0] != '/' {
uriFriendlyPath = "/" + uriFriendlyPath
}
pathURI := url.URL{
Scheme: "file",
Path: uriFriendlyPath,
Expand Down
18 changes: 18 additions & 0 deletions check/checkdata/schema/schema_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package schema

import (
"runtime"
"testing"

"github.com/arduino/go-paths-helper"
"github.com/stretchr/testify/require"
)

func TestPathURI(t *testing.T) {
switch runtime.GOOS {
case "windows":
require.Equal(t, "file:///c:/foo%20bar", pathURI(paths.New("c:/foo bar")))
default:
require.Equal(t, "file:///foo%20bar", pathURI(paths.New("/foo bar")))
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ require (
github.com/arduino/go-paths-helper v1.3.2
github.com/arduino/go-properties-orderedmap v1.4.0
github.com/sirupsen/logrus v1.6.0
github.com/stretchr/testify v1.6.1
github.com/xeipuuv/gojsonschema v1.2.0
)