-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2ef54dd
commit de774d3
Showing
8 changed files
with
86 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package filesystem | ||
|
||
import ( | ||
"net/url" | ||
"path/filepath" | ||
) | ||
|
||
func URIFromPath(path string) string { | ||
p := filepath.ToSlash(path) | ||
p = wrapPath(p) | ||
|
||
u := &url.URL{ | ||
Scheme: "file", | ||
Path: p, | ||
} | ||
return u.String() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// +build !windows | ||
|
||
package filesystem | ||
|
||
// wrapPath is no-op for unix-style paths | ||
func wrapPath(path string) string { | ||
return path | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// +build !windows | ||
|
||
package filesystem | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestURIFromPath(t *testing.T) { | ||
path := "/random/path" | ||
uri := URIFromPath(path) | ||
|
||
expectedURI := "file:///random/path" | ||
if uri != expectedURI { | ||
t.Fatalf("URI doesn't match.\nExpected: %q\nGiven: %q", | ||
expectedURI, uri) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package filesystem | ||
|
||
// wrapPath prepends Windows-style paths (C:\path) | ||
// with an additional slash to account for an empty hostname | ||
// in a valid file-scheme URI per RFC 8089 | ||
func wrapPath(path string) string { | ||
return "/" + path | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package filesystem | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestURIFromPath(t *testing.T) { | ||
path := `C:\Users\With Space\file.tf` | ||
uri := URIFromPath(path) | ||
|
||
expectedURI := "file:///C:/Users/With%20Space/file.tf" | ||
if uri != expectedURI { | ||
t.Fatalf("URI doesn't match.\nExpected: %q\nGiven: %q", | ||
expectedURI, uri) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters