path_windows_test.go 2.3 KB

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