path_windows_test.go 2.3 KB

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