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

Fix extension engine range not working for some ^ ranges (#4554)

This commit is contained in:
Sebastian Malton 2021-12-14 11:57:34 -05:00 committed by GitHub
parent d775d53078
commit 1e22cffcd8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 59 additions and 25 deletions

View File

@ -19,22 +19,17 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
import { isCompatibleExtension } from "../extension-compatibility"; import { rawIsCompatibleExtension } from "../extension-compatibility";
import { Console } from "console"; import { Console } from "console";
import { stdout, stderr } from "process"; import { stdout, stderr } from "process";
import type { LensExtensionManifest } from "../lens-extension"; import type { LensExtensionManifest } from "../lens-extension";
import { appSemVer } from "../../common/vars"; import { SemVer } from "semver";
console = new Console(stdout, stderr); console = new Console(stdout, stderr);
describe("extension compatibility", () => { describe("extension compatibility", () => {
describe("appSemVer with no prerelease tag", () => { describe("appSemVer with no prerelease tag", () => {
beforeAll(() => { const isCompatibleExtension = rawIsCompatibleExtension(new SemVer("5.0.3"));
appSemVer.major = 5;
appSemVer.minor = 0;
appSemVer.patch = 3;
appSemVer.prerelease = [];
});
it("has no extension comparator", () => { it("has no extension comparator", () => {
const manifest = { name: "extensionName", version: "0.0.1" }; const manifest = { name: "extensionName", version: "0.0.1" };
@ -83,11 +78,24 @@ describe("extension compatibility", () => {
}); });
describe("appSemVer with prerelease tag", () => { describe("appSemVer with prerelease tag", () => {
beforeAll(() => { const isCompatibleExtension = rawIsCompatibleExtension(new SemVer("5.0.3-beta.3"));
appSemVer.major = 5;
appSemVer.minor = 0; it("^5.1.0 should work when lens' version is 5.1.0-latest.123456789", () => {
appSemVer.patch = 3; const comparer = rawIsCompatibleExtension(new SemVer("5.1.0-latest.123456789"));
appSemVer.prerelease = ["beta", 3];
expect(comparer({ name: "extensionName", version: "0.0.1", engines: { lens: "^5.1.0" }})).toBe(true);
});
it("^5.1.0 should not when lens' version is 5.1.0-beta.1.123456789", () => {
const comparer = rawIsCompatibleExtension(new SemVer("5.1.0-beta.123456789"));
expect(comparer({ name: "extensionName", version: "0.0.1", engines: { lens: "^5.1.0" }})).toBe(false);
});
it("^5.1.0 should not when lens' version is 5.1.0-alpha.1.123456789", () => {
const comparer = rawIsCompatibleExtension(new SemVer("5.1.0-alpha.123456789"));
expect(comparer({ name: "extensionName", version: "0.0.1", engines: { lens: "^5.1.0" }})).toBe(false);
}); });
it("has no extension comparator", () => { it("has no extension comparator", () => {
@ -130,9 +138,7 @@ describe("extension compatibility", () => {
expected: false, expected: false,
}, },
])("extension comparator test: %p", ({ comparator, expected }) => { ])("extension comparator test: %p", ({ comparator, expected }) => {
const manifest: LensExtensionManifest = { name: "extensionName", version: "0.0.1", engines: { lens: comparator }}; expect(isCompatibleExtension({ name: "extensionName", version: "0.0.1", engines: { lens: comparator }})).toBe(expected);
expect(isCompatibleExtension(manifest)).toBe(expected);
}); });
}); });
}); });

View File

@ -19,19 +19,47 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
import semver from "semver"; import semver, { SemVer } from "semver";
import { appSemVer, isProduction } from "../common/vars"; import { appSemVer, isProduction } from "../common/vars";
import type { LensExtensionManifest } from "./lens-extension"; import type { LensExtensionManifest } from "./lens-extension";
export function isCompatibleExtension(manifest: LensExtensionManifest): boolean { export function rawIsCompatibleExtension(version: SemVer): (manifest: LensExtensionManifest) => boolean {
if (manifest.engines?.lens) { const { major, minor, patch, prerelease: oldPrelease } = version;
/* include Lens's prerelease tag in the matching so the extension's compatibility is not limited by it */ let prerelease = "";
return semver.satisfies(appSemVer, manifest.engines.lens, { includePrerelease: true });
if (oldPrelease.length > 0) {
const [first] = oldPrelease;
if (first === "alpha" || first === "beta" || first === "rc") {
/**
* Strip the build IDs and "latest" prerelease tag as that is not really
* a part of API version
*/
prerelease = `-${oldPrelease.slice(0, 2).join(".")}`;
}
} }
return false; /**
* We unfortunately have to format as string because the constructor only
* takes an instance or a string.
*/
const strippedVersion = new SemVer(`${major}.${minor}.${patch}${prerelease}`, { includePrerelease: true });
return (manifest: LensExtensionManifest): boolean => {
if (manifest.engines?.lens) {
/**
* include Lens's prerelease tag in the matching so the extension's
* compatibility is not limited by it
*/
return semver.satisfies(strippedVersion, manifest.engines.lens, { includePrerelease: true });
}
return false;
};
} }
export const isCompatibleExtension = rawIsCompatibleExtension(appSemVer);
export function isCompatibleBundledExtension(manifest: LensExtensionManifest): boolean { export function isCompatibleBundledExtension(manifest: LensExtensionManifest): boolean {
return !isProduction || manifest.version === appSemVer.raw; return !isProduction || manifest.version === appSemVer.raw;
} }