Version range compression utils (#2840)

* DNF normalization wip

* a bunch of wip stuff

* it is alive!

* tests

* deduplicate strings in tests

* fix != flavor behavior & parse flavor constraints & equals shorthand for normalize

* use normalization

* more comments & fix tests not running because of bad rebase

* fix comments+tests

* slightly better comment

* fix dependency & typos

---------

Co-authored-by: Aiden McClelland <me@drbonez.dev>
This commit is contained in:
Sam Sartor
2025-03-04 15:55:20 -07:00
committed by GitHub
parent 63bc71da13
commit e662b2f393
7 changed files with 779 additions and 156 deletions

View File

@@ -71,6 +71,19 @@ describe("ExVer", () => {
})
}
{
// TODO: this this correct? if not, also fix normalize
const checker = VersionRange.parse("=1")
test(`VersionRange.parse("=1") valid`, () => {
expect(checker.satisfiedBy(ExtendedVersion.parse("1.0.0:0"))).toEqual(
true,
)
})
test(`VersionRange.parse("=1") invalid`, () => {
expect(checker.satisfiedBy(ExtendedVersion.parse("1.0.1:0"))).toEqual(false)
expect(checker.satisfiedBy(ExtendedVersion.parse("1.0.0:1"))).toEqual(false)
})
} {
const checker = VersionRange.parse(">=1.2.3:4")
test(`VersionRange.parse(">=1.2.3:4") valid`, () => {
expect(checker.satisfiedBy(ExtendedVersion.parse("2:0"))).toEqual(true)
@@ -290,6 +303,38 @@ describe("ExVer", () => {
})
}
{
function testNormalization(input: string, expected: string) {
test(`"${input}" normalizes to "${expected}"`, () => {
const checker = VersionRange.parse(input).normalize();
expect(checker.toString()).toEqual(expected);
});
}
testNormalization("=2.0", "=2.0:0");
testNormalization("=1 && =2", "!");
testNormalization("!(=1 && =2)", "*");
testNormalization("!=1 || !=2", "*");
testNormalization("(!=#foo:1 || !=#foo:2) && #foo", "#foo");
testNormalization("!=#foo:1 || !=#bar:2", "<#foo:1:0 || >#foo:1:0 || !#foo || <#bar:2:0 || >#bar:2:0 || !#bar");
testNormalization("!(=1 || =2)", "<1:0 || (>1:0 && <2:0) || >2:0 || !#");
testNormalization("=1 && (=2 || =3)", "!");
testNormalization("=1 && (=1 || =2)", "=1:0");
testNormalization("=#foo:1 && =#bar:1", "!");
testNormalization("!(=#foo:1) && !(=#bar:1)", "<#foo:1:0 || >#foo:1:0 || <#bar:1:0 || >#bar:1:0 || (!#foo && !#bar)");
testNormalization("!(=#foo:1) && !(=#bar:1) && >2", ">2:0");
testNormalization("~1.2.3", ">=1.2.3:0 && <1.3.0:0");
testNormalization("^1.2.3", ">=1.2.3:0 && <2.0.0:0");
testNormalization("^1.2.3 && >=1 && >=1.2 && >=1.3", ">=1.3:0 && <2.0.0:0");
testNormalization("(>=1.0 && <1.1) || (>=1.1 && <1.2) || (>=1.2 && <1.3)", ">=1.0:0 && <1.3:0");
testNormalization(">1 || <2", "#");
testNormalization("=1 && =1.2 && =1.2.3", "!");
// testNormalization("=1 && =1.2 && =1.2.3", "=1.2.3:0"); TODO: should it be this instead?
testNormalization("=1 || =1.2 || =1.2.3", "=1:0 || =1.2:0 || =1.2.3:0");
// testNormalization("=1 || =1.2 || =1.2.3", "=1:0"); TODO: should it be this instead?
}
{
test(">1 && =1.2", () => {
const checker = VersionRange.parse(">1 && =1.2")
@@ -305,6 +350,8 @@ describe("ExVer", () => {
const checker = VersionRange.parse("=1 || =2")
expect(checker.satisfiedBy(ExtendedVersion.parse("1:0"))).toEqual(true)
expect(checker.satisfiedBy(ExtendedVersion.parse("1.2:0"))).toEqual(false) // really?
expect(checker.satisfiedBy(ExtendedVersion.parse("1.2.3:0"))).toEqual(false) // really?
expect(checker.satisfiedBy(ExtendedVersion.parse("2:0"))).toEqual(true)
expect(checker.satisfiedBy(ExtendedVersion.parse("3:0"))).toEqual(false)
})