Change engines.lens signature to only respect minimums (#5290)

Co-authored-by: Jim Ehrismann <40840436+jim-docker@users.noreply.github.com>
This commit is contained in:
RomanandJim Ehrismann authored and GitHub committed 2022-05-12 14:18:25 -04:00
1 parent b1df407715
commit 74b4a752eb
17 files changed
+179 -229

No files matched your search

@@ -1,128 +0,0 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { rawIsCompatibleExtension } from "../extension-compatibility";
import { Console } from "console";
import { stdout, stderr } from "process";
import type { LensExtensionManifest } from "../lens-extension";
import { SemVer } from "semver";
console = new Console(stdout, stderr);
describe("extension compatibility", () => {
describe("appSemVer with no prerelease tag", () => {
const isCompatibleExtension = rawIsCompatibleExtension(new SemVer("5.0.3"));
it("has no extension comparator", () => {
const manifest = { name: "extensionName", version: "0.0.1" };
expect(isCompatibleExtension(manifest)).toBe(false);
});
it.each([
{
comparator: "",
expected: false,
},
{
comparator: "bad comparator",
expected: false,
},
{
comparator: "^4.0.0",
expected: false,
},
{
comparator: "^5.0.0",
expected: true,
},
{
comparator: "^6.0.0",
expected: false,
},
{
comparator: "^4.0.0-alpha.1",
expected: false,
},
{
comparator: "^5.0.0-alpha.1",
expected: true,
},
{
comparator: "^6.0.0-alpha.1",
expected: false,
},
])("extension comparator test: %p", ({ comparator, expected }) => {
const manifest: LensExtensionManifest = { name: "extensionName", version: "0.0.1", engines: { lens: comparator }};
expect(isCompatibleExtension(manifest)).toBe(expected);
});
});
describe("appSemVer with prerelease tag", () => {
const isCompatibleExtension = rawIsCompatibleExtension(new SemVer("5.0.3-beta.3"));
it("^5.1.0 should work when lens' version is 5.1.0-latest.123456789", () => {
const comparer = rawIsCompatibleExtension(new SemVer("5.1.0-latest.123456789"));
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", () => {
const manifest = { name: "extensionName", version: "0.0.1" };
expect(isCompatibleExtension(manifest)).toBe(false);
});
it.each([
{
comparator: "",
expected: false,
},
{
comparator: "bad comparator",
expected: false,
},
{
comparator: "^4.0.0",
expected: false,
},
{
comparator: "^5.0.0",
expected: true,
},
{
comparator: "^6.0.0",
expected: false,
},
{
comparator: "^4.0.0-alpha.1",
expected: false,
},
{
comparator: "^5.0.0-alpha.1",
expected: true,
},
{
comparator: "^6.0.0-alpha.1",
expected: false,
},
])("extension comparator test: %p", ({ comparator, expected }) => {
expect(isCompatibleExtension({ name: "extensionName", version: "0.0.1", engines: { lens: comparator }})).toBe(expected);
});
});
});
@@ -0,0 +1,121 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import semver from "semver";
import {
isCompatibleExtension,
} from "../extension-discovery/is-compatible-extension/is-compatible-extension";
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"),
})(getExtensionManifestMock({
lensEngine: "5.5.0",
}))).toBeTruthy();
});
it("is compatible with upper %PATCH versions of base app", () => {
expect(isCompatibleExtension({
appSemVer: semver.coerce("5.5.5"),
})(getExtensionManifestMock({
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();
expect(isCompatibleExtension({
appSemVer: semver.coerce("5.5.0-alpha.0"),
})(getExtensionManifestMock({
lensEngine: "^5.5.0",
}))).toBeTruthy();
expect(isCompatibleExtension({
appSemVer: semver.coerce("5.5"),
})(getExtensionManifestMock({
lensEngine: "^5.6.0",
}))).toBeFalsy();
});
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("6.0.0"), // current lens-version
})(getExtensionManifestMock({
lensEngine: "5.5.0",
}))).toBeFalsy(); // extension with lens@5.5 is not compatible with app@6.0
});
it("is compatible with lensEngine with prerelease", () => {
expect(isCompatibleExtension({
appSemVer: semver.parse("5.5.0-alpha.0"),
})(getExtensionManifestMock({
lensEngine: "^5.4.0-alpha.0",
}))).toBeTruthy();
});
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);
});
});
});
function getExtensionManifestMock(
{
lensEngine = "1.0",
} = {}): LensExtensionManifest {
return {
name: "some-extension",
version: "1.0",
engines: {
lens: lensEngine,
},
};
}
@@ -17,6 +17,7 @@ describe("lens extension", () => {
manifest: {
name: "foo-bar",
version: "0.1.1",
engines: { lens: "^5.5.0" },
},
id: "/this/is/fake/package.json",
absolutePath: "/absolute/fake/",