From f0f84f8086b4add3f1f94c66719bc6575d9658c6 Mon Sep 17 00:00:00 2001 From: hannesrudolph Date: Wed, 2 Jul 2025 15:51:18 -0600 Subject: [PATCH] fix: make path tests platform-agnostic for Windows compatibility - Updated workspace-utils tests to use path.normalize() and path.join() - Updated get-relative-path tests to handle platform-specific path separators - Fixed failing Windows CI tests by ensuring cross-platform path handling --- .../__tests__/get-relative-path.spec.ts | 47 ++++++++------- .../shared/__tests__/workspace-utils.spec.ts | 58 ++++++++++++------- 2 files changed, 64 insertions(+), 41 deletions(-) diff --git a/src/services/code-index/shared/__tests__/get-relative-path.spec.ts b/src/services/code-index/shared/__tests__/get-relative-path.spec.ts index 432bae4b42..2b1cfe27e9 100644 --- a/src/services/code-index/shared/__tests__/get-relative-path.spec.ts +++ b/src/services/code-index/shared/__tests__/get-relative-path.spec.ts @@ -50,14 +50,16 @@ describe("get-relative-path", () => { }) it("should return normalized absolute paths unchanged", () => { - const absolutePath = "/some/absolute/path/file.ts" + // Use path.resolve to create a proper absolute path for the current platform + const absolutePath = path.resolve("/some/absolute/path/file.ts") const result = generateNormalizedAbsolutePath(absolutePath) - expect(result).toBe(path.normalize(absolutePath)) + expect(result).toBe(absolutePath) }) it("should use custom workspace root when provided", () => { - const result = generateNormalizedAbsolutePath("src/file.ts", "/custom/workspace") - const expected = path.join("/custom/workspace", "src/file.ts") + const customWorkspace = path.normalize("/custom/workspace") + const result = generateNormalizedAbsolutePath("src/file.ts", customWorkspace) + const expected = path.join(customWorkspace, "src/file.ts") expect(result).toBe(expected) }) @@ -71,23 +73,26 @@ describe("get-relative-path", () => { describe("generateRelativeFilePath", () => { it("should generate relative path when workspace root is provided", () => { - const absolutePath = "/custom/workspace/src/file.ts" - const result = generateRelativeFilePath(absolutePath, "/custom/workspace") - expect(result).toBe(path.normalize("src/file.ts")) + const workspaceRoot = path.normalize("/custom/workspace") + const absolutePath = path.join(workspaceRoot, "src", "file.ts") + const result = generateRelativeFilePath(absolutePath, workspaceRoot) + expect(result).toBe(path.join("src", "file.ts")) }) it("should return null for paths outside the provided workspace root", () => { - const absolutePath = "/other/location/file.ts" - const result = generateRelativeFilePath(absolutePath, "/custom/workspace") + const absolutePath = path.normalize("/other/location/file.ts") + const workspaceRoot = path.normalize("/custom/workspace") + const result = generateRelativeFilePath(absolutePath, workspaceRoot) expect(result).toBeNull() }) it("should auto-detect workspace root in multi-root workspace", () => { - const absolutePath = "/workspace/project1/src/file.ts" - vi.mocked(workspaceUtils.getWorkspaceRootForFile).mockReturnValue("/workspace/project1") + const workspaceRoot = path.normalize("/workspace/project1") + const absolutePath = path.join(workspaceRoot, "src", "file.ts") + vi.mocked(workspaceUtils.getWorkspaceRootForFile).mockReturnValue(workspaceRoot) const result = generateRelativeFilePath(absolutePath) - expect(result).toBe(path.normalize("src/file.ts")) + expect(result).toBe(path.join("src", "file.ts")) expect(workspaceUtils.getWorkspaceRootForFile).toHaveBeenCalledWith(absolutePath) }) @@ -100,23 +105,25 @@ describe("get-relative-path", () => { }) it("should handle workspace root as the file path", () => { - const workspaceRoot = "/workspace/project" + const workspaceRoot = path.normalize("/workspace/project") const result = generateRelativeFilePath(workspaceRoot, workspaceRoot) - expect(result).toBe(path.normalize(".")) + expect(result).toBe(".") }) it("should handle paths with .. when workspace root is provided", () => { - const absolutePath = "/workspace/../outside/file.ts" - const result = generateRelativeFilePath(absolutePath, "/workspace") + const workspaceRoot = path.normalize("/workspace") + const absolutePath = path.normalize("/workspace/../outside/file.ts") + const result = generateRelativeFilePath(absolutePath, workspaceRoot) expect(result).toBeNull() }) it("should prioritize provided workspace root over auto-detection", () => { - const absolutePath = "/workspace/project1/src/file.ts" - vi.mocked(workspaceUtils.getWorkspaceRootForFile).mockReturnValue("/workspace/project2") + const workspaceRoot = path.normalize("/workspace/project1") + const absolutePath = path.join(workspaceRoot, "src", "file.ts") + vi.mocked(workspaceUtils.getWorkspaceRootForFile).mockReturnValue(path.normalize("/workspace/project2")) - const result = generateRelativeFilePath(absolutePath, "/workspace/project1") - expect(result).toBe(path.normalize("src/file.ts")) + const result = generateRelativeFilePath(absolutePath, workspaceRoot) + expect(result).toBe(path.join("src", "file.ts")) expect(workspaceUtils.getWorkspaceRootForFile).not.toHaveBeenCalled() }) }) diff --git a/src/services/code-index/shared/__tests__/workspace-utils.spec.ts b/src/services/code-index/shared/__tests__/workspace-utils.spec.ts index d1573c14d4..c4dfa20c9a 100644 --- a/src/services/code-index/shared/__tests__/workspace-utils.spec.ts +++ b/src/services/code-index/shared/__tests__/workspace-utils.spec.ts @@ -1,5 +1,6 @@ import { describe, it, expect, vi, beforeEach } from "vitest" import * as vscode from "vscode" +import * as path from "path" import { getWorkspaceRootForFile, isFileInWorkspace, @@ -27,38 +28,46 @@ describe("workspace-utils", () => { }) it("should return the correct workspace root for a file", () => { + const project1Path = path.normalize("/workspace/project1") + const project2Path = path.normalize("/workspace/project2") ;(vscode.workspace as any).workspaceFolders = [ - { uri: { fsPath: "/workspace/project1" } }, - { uri: { fsPath: "/workspace/project2" } }, + { uri: { fsPath: project1Path } }, + { uri: { fsPath: project2Path } }, ] - const result = getWorkspaceRootForFile("/workspace/project1/src/file.ts") - expect(result).toBe("/workspace/project1") + const filePath = path.join(project1Path, "src", "file.ts") + const result = getWorkspaceRootForFile(filePath) + expect(result).toBe(project1Path) }) it("should handle nested workspace folders correctly", () => { + const parentPath = path.normalize("/workspace/parent") + const childPath = path.join(parentPath, "child") ;(vscode.workspace as any).workspaceFolders = [ - { uri: { fsPath: "/workspace/parent" } }, - { uri: { fsPath: "/workspace/parent/child" } }, + { uri: { fsPath: parentPath } }, + { uri: { fsPath: childPath } }, ] // File in child workspace should return child workspace root - const result = getWorkspaceRootForFile("/workspace/parent/child/src/file.ts") - expect(result).toBe("/workspace/parent/child") + const filePath = path.join(childPath, "src", "file.ts") + const result = getWorkspaceRootForFile(filePath) + expect(result).toBe(childPath) }) it("should return undefined for files outside all workspace roots", () => { - ;(vscode.workspace as any).workspaceFolders = [{ uri: { fsPath: "/workspace/project1" } }] + const workspacePath = path.normalize("/workspace/project1") + ;(vscode.workspace as any).workspaceFolders = [{ uri: { fsPath: workspacePath } }] const result = getWorkspaceRootForFile("/other/location/file.ts") expect(result).toBeUndefined() }) it("should handle exact workspace root path", () => { - ;(vscode.workspace as any).workspaceFolders = [{ uri: { fsPath: "/workspace/project" } }] + const workspacePath = path.normalize("/workspace/project") + ;(vscode.workspace as any).workspaceFolders = [{ uri: { fsPath: workspacePath } }] - const result = getWorkspaceRootForFile("/workspace/project") - expect(result).toBe("/workspace/project") + const result = getWorkspaceRootForFile(workspacePath) + expect(result).toBe(workspacePath) }) }) @@ -92,26 +101,33 @@ describe("workspace-utils", () => { }) it("should return all workspace root paths", () => { + const project1Path = path.normalize("/workspace/project1") + const project2Path = path.normalize("/workspace/project2") + const project3Path = path.normalize("/workspace/project3") ;(vscode.workspace as any).workspaceFolders = [ - { uri: { fsPath: "/workspace/project1" } }, - { uri: { fsPath: "/workspace/project2" } }, - { uri: { fsPath: "/workspace/project3" } }, + { uri: { fsPath: project1Path } }, + { uri: { fsPath: project2Path } }, + { uri: { fsPath: project3Path } }, ] const result = getAllWorkspaceRoots() - expect(result).toEqual(["/workspace/project1", "/workspace/project2", "/workspace/project3"]) + expect(result).toEqual([project1Path, project2Path, project3Path]) }) it("should normalize paths", () => { + const project1Path = "/workspace/project1/" + const project2Path = "/workspace//project2" ;(vscode.workspace as any).workspaceFolders = [ - { uri: { fsPath: "/workspace/project1/" } }, - { uri: { fsPath: "/workspace//project2" } }, + { uri: { fsPath: project1Path } }, + { uri: { fsPath: project2Path } }, ] const result = getAllWorkspaceRoots() - // path.normalize may keep trailing slashes on some platforms - expect(result[0]).toMatch(/^\/workspace\/project1\/?$/) - expect(result[1]).toMatch(/^\/workspace\/project2\/?$/) + // Normalize the expected paths for comparison + const normalized1 = path.normalize(project1Path) + const normalized2 = path.normalize(project2Path) + expect(result[0]).toBe(normalized1) + expect(result[1]).toBe(normalized2) }) })