|
| 1 | +import assert from "assert"; |
| 2 | +import {writeFile} from "fs/promises"; |
| 3 | +import * as tmp from "tmp"; |
| 4 | +import {anything, instance, mock, when} from "ts-mockito"; |
| 5 | +import {ExtensionContext} from "vscode"; |
| 6 | +import { |
| 7 | + getCorrectVsixInstallString, |
| 8 | + getMetadata, |
| 9 | + isCompatibleArchitecture, |
| 10 | + isEqual, |
| 11 | + nodeArchMap, |
| 12 | + nodeOsMap, |
| 13 | + vsixArchMap, |
| 14 | +} from "./packageJsonUtils"; |
| 15 | + |
| 16 | +describe(__filename, () => { |
| 17 | + it("should correctly check compatibility", () => { |
| 18 | + assert.ok( |
| 19 | + !isCompatibleArchitecture( |
| 20 | + "no match", |
| 21 | + {os: "macos", arch: "arm64"}, |
| 22 | + {os: "linux", arch: "arm64"}, |
| 23 | + {packageName: "test", version: "0.0.0"} |
| 24 | + ) |
| 25 | + ); |
| 26 | + |
| 27 | + assert.ok( |
| 28 | + isCompatibleArchitecture( |
| 29 | + "match", |
| 30 | + {os: "macos", arch: "arm64"}, |
| 31 | + {os: "macos", arch: "arm64"}, |
| 32 | + {packageName: "test", version: "0.0.0"} |
| 33 | + ) |
| 34 | + ); |
| 35 | + }); |
| 36 | + |
| 37 | + it("should correctly compare archs", () => { |
| 38 | + assert.ok( |
| 39 | + !isEqual({os: "macos", arch: "arm64"}, {os: "linux", arch: "arm64"}) |
| 40 | + ); |
| 41 | + |
| 42 | + assert.ok( |
| 43 | + isEqual({os: "macos", arch: "arm64"}, {os: "macos", arch: "arm64"}) |
| 44 | + ); |
| 45 | + }); |
| 46 | + |
| 47 | + it("should correctly read metadata", async () => { |
| 48 | + const {name: path} = tmp.fileSync(); |
| 49 | + const metaData = { |
| 50 | + name: "name", |
| 51 | + version: "version", |
| 52 | + arch: { |
| 53 | + bricksArch: "bricksArch", |
| 54 | + vsixArch: "vsixArch", |
| 55 | + }, |
| 56 | + commitSha: "commitSha", |
| 57 | + }; |
| 58 | + await writeFile(path, JSON.stringify(metaData)); |
| 59 | + |
| 60 | + const context = mock<ExtensionContext>(); |
| 61 | + when(context.asAbsolutePath(anything())).thenReturn(path); |
| 62 | + |
| 63 | + const actualMetadata = await getMetadata(instance(context)); |
| 64 | + |
| 65 | + assert.deepEqual(actualMetadata, { |
| 66 | + packageName: "name", |
| 67 | + version: "version", |
| 68 | + bricksArch: "bricksArch", |
| 69 | + vsixArch: "vsixArch", |
| 70 | + commitSha: "commitSha", |
| 71 | + }); |
| 72 | + }); |
| 73 | + |
| 74 | + it("should correctly format vsix install string", () => { |
| 75 | + nodeOsMap.forEach((os) => { |
| 76 | + nodeArchMap.forEach((arch) => { |
| 77 | + const archDetails = {os, arch}; |
| 78 | + const actual = getCorrectVsixInstallString(archDetails, { |
| 79 | + packageName: "name", |
| 80 | + version: "0.0.0", |
| 81 | + }); |
| 82 | + |
| 83 | + const vsixArchString = Array.from(vsixArchMap).find( |
| 84 | + (keyValue) => isEqual(keyValue[1], archDetails) |
| 85 | + )?.[0]; |
| 86 | + |
| 87 | + vsixArchString |
| 88 | + ? assert.equal( |
| 89 | + actual, |
| 90 | + `Please install name-${vsixArchString}-0.0.0.vsix` |
| 91 | + ) |
| 92 | + : assert.equal( |
| 93 | + actual, |
| 94 | + "Current system architecture is not supported." |
| 95 | + ); |
| 96 | + }); |
| 97 | + }); |
| 98 | + }); |
| 99 | +}); |
0 commit comments