expandpath_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package parser
  2. import (
  3. "os"
  4. "os/user"
  5. "path/filepath"
  6. "runtime"
  7. "testing"
  8. )
  9. func TestExpandPath(t *testing.T) {
  10. mockCurrentUser := func() (*user.User, error) {
  11. return &user.User{
  12. Username: "testuser",
  13. HomeDir: func() string {
  14. if os.PathSeparator == '\\' {
  15. return filepath.FromSlash("D:/home/testuser")
  16. }
  17. return "/home/testuser"
  18. }(),
  19. }, nil
  20. }
  21. mockLookupUser := func(username string) (*user.User, error) {
  22. fakeUsers := map[string]string{
  23. "testuser": func() string {
  24. if os.PathSeparator == '\\' {
  25. return filepath.FromSlash("D:/home/testuser")
  26. }
  27. return "/home/testuser"
  28. }(),
  29. "anotheruser": func() string {
  30. if os.PathSeparator == '\\' {
  31. return filepath.FromSlash("D:/home/anotheruser")
  32. }
  33. return "/home/anotheruser"
  34. }(),
  35. }
  36. if homeDir, ok := fakeUsers[username]; ok {
  37. return &user.User{
  38. Username: username,
  39. HomeDir: homeDir,
  40. }, nil
  41. }
  42. return nil, os.ErrNotExist
  43. }
  44. pwd, err := os.Getwd()
  45. if err != nil {
  46. t.Fatal(err)
  47. }
  48. t.Run("unix tests", func(t *testing.T) {
  49. if runtime.GOOS == "windows" {
  50. return
  51. }
  52. tests := []struct {
  53. path string
  54. relativeDir string
  55. expected string
  56. shouldErr bool
  57. }{
  58. {"~", "", "/home/testuser", false},
  59. {"~/myfolder/myfile.txt", "", "/home/testuser/myfolder/myfile.txt", false},
  60. {"~anotheruser/docs/file.txt", "", "/home/anotheruser/docs/file.txt", false},
  61. {"~nonexistentuser/file.txt", "", "", true},
  62. {"relative/path/to/file", "", filepath.Join(pwd, "relative/path/to/file"), false},
  63. {"/absolute/path/to/file", "", "/absolute/path/to/file", false},
  64. {"/absolute/path/to/file", "someotherdir/", "/absolute/path/to/file", false},
  65. {".", pwd, pwd, false},
  66. {".", "", pwd, false},
  67. {"somefile", "somedir", filepath.Join(pwd, "somedir", "somefile"), false},
  68. }
  69. for _, test := range tests {
  70. result, err := expandPathImpl(test.path, test.relativeDir, mockCurrentUser, mockLookupUser)
  71. if (err != nil) != test.shouldErr {
  72. t.Errorf("expandPathImpl(%q) returned error: %v, expected error: %v", test.path, err != nil, test.shouldErr)
  73. }
  74. if result != test.expected && !test.shouldErr {
  75. t.Errorf("expandPathImpl(%q) = %q, want %q", test.path, result, test.expected)
  76. }
  77. }
  78. })
  79. t.Run("windows tests", func(t *testing.T) {
  80. if runtime.GOOS != "windows" {
  81. return
  82. }
  83. tests := []struct {
  84. path string
  85. relativeDir string
  86. expected string
  87. shouldErr bool
  88. }{
  89. {"~", "", "D:\\home\\testuser", false},
  90. {"~/myfolder/myfile.txt", "", "D:\\home\\testuser\\myfolder\\myfile.txt", false},
  91. {"~anotheruser/docs/file.txt", "", "D:\\home\\anotheruser\\docs\\file.txt", false},
  92. {"~nonexistentuser/file.txt", "", "", true},
  93. {"relative\\path\\to\\file", "", filepath.Join(pwd, "relative\\path\\to\\file"), false},
  94. {"D:\\absolute\\path\\to\\file", "", "D:\\absolute\\path\\to\\file", false},
  95. {"D:\\absolute\\path\\to\\file", "someotherdir/", "D:\\absolute\\path\\to\\file", false},
  96. {".", pwd, pwd, false},
  97. {".", "", pwd, false},
  98. {"somefile", "somedir", filepath.Join(pwd, "somedir", "somefile"), false},
  99. }
  100. for _, test := range tests {
  101. result, err := expandPathImpl(test.path, test.relativeDir, mockCurrentUser, mockLookupUser)
  102. if (err != nil) != test.shouldErr {
  103. t.Errorf("expandPathImpl(%q) returned error: %v, expected error: %v", test.path, err != nil, test.shouldErr)
  104. }
  105. if result != test.expected && !test.shouldErr {
  106. t.Errorf("expandPathImpl(%q) = %q, want %q", test.path, result, test.expected)
  107. }
  108. }
  109. })
  110. }