|
| 1 | +import { platform } from "os"; |
| 2 | +import type { BaseLogger } from "../../../src/common/logging"; |
| 3 | +import { expandShortPaths } from "../../../src/common/short-paths"; |
| 4 | +import { join } from "path"; |
| 5 | + |
| 6 | +describe("expandShortPaths", () => { |
| 7 | + let logger: BaseLogger; |
| 8 | + |
| 9 | + beforeEach(() => { |
| 10 | + logger = { |
| 11 | + log: jest.fn(), |
| 12 | + }; |
| 13 | + }); |
| 14 | + |
| 15 | + describe("on POSIX", () => { |
| 16 | + if (platform() === "win32") { |
| 17 | + console.log(`Skipping test on Windows`); |
| 18 | + return; |
| 19 | + } |
| 20 | + |
| 21 | + it("should return the same path for non-Windows platforms", async () => { |
| 22 | + const path = "/home/user/some~path"; |
| 23 | + const result = await expandShortPaths(path, logger); |
| 24 | + |
| 25 | + expect(logger.log).not.toHaveBeenCalled(); |
| 26 | + expect(result).toBe(path); |
| 27 | + }); |
| 28 | + }); |
| 29 | + |
| 30 | + describe("on Windows", () => { |
| 31 | + if (platform() !== "win32") { |
| 32 | + console.log(`Skipping test on non-Windows`); |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + it("should return the same path if no short components", async () => { |
| 37 | + const path = "C:\\Program Files\\Some Folder"; |
| 38 | + const result = await expandShortPaths(path, logger); |
| 39 | + |
| 40 | + expect(logger.log).toHaveBeenCalledWith( |
| 41 | + `Expanding short paths in: ${path}`, |
| 42 | + ); |
| 43 | + expect(logger.log).toHaveBeenCalledWith( |
| 44 | + "Skipping due to no short components", |
| 45 | + ); |
| 46 | + expect(result).toBe(path); |
| 47 | + }); |
| 48 | + |
| 49 | + it("should not attempt to expand long paths with '~' in the name", async () => { |
| 50 | + const testDir = join(__dirname, "../data/short-paths"); |
| 51 | + const path = join(testDir, "textfile-with~tilde.txt"); |
| 52 | + const result = await expandShortPaths(path, logger); |
| 53 | + |
| 54 | + expect(logger.log).toHaveBeenCalledWith( |
| 55 | + `Expanding short paths in: ${path}`, |
| 56 | + ); |
| 57 | + expect(logger.log).toHaveBeenCalledWith( |
| 58 | + `Expanding short path component: textfile-with~tilde.txt`, |
| 59 | + ); |
| 60 | + expect(logger.log).toHaveBeenCalledWith(`Component is not a short name`); |
| 61 | + expect(result).toBe(join(testDir, "textfile-with~tilde.txt")); |
| 62 | + }); |
| 63 | + |
| 64 | + it("should expand a short path", async () => { |
| 65 | + const path = "C:\\PROGRA~1\\Some Folder"; |
| 66 | + const result = await expandShortPaths(path, logger); |
| 67 | + |
| 68 | + expect(logger.log).toHaveBeenCalledWith( |
| 69 | + `Expanding short paths in: ${path}`, |
| 70 | + ); |
| 71 | + expect(logger.log).toHaveBeenCalledWith( |
| 72 | + `Expanding short path component: PROGRA~1`, |
| 73 | + ); |
| 74 | + expect(result).toBe("C:\\Program Files\\Some Folder"); |
| 75 | + }); |
| 76 | + |
| 77 | + it("should expand multiple short paths", async () => { |
| 78 | + const testDir = join(__dirname, "../data/short-paths"); |
| 79 | + const path = join(testDir, "FOLDER~1", "TEXTFI~1.TXT"); |
| 80 | + const result = await expandShortPaths(path, logger); |
| 81 | + |
| 82 | + expect(logger.log).toHaveBeenCalledWith( |
| 83 | + `Expanding short paths in: ${path}`, |
| 84 | + ); |
| 85 | + expect(logger.log).toHaveBeenCalledWith( |
| 86 | + `Expanding short path component: FOLDER~1`, |
| 87 | + ); |
| 88 | + expect(logger.log).toHaveBeenCalledWith( |
| 89 | + `Expanding short path component: TEXTFI~1.TXT`, |
| 90 | + ); |
| 91 | + expect(result).toBe( |
| 92 | + join(testDir, "folder with space", ".textfile+extra.characters.txt"), |
| 93 | + ); |
| 94 | + }); |
| 95 | + }); |
| 96 | +}); |
0 commit comments