github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/pkg/system/path_windows_test.go (about) 1 // +build windows 2 3 package system // import "github.com/demonoid81/moby/pkg/system" 4 5 import ( 6 "testing" 7 8 "github.com/containerd/continuity/pathdriver" 9 ) 10 11 // TestCheckSystemDriveAndRemoveDriveLetter tests CheckSystemDriveAndRemoveDriveLetter 12 func TestCheckSystemDriveAndRemoveDriveLetter(t *testing.T) { 13 // Fails if not C drive. 14 _, err := CheckSystemDriveAndRemoveDriveLetter(`d:\`, pathdriver.LocalPathDriver) 15 if err == nil || err.Error() != "The specified path is not on the system drive (C:)" { 16 t.Fatalf("Expected error for d:") 17 } 18 19 // Single character is unchanged 20 var path string 21 if path, err = CheckSystemDriveAndRemoveDriveLetter("z", pathdriver.LocalPathDriver); err != nil { 22 t.Fatalf("Single character should pass") 23 } 24 if path != "z" { 25 t.Fatalf("Single character should be unchanged") 26 } 27 28 // Two characters without colon is unchanged 29 if path, err = CheckSystemDriveAndRemoveDriveLetter("AB", pathdriver.LocalPathDriver); err != nil { 30 t.Fatalf("2 characters without colon should pass") 31 } 32 if path != "AB" { 33 t.Fatalf("2 characters without colon should be unchanged") 34 } 35 36 // Abs path without drive letter 37 if path, err = CheckSystemDriveAndRemoveDriveLetter(`\l`, pathdriver.LocalPathDriver); err != nil { 38 t.Fatalf("abs path no drive letter should pass") 39 } 40 if path != `\l` { 41 t.Fatalf("abs path without drive letter should be unchanged") 42 } 43 44 // Abs path without drive letter, linux style 45 if path, err = CheckSystemDriveAndRemoveDriveLetter(`/l`, pathdriver.LocalPathDriver); err != nil { 46 t.Fatalf("abs path no drive letter linux style should pass") 47 } 48 if path != `\l` { 49 t.Fatalf("abs path without drive letter linux failed %s", path) 50 } 51 52 // Drive-colon should be stripped 53 if path, err = CheckSystemDriveAndRemoveDriveLetter(`c:\`, pathdriver.LocalPathDriver); err != nil { 54 t.Fatalf("An absolute path should pass") 55 } 56 if path != `\` { 57 t.Fatalf(`An absolute path should have been shortened to \ %s`, path) 58 } 59 60 // Verify with a linux-style path 61 if path, err = CheckSystemDriveAndRemoveDriveLetter(`c:/`, pathdriver.LocalPathDriver); err != nil { 62 t.Fatalf("An absolute path should pass") 63 } 64 if path != `\` { 65 t.Fatalf(`A linux style absolute path should have been shortened to \ %s`, path) 66 } 67 68 // Failure on c: 69 if path, err = CheckSystemDriveAndRemoveDriveLetter(`c:`, pathdriver.LocalPathDriver); err == nil { 70 t.Fatalf("c: should fail") 71 } 72 if err.Error() != `No relative path specified in "c:"` { 73 t.Fatalf(path, err) 74 } 75 76 // Failure on d: 77 if path, err = CheckSystemDriveAndRemoveDriveLetter(`d:`, pathdriver.LocalPathDriver); err == nil { 78 t.Fatalf("c: should fail") 79 } 80 if err.Error() != `No relative path specified in "d:"` { 81 t.Fatalf(path, err) 82 } 83 }