Skip to content

Commit

Permalink
Update path tests to work cross-platform
Browse files Browse the repository at this point in the history
  • Loading branch information
nissa-seru committed Feb 5, 2025
1 parent cb82b91 commit bb34091
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 20 deletions.
28 changes: 18 additions & 10 deletions src/utils/__tests__/path.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ describe("Path Utilities", () => {
Object.defineProperty(process, "platform", {
value: originalPlatform,
})
jest.restoreAllMocks()
})

describe("String.prototype.toPosix", () => {
Expand Down Expand Up @@ -78,20 +79,26 @@ describe("Path Utilities", () => {
describe("edge cases", () => {
it("should handle undefined paths", () => {
expect(arePathsEqual(undefined, undefined)).toBe(true)
expect(arePathsEqual("/test", undefined)).toBe(false)
expect(arePathsEqual(undefined, "/test")).toBe(false)
})
expect(arePathsEqual("/test", undefined)).toBe(false)
expect(arePathsEqual(undefined, "/test")).toBe(false)
})

it("should handle root paths with trailing slashes", () => {
expect(arePathsEqual("/", "/")).toBe(true)
expect(arePathsEqual("C:\\", "C:\\")).toBe(true)
})
it("should handle root paths with trailing slashes", () => {
expect(arePathsEqual("/", "/")).toBe(true)
expect(arePathsEqual("C:\\", "C:\\")).toBe(true)
})
})

describe("getReadablePath", () => {
const homeDir = os.homedir()
const desktop = path.join(homeDir, "Desktop")
beforeEach(() => {
jest.spyOn(os, 'platform').mockReturnValue('linux');
jest.spyOn(os, 'platform').mockReturnValue('linux');
// jest.spyOn(os, 'homedir').mockReturnValue('/Users/test');
})

// const homeDir = os.homedir()
// const desktop = path.posix.join(homeDir, "Desktop")

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

it("should handle Desktop as cwd", () => {
const filePath = path.join(desktop, "file.txt")
expect(getReadablePath(desktop, filePath)).toBe(filePath.toPosix())
const desktop = "/Users/test/Desktop"
const filePath = "/Users/test/Desktop/file.txt"
expect(getReadablePath(desktop, filePath)).toBe(filePath)
})

it("should handle undefined relative path", () => {
Expand Down
20 changes: 10 additions & 10 deletions src/utils/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export function arePathsEqual(path1?: string, path2?: string): boolean {

function normalizePath(p: string): string {
// normalize resolve ./.. segments, removes duplicate slashes, and standardizes path separators
let normalized = path.normalize(p)
let normalized = path.posix.normalize(p)
// however it doesn't remove trailing slashes
// remove trailing slash, except for root paths
if (normalized.length > 1 && (normalized.endsWith("/") || normalized.endsWith("\\"))) {
Expand All @@ -81,26 +81,26 @@ function normalizePath(p: string): string {
export function getReadablePath(cwd: string, relPath?: string): string {
relPath = relPath || ""
// 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
const absolutePath = path.resolve(cwd, relPath)
if (arePathsEqual(cwd, path.join(os.homedir(), "Desktop"))) {
const absolutePath = path.posix.resolve(cwd, relPath)
if (arePathsEqual(cwd, path.posix.join(os.homedir(), "Desktop"))) {
// 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
return absolutePath.toPosix()
return absolutePath
}
if (arePathsEqual(path.normalize(absolutePath), path.normalize(cwd))) {
return path.basename(absolutePath).toPosix()
if (arePathsEqual(path.posix.normalize(absolutePath), path.posix.normalize(cwd))) {
return path.posix.basename(absolutePath)
} else {
// show the relative path to the cwd
const normalizedRelPath = path.relative(cwd, absolutePath)
const normalizedRelPath = path.posix.relative(cwd, absolutePath)
if (absolutePath.includes(cwd)) {
return normalizedRelPath.toPosix()
return normalizedRelPath
} else {
// we are outside the cwd, so show the absolute path (useful for when cline passes in '../../' for example)
return absolutePath.toPosix()
return absolutePath
}
}
}

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

0 comments on commit bb34091

Please sign in to comment.