Skip to content

Commit 69b6b4e

Browse files
committed
Fix failed windows tests that depends on cwd
Signed-off-by: Youyuan Wu <youyuanwu@outlook.com>
1 parent 285d864 commit 69b6b4e

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

normalizer_test.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,24 @@ import (
1414

1515
const windowsOS = "windows"
1616

17+
// only used for windows
18+
var currentDriveLetter = getCurrentDrive()
19+
20+
// get the current drive letter in lowercase on windows that the test is running
21+
func getCurrentDrive() string {
22+
if runtime.GOOS != windowsOS {
23+
return ""
24+
}
25+
p, err := filepath.Abs("/")
26+
if err != nil {
27+
panic(err)
28+
}
29+
if len(p) == 0 {
30+
panic("current windows drive letter is empty")
31+
}
32+
return strings.ToLower(string(p[0]))
33+
}
34+
1735
func TestNormalizer_NormalizeURI(t *testing.T) {
1836
type testNormalizePathsTestCases []struct {
1937
refPath string
@@ -299,7 +317,7 @@ func TestNormalizer_NormalizeBase(t *testing.T) {
299317
{
300318
// path clean
301319
Base: "///folder//subfolder///file.json/",
302-
Expected: "file:///c:/folder/subfolder/file.json",
320+
Expected: "file:///" + currentDriveLetter + ":/folder/subfolder/file.json",
303321
Windows: true,
304322
},
305323
{
@@ -326,7 +344,7 @@ func TestNormalizer_NormalizeBase(t *testing.T) {
326344
{
327345
// handling dots (3/6): valid, cleaned to /c:/ on windows
328346
Base: "/..",
329-
Expected: "file:///c:",
347+
Expected: "file:///" + currentDriveLetter + ":",
330348
Windows: true,
331349
},
332350
{
@@ -359,7 +377,7 @@ func TestNormalizer_NormalizeBase(t *testing.T) {
359377
// windows-only cases
360378
{
361379
Base: "/base/sub/file.json",
362-
Expected: "file:///c:/base/sub/file.json", // on windows, filepath.Abs("/a/b") prepends the "c:" drive
380+
Expected: "file:///" + currentDriveLetter + ":/base/sub/file.json", // on windows, filepath.Abs("/a/b") prepends the "c:" drive
363381
Windows: true,
364382
},
365383
{

0 commit comments

Comments
 (0)