|
| 1 | +import { |
| 2 | + getVersionInformation, |
| 3 | + parseElectronVersion, |
| 4 | +} from "../../../../scripts/util/vscode-versions"; |
| 5 | + |
| 6 | +const electronReleases = [ |
| 7 | + { |
| 8 | + version: "29.4.0", |
| 9 | + node: "20.9.0", |
| 10 | + chrome: "122.0.6261.156", |
| 11 | + }, |
| 12 | + { |
| 13 | + version: "42.10.0", |
| 14 | + node: "24.18.1", |
| 15 | + chrome: "148.0.7778.280", |
| 16 | + }, |
| 17 | +]; |
| 18 | + |
| 19 | +function mockFetch( |
| 20 | + responses: Record<string, unknown>, |
| 21 | +): jest.SpiedFunction<typeof fetch> { |
| 22 | + return jest.spyOn(globalThis, "fetch").mockImplementation(async (input) => { |
| 23 | + const url = input.toString(); |
| 24 | + const response = responses[url]; |
| 25 | + if (response === undefined) { |
| 26 | + throw new Error(`Unexpected URL: ${url}`); |
| 27 | + } |
| 28 | + |
| 29 | + return { |
| 30 | + ok: true, |
| 31 | + json: async () => response, |
| 32 | + text: async () => response, |
| 33 | + } as Response; |
| 34 | + }); |
| 35 | +} |
| 36 | + |
| 37 | +afterEach(() => { |
| 38 | + jest.restoreAllMocks(); |
| 39 | +}); |
| 40 | + |
| 41 | +describe("parseElectronVersion", () => { |
| 42 | + it("parses the Electron target from a VS Code .npmrc", () => { |
| 43 | + expect( |
| 44 | + parseElectronVersion( |
| 45 | + ['target="42.10.0"', 'runtime="electron"', "disturl=example"].join( |
| 46 | + "\n", |
| 47 | + ), |
| 48 | + ), |
| 49 | + ).toBe("42.10.0"); |
| 50 | + }); |
| 51 | + |
| 52 | + it.each([ |
| 53 | + ["a missing target", 'runtime="electron"'], |
| 54 | + ["an invalid target", 'target="not-a-version"'], |
| 55 | + ["an unquoted target", "target=42.10.0"], |
| 56 | + ])("rejects %s", (_description, npmrc) => { |
| 57 | + expect(() => parseElectronVersion(npmrc)).toThrow( |
| 58 | + "Could not find a valid Electron version in VS Code .npmrc", |
| 59 | + ); |
| 60 | + }); |
| 61 | +}); |
| 62 | + |
| 63 | +describe("getVersionInformation", () => { |
| 64 | + it("reads Electron from package.json for older VS Code releases", async () => { |
| 65 | + const fetchMock = mockFetch({ |
| 66 | + "https://raw.githubusercontent.com/microsoft/vscode/1.90.0/package.json": |
| 67 | + { |
| 68 | + devDependencies: { |
| 69 | + electron: "29.4.0", |
| 70 | + }, |
| 71 | + }, |
| 72 | + "https://releases.electronjs.org/releases.json": electronReleases, |
| 73 | + }); |
| 74 | + |
| 75 | + await expect(getVersionInformation("1.90.0")).resolves.toEqual({ |
| 76 | + vscodeVersion: "1.90.0", |
| 77 | + electronVersion: "29.4.0", |
| 78 | + nodeVersion: "20.9.0", |
| 79 | + chromiumVersion: "122.0.6261.156", |
| 80 | + }); |
| 81 | + expect(fetchMock).not.toHaveBeenCalledWith( |
| 82 | + "https://raw.githubusercontent.com/microsoft/vscode/1.90.0/.npmrc", |
| 83 | + ); |
| 84 | + }); |
| 85 | + |
| 86 | + it("falls back to .npmrc for newer VS Code releases", async () => { |
| 87 | + mockFetch({ |
| 88 | + "https://raw.githubusercontent.com/microsoft/vscode/1.137.0/package.json": |
| 89 | + { |
| 90 | + devDependencies: {}, |
| 91 | + }, |
| 92 | + "https://raw.githubusercontent.com/microsoft/vscode/1.137.0/.npmrc": |
| 93 | + 'target="42.10.0"\nruntime="electron"\n', |
| 94 | + "https://releases.electronjs.org/releases.json": electronReleases, |
| 95 | + }); |
| 96 | + |
| 97 | + await expect(getVersionInformation("1.137.0")).resolves.toEqual({ |
| 98 | + vscodeVersion: "1.137.0", |
| 99 | + electronVersion: "42.10.0", |
| 100 | + nodeVersion: "24.18.1", |
| 101 | + chromiumVersion: "148.0.7778.280", |
| 102 | + }); |
| 103 | + }); |
| 104 | +}); |
0 commit comments