|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT license. |
| 3 | + |
| 4 | +import * as assert from "assert"; |
| 5 | +import * as fs from "fs"; |
| 6 | +import * as os from "os"; |
| 7 | +import * as path from "path"; |
| 8 | +import * as vscode from "vscode"; |
| 9 | + |
| 10 | +import { JavaDebugConfigurationProvider } from "../src/configurationProvider"; |
| 11 | + |
| 12 | +interface TestWorkspace { |
| 13 | + root: string; |
| 14 | + folder: vscode.WorkspaceFolder; |
| 15 | + libDir: string; |
| 16 | + jarPath: string; |
| 17 | +} |
| 18 | + |
| 19 | +type FilterExcluded = ( |
| 20 | + folder: vscode.WorkspaceFolder | undefined, |
| 21 | + paths: string[], |
| 22 | +) => Promise<string[]>; |
| 23 | + |
| 24 | +function createTestWorkspace(): TestWorkspace { |
| 25 | + const root = fs.mkdtempSync(path.join(os.tmpdir(), "java-debug-cp-test-")); |
| 26 | + const libDir = path.join(root, "lib"); |
| 27 | + fs.mkdirSync(libDir); |
| 28 | + const jarPath = path.join(libDir, "foo.jar"); |
| 29 | + fs.writeFileSync(jarPath, ""); |
| 30 | + return { |
| 31 | + root, |
| 32 | + folder: { |
| 33 | + uri: vscode.Uri.file(root), |
| 34 | + name: "test-workspace", |
| 35 | + index: 0, |
| 36 | + }, |
| 37 | + libDir, |
| 38 | + jarPath, |
| 39 | + }; |
| 40 | +} |
| 41 | + |
| 42 | +function getFilterExcluded(provider: JavaDebugConfigurationProvider): FilterExcluded { |
| 43 | + return (provider as unknown as { filterExcluded: FilterExcluded }).filterExcluded.bind(provider); |
| 44 | +} |
| 45 | + |
| 46 | +suite("JavaDebugConfigurationProvider", () => { |
| 47 | + const workspaces: TestWorkspace[] = []; |
| 48 | + |
| 49 | + suiteSetup(() => { |
| 50 | + // configurationProvider requires ../package.json relative to out/src/ |
| 51 | + const outPackageJson = path.join(__dirname, "../package.json"); |
| 52 | + if (!fs.existsSync(outPackageJson)) { |
| 53 | + fs.copyFileSync(path.join(__dirname, "../../package.json"), outPackageJson); |
| 54 | + } |
| 55 | + }); |
| 56 | + |
| 57 | + teardown(() => { |
| 58 | + while (workspaces.length > 0) { |
| 59 | + const workspace = workspaces.pop()!; |
| 60 | + fs.rmSync(workspace.root, { recursive: true, force: true }); |
| 61 | + } |
| 62 | + }); |
| 63 | + |
| 64 | + suite("filterExcluded exact-match exclusions", () => { |
| 65 | + async function assertExactDirectoryExclusion( |
| 66 | + excludeSuffix: "\\" | "/", |
| 67 | + label: string, |
| 68 | + ): Promise<void> { |
| 69 | + const workspace = createTestWorkspace(); |
| 70 | + workspaces.push(workspace); |
| 71 | + |
| 72 | + const libDirFs = vscode.Uri.file(workspace.libDir).fsPath; |
| 73 | + const jarFs = vscode.Uri.file(workspace.jarPath).fsPath; |
| 74 | + const filterExcluded = getFilterExcluded(new JavaDebugConfigurationProvider()); |
| 75 | + const result = await filterExcluded(workspace.folder, [ |
| 76 | + libDirFs, |
| 77 | + jarFs, |
| 78 | + `!${workspace.libDir}${excludeSuffix}`, |
| 79 | + ]); |
| 80 | + |
| 81 | + assert.deepStrictEqual( |
| 82 | + result, |
| 83 | + [jarFs], |
| 84 | + `${label}: trailing slash should exact-exclude only the directory entry, not paths beneath it`, |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | + test("treats a trailing backslash as an exact match (Windows-style paths)", async () => { |
| 89 | + await assertExactDirectoryExclusion("\\", "Windows-style"); |
| 90 | + }); |
| 91 | + |
| 92 | + test("treats a trailing forward slash as an exact match (Linux-style paths)", async () => { |
| 93 | + await assertExactDirectoryExclusion("/", "Linux-style"); |
| 94 | + }); |
| 95 | + }); |
| 96 | +}); |
0 commit comments