|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT license. |
| 3 | + |
| 4 | +import * as assert from "assert"; |
| 5 | + |
| 6 | +import { buildNoConfigPathAppendValue } from "../src/pathUtil"; |
| 7 | + |
| 8 | +// Regression tests for issue #1637: the extension was appending its |
| 9 | +// noConfigScripts directory to PATH without a separator on some terminal |
| 10 | +// PATH configurations, gluing it onto the last entry of the user's PATH. |
| 11 | +suite("buildNoConfigPathAppendValue", () => { |
| 12 | + |
| 13 | + const winDir = "C:\\Users\\me\\.vscode\\extensions\\vscjava.vscode-java-debug-0.59.0\\bundled\\scripts\\noConfigScripts"; |
| 14 | + const posixDir = "/home/me/.vscode/extensions/vscjava.vscode-java-debug-0.59.0/bundled/scripts/noConfigScripts"; |
| 15 | + |
| 16 | + test("uses ';' as separator on Windows", () => { |
| 17 | + const result = buildNoConfigPathAppendValue(winDir, "win32"); |
| 18 | + assert.strictEqual(result, `;${winDir}`); |
| 19 | + }); |
| 20 | + |
| 21 | + test("uses ':' as separator on Linux", () => { |
| 22 | + const result = buildNoConfigPathAppendValue(posixDir, "linux"); |
| 23 | + assert.strictEqual(result, `:${posixDir}`); |
| 24 | + }); |
| 25 | + |
| 26 | + test("uses ':' as separator on macOS", () => { |
| 27 | + const result = buildNoConfigPathAppendValue(posixDir, "darwin"); |
| 28 | + assert.strictEqual(result, `:${posixDir}`); |
| 29 | + }); |
| 30 | + |
| 31 | + test("always starts with a path separator (Windows)", () => { |
| 32 | + const result = buildNoConfigPathAppendValue(winDir, "win32"); |
| 33 | + assert.ok(result.startsWith(";"), `expected leading ';', got: ${result}`); |
| 34 | + }); |
| 35 | + |
| 36 | + test("always starts with a path separator (POSIX)", () => { |
| 37 | + const result = buildNoConfigPathAppendValue(posixDir, "linux"); |
| 38 | + assert.ok(result.startsWith(":"), `expected leading ':', got: ${result}`); |
| 39 | + }); |
| 40 | + |
| 41 | + test("never collapses scriptsDir into the previous PATH entry on Windows", () => { |
| 42 | + // Simulates the exact scenario from issue #1637: a user PATH whose |
| 43 | + // last entry has no trailing separator. After append, the script dir |
| 44 | + // must not be glued onto 'jreleaser\'. |
| 45 | + const userPath = "C:\\foo;C:\\Program Files\\jreleaser\\"; |
| 46 | + const finalPath = userPath + buildNoConfigPathAppendValue(winDir, "win32"); |
| 47 | + |
| 48 | + const entries = finalPath.split(";"); |
| 49 | + assert.ok( |
| 50 | + entries.includes("C:\\Program Files\\jreleaser\\"), |
| 51 | + `expected 'jreleaser\\' to remain a standalone PATH entry, got entries: ${JSON.stringify(entries)}`, |
| 52 | + ); |
| 53 | + assert.ok( |
| 54 | + entries.includes(winDir), |
| 55 | + `expected scripts dir to be a standalone PATH entry, got entries: ${JSON.stringify(entries)}`, |
| 56 | + ); |
| 57 | + }); |
| 58 | + |
| 59 | + test("never collapses scriptsDir into the previous PATH entry on POSIX", () => { |
| 60 | + const userPath = "/usr/bin:/opt/jreleaser/bin"; |
| 61 | + const finalPath = userPath + buildNoConfigPathAppendValue(posixDir, "linux"); |
| 62 | + |
| 63 | + const entries = finalPath.split(":"); |
| 64 | + assert.ok( |
| 65 | + entries.includes("/opt/jreleaser/bin"), |
| 66 | + `expected '/opt/jreleaser/bin' to remain a standalone PATH entry, got entries: ${JSON.stringify(entries)}`, |
| 67 | + ); |
| 68 | + assert.ok( |
| 69 | + entries.includes(posixDir), |
| 70 | + `expected scripts dir to be a standalone PATH entry, got entries: ${JSON.stringify(entries)}`, |
| 71 | + ); |
| 72 | + }); |
| 73 | + |
| 74 | + test("yields only an empty (harmless) entry when the user's PATH already ends with a separator", () => { |
| 75 | + // If the resolved terminal PATH already ends with ';', append produces |
| 76 | + // ';;'. The empty middle entry is ignored by Windows and (in this |
| 77 | + // position) effectively a no-op on POSIX shells. |
| 78 | + const userPath = "C:\\foo;C:\\bar;"; |
| 79 | + const finalPath = userPath + buildNoConfigPathAppendValue(winDir, "win32"); |
| 80 | + |
| 81 | + const entries = finalPath.split(";"); |
| 82 | + // The scripts dir must still be a standalone, valid entry. |
| 83 | + assert.ok( |
| 84 | + entries.includes(winDir), |
| 85 | + `expected scripts dir to remain standalone, got entries: ${JSON.stringify(entries)}`, |
| 86 | + ); |
| 87 | + // No real entry should be merged with our scripts dir. |
| 88 | + assert.ok( |
| 89 | + !entries.some((e) => e !== winDir && e.endsWith(winDir)), |
| 90 | + `no entry should be glued to scripts dir, got entries: ${JSON.stringify(entries)}`, |
| 91 | + ); |
| 92 | + }); |
| 93 | + |
| 94 | + test("scriptsDir appears unchanged at the end of the appended value", () => { |
| 95 | + const result = buildNoConfigPathAppendValue(winDir, "win32"); |
| 96 | + assert.ok(result.endsWith(winDir), `expected value to end with scriptsDir, got: ${result}`); |
| 97 | + }); |
| 98 | +}); |
0 commit comments