2022-10-08 13:28:31 +00:00
|
|
|
package archive
|
2016-04-27 02:26:12 +00:00
|
|
|
|
2017-08-04 00:22:00 +00:00
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
)
|
2016-04-27 02:26:12 +00:00
|
|
|
|
|
|
|
// TestCheckSystemDriveAndRemoveDriveLetter tests CheckSystemDriveAndRemoveDriveLetter
|
|
|
|
func TestCheckSystemDriveAndRemoveDriveLetter(t *testing.T) {
|
|
|
|
// Fails if not C drive.
|
2022-09-23 00:49:47 +00:00
|
|
|
_, err := CheckSystemDriveAndRemoveDriveLetter(`d:\`)
|
2022-10-07 21:27:47 +00:00
|
|
|
if err == nil || err.Error() != "the specified path is not on the system drive (C:)" {
|
2016-04-27 02:26:12 +00:00
|
|
|
t.Fatalf("Expected error for d:")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Single character is unchanged
|
2017-09-08 22:00:14 +00:00
|
|
|
var path string
|
2022-09-23 00:49:47 +00:00
|
|
|
if path, err = CheckSystemDriveAndRemoveDriveLetter("z"); err != nil {
|
2016-04-27 02:26:12 +00:00
|
|
|
t.Fatalf("Single character should pass")
|
|
|
|
}
|
|
|
|
if path != "z" {
|
|
|
|
t.Fatalf("Single character should be unchanged")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Two characters without colon is unchanged
|
2022-09-23 00:49:47 +00:00
|
|
|
if path, err = CheckSystemDriveAndRemoveDriveLetter("AB"); err != nil {
|
2016-04-27 02:26:12 +00:00
|
|
|
t.Fatalf("2 characters without colon should pass")
|
|
|
|
}
|
|
|
|
if path != "AB" {
|
|
|
|
t.Fatalf("2 characters without colon should be unchanged")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Abs path without drive letter
|
2022-09-23 00:49:47 +00:00
|
|
|
if path, err = CheckSystemDriveAndRemoveDriveLetter(`\l`); err != nil {
|
2016-04-27 02:26:12 +00:00
|
|
|
t.Fatalf("abs path no drive letter should pass")
|
|
|
|
}
|
|
|
|
if path != `\l` {
|
|
|
|
t.Fatalf("abs path without drive letter should be unchanged")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Abs path without drive letter, linux style
|
2022-09-23 00:49:47 +00:00
|
|
|
if path, err = CheckSystemDriveAndRemoveDriveLetter(`/l`); err != nil {
|
2016-04-27 02:26:12 +00:00
|
|
|
t.Fatalf("abs path no drive letter linux style should pass")
|
|
|
|
}
|
|
|
|
if path != `\l` {
|
|
|
|
t.Fatalf("abs path without drive letter linux failed %s", path)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Drive-colon should be stripped
|
2022-09-23 00:49:47 +00:00
|
|
|
if path, err = CheckSystemDriveAndRemoveDriveLetter(`c:\`); err != nil {
|
2016-04-27 02:26:12 +00:00
|
|
|
t.Fatalf("An absolute path should pass")
|
|
|
|
}
|
|
|
|
if path != `\` {
|
|
|
|
t.Fatalf(`An absolute path should have been shortened to \ %s`, path)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify with a linux-style path
|
2022-09-23 00:49:47 +00:00
|
|
|
if path, err = CheckSystemDriveAndRemoveDriveLetter(`c:/`); err != nil {
|
2016-04-27 02:26:12 +00:00
|
|
|
t.Fatalf("An absolute path should pass")
|
|
|
|
}
|
|
|
|
if path != `\` {
|
|
|
|
t.Fatalf(`A linux style absolute path should have been shortened to \ %s`, path)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Failure on c:
|
2022-09-23 00:49:47 +00:00
|
|
|
if path, err = CheckSystemDriveAndRemoveDriveLetter(`c:`); err == nil {
|
2016-04-27 02:26:12 +00:00
|
|
|
t.Fatalf("c: should fail")
|
|
|
|
}
|
2022-10-07 21:27:47 +00:00
|
|
|
if err.Error() != `no relative path specified in "c:"` {
|
2016-04-27 02:26:12 +00:00
|
|
|
t.Fatalf(path, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Failure on d:
|
2022-09-23 00:49:47 +00:00
|
|
|
if path, err = CheckSystemDriveAndRemoveDriveLetter(`d:`); err == nil {
|
2016-04-27 02:26:12 +00:00
|
|
|
t.Fatalf("c: should fail")
|
|
|
|
}
|
2022-10-07 21:27:47 +00:00
|
|
|
if err.Error() != `no relative path specified in "d:"` {
|
2016-04-27 02:26:12 +00:00
|
|
|
t.Fatalf(path, err)
|
|
|
|
}
|
|
|
|
}
|