path.test.ts 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { normalizePath, arePathsEqual } from "../path.js"
  2. // Helper to create platform-specific expected paths
  3. const expectedPath = (...segments: string[]) => {
  4. // On Windows, path.normalize converts forward slashes to backslashes
  5. // and paths like /Users become \Users (without a drive letter)
  6. if (process.platform === "win32") {
  7. return "\\" + segments.join("\\")
  8. }
  9. return "/" + segments.join("/")
  10. }
  11. describe("normalizePath", () => {
  12. it("should remove trailing slashes", () => {
  13. expect(normalizePath("/Users/test/project/")).toBe(expectedPath("Users", "test", "project"))
  14. expect(normalizePath("/Users/test/project//")).toBe(expectedPath("Users", "test", "project"))
  15. })
  16. it("should handle paths without trailing slashes", () => {
  17. expect(normalizePath("/Users/test/project")).toBe(expectedPath("Users", "test", "project"))
  18. })
  19. it("should normalize path separators", () => {
  20. // path.normalize handles this
  21. expect(normalizePath("/Users//test/project")).toBe(expectedPath("Users", "test", "project"))
  22. })
  23. })
  24. describe("arePathsEqual", () => {
  25. it("should return true for identical paths", () => {
  26. expect(arePathsEqual("/Users/test/project", "/Users/test/project")).toBe(true)
  27. })
  28. it("should return true for paths differing only by trailing slash", () => {
  29. expect(arePathsEqual("/Users/test/project", "/Users/test/project/")).toBe(true)
  30. expect(arePathsEqual("/Users/test/project/", "/Users/test/project")).toBe(true)
  31. })
  32. it("should return false for undefined or empty paths", () => {
  33. expect(arePathsEqual(undefined, "/Users/test/project")).toBe(false)
  34. expect(arePathsEqual("/Users/test/project", undefined)).toBe(false)
  35. expect(arePathsEqual(undefined, undefined)).toBe(false)
  36. expect(arePathsEqual("", "/Users/test/project")).toBe(false)
  37. expect(arePathsEqual("/Users/test/project", "")).toBe(false)
  38. })
  39. it("should return false for different paths", () => {
  40. expect(arePathsEqual("/Users/test/project1", "/Users/test/project2")).toBe(false)
  41. expect(arePathsEqual("/Users/test/project", "/Users/other/project")).toBe(false)
  42. })
  43. // Case sensitivity behavior depends on platform
  44. if (process.platform === "darwin" || process.platform === "win32") {
  45. it("should be case-insensitive on macOS/Windows", () => {
  46. expect(arePathsEqual("/Users/Test/Project", "/users/test/project")).toBe(true)
  47. expect(arePathsEqual("/USERS/TEST/PROJECT", "/Users/test/project")).toBe(true)
  48. })
  49. } else {
  50. it("should be case-sensitive on Linux", () => {
  51. expect(arePathsEqual("/Users/Test/Project", "/users/test/project")).toBe(false)
  52. })
  53. }
  54. it("should handle paths with multiple trailing slashes", () => {
  55. expect(arePathsEqual("/Users/test/project///", "/Users/test/project")).toBe(true)
  56. })
  57. })