Skip to content

Commit bb34091

Browse files
committed
Update path tests to work cross-platform
1 parent cb82b91 commit bb34091

File tree

2 files changed

+28
-20
lines changed

2 files changed

+28
-20
lines changed

src/utils/__tests__/path.test.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ describe("Path Utilities", () => {
99
Object.defineProperty(process, "platform", {
1010
value: originalPlatform,
1111
})
12+
jest.restoreAllMocks()
1213
})
1314

1415
describe("String.prototype.toPosix", () => {
@@ -78,20 +79,26 @@ describe("Path Utilities", () => {
7879
describe("edge cases", () => {
7980
it("should handle undefined paths", () => {
8081
expect(arePathsEqual(undefined, undefined)).toBe(true)
81-
expect(arePathsEqual("/test", undefined)).toBe(false)
82-
expect(arePathsEqual(undefined, "/test")).toBe(false)
8382
})
83+
expect(arePathsEqual("/test", undefined)).toBe(false)
84+
expect(arePathsEqual(undefined, "/test")).toBe(false)
85+
})
8486

85-
it("should handle root paths with trailing slashes", () => {
86-
expect(arePathsEqual("/", "/")).toBe(true)
87-
expect(arePathsEqual("C:\\", "C:\\")).toBe(true)
88-
})
87+
it("should handle root paths with trailing slashes", () => {
88+
expect(arePathsEqual("/", "/")).toBe(true)
89+
expect(arePathsEqual("C:\\", "C:\\")).toBe(true)
8990
})
9091
})
9192

9293
describe("getReadablePath", () => {
93-
const homeDir = os.homedir()
94-
const desktop = path.join(homeDir, "Desktop")
94+
beforeEach(() => {
95+
jest.spyOn(os, 'platform').mockReturnValue('linux');
96+
jest.spyOn(os, 'platform').mockReturnValue('linux');
97+
// jest.spyOn(os, 'homedir').mockReturnValue('/Users/test');
98+
})
99+
100+
// const homeDir = os.homedir()
101+
// const desktop = path.posix.join(homeDir, "Desktop")
95102

96103
it("should return basename when path equals cwd", () => {
97104
const cwd = "/Users/test/project"
@@ -111,8 +118,9 @@ describe("Path Utilities", () => {
111118
})
112119

113120
it("should handle Desktop as cwd", () => {
114-
const filePath = path.join(desktop, "file.txt")
115-
expect(getReadablePath(desktop, filePath)).toBe(filePath.toPosix())
121+
const desktop = "/Users/test/Desktop"
122+
const filePath = "/Users/test/Desktop/file.txt"
123+
expect(getReadablePath(desktop, filePath)).toBe(filePath)
116124
})
117125

118126
it("should handle undefined relative path", () => {

src/utils/path.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export function arePathsEqual(path1?: string, path2?: string): boolean {
6969

7070
function normalizePath(p: string): string {
7171
// normalize resolve ./.. segments, removes duplicate slashes, and standardizes path separators
72-
let normalized = path.normalize(p)
72+
let normalized = path.posix.normalize(p)
7373
// however it doesn't remove trailing slashes
7474
// remove trailing slash, except for root paths
7575
if (normalized.length > 1 && (normalized.endsWith("/") || normalized.endsWith("\\"))) {
@@ -81,26 +81,26 @@ function normalizePath(p: string): string {
8181
export function getReadablePath(cwd: string, relPath?: string): string {
8282
relPath = relPath || ""
8383
// path.resolve is flexible in that it will resolve relative paths like '../../' to the cwd and even ignore the cwd if the relPath is actually an absolute path
84-
const absolutePath = path.resolve(cwd, relPath)
85-
if (arePathsEqual(cwd, path.join(os.homedir(), "Desktop"))) {
84+
const absolutePath = path.posix.resolve(cwd, relPath)
85+
if (arePathsEqual(cwd, path.posix.join(os.homedir(), "Desktop"))) {
8686
// User opened vscode without a workspace, so cwd is the Desktop. Show the full absolute path to keep the user aware of where files are being created
87-
return absolutePath.toPosix()
87+
return absolutePath
8888
}
89-
if (arePathsEqual(path.normalize(absolutePath), path.normalize(cwd))) {
90-
return path.basename(absolutePath).toPosix()
89+
if (arePathsEqual(path.posix.normalize(absolutePath), path.posix.normalize(cwd))) {
90+
return path.posix.basename(absolutePath)
9191
} else {
9292
// show the relative path to the cwd
93-
const normalizedRelPath = path.relative(cwd, absolutePath)
93+
const normalizedRelPath = path.posix.relative(cwd, absolutePath)
9494
if (absolutePath.includes(cwd)) {
95-
return normalizedRelPath.toPosix()
95+
return normalizedRelPath
9696
} else {
9797
// we are outside the cwd, so show the absolute path (useful for when cline passes in '../../' for example)
98-
return absolutePath.toPosix()
98+
return absolutePath
9999
}
100100
}
101101
}
102102

103103
export const toRelativePath = (filePath: string, cwd: string) => {
104-
const relativePath = path.relative(cwd, filePath).toPosix()
104+
const relativePath = path.posix.relative(cwd, filePath)
105105
return filePath.endsWith("/") ? relativePath + "/" : relativePath
106106
}

0 commit comments

Comments
 (0)