1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

update tests

Signed-off-by: Roman <ixrock@gmail.com>
This commit is contained in:
Roman 2022-05-09 15:57:01 +03:00
parent 460df0dca5
commit 2a9c5f8137
2 changed files with 80 additions and 27 deletions

View File

@ -12,42 +12,78 @@ import type { LensExtensionManifest } from "../lens-extension";
describe("Extension/App versions compatibility check", () => {
it("is compatible with exact version matching", () => {
expect(isCompatibleExtension({
appSemVer: semver.coerce("5.5.0"), // current app version
})(getExtensionManifestMock({
lensEngine: "5.5.0", // requested app version by extension ("semver"-format)
}))).toBeTruthy();
});
it("is compatible with higher patch-versions of main app", () => {
expect(isCompatibleExtension({
appSemVer: semver.coerce("5.5.5"), // for patch-versions
})(getExtensionManifestMock({
lensEngine: "^5.5.0",
}))).toBeTruthy();
});
it("supports short versions format for engines.lens", () => {
const isCompatible = isCompatibleExtension({
appSemVer: semver.coerce("5.5.0"),
})(getExtensionManifestMock({
lensEngine: "5.5",
}));
expect(isCompatible).toBeTruthy();
lensEngine: "5.5.0",
}))).toBeTruthy();
});
it("supporting `manifest.engines.lens='*'` to match any base-app version", () => {
it("is compatible with upper %PATCH versions of base app", () => {
expect(isCompatibleExtension({
appSemVer: semver.coerce("1.0.0"),
appSemVer: semver.coerce("5.5.5"),
})(getExtensionManifestMock({
lensEngine: "*",
lensEngine: "5.5.0",
}))).toBeTruthy();
});
it("is compatible with upper %MINOR version of base app", () => {
expect(isCompatibleExtension({
appSemVer: semver.coerce("5.6.0"),
})(getExtensionManifestMock({
lensEngine: "5.5.0",
}))).toBeTruthy();
});
it("is not compatible with upper %MAJOR version of base app", () => {
expect(isCompatibleExtension({
appSemVer: semver.coerce("5.5.0"), // current lens-version
})(getExtensionManifestMock({
lensEngine: "6.0.0",
}))).toBeFalsy(); // extension with lens@6.0 is not compatible with app@5.5
expect(isCompatibleExtension({
appSemVer: semver.coerce("2.0.0"),
appSemVer: semver.coerce("6.0.0"), // current lens-version
})(getExtensionManifestMock({
lensEngine: "*",
}))).toBeTruthy();
lensEngine: "5.5.0",
}))).toBeFalsy(); // extension with lens@5.5 is not compatible with app@6.0
});
describe("supported formats for manifest.engines.lens", () => {
it("short version format for engines.lens", () => {
expect(isCompatibleExtension({
appSemVer: semver.coerce("5.5.0"),
})(getExtensionManifestMock({
lensEngine: "5.5",
}))).toBeTruthy();
});
it("validates version and throws if incorrect format", () => {
expect(() => isCompatibleExtension({
appSemVer: semver.coerce("1.0.0"),
})(getExtensionManifestMock({
lensEngine: "1.0",
}))).not.toThrow();
expect(() => isCompatibleExtension({
appSemVer: semver.coerce("1.0.0"),
})(getExtensionManifestMock({
lensEngine: "^1.0",
}))).not.toThrow();
expect(() => isCompatibleExtension({
appSemVer: semver.coerce("1.0.0"),
})(getExtensionManifestMock({
lensEngine: ">=2.0",
}))).toThrow(/Invalid format/i);
});
it("'*' cannot be used for any version matching (at least in the prefix)", () => {
expect(() => isCompatibleExtension({
appSemVer: semver.coerce("1.0.0"),
})(getExtensionManifestMock({
lensEngine: "*",
}))).toThrowError(/Invalid format/i);
});
});
});

View File

@ -12,7 +12,24 @@ interface Dependencies {
export const isCompatibleExtension = ({ appSemVer }: Dependencies): ((manifest: LensExtensionManifest) => boolean) => {
return (manifest: LensExtensionManifest): boolean => {
const { raw: appVersion } = appSemVer;
const supportedVersionsByExtension: string = semver.validRange(manifest.engines.lens);
const manifestLensEngine = manifest.engines.lens;
const validVersion = manifestLensEngine.match(/^[\^0-9]\d*\.\d+\b/); // must start from ^ or number
if (!validVersion) {
const errorInfo = [
`Invalid format for "manifest.engines.lens"="${manifestLensEngine}"`,
`Range versions could be specified only starting with '^'.`,
`Otherwise it's recommended to use plain %MAJOR.%MINOR to match with supported Lens version.`,
].join("\n");
throw new Error(errorInfo);
}
const { major: extMajor, minor: extMinor } = semver.coerce(manifestLensEngine, {
loose: true,
includePrerelease: false,
});
const supportedVersionsByExtension: string = semver.validRange(`^${extMajor}.${extMinor}`);
return semver.satisfies(appVersion, supportedVersionsByExtension, {
loose: true,