feat(semver): add "semver" module (#2434)

Co-authored-by: Yoshiya Hinosawa <stibium121@gmail.com>
This commit is contained in:
迷渡 2022-07-18 19:17:42 +08:00 committed by GitHub
parent 66671c4495
commit e169864767
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 3429 additions and 0 deletions

307
semver/README.md Normal file
View File

@ -0,0 +1,307 @@
<!-- Copyright Isaac Z. Schlueter and Contributors. All rights reserved. ISC license.
Copyright 2018-2022 the Deno authors. All rights reserved. MIT license. -->
# Semver
## Usage
```ts
import * as semver from "https://deno.land/std@$STD_VERSION/semver/mod.ts";
semver.valid("1.2.3"); // "1.2.3"
semver.valid("a.b.c"); // null
semver.satisfies("1.2.3", "1.x || >=2.5.0 || 5.0.0 - 7.2.3"); // true
semver.gt("1.2.3", "9.8.7"); // false
semver.lt("1.2.3", "9.8.7"); // true
semver.minVersion(">=1.0.0"); // "1.0.0"
```
## Versions
A "version" is described by the `v2.0.0` specification found at
<https://semver.org>.
A leading `"="` or `"v"` character is stripped off and ignored.
## Ranges
A `version range` is a set of `comparators` which specify versions that satisfy
the range.
A `comparator` is composed of an `operator` and a `version`. The set of
primitive `operators` is:
- `<` Less than
- `<=` Less than or equal to
- `>` Greater than
- `>=` Greater than or equal to
- `=` Equal. If no operator is specified, then equality is assumed, so this
operator is optional, but MAY be included.
For example, the comparator `>=1.2.7` would match the versions `1.2.7`, `1.2.8`,
`2.5.3`, and `1.3.9`, but not the versions `1.2.6` or `1.1.0`.
Comparators can be joined by whitespace to form a `comparator set`, which is
satisfied by the **intersection** of all of the comparators it includes.
A range is composed of one or more comparator sets, joined by `||`. A version
matches a range if and only if every comparator in at least one of the
`||`-separated comparator sets is satisfied by the version.
For example, the range `>=1.2.7 <1.3.0` would match the versions `1.2.7`,
`1.2.8`, and `1.2.99`, but not the versions `1.2.6`, `1.3.0`, or `1.1.0`.
The range `1.2.7 || >=1.2.9 <2.0.0` would match the versions `1.2.7`, `1.2.9`,
and `1.4.6`, but not the versions `1.2.8` or `2.0.0`.
### Prerelease Tags
If a version has a prerelease tag (for example, `1.2.3-alpha.3`) then it will
only be allowed to satisfy comparator sets if at least one comparator with the
same `[major, minor, patch]` tuple also has a prerelease tag.
For example, the range `>1.2.3-alpha.3` would be allowed to match the version
`1.2.3-alpha.7`, but it would _not_ be satisfied by `3.4.5-alpha.9`, even though
`3.4.5-alpha.9` is technically "greater than" `1.2.3-alpha.3` according to the
SemVer sort rules. The version range only accepts prerelease tags on the `1.2.3`
version. The version `3.4.5` _would_ satisfy the range, because it does not have
a prerelease flag, and `3.4.5` is greater than `1.2.3-alpha.7`.
The purpose for this behavior is twofold. First, prerelease versions frequently
are updated very quickly, and contain many breaking changes that are (by the
author"s design) not yet fit for public consumption. Therefore, by default, they
are excluded from range matching semantics.
Second, a user who has opted into using a prerelease version has clearly
indicated the intent to use _that specific_ set of alpha/beta/rc versions. By
including a prerelease tag in the range, the user is indicating that they are
aware of the risk. However, it is still not appropriate to assume that they have
opted into taking a similar risk on the _next_ set of prerelease versions.
Note that this behavior can be suppressed (treating all prerelease versions as
if they were normal versions, for the purpose of range matching) by setting the
`includePrerelease` flag on the options object to any [functions](#functions)
that do range matching.
#### Prerelease Identifiers
The method `.inc` takes an additional `identifier` string argument that will
append the value of the string as a prerelease identifier:
```javascript
semver.inc("1.2.3", "prerelease", "beta");
// "1.2.4-beta.0"
```
### Advanced Range Syntax
Advanced range syntax desugars to primitive comparators in deterministic ways.
Advanced ranges may be combined in the same way as primitive comparators using
white space or `||`.
#### Hyphen Ranges `X.Y.Z - A.B.C`
Specifies an inclusive set.
- `1.2.3 - 2.3.4` := `>=1.2.3 <=2.3.4`
If a partial version is provided as the first version in the inclusive range,
then the missing pieces are replaced with zeroes.
- `1.2 - 2.3.4` := `>=1.2.0 <=2.3.4`
If a partial version is provided as the second version in the inclusive range,
then all versions that start with the supplied parts of the tuple are accepted,
but nothing that would be greater than the provided tuple parts.
- `1.2.3 - 2.3` := `>=1.2.3 <2.4.0`
- `1.2.3 - 2` := `>=1.2.3 <3.0.0`
#### X-Ranges `1.2.x` `1.X` `1.2.*` `*`
Any of `X`, `x`, or `*` may be used to "stand in" for one of the numeric values
in the `[major, minor, patch]` tuple.
- `*` := `>=0.0.0` (Any version satisfies)
- `1.x` := `>=1.0.0 <2.0.0` (Matching major version)
- `1.2.x` := `>=1.2.0 <1.3.0` (Matching major and minor versions)
A partial version range is treated as an X-Range, so the special character is in
fact optional.
- `""` (empty string) := `*` := `>=0.0.0`
- `1` := `1.x.x` := `>=1.0.0 <2.0.0`
- `1.2` := `1.2.x` := `>=1.2.0 <1.3.0`
#### Tilde Ranges `~1.2.3` `~1.2` `~1`
Allows patch-level changes if a minor version is specified on the comparator.
Allows minor-level changes if not.
- `~1.2.3` := `>=1.2.3 <1.(2+1).0` := `>=1.2.3 <1.3.0`
- `~1.2` := `>=1.2.0 <1.(2+1).0` := `>=1.2.0 <1.3.0` (Same as `1.2.x`)
- `~1` := `>=1.0.0 <(1+1).0.0` := `>=1.0.0 <2.0.0` (Same as `1.x`)
- `~0.2.3` := `>=0.2.3 <0.(2+1).0` := `>=0.2.3 <0.3.0`
- `~0.2` := `>=0.2.0 <0.(2+1).0` := `>=0.2.0 <0.3.0` (Same as `0.2.x`)
- `~0` := `>=0.0.0 <(0+1).0.0` := `>=0.0.0 <1.0.0` (Same as `0.x`)
- `~1.2.3-beta.2` := `>=1.2.3-beta.2 <1.3.0` Note that prereleases in the
`1.2.3` version will be allowed, if they are greater than or equal to
`beta.2`. So, `1.2.3-beta.4` would be allowed, but `1.2.4-beta.2` would not,
because it is a prerelease of a different `[major, minor, patch]` tuple.
#### Caret Ranges `^1.2.3` `^0.2.5` `^0.0.4`
Allows changes that do not modify the left-most non-zero element in the
`[major, minor, patch]` tuple. In other words, this allows patch and minor
updates for versions `1.0.0` and above, patch updates for versions
`0.X >=0.1.0`, and _no_ updates for versions `0.0.X`.
Many authors treat a `0.x` version as if the `x` were the major
"breaking-change" indicator.
Caret ranges are ideal when an author may make breaking changes between `0.2.4`
and `0.3.0` releases, which is a common practice. However, it presumes that
there will _not_ be breaking changes between `0.2.4` and `0.2.5`. It allows for
changes that are presumed to be additive (but non-breaking), according to
commonly observed practices.
- `^1.2.3` := `>=1.2.3 <2.0.0`
- `^0.2.3` := `>=0.2.3 <0.3.0`
- `^0.0.3` := `>=0.0.3 <0.0.4`
- `^1.2.3-beta.2` := `>=1.2.3-beta.2 <2.0.0` Note that prereleases in the
`1.2.3` version will be allowed, if they are greater than or equal to
`beta.2`. So, `1.2.3-beta.4` would be allowed, but `1.2.4-beta.2` would not,
because it is a prerelease of a different `[major, minor, patch]` tuple.
- `^0.0.3-beta` := `>=0.0.3-beta <0.0.4` Note that prereleases in the `0.0.3`
version _only_ will be allowed, if they are greater than or equal to `beta`.
So, `0.0.3-pr.2` would be allowed.
When parsing caret ranges, a missing `patch` value desugars to the number `0`,
but will allow flexibility within that value, even if the major and minor
versions are both `0`.
- `^1.2.x` := `>=1.2.0 <2.0.0`
- `^0.0.x` := `>=0.0.0 <0.1.0`
- `^0.0` := `>=0.0.0 <0.1.0`
A missing `minor` and `patch` values will desugar to zero, but also allow
flexibility within those values, even if the major version is zero.
- `^1.x` := `>=1.0.0 <2.0.0`
- `^0.x` := `>=0.0.0 <1.0.0`
### Range Grammar
Putting all this together, here is a Backus-Naur grammar for ranges, for the
benefit of parser authors:
```bnf
range-set ::= range ( logical-or range ) *
logical-or ::= ( " " ) * "||" ( " " ) *
range ::= hyphen | simple ( " " simple ) * | ""
hyphen ::= partial " - " partial
simple ::= primitive | partial | tilde | caret
primitive ::= ( "<" | ">" | ">=" | "<=" | "=" ) partial
partial ::= xr ( "." xr ( "." xr qualifier ? )? )?
xr ::= "x" | "X" | "*" | nr
nr ::= "0" | ["1"-"9"] ( ["0"-"9"] ) *
tilde ::= "~" partial
caret ::= "^" partial
qualifier ::= ( "-" pre )? ( "+" build )?
pre ::= parts
build ::= parts
parts ::= part ( "." part ) *
part ::= nr | [-0-9A-Za-z]+
```
## Functions
All methods and classes take a final `options` object argument. All options in
this object are `false` by default. The options supported are:
- `includePrerelease` Set to suppress the
[default behavior](https://github.com/denoland/deno_std/tree/main/semver#prerelease-tags)
of excluding prerelease tagged versions from ranges unless they are explicitly
opted into.
Strict-mode Comparators and Ranges will be strict about the SemVer strings that
they parse.
- `valid(v)`: Return the parsed version, or null if it"s not valid.
- `inc(v, release)`: Return the version incremented by the release type
(`major`, `premajor`, `minor`, `preminor`, `patch`, `prepatch`, or
`prerelease`), or null if it"s not valid
- `premajor` in one call will bump the version up to the next major version
and down to a prerelease of that major version. `preminor`, and `prepatch`
work the same way.
- If called from a non-prerelease version, the `prerelease` will work the same
as `prepatch`. It increments the patch version, then makes a prerelease. If
the input version is already a prerelease it simply increments it.
- `prerelease(v)`: Returns an array of prerelease components, or null if none
exist. Example: `prerelease("1.2.3-alpha.1") -> ["alpha", 1]`
- `major(v)`: Return the major version number.
- `minor(v)`: Return the minor version number.
- `patch(v)`: Return the patch version number.
- `intersects(r1, r2)`: Return true if the two supplied ranges or comparators
intersect.
- `parse(v)`: Attempt to parse a string as a semantic version, returning either
a `SemVer` object or `null`.
### Comparison
- `gt(v1, v2)`: `v1 > v2`
- `gte(v1, v2)`: `v1 >= v2`
- `lt(v1, v2)`: `v1 < v2`
- `lte(v1, v2)`: `v1 <= v2`
- `eq(v1, v2)`: `v1 == v2` This is true if they"re logically equivalent, even if
they"re not the exact same string. You already know how to compare strings.
- `neq(v1, v2)`: `v1 != v2` The opposite of `eq`.
- `cmp(v1, comparator, v2)`: Pass in a comparison string, and it'll call the
corresponding function above. `"==="` and `"!=="` do simple string comparison,
but are included for completeness. Throws if an invalid comparison string is
provided.
- `compare(v1, v2)`: Return `0` if `v1 == v2`, or `1` if `v1` is greater, or
`-1` if `v2` is greater. Sorts in ascending order if passed to `Array.sort()`.
- `rcompare(v1, v2)`: The reverse of compare. Sorts an array of versions in
descending order when passed to `Array.sort()`.
- `compareBuild(v1, v2)`: The same as `compare` but considers `build` when two
versions are equal. Sorts in ascending order if passed to `Array.sort()`. `v2`
is greater. Sorts in ascending order if passed to `Array.sort()`.
- `diff(v1, v2)`: Returns difference between two versions by the release type
(`major`, `premajor`, `minor`, `preminor`, `patch`, `prepatch`, or
`prerelease`), or null if the versions are the same.
### Comparators
- `intersects(comparator)`: Return true if the comparators intersect
### Ranges
- `validRange(range)`: Return the valid range or null if it"s not valid
- `satisfies(version, range)`: Return true if the version satisfies the range.
- `maxSatisfying(versions, range)`: Return the highest version in the list that
satisfies the range, or `null` if none of them do.
- `minSatisfying(versions, range)`: Return the lowest version in the list that
satisfies the range, or `null` if none of them do.
- `minVersion(range)`: Return the lowest version that can possibly match the
given range.
- `gtr(version, range)`: Return `true` if version is greater than all the
versions possible in the range.
- `ltr(version, range)`: Return `true` if version is less than all the versions
possible in the range.
- `outside(version, range, hilo)`: Return true if the version is outside the
bounds of the range in either the high or low direction. The `hilo` argument
must be either the string `">"` or `"<"`. (This is the function called by
`gtr` and `ltr`.)
- `intersects(range)`: Return true if any of the ranges comparators intersect
Note that, since ranges may be non-contiguous, a version might not be greater
than a range, less than a range, _or_ satisfy a range! For example, the range
`1.2 <1.2.9 || >2.0.0` would have a hole from `1.2.9` until `2.0.0`, so the
version `1.2.10` would not be greater than the range (because `2.0.1` satisfies,
which is higher), nor less than the range (since `1.2.8` satisfies, which is
lower), and it also does not satisfy the range.
If you want to know if a version satisfies or does not satisfy a range, use the
`satisfies(version, range)` function.

163
semver/comparators_test.ts Normal file
View File

@ -0,0 +1,163 @@
// Copyright Isaac Z. Schlueter and Contributors. All rights reserved. ISC license.
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import { assert, assertEquals } from "../testing/asserts.ts";
import * as semver from "./mod.ts";
type Version = string;
Deno.test("comparators", function (): void {
// [range, comparators]
// turn range into a set of individual comparators
const versions: [Version, string[][]][] = [
["1.0.0 - 2.0.0", [[">=1.0.0", "<=2.0.0"]]],
["1.0.0", [["1.0.0"]]],
[">=*", [[""]]],
["", [[""]]],
["*", [[""]]],
["*", [[""]]],
[">=1.0.0", [[">=1.0.0"]]],
[">=1.0.0", [[">=1.0.0"]]],
[">=1.0.0", [[">=1.0.0"]]],
[">1.0.0", [[">1.0.0"]]],
[">1.0.0", [[">1.0.0"]]],
["<=2.0.0", [["<=2.0.0"]]],
["1", [[">=1.0.0", "<2.0.0"]]],
["<=2.0.0", [["<=2.0.0"]]],
["<=2.0.0", [["<=2.0.0"]]],
["<2.0.0", [["<2.0.0"]]],
["<2.0.0", [["<2.0.0"]]],
[">=0.1.97", [[">=0.1.97"]]],
[">=0.1.97", [[">=0.1.97"]]],
["0.1.20 || 1.2.4", [["0.1.20"], ["1.2.4"]]],
[">=0.2.3 || <0.0.1", [[">=0.2.3"], ["<0.0.1"]]],
[">=0.2.3 || <0.0.1", [[">=0.2.3"], ["<0.0.1"]]],
[">=0.2.3 || <0.0.1", [[">=0.2.3"], ["<0.0.1"]]],
["||", [[""], [""]]],
["2.x.x", [[">=2.0.0", "<3.0.0"]]],
["1.2.x", [[">=1.2.0", "<1.3.0"]]],
[
"1.2.x || 2.x",
[
[">=1.2.0", "<1.3.0"],
[">=2.0.0", "<3.0.0"],
],
],
[
"1.2.x || 2.x",
[
[">=1.2.0", "<1.3.0"],
[">=2.0.0", "<3.0.0"],
],
],
["x", [[""]]],
["2.*.*", [[">=2.0.0", "<3.0.0"]]],
["1.2.*", [[">=1.2.0", "<1.3.0"]]],
[
"1.2.* || 2.*",
[
[">=1.2.0", "<1.3.0"],
[">=2.0.0", "<3.0.0"],
],
],
[
"1.2.* || 2.*",
[
[">=1.2.0", "<1.3.0"],
[">=2.0.0", "<3.0.0"],
],
],
["*", [[""]]],
["2", [[">=2.0.0", "<3.0.0"]]],
["2.3", [[">=2.3.0", "<2.4.0"]]],
["~2.4", [[">=2.4.0", "<2.5.0"]]],
["~2.4", [[">=2.4.0", "<2.5.0"]]],
["~>3.2.1", [[">=3.2.1", "<3.3.0"]]],
["~1", [[">=1.0.0", "<2.0.0"]]],
["~>1", [[">=1.0.0", "<2.0.0"]]],
["~1.0", [[">=1.0.0", "<1.1.0"]]],
["<1", [["<1.0.0"]]],
[">=1", [[">=1.0.0"]]],
["<1.2", [["<1.2.0"]]],
["1", [[">=1.0.0", "<2.0.0"]]],
["1 2", [[">=1.0.0", "<2.0.0", ">=2.0.0", "<3.0.0"]]],
["1.2 - 3.4.5", [[">=1.2.0", "<=3.4.5"]]],
["1.2.3 - 3.4", [[">=1.2.3", "<3.5.0"]]],
["1.2.3 - 3", [[">=1.2.3", "<4.0.0"]]],
[">*", [["<0.0.0"]]],
["<*", [["<0.0.0"]]],
];
versions.forEach(function (v) {
const pre = v[0];
const wanted = v[1];
const found = semver.toComparators(v[0]);
const jw = JSON.stringify(wanted);
assertEquals(found, wanted, "toComparators(" + pre + ") === " + jw);
});
});
Deno.test("test", function (): void {
const c = new semver.Comparator(">=1.2.3");
assert(c.test("1.2.4"));
const c2 = new semver.Comparator(c);
assert(c2.test("1.2.4"));
});
Deno.test("intersect", function (): void {
const versions: [string, string, boolean][] = [
// One is a Version
["1.3.0", ">=1.3.0", true],
["1.3.0", ">1.3.0", false],
[">=1.3.0", "1.3.0", true],
[">1.3.0", "1.3.0", false],
// Same direction increasing
[">1.3.0", ">1.2.0", true],
[">1.2.0", ">1.3.0", true],
[">=1.2.0", ">1.3.0", true],
[">1.2.0", ">=1.3.0", true],
// Same direction decreasing
["<1.3.0", "<1.2.0", true],
["<1.2.0", "<1.3.0", true],
["<=1.2.0", "<1.3.0", true],
["<1.2.0", "<=1.3.0", true],
// Different directions, same semver and inclusive operator
[">=1.3.0", "<=1.3.0", true],
[">=v1.3.0", "<=1.3.0", true],
[">=1.3.0", ">=1.3.0", true],
["<=1.3.0", "<=1.3.0", true],
["<=1.3.0", "<=v1.3.0", true],
[">1.3.0", "<=1.3.0", false],
[">=1.3.0", "<1.3.0", false],
// Opposite matching directions
[">1.0.0", "<2.0.0", true],
[">=1.0.0", "<2.0.0", true],
[">=1.0.0", "<=2.0.0", true],
[">1.0.0", "<=2.0.0", true],
["<=2.0.0", ">1.0.0", true],
["<=1.0.0", ">=2.0.0", false],
];
versions.forEach(function (v) {
const comparator1 = new semver.Comparator(v[0]);
const comparator2 = new semver.Comparator(v[1]);
const expect = v[2];
const actual1 = comparator1.intersects(comparator2);
const actual2 = comparator2.intersects(comparator1);
const actual3 = semver.intersects(comparator1, comparator2);
const actual4 = semver.intersects(comparator2, comparator1);
const actual5 = semver.intersects(v[0], v[1]);
const actual6 = semver.intersects(v[1], v[0]);
assertEquals(actual1, expect);
assertEquals(actual2, expect);
assertEquals(actual3, expect);
assertEquals(actual4, expect);
assertEquals(actual5, expect);
assertEquals(actual6, expect);
});
});
Deno.test("tostrings", function (): void {
assertEquals(new semver.Comparator(">= v1.2.3").toString(), ">=1.2.3");
});

121
semver/comparison_test.ts Normal file
View File

@ -0,0 +1,121 @@
// Copyright Isaac Z. Schlueter and Contributors. All rights reserved. ISC license.
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import { assert, assertEquals, assertThrows } from "../testing/asserts.ts";
import * as semver from "./mod.ts";
Deno.test("comparison", function (): void {
// [version1, version2]
// version1 should be greater than version2
const versions: [string, string][] = [
["0.0.0", "0.0.0-foo"],
["0.0.1", "0.0.0"],
["1.0.0", "0.9.9"],
["0.10.0", "0.9.0"],
["0.99.0", "0.10.0"],
["2.0.0", "1.2.3"],
["1.2.3", "1.2.3-asdf"],
["1.2.3", "1.2.3-4"],
["1.2.3", "1.2.3-4-foo"],
["1.2.3-5-foo", "1.2.3-5"],
["1.2.3-5", "1.2.3-4"],
["1.2.3-5-foo", "1.2.3-5-Foo"],
["3.0.0", "2.7.2+asdf"],
["1.2.3-a.10", "1.2.3-a.5"],
["1.2.3-a.b", "1.2.3-a.5"],
["1.2.3-a.b", "1.2.3-a"],
["1.2.3-a.b.c.10.d.5", "1.2.3-a.b.c.5.d.100"],
["1.2.3-r2", "1.2.3-r100"],
["1.2.3-r100", "1.2.3-R2"],
];
versions.forEach(function (v) {
const v0 = v[0];
const v1 = v[1];
assert(semver.gt(v0, v1), "gt('" + v0 + "', '" + v1 + "')");
assert(semver.lt(v1, v0), "lt('" + v1 + "', '" + v0 + "')");
assert(!semver.gt(v1, v0), "!gt('" + v1 + "', '" + v0 + "')");
assert(!semver.lt(v0, v1), "!lt('" + v0 + "', '" + v1 + "')");
assert(semver.eq(v0, v0), "eq('" + v0 + "', '" + v0 + "')");
assert(semver.eq(v1, v1), "eq('" + v1 + "', '" + v1 + "')");
assert(semver.neq(v0, v1), "neq('" + v0 + "', '" + v1 + "')");
assert(
semver.cmp(v1, "==", v1),
"cmp('" + v1 + "' == '" + v1 + "')",
);
assert(
semver.cmp(v0, ">=", v1),
"cmp('" + v0 + "' >= '" + v1 + "')",
);
assert(
semver.cmp(v1, "<=", v0),
"cmp('" + v1 + "' <= '" + v0 + "')",
);
assert(
semver.cmp(v0, "!=", v1),
"cmp('" + v0 + "' != '" + v1 + "')",
);
});
});
Deno.test("compareBuild", function (): void {
const noBuild = new semver.SemVer("1.0.0");
const build0 = new semver.SemVer("1.0.0+0");
const build1 = new semver.SemVer("1.0.0+1");
const build10 = new semver.SemVer("1.0.0+1.0");
assertEquals(noBuild.compareBuild(build0), -1);
assertEquals(build0.compareBuild(build0), 0);
assertEquals(build0.compareBuild(noBuild), 1);
assertEquals(build0.compareBuild("1.0.0+0.0"), -1);
assertEquals(build0.compareBuild(build1), -1);
assertEquals(build1.compareBuild(build0), 1);
assertEquals(build10.compareBuild(build1), 1);
});
Deno.test("rcompare", function (): void {
assertEquals(semver.rcompare("1.0.0", "1.0.1"), 1);
assertEquals(semver.rcompare("1.0.0", "1.0.0"), 0);
assertEquals(semver.rcompare("1.0.0+0", "1.0.0"), 0);
assertEquals(semver.rcompare("1.0.1", "1.0.0"), -1);
});
Deno.test("compareMainVsPre", function (): void {
const s = new semver.SemVer("1.2.3");
assertEquals(s.compareMain("2.3.4"), -1);
assertEquals(s.compareMain("1.2.4"), -1);
assertEquals(s.compareMain("0.1.2"), 1);
assertEquals(s.compareMain("1.2.2"), 1);
assertEquals(s.compareMain("1.2.3-pre"), 0);
const p = new semver.SemVer("1.2.3-alpha.0.pr.1");
assertEquals(p.comparePre("9.9.9-alpha.0.pr.1"), 0);
assertEquals(p.comparePre("1.2.3"), -1);
assertEquals(p.comparePre("1.2.3-alpha.0.pr.2"), -1);
assertEquals(p.comparePre("1.2.3-alpha.0.2"), 1);
});
Deno.test("compareIdentifierst", function (): void {
const set = [
["1", "2"],
["alpha", "beta"],
["0", "beta"],
];
set.forEach(function (ab) {
const a = ab[0];
const b = ab[1];
assertEquals(semver.compareIdentifiers(a, b), -1);
assertEquals(semver.rcompareIdentifiers(a, b), 1);
});
assertEquals(semver.compareIdentifiers("0", "0"), 0);
assertEquals(semver.rcompareIdentifiers("0", "0"), 0);
});
Deno.test("invalidCmpUsage", function (): void {
assertThrows(
function () {
semver.cmp("1.2.3", "a frog" as semver.Operator, "4.5.6");
},
TypeError,
"Invalid operator: a frog",
);
});

32
semver/diff_test.ts Normal file
View File

@ -0,0 +1,32 @@
// Copyright Isaac Z. Schlueter and Contributors. All rights reserved. ISC license.
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import { assertEquals } from "../testing/asserts.ts";
import * as semver from "./mod.ts";
Deno.test("diff", function (): void {
// [version1, version2, result]
// diff(version1, version2) -> result
const versions: [string, string, semver.ReleaseType | null][] = [
["1.2.3", "0.2.3", "major"],
["1.4.5", "0.2.3", "major"],
["1.2.3", "2.0.0-pre", "premajor"],
["1.2.3", "1.3.3", "minor"],
["1.0.1", "1.1.0-pre", "preminor"],
["1.2.3", "1.2.4", "patch"],
["1.2.3", "1.2.4-pre", "prepatch"],
["0.0.1", "0.0.1-pre", "prerelease"],
["0.0.1", "0.0.1-pre-2", "prerelease"],
["1.1.0", "1.1.0-pre", "prerelease"],
["1.1.0-pre-1", "1.1.0-pre-2", "prerelease"],
["1.0.0", "1.0.0", null],
];
versions.forEach(function (v) {
const version1 = v[0];
const version2 = v[1];
const wanted = v[2];
const found = semver.diff(version1, version2);
const cmd = "diff(" + version1 + ", " + version2 + ")";
assertEquals(found, wanted, cmd + " === " + wanted);
});
});

41
semver/equality_test.ts Normal file
View File

@ -0,0 +1,41 @@
// Copyright Isaac Z. Schlueter and Contributors. All rights reserved. ISC license.
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import { assert } from "../testing/asserts.ts";
import * as semver from "./mod.ts";
Deno.test("equality", function (): void {
// [version1, version2]
// version1 should be equivalent to version2
const versions: [string, string][] = [
["1.2.3-beta+build", "1.2.3-beta+otherbuild"],
["1.2.3+build", "1.2.3+otherbuild"],
[" v1.2.3+build", "1.2.3+otherbuild"],
];
versions.forEach(function (v) {
const v0 = v[0];
const v1 = v[1];
assert(semver.eq(v0, v1), "eq('" + v0 + "', '" + v1 + "')");
assert(!semver.neq(v0, v1), "!neq('" + v0 + "', '" + v1 + "')");
assert(semver.cmp(v0, "==", v1), "cmp(" + v0 + "==" + v1 + ")");
assert(!semver.cmp(v0, "!=", v1), "!cmp(" + v0 + "!=" + v1 + ")");
assert(!semver.cmp(v0, "===", v1), "!cmp(" + v0 + "===" + v1 + ")");
assert(semver.cmp(v0, "!==", v1), "cmp(" + v0 + "!==" + v1 + ")");
assert(
!semver.cmp(
new semver.SemVer(v0),
"!==",
new semver.SemVer(v1),
),
"cmp(" + v0 + "!==" + v1 + ") object",
);
assert(!semver.gt(v0, v1), "!gt('" + v0 + "', '" + v1 + "')");
assert(semver.gte(v0, v1), "gte('" + v0 + "', '" + v1 + "')");
assert(!semver.lt(v0, v1), "!lt('" + v0 + "', '" + v1 + "')");
assert(semver.lte(v0, v1), "lte('" + v0 + "', '" + v1 + "')");
});
});

148
semver/gtr_test.ts Normal file
View File

@ -0,0 +1,148 @@
// Copyright Isaac Z. Schlueter and Contributors. All rights reserved. ISC license.
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import { assert } from "../testing/asserts.ts";
import * as semver from "./mod.ts";
Deno.test("gtr", function (): void {
// [range, version]
// Version should be greater than range
const versions: ReadonlyArray<[string, string]> = [
["~1.2.2", "1.3.0"],
["~0.6.1-1", "0.7.1-1"],
["1.0.0 - 2.0.0", "2.0.1"],
["1.0.0", "1.0.1-beta1"],
["1.0.0", "2.0.0"],
["<=2.0.0", "2.1.1"],
["<=2.0.0", "3.2.9"],
["<2.0.0", "2.0.0"],
["0.1.20 || 1.2.4", "1.2.5"],
["2.x.x", "3.0.0"],
["1.2.x", "1.3.0"],
["1.2.x || 2.x", "3.0.0"],
["2.*.*", "5.0.1"],
["1.2.*", "1.3.3"],
["1.2.* || 2.*", "4.0.0"],
["2", "3.0.0"],
["2.3", "2.4.2"],
["~2.4", "2.5.0"], // >=2.4.0 <2.5.0
["~2.4", "2.5.5"],
["~>3.2.1", "3.3.0"], // >=3.2.1 <3.3.0
["~1", "2.2.3"], // >=1.0.0 <2.0.0
["~>1", "2.2.4"],
["~1.0", "1.1.2"], // >=1.0.0 <1.1.0
["<1.2", "1.2.0"],
["~v0.5.4-pre", "0.6.0"],
["~v0.5.4-pre", "0.6.1-pre"],
["=0.7.x", "0.8.0"],
["=0.7.x", "0.8.0-asdf"],
["<0.7.x", "0.7.0"],
["~1.2.2", "1.3.0"],
["1.0.0 - 2.0.0", "2.2.3"],
["1.0.0", "1.0.1"],
["<=2.0.0", "3.0.0"],
["<=2.0.0", "2.9999.9999"],
["<=2.0.0", "2.2.9"],
["<2.0.0", "2.9999.9999"],
["<2.0.0", "2.2.9"],
["2.x.x", "3.1.3"],
["1.2.x", "1.3.3"],
["1.2.x || 2.x", "3.1.3"],
["2.*.*", "3.1.3"],
["1.2.*", "1.3.3"],
["1.2.* || 2.*", "3.1.3"],
["2", "3.1.2"],
["2.3", "2.4.1"],
["~2.4", "2.5.0"], // >=2.4.0 <2.5.0
["~>3.2.1", "3.3.2"], // >=3.2.1 <3.3.0
["~1", "2.2.3"], // >=1.0.0 <2.0.0
["~>1", "2.2.3"],
["~1.0", "1.1.0"], // >=1.0.0 <1.1.0
["<1", "1.0.0"],
["=0.7.x", "0.8.2"],
["<0.7.x", "0.7.2"],
];
versions.forEach(function (tuple) {
const range = tuple[0];
const version = tuple[1];
const msg = `gtr(${version}, ${range})`;
assert(semver.gtr(version, range), msg);
});
});
Deno.test("gtrNegative", function (): void {
// [range, version]
// Version should be greater than range
const versions: ReadonlyArray<[string, string]> = [
["~0.6.1-1", "0.6.1-1"],
["1.0.0 - 2.0.0", "1.2.3"],
["1.0.0 - 2.0.0", "0.9.9"],
["1.0.0", "1.0.0"],
[">=*", "0.2.4"],
["*", "1.2.3"],
["*", "v1.2.3-foo"],
[">=1.0.0", "1.0.0"],
[">=1.0.0", "1.0.1"],
[">=1.0.0", "1.1.0"],
[">1.0.0", "1.0.1"],
[">1.0.0", "1.1.0"],
["<=2.0.0", "2.0.0"],
["<=2.0.0", "1.9999.9999"],
["<=2.0.0", "0.2.9"],
["<2.0.0", "1.9999.9999"],
["<2.0.0", "0.2.9"],
[">=0.1.97", "v0.1.97"],
[">=0.1.97", "0.1.97"],
["0.1.20 || 1.2.4", "1.2.4"],
["0.1.20 || >1.2.4", "1.2.4"],
["0.1.20 || 1.2.4", "1.2.3"],
["0.1.20 || 1.2.4", "0.1.20"],
[">=0.2.3 || <0.0.1", "0.0.0"],
[">=0.2.3 || <0.0.1", "0.2.3"],
[">=0.2.3 || <0.0.1", "0.2.4"],
["||", "1.3.4"],
["2.x.x", "2.1.3"],
["1.2.x", "1.2.3"],
["1.2.x || 2.x", "2.1.3"],
["1.2.x || 2.x", "1.2.3"],
["x", "1.2.3"],
["2.*.*", "2.1.3"],
["1.2.*", "1.2.3"],
["1.2.* || 2.*", "2.1.3"],
["1.2.* || 2.*", "1.2.3"],
["1.2.* || 2.*", "1.2.3"],
["*", "1.2.3"],
["2", "2.1.2"],
["2.3", "2.3.1"],
["~2.4", "2.4.0"], // >=2.4.0 <2.5.0
["~2.4", "2.4.5"],
["~>3.2.1", "3.2.2"], // >=3.2.1 <3.3.0
["~1", "1.2.3"], // >=1.0.0 <2.0.0
["~>1", "1.2.3"],
["~1.0", "1.0.2"], // >=1.0.0 <1.1.0
[">=1", "1.0.0"],
["<1.2", "1.1.1"],
["~v0.5.4-pre", "0.5.5"],
["~v0.5.4-pre", "0.5.4"],
["=0.7.x", "0.7.2"],
[">=0.7.x", "0.7.2"],
["=0.7.x", "0.7.0-asdf"],
[">=0.7.x", "0.7.0-asdf"],
["<=0.7.x", "0.6.2"],
[">0.2.3 >0.2.4 <=0.2.5", "0.2.5"],
[">=0.2.3 <=0.2.4", "0.2.4"],
["1.0.0 - 2.0.0", "2.0.0"],
["^1", "0.0.0-0"],
["^3.0.0", "2.0.0"],
["^1.0.0 || ~2.0.1", "2.0.0"],
["^0.1.0 || ~3.0.1 || 5.0.0", "3.2.0"],
["^0.1.0 || ~3.0.1 || >4 <=5.0.0", "3.5.0"],
];
versions.forEach(function (tuple) {
const range = tuple[0];
const version = tuple[1];
const msg = `!gtr(${version}, ${range})`;
assert(!semver.gtr(version, range), msg);
});
});

176
semver/increment_test.ts Normal file
View File

@ -0,0 +1,176 @@
// Copyright Isaac Z. Schlueter and Contributors. All rights reserved. ISC license.
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import { assertEquals, assertThrows } from "../testing/asserts.ts";
import * as semver from "./mod.ts";
import type { Options, ReleaseType } from "./mod.ts";
Deno.test("increment", function (): void {
// [version, inc, result, identifier]
// inc(version, inc) -> result
const versions: [string, ReleaseType, string | null, Options?, string?][] = [
["1.2.3", "major", "2.0.0"],
["1.2.3", "minor", "1.3.0"],
["1.2.3", "patch", "1.2.4"],
["1.2.3-tag", "major", "2.0.0"],
["1.2.3", "fake" as ReleaseType, null],
["1.2.0-0", "patch", "1.2.0"],
["fake", "major", null],
["1.2.3-4", "major", "2.0.0"],
["1.2.3-4", "minor", "1.3.0"],
["1.2.3-4", "patch", "1.2.3"],
["1.2.3-alpha.0.beta", "major", "2.0.0"],
["1.2.3-alpha.0.beta", "minor", "1.3.0"],
["1.2.3-alpha.0.beta", "patch", "1.2.3"],
["1.2.4", "prerelease", "1.2.5-0"],
["1.2.3-0", "prerelease", "1.2.3-1"],
["1.2.3-alpha.0", "prerelease", "1.2.3-alpha.1"],
["1.2.3-alpha.1", "prerelease", "1.2.3-alpha.2"],
["1.2.3-alpha.2", "prerelease", "1.2.3-alpha.3"],
["1.2.3-alpha.0.beta", "prerelease", "1.2.3-alpha.1.beta"],
["1.2.3-alpha.1.beta", "prerelease", "1.2.3-alpha.2.beta"],
["1.2.3-alpha.2.beta", "prerelease", "1.2.3-alpha.3.beta"],
["1.2.3-alpha.10.0.beta", "prerelease", "1.2.3-alpha.10.1.beta"],
["1.2.3-alpha.10.1.beta", "prerelease", "1.2.3-alpha.10.2.beta"],
["1.2.3-alpha.10.2.beta", "prerelease", "1.2.3-alpha.10.3.beta"],
["1.2.3-alpha.10.beta.0", "prerelease", "1.2.3-alpha.10.beta.1"],
["1.2.3-alpha.10.beta.1", "prerelease", "1.2.3-alpha.10.beta.2"],
["1.2.3-alpha.10.beta.2", "prerelease", "1.2.3-alpha.10.beta.3"],
["1.2.3-alpha.9.beta", "prerelease", "1.2.3-alpha.10.beta"],
["1.2.3-alpha.10.beta", "prerelease", "1.2.3-alpha.11.beta"],
["1.2.3-alpha.11.beta", "prerelease", "1.2.3-alpha.12.beta"],
["1.2.0", "prepatch", "1.2.1-0"],
["1.2.0-1", "prepatch", "1.2.1-0"],
["1.2.0", "preminor", "1.3.0-0"],
["1.2.3-1", "preminor", "1.3.0-0"],
["1.2.0", "premajor", "2.0.0-0"],
["1.2.3-1", "premajor", "2.0.0-0"],
["1.2.0-1", "minor", "1.2.0"],
["1.0.0-1", "major", "1.0.0"],
["1.2.3", "major", "2.0.0", undefined, "dev"],
["1.2.3", "minor", "1.3.0", undefined, "dev"],
["1.2.3", "patch", "1.2.4", undefined, "dev"],
["1.2.3-tag", "major", "2.0.0", undefined, "dev"],
["1.2.3", "fake" as ReleaseType, null, undefined, "dev"],
["1.2.0-0", "patch", "1.2.0", undefined, "dev"],
["fake", "major", null, undefined, "dev"],
["1.2.3-4", "major", "2.0.0", undefined, "dev"],
["1.2.3-4", "minor", "1.3.0", undefined, "dev"],
["1.2.3-4", "patch", "1.2.3", undefined, "dev"],
["1.2.3-alpha.0.beta", "major", "2.0.0", undefined, "dev"],
["1.2.3-alpha.0.beta", "minor", "1.3.0", undefined, "dev"],
["1.2.3-alpha.0.beta", "patch", "1.2.3", undefined, "dev"],
["1.2.4", "prerelease", "1.2.5-dev.0", undefined, "dev"],
["1.2.3-0", "prerelease", "1.2.3-dev.0", undefined, "dev"],
["1.2.3-alpha.0", "prerelease", "1.2.3-dev.0", undefined, "dev"],
["1.2.3-alpha.0", "prerelease", "1.2.3-alpha.1", undefined, "alpha"],
["1.2.3-alpha.0.beta", "prerelease", "1.2.3-dev.0", undefined, "dev"],
[
"1.2.3-alpha.0.beta",
"prerelease",
"1.2.3-alpha.1.beta",
undefined,
"alpha",
],
["1.2.3-alpha.10.0.beta", "prerelease", "1.2.3-dev.0", undefined, "dev"],
[
"1.2.3-alpha.10.0.beta",
"prerelease",
"1.2.3-alpha.10.1.beta",
undefined,
"alpha",
],
[
"1.2.3-alpha.10.1.beta",
"prerelease",
"1.2.3-alpha.10.2.beta",
undefined,
"alpha",
],
[
"1.2.3-alpha.10.2.beta",
"prerelease",
"1.2.3-alpha.10.3.beta",
undefined,
"alpha",
],
["1.2.3-alpha.10.beta.0", "prerelease", "1.2.3-dev.0", undefined, "dev"],
[
"1.2.3-alpha.10.beta.0",
"prerelease",
"1.2.3-alpha.10.beta.1",
undefined,
"alpha",
],
[
"1.2.3-alpha.10.beta.1",
"prerelease",
"1.2.3-alpha.10.beta.2",
undefined,
"alpha",
],
[
"1.2.3-alpha.10.beta.2",
"prerelease",
"1.2.3-alpha.10.beta.3",
undefined,
"alpha",
],
["1.2.3-alpha.9.beta", "prerelease", "1.2.3-dev.0", undefined, "dev"],
[
"1.2.3-alpha.9.beta",
"prerelease",
"1.2.3-alpha.10.beta",
undefined,
"alpha",
],
[
"1.2.3-alpha.10.beta",
"prerelease",
"1.2.3-alpha.11.beta",
undefined,
"alpha",
],
[
"1.2.3-alpha.11.beta",
"prerelease",
"1.2.3-alpha.12.beta",
undefined,
"alpha",
],
["1.2.0", "prepatch", "1.2.1-dev.0", undefined, "dev"],
["1.2.0-1", "prepatch", "1.2.1-dev.0", undefined, "dev"],
["1.2.0", "preminor", "1.3.0-dev.0", undefined, "dev"],
["1.2.3-1", "preminor", "1.3.0-dev.0", undefined, "dev"],
["1.2.0", "premajor", "2.0.0-dev.0", undefined, "dev"],
["1.2.3-1", "premajor", "2.0.0-dev.0", undefined, "dev"],
["1.2.0-1", "minor", "1.2.0", undefined, "dev"],
["1.0.0-1", "major", "1.0.0", "dev" as Options],
["1.2.3-dev.bar", "prerelease", "1.2.3-dev.0", undefined, "dev"],
];
versions.forEach(function (v) {
const pre = v[0];
const what = v[1];
const wanted = v[2];
const options = v[3];
const id = v[4];
const found = semver.inc(pre, what, options, id);
const cmd = "inc(" + pre + ", " + what + ", " + id + ")";
assertEquals(found, wanted, cmd + " === " + wanted);
const parsed = semver.parse(pre, options);
if (wanted && parsed) {
//todo ?
parsed.inc(what, id);
assertEquals(parsed.version, wanted, cmd + " object version updated");
assertEquals(parsed.raw, wanted, cmd + " object raw field updated");
} else if (parsed) {
assertThrows(function () {
parsed.inc(what, id);
});
} else {
assertEquals(parsed, null);
}
});
});

151
semver/ltr_test.ts Normal file
View File

@ -0,0 +1,151 @@
// Copyright Isaac Z. Schlueter and Contributors. All rights reserved. ISC license.
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import { assert } from "../testing/asserts.ts";
import * as semver from "./mod.ts";
Deno.test("ltr", function (): void {
// [range, version]
// Version should be greater than range
const versions: ReadonlyArray<[string, string]> = [
["~1.2.2", "1.2.1"],
["~0.6.1-1", "0.6.1-0"],
["1.0.0 - 2.0.0", "0.0.1"],
["1.0.0-beta.2", "1.0.0-beta.1"],
["1.0.0", "0.0.0"],
[">=2.0.0", "1.1.1"],
[">=2.0.0", "1.2.9"],
[">2.0.0", "2.0.0"],
["0.1.20 || 1.2.4", "0.1.5"],
["2.x.x", "1.0.0"],
["1.2.x", "1.1.0"],
["1.2.x || 2.x", "1.0.0"],
["2.*.*", "1.0.1"],
["1.2.*", "1.1.3"],
["1.2.* || 2.*", "1.1.9999"],
["2", "1.0.0"],
["2.3", "2.2.2"],
["~2.4", "2.3.0"], // >=2.4.0 <2.5.0
["~2.4", "2.3.5"],
["~>3.2.1", "3.2.0"], // >=3.2.1 <3.3.0
["~1", "0.2.3"], // >=1.0.0 <2.0.0
["~>1", "0.2.4"],
["~1.0", "0.1.2"], // >=1.0.0 <1.1.0
[">1.2", "1.2.0"],
["~v0.5.4-pre", "0.5.4-alpha"],
["~v0.5.4-pre", "0.5.4-alpha"],
["=0.7.x", "0.6.0"],
["=0.7.x", "0.6.0-asdf"],
[">=0.7.x", "0.6.0"],
["~1.2.2", "1.2.1"],
["1.0.0 - 2.0.0", "0.2.3"],
["1.0.0", "0.0.1"],
[">=2.0.0", "1.0.0"],
[">=2.0.0", "1.9999.9999"],
[">=2.0.0", "1.2.9"],
[">2.0.0", "2.0.0"],
[">2.0.0", "1.2.9"],
["2.x.x", "1.1.3"],
["1.2.x", "1.1.3"],
["1.2.x || 2.x", "1.1.3"],
["2.*.*", "1.1.3"],
["1.2.*", "1.1.3"],
["1.2.* || 2.*", "1.1.3"],
["2", "1.9999.9999"],
["2.3", "2.2.1"],
["~2.4", "2.3.0"], // >=2.4.0 <2.5.0
["~>3.2.1", "2.3.2"], // >=3.2.1 <3.3.0
["~1", "0.2.3"], // >=1.0.0 <2.0.0
["~>1", "0.2.3"],
["~1.0", "0.0.0"], // >=1.0.0 <1.1.0
[">1", "1.0.0"],
["=0.7.x", "0.6.2"],
["=0.7.x", "0.7.0-asdf"],
["^1", "1.0.0-0"],
[">=0.7.x", "0.7.0-asdf"],
[">=0.7.x", "0.6.2"],
[">1.2.3", "1.3.0-alpha"],
];
versions.forEach(function (tuple) {
const range = tuple[0];
const version = tuple[1];
const msg = `ltr(${version}, ${range})`;
assert(semver.ltr(version, range), msg);
});
});
Deno.test("ltrNegative", function (): void {
// [range, version]
// Version should be greater than range
const versions: ReadonlyArray<[string, string]> = [
["~0.6.1-1", "0.6.1-1"],
["1.0.0 - 2.0.0", "1.2.3"],
["1.0.0 - 2.0.0", "2.9.9"],
["1.0.0", "1.0.0"],
[">=*", "0.2.4"],
["*", "1.2.3"],
[">=1.0.0", "1.0.0"],
[">=1.0.0", "1.0.1"],
[">=1.0.0", "1.1.0"],
[">1.0.0", "1.0.1"],
[">1.0.0", "1.1.0"],
["<=2.0.0", "2.0.0"],
["<=2.0.0", "1.9999.9999"],
["<=2.0.0", "0.2.9"],
["<2.0.0", "1.9999.9999"],
["<2.0.0", "0.2.9"],
[">=0.1.97", "v0.1.97"],
[">=0.1.97", "0.1.97"],
["0.1.20 || 1.2.4", "1.2.4"],
["0.1.20 || >1.2.4", "1.2.4"],
["0.1.20 || 1.2.4", "1.2.3"],
["0.1.20 || 1.2.4", "0.1.20"],
[">=0.2.3 || <0.0.1", "0.0.0"],
[">=0.2.3 || <0.0.1", "0.2.3"],
[">=0.2.3 || <0.0.1", "0.2.4"],
["||", "1.3.4"],
["2.x.x", "2.1.3"],
["1.2.x", "1.2.3"],
["1.2.x || 2.x", "2.1.3"],
["1.2.x || 2.x", "1.2.3"],
["x", "1.2.3"],
["2.*.*", "2.1.3"],
["1.2.*", "1.2.3"],
["1.2.* || 2.*", "2.1.3"],
["1.2.* || 2.*", "1.2.3"],
["1.2.* || 2.*", "1.2.3"],
["*", "1.2.3"],
["2", "2.1.2"],
["2.3", "2.3.1"],
["~2.4", "2.4.0"], // >=2.4.0 <2.5.0
["~2.4", "2.4.5"],
["~>3.2.1", "3.2.2"], // >=3.2.1 <3.3.0
["~1", "1.2.3"], // >=1.0.0 <2.0.0
["~>1", "1.2.3"],
["~1.0", "1.0.2"], // >=1.0.0 <1.1.0
[">=1", "1.0.0"],
["<1.2", "1.1.1"],
["~v0.5.4-pre", "0.5.5"],
["~v0.5.4-pre", "0.5.4"],
["=0.7.x", "0.7.2"],
[">=0.7.x", "0.7.2"],
["<=0.7.x", "0.6.2"],
[">0.2.3 >0.2.4 <=0.2.5", "0.2.5"],
[">=0.2.3 <=0.2.4", "0.2.4"],
["1.0.0 - 2.0.0", "2.0.0"],
["^3.0.0", "4.0.0"],
["^1.0.0 || ~2.0.1", "2.0.0"],
["^0.1.0 || ~3.0.1 || 5.0.0", "3.2.0"],
["^0.1.0 || ~3.0.1 || >4 <=5.0.0", "3.5.0"],
["^1.0.0-alpha", "1.0.0-beta"],
["~1.0.0-alpha", "1.0.0-beta"],
["=0.1.0", "1.0.0"],
];
versions.forEach(function (tuple) {
const range = tuple[0];
const version = tuple[1];
const msg = `!ltr(${version}, ${range})`;
assert(!semver.ltr(version, range), msg);
});
});

25
semver/major_test.ts Normal file
View File

@ -0,0 +1,25 @@
// Copyright Isaac Z. Schlueter and Contributors. All rights reserved. ISC license.
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import { assertEquals } from "../testing/asserts.ts";
import * as semver from "./mod.ts";
Deno.test("major", function (): void {
// [range, version]
// Version should be detectable despite extra characters
const versions: [string, number][] = [
["1.2.3", 1],
[" 1.2.3 ", 1],
[" 2.2.3-4 ", 2],
[" 3.2.3-pre ", 3],
["v5.2.3", 5],
[" v8.2.3 ", 8],
["\t13.2.3", 13],
];
versions.forEach(function (tuple) {
const range = tuple[0];
const version = tuple[1];
const msg = `major(${range}) = ${version}`;
assertEquals(semver.major(range), version, msg);
});
});

77
semver/minVersion_test.ts Normal file
View File

@ -0,0 +1,77 @@
// Copyright Isaac Z. Schlueter and Contributors. All rights reserved. ISC license.
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import { assert } from "../testing/asserts.ts";
import * as semver from "./mod.ts";
Deno.test("minVersion", function (): void {
// [range, version]
const versions: [string, string | null][] = [
// Stars
["*", "0.0.0"],
["* || >=2", "0.0.0"],
[">=2 || *", "0.0.0"],
[">2 || *", "0.0.0"],
// equal
["1.0.0", "1.0.0"],
["1.0", "1.0.0"],
["1.0.x", "1.0.0"],
["1.0.*", "1.0.0"],
["1", "1.0.0"],
["1.x.x", "1.0.0"],
["1.x.x", "1.0.0"],
["1.*.x", "1.0.0"],
["1.x.*", "1.0.0"],
["1.x", "1.0.0"],
["1.*", "1.0.0"],
["=1.0.0", "1.0.0"],
// Tilde
["~1.1.1", "1.1.1"],
["~1.1.1-beta", "1.1.1-beta"],
["~1.1.1 || >=2", "1.1.1"],
// Carot
["^1.1.1", "1.1.1"],
["^1.1.1-beta", "1.1.1-beta"],
["^1.1.1 || >=2", "1.1.1"],
// '-' operator
["1.1.1 - 1.8.0", "1.1.1"],
["1.1 - 1.8.0", "1.1.0"],
// Less / less or equal
["<2", "0.0.0"],
["<0.0.0-beta", "0.0.0-0"],
["<0.0.1-beta", "0.0.0"],
["<2 || >4", "0.0.0"],
[">4 || <2", "0.0.0"],
["<=2 || >=4", "0.0.0"],
[">=4 || <=2", "0.0.0"],
["<0.0.0-beta >0.0.0-alpha", "0.0.0-alpha.0"],
[">0.0.0-alpha <0.0.0-beta", "0.0.0-alpha.0"],
// Greater than or equal
[">=1.1.1 <2 || >=2.2.2 <2", "1.1.1"],
[">=2.2.2 <2 || >=1.1.1 <2", "1.1.1"],
// Greater than but not equal
[">1.0.0", "1.0.1"],
[">1.0.0-0", "1.0.0-0.0"],
[">1.0.0-beta", "1.0.0-beta.0"],
[">2 || >1.0.0", "1.0.1"],
[">2 || >1.0.0-0", "1.0.0-0.0"],
[">2 || >1.0.0-beta", "1.0.0-beta.0"],
// Impossible range
[">4 <3", null],
];
versions.forEach(function (tuple) {
const range = tuple[0];
const version = tuple[1];
const msg = `minVersion(${range}) = ${version}`;
const min = semver.minVersion(range);
assert((min as null) === version || (min && min.version === version), msg);
});
});

25
semver/minor_test.ts Normal file
View File

@ -0,0 +1,25 @@
// Copyright Isaac Z. Schlueter and Contributors. All rights reserved. ISC license.
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import { assertEquals } from "../testing/asserts.ts";
import * as semver from "./mod.ts";
Deno.test("minor", function (): void {
// [range, version]
// Version should be detectable despite extra characters
const versions: [string, number][] = [
["1.1.3", 1],
[" 1.1.3 ", 1],
[" 1.2.3-4 ", 2],
[" 1.3.3-pre ", 3],
["v1.5.3", 5],
[" v1.8.3 ", 8],
["\t1.13.3", 13],
];
versions.forEach(function (tuple) {
const range = tuple[0];
const version = tuple[1];
const msg = `minor(${range}) = ${version}`;
assertEquals(semver.minor(range), version, msg);
});
});

1621
semver/mod.ts Normal file

File diff suppressed because it is too large Load Diff

25
semver/patch_test.ts Normal file
View File

@ -0,0 +1,25 @@
// Copyright Isaac Z. Schlueter and Contributors. All rights reserved. ISC license.
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import { assertEquals } from "../testing/asserts.ts";
import * as semver from "./mod.ts";
Deno.test("patch", function (): void {
// [range, version]
// Version should be detectable despite extra characters
const versions: [string, number][] = [
["1.2.1", 1],
[" 1.2.1 ", 1],
[" 1.2.2-4 ", 2],
[" 1.2.3-pre ", 3],
["v1.2.5", 5],
[" v1.2.8 ", 8],
["\t1.2.13", 13],
];
versions.forEach(function (tuple) {
const range = tuple[0];
const version = tuple[1];
const msg = `patch(${range}) = ${version}`;
assertEquals(semver.patch(range), version, msg);
});
});

30
semver/prerelease_test.ts Normal file
View File

@ -0,0 +1,30 @@
// Copyright Isaac Z. Schlueter and Contributors. All rights reserved. ISC license.
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import { assertEquals } from "../testing/asserts.ts";
import * as semver from "./mod.ts";
type PrereleaseParts = [string | number, (number | string)?];
Deno.test("prerelease", function (): void {
// [prereleaseParts, version]
const versions: [PrereleaseParts | null, string][] = [
[["alpha", 1], "1.2.2-alpha.1"],
[[1], "0.6.1-1"],
[["beta", 2], "1.0.0-beta.2"],
[["pre"], "v0.5.4-pre"],
[["alpha", 1], "1.2.2-alpha.1"],
[null, "~2.0.0-alpha.1"],
[null, "invalid version"],
];
versions.forEach(function (tuple) {
const expected = tuple[0];
const version = tuple[1];
const msg = "prerelease(" + version + ")";
assertEquals(
semver.prerelease(version),
expected as unknown as string[],
msg,
);
});
});

389
semver/range_test.ts Normal file
View File

@ -0,0 +1,389 @@
// Copyright Isaac Z. Schlueter and Contributors. All rights reserved. ISC license.
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import { assert, assertEquals, assertThrows } from "../testing/asserts.ts";
import * as semver from "./mod.ts";
import type { Options } from "./mod.ts";
Deno.test("range", function (): void {
// [range, version, options]
// version should be included by range
const versions: [string, string, Options?][] = [
["1.0.0 - 2.0.0", "1.2.3"],
["^1.2.3+build", "1.2.3"],
["^1.2.3+build", "1.3.0"],
["1.2.3-pre+asdf - 2.4.3-pre+asdf", "1.2.3"],
["1.2.3-pre+asdf - 2.4.3-pre+asdf", "1.2.3-pre.2"],
["1.2.3-pre+asdf - 2.4.3-pre+asdf", "2.4.3-alpha"],
["1.2.3+asdf - 2.4.3+asdf", "1.2.3"],
["1.0.0", "1.0.0"],
[">=*", "0.2.4"],
["", "1.0.0"],
["*", "1.2.3", {}],
[">=1.0.0", "1.0.0", /asdf/ as unknown as Options],
[">1.0.0", "1.1.0"],
["<=2.0.0", "2.0.0"],
["<=2.0.0", "1.9999.9999"],
["<=2.0.0", "0.2.9"],
["<2.0.0", "1.9999.9999"],
["<2.0.0", "0.2.9"],
[">=0.1.97", "0.1.97"],
["0.1.20 || 1.2.4", "1.2.4"],
[">=0.2.3 || <0.0.1", "0.0.0"],
[">=0.2.3 || <0.0.1", "0.2.3"],
[">=0.2.3 || <0.0.1", "0.2.4"],
["||", "1.3.4"],
["2.x.x", "2.1.3"],
["1.2.x", "1.2.3"],
["1.2.x || 2.x", "2.1.3"],
["1.2.x || 2.x", "1.2.3"],
["x", "1.2.3"],
["2.*.*", "2.1.3"],
["1.2.*", "1.2.3"],
["1.2.* || 2.*", "2.1.3"],
["1.2.* || 2.*", "1.2.3"],
["*", "1.2.3"],
["2", "2.1.2"],
["2.3", "2.3.1"],
["~0.0.1", "0.0.1"],
["~0.0.1", "0.0.2"],
["~x", "0.0.9"], // >=2.4.0 <2.5.0
["~2", "2.0.9"], // >=2.4.0 <2.5.0
["~2.4", "2.4.0"], // >=2.4.0 <2.5.0
["~2.4", "2.4.5"],
["~>3.2.1", "3.2.2"], // >=3.2.1 <3.3.0,
["~1", "1.2.3"], // >=1.0.0 <2.0.0
["~>1", "1.2.3"],
["~1.0", "1.0.2"], // >=1.0.0 <1.1.0,
[">=1", "1.0.0"],
["<1.2", "1.1.1"],
["~v0.5.4-pre", "0.5.5"],
["~v0.5.4-pre", "0.5.4"],
["=0.7.x", "0.7.2"],
["<=0.7.x", "0.7.2"],
[">=0.7.x", "0.7.2"],
["<=0.7.x", "0.6.2"],
["~1.2.1 >=1.2.3", "1.2.3"],
["~1.2.1 =1.2.3", "1.2.3"],
["~1.2.1 1.2.3", "1.2.3"],
["~1.2.1 >=1.2.3 1.2.3", "1.2.3"],
["~1.2.1 1.2.3 >=1.2.3", "1.2.3"],
["~1.2.1 1.2.3", "1.2.3"],
[">=1.2.1 1.2.3", "1.2.3"],
["1.2.3 >=1.2.1", "1.2.3"],
[">=1.2.3 >=1.2.1", "1.2.3"],
[">=1.2.1 >=1.2.3", "1.2.3"],
[">=1.2", "1.2.8"],
["^1.2.3", "1.8.1"],
["^0.1.2", "0.1.2"],
["^0.1", "0.1.2"],
["^0.0.1", "0.0.1"],
["^1.2", "1.4.2"],
["^1.2 ^1", "1.4.2"],
["^1.2.3-alpha", "1.2.3-pre"],
["^1.2.0-alpha", "1.2.0-pre"],
["^0.0.1-alpha", "0.0.1-beta"],
["^0.0.1-alpha", "0.0.1"],
["^0.1.1-alpha", "0.1.1-beta"],
["^x", "1.2.3"],
["x - 1.0.0", "0.9.7"],
["x - 1.x", "0.9.7"],
["1.0.0 - x", "1.9.7"],
["1.x - x", "1.9.7"],
["<=7.x", "7.9.9"],
];
versions.forEach(function (v) {
const range = v[0];
const ver = v[1];
const options = v[2];
assert(
semver.satisfies(ver, range, options),
range + " satisfied by " + ver,
);
});
});
Deno.test("negativeRange", function (): void {
// [range, version]
// version should not be included by range
const versions: [string, string, Options?][] = [
["1.0.0 - 2.0.0", "2.2.3"],
["1.2.3+asdf - 2.4.3+asdf", "1.2.3-pre.2"],
["1.2.3+asdf - 2.4.3+asdf", "2.4.3-alpha"],
["^1.2.3+build", "2.0.0"],
["^1.2.3+build", "1.2.0"],
["^1.2.3", "1.2.3-pre"],
["^1.2", "1.2.0-pre"],
[">1.2", "1.3.0-beta"],
["<=1.2.3", "1.2.3-beta"],
["^1.2.3", "1.2.3-beta"],
["=0.7.x", "0.7.0-asdf"],
[">=0.7.x", "0.7.0-asdf"],
["1.0.0", "1.0.1"],
[">=1.0.0", "0.0.0"],
[">=1.0.0", "0.0.1"],
[">=1.0.0", "0.1.0"],
[">1.0.0", "0.0.1"],
[">1.0.0", "0.1.0"],
["<=2.0.0", "3.0.0"],
["<=2.0.0", "2.9999.9999"],
["<=2.0.0", "2.2.9"],
["<2.0.0", "2.9999.9999"],
["<2.0.0", "2.2.9"],
[">=0.1.97", "0.1.93"],
["0.1.20 || 1.2.4", "1.2.3"],
[">=0.2.3 || <0.0.1", "0.0.3"],
[">=0.2.3 || <0.0.1", "0.2.2"],
["2.x.x", "3.1.3"],
["1.2.x", "1.3.3"],
["1.2.x || 2.x", "3.1.3"],
["1.2.x || 2.x", "1.1.3"],
["2.*.*", "1.1.3"],
["2.*.*", "3.1.3"],
["1.2.*", "1.3.3"],
["1.2.* || 2.*", "3.1.3"],
["1.2.* || 2.*", "1.1.3"],
["2", "1.1.2"],
["2.3", "2.4.1"],
["~0.0.1", "0.1.0-alpha"],
["~0.0.1", "0.1.0"],
["~2.4", "2.5.0"], // >=2.4.0 <2.5.0
["~2.4", "2.3.9"],
["~>3.2.1", "3.3.2"], // >=3.2.1 <3.3.0
["~>3.2.1", "3.2.0"], // >=3.2.1 <3.3.0
["~1", "0.2.3"], // >=1.0.0 <2.0.0
["~>1", "2.2.3"],
["~1.0", "1.1.0"], // >=1.0.0 <1.1.0
["<1", "1.0.0"],
[">=1.2", "1.1.1"],
["~v0.5.4-beta", "0.5.4-alpha"],
["=0.7.x", "0.8.2"],
[">=0.7.x", "0.6.2"],
["<0.7.x", "0.7.2"],
["<1.2.3", "1.2.3-beta"],
["=1.2.3", "1.2.3-beta"],
[">1.2", "1.2.8"],
["^0.0.1", "0.0.2-alpha"],
["^0.0.1", "0.0.2"],
["^1.2.3", "2.0.0-alpha"],
["^1.2.3", "1.2.2"],
["^1.2", "1.1.9"],
// invalid ranges never satisfied!
["blerg", "1.2.3"],
["^1.2.3", "2.0.0-pre"],
];
versions.forEach(function (v) {
const range = v[0];
const ver = v[1];
const options = v[2];
const found = semver.satisfies(ver, range, options);
assert(!found, ver + " not satisfied by " + range);
});
});
Deno.test("unlockedPrereleaseRange", function (): void {
// [range, version]
// version should be included by range
const versions: [string, string][] = [
["*", "1.0.0-rc1"],
["^1.0.0", "2.0.0-rc1"],
["^1.0.0-0", "1.0.1-rc1"],
["^1.0.0-rc2", "1.0.1-rc1"],
["^1.0.0", "1.0.1-rc1"],
["^1.0.0", "1.1.0-rc1"],
];
versions.forEach(function (v) {
const range = v[0];
const ver = v[1];
const options = { includePrerelease: true };
assert(
semver.satisfies(ver, range, options),
range + " satisfied by " + ver,
);
});
});
Deno.test("negativeUnlockedPrereleaseRange", function (): void {
// [range, version]
// version should be included by range
const versions: [string, string][] = [
["^1.0.0", "1.0.0-rc1"],
["^1.2.3-rc2", "2.0.0"],
];
versions.forEach(function (v) {
const range = v[0];
const ver = v[1];
const options = { includePrerelease: true };
const found = semver.satisfies(ver, range, options);
assert(!found, ver + " not satisfied by " + range);
});
});
Deno.test("validRange", function (): void {
// [range, result]
// validRange(range) -> result
// translate ranges into their canonical form
const versions: [string | null, string | null, Options?][] = [
["1.0.0 - 2.0.0", ">=1.0.0 <=2.0.0"],
["1.0.0", "1.0.0"],
[">=*", "*"],
["", "*"],
["*", "*"],
["*", "*"],
[">=1.0.0", ">=1.0.0"],
[">1.0.0", ">1.0.0"],
["<=2.0.0", "<=2.0.0"],
["1", ">=1.0.0 <2.0.0"],
["<=2.0.0", "<=2.0.0"],
["<=2.0.0", "<=2.0.0"],
["<2.0.0", "<2.0.0"],
["<2.0.0", "<2.0.0"],
[">=0.1.97", ">=0.1.97"],
[">=0.1.97", ">=0.1.97"],
["0.1.20 || 1.2.4", "0.1.20||1.2.4"],
[">=0.2.3 || <0.0.1", ">=0.2.3||<0.0.1"],
[">=0.2.3 || <0.0.1", ">=0.2.3||<0.0.1"],
[">=0.2.3 || <0.0.1", ">=0.2.3||<0.0.1"],
["||", "||"],
["2.x.x", ">=2.0.0 <3.0.0"],
["1.2.x", ">=1.2.0 <1.3.0"],
["1.2.x || 2.x", ">=1.2.0 <1.3.0||>=2.0.0 <3.0.0"],
["1.2.x || 2.x", ">=1.2.0 <1.3.0||>=2.0.0 <3.0.0"],
["x", "*"],
["2.*.*", ">=2.0.0 <3.0.0"],
["1.2.*", ">=1.2.0 <1.3.0"],
["1.2.* || 2.*", ">=1.2.0 <1.3.0||>=2.0.0 <3.0.0"],
["*", "*"],
["2", ">=2.0.0 <3.0.0"],
["2.3", ">=2.3.0 <2.4.0"],
["~2.4", ">=2.4.0 <2.5.0"],
["~2.4", ">=2.4.0 <2.5.0"],
["~>3.2.1", ">=3.2.1 <3.3.0"],
["~1", ">=1.0.0 <2.0.0"],
["~>1", ">=1.0.0 <2.0.0"],
["~1.0", ">=1.0.0 <1.1.0"],
["^0", ">=0.0.0 <1.0.0"],
["^0.1", ">=0.1.0 <0.2.0"],
["^1.0", ">=1.0.0 <2.0.0"],
["^1.2", ">=1.2.0 <2.0.0"],
["^0.0.1", ">=0.0.1 <0.0.2"],
["^0.0.1-beta", ">=0.0.1-beta <0.0.2"],
["^0.1.2", ">=0.1.2 <0.2.0"],
["^1.2.3", ">=1.2.3 <2.0.0"],
["^1.2.3-beta.4", ">=1.2.3-beta.4 <2.0.0"],
["<1", "<1.0.0"],
[">=1", ">=1.0.0"],
["<1.2", "<1.2.0"],
["1", ">=1.0.0 <2.0.0"],
];
versions.forEach(function (v) {
const pre = v[0];
const wanted = v[1];
const options = v[2];
const found = semver.validRange(pre, options);
assertEquals(found, wanted, "validRange(" + pre + ") === " + wanted);
});
});
Deno.test("missingRangeParameterInRangeIntersect", function (): void {
assertThrows(
function () {
new semver.Range("1.0.0").intersects(undefined);
},
TypeError,
"a Range is required",
);
});
Deno.test("tostrings", function (): void {
assertEquals(new semver.Range(">=v1.2.3").toString(), ">=1.2.3");
});
Deno.test("rangesIntersect", function (): void {
const versions: [string, string, boolean][] = [
["1.3.0 || <1.0.0 >2.0.0", "1.3.0 || <1.0.0 >2.0.0", true],
["<1.0.0 >2.0.0", ">0.0.0", false],
[">0.0.0", "<1.0.0 >2.0.0", false],
["<1.0.0 >2.0.0", ">1.4.0 <1.6.0", false],
["<1.0.0 >2.0.0", ">1.4.0 <1.6.0 || 2.0.0", false],
[">1.0.0 <=2.0.0", "2.0.0", true],
["<1.0.0 >=2.0.0", "2.1.0", false],
["<1.0.0 >=2.0.0", ">1.4.0 <1.6.0 || 2.0.0", false],
["1.5.x", "<1.5.0 || >=1.6.0", false],
["<1.5.0 || >=1.6.0", "1.5.x", false],
[
"<1.6.16 || >=1.7.0 <1.7.11 || >=1.8.0 <1.8.2",
">=1.6.16 <1.7.0 || >=1.7.11 <1.8.0 || >=1.8.2",
false,
],
[
"<=1.6.16 || >=1.7.0 <1.7.11 || >=1.8.0 <1.8.2",
">=1.6.16 <1.7.0 || >=1.7.11 <1.8.0 || >=1.8.2",
true,
],
[">=1.0.0", "<=1.0.0", true],
[">1.0.0 <1.0.0", "<=0.0.0", false],
["*", "0.0.1", true],
["*", ">=1.0.0", true],
["*", ">1.0.0", true],
["*", "~1.0.0", true],
["*", "<1.6.0", true],
["*", "<=1.6.0", true],
["1.*", "0.0.1", false],
["1.*", "2.0.0", false],
["1.*", "1.0.0", true],
["1.*", "<2.0.0", true],
["1.*", ">1.0.0", true],
["1.*", "<=1.0.0", true],
["1.*", "^1.0.0", true],
["1.0.*", "0.0.1", false],
["1.0.*", "<0.0.1", false],
["1.0.*", ">0.0.1", true],
["*", "1.3.0 || <1.0.0 >2.0.0", true],
["1.3.0 || <1.0.0 >2.0.0", "*", true],
["1.*", "1.3.0 || <1.0.0 >2.0.0", true],
["x", "0.0.1", true],
["x", ">=1.0.0", true],
["x", ">1.0.0", true],
["x", "~1.0.0", true],
["x", "<1.6.0", true],
["x", "<=1.6.0", true],
["1.x", "0.0.1", false],
["1.x", "2.0.0", false],
["1.x", "1.0.0", true],
["1.x", "<2.0.0", true],
["1.x", ">1.0.0", true],
["1.x", "<=1.0.0", true],
["1.x", "^1.0.0", true],
["1.0.x", "0.0.1", false],
["1.0.x", "<0.0.1", false],
["1.0.x", ">0.0.1", true],
["x", "1.3.0 || <1.0.0 >2.0.0", true],
["1.3.0 || <1.0.0 >2.0.0", "x", true],
["1.x", "1.3.0 || <1.0.0 >2.0.0", true],
["*", "*", true],
["x", "", true],
];
versions.forEach(function (v) {
const range1 = new semver.Range(v[0]);
const range2 = new semver.Range(v[1]);
const expect = v[2];
const actual1 = range1.intersects(range2);
const actual2 = range2.intersects(range1);
const actual3 = semver.intersects(v[1], v[0]);
const actual4 = semver.intersects(v[0], v[1]);
const actual5 = semver.intersects(range1, range2);
const actual6 = semver.intersects(range2, range1);
assertEquals(actual1, expect);
assertEquals(actual2, expect);
assertEquals(actual3, expect);
assertEquals(actual4, expect);
assertEquals(actual5, expect);
assertEquals(actual6, expect);
});
});

69
semver/semver_test.ts Normal file
View File

@ -0,0 +1,69 @@
// Copyright Isaac Z. Schlueter and Contributors. All rights reserved. ISC license.
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import { assertEquals, assertThrows } from "../testing/asserts.ts";
import * as semver from "./mod.ts";
Deno.test("invalidVersion", function (): void {
const versions = ["1.2.3.4", "NOT VALID", 1.2, null, "Infinity.NaN.Infinity"];
versions.forEach(function (v) {
assertThrows(
function () {
new semver.SemVer(v as string);
},
TypeError,
`Invalid Version: ${v}`,
);
});
});
Deno.test("maxSatisfying", function (): void {
const versions: [string[], string, string][] = [
[["1.2.3", "1.2.4"], "1.2", "1.2.4"],
[["1.2.4", "1.2.3"], "1.2", "1.2.4"],
[["1.2.3", "1.2.4", "1.2.5", "1.2.6"], "~1.2.3", "1.2.6"],
];
versions.forEach(function (v) {
const versions = v[0];
const range = v[1];
const expect = v[2];
const actual = semver.maxSatisfying(versions, range);
assertEquals(actual, expect);
});
});
Deno.test("minSatisfying", function (): void {
const versions: [string[], string, string][] = [
[["1.2.3", "1.2.4"], "1.2", "1.2.3"],
[["1.2.4", "1.2.3"], "1.2", "1.2.3"],
[["1.2.3", "1.2.4", "1.2.5", "1.2.6"], "~1.2.3", "1.2.3"],
];
versions.forEach(function (v) {
const versions = v[0];
const range = v[1];
const expect = v[2];
const actual = semver.minSatisfying(versions, range);
assertEquals(actual, expect);
});
});
Deno.test("sorting", function (): void {
const list = ["1.2.3+1", "1.2.3+0", "1.2.3", "5.9.6", "0.1.2"];
const sorted = ["0.1.2", "1.2.3", "1.2.3+0", "1.2.3+1", "5.9.6"];
const rsorted = ["5.9.6", "1.2.3+1", "1.2.3+0", "1.2.3", "0.1.2"];
assertEquals(semver.sort(list), sorted);
assertEquals(semver.rsort(list), rsorted);
});
Deno.test("badRangesInMaxOrMinSatisfying", function (): void {
const r = "some frogs and sneks-v2.5.6";
assertEquals(semver.maxSatisfying([], r), null);
assertEquals(semver.minSatisfying([], r), null);
});
Deno.test("bigNumericPrerelease", function (): void {
const r = new semver.SemVer("1.2.3-beta." + Number.MAX_SAFE_INTEGER + "0");
assertEquals(r.prerelease, ["beta", "90071992547409910"]);
});

29
semver/tooLong_test.ts Normal file
View File

@ -0,0 +1,29 @@
// Copyright Isaac Z. Schlueter and Contributors. All rights reserved. ISC license.
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import { assertEquals, assertThrows } from "../testing/asserts.ts";
import * as semver from "./mod.ts";
Deno.test("versionIsTooLong", function (): void {
const v: string = "1.2." + new Array(256).join("1");
assertThrows(function () {
new semver.SemVer(v);
});
assertEquals(semver.valid(v), null);
assertEquals(semver.inc(v, "patch"), null);
});
Deno.test("tooBig", function (): void {
const v = "1.2." + new Array(100).join("1");
assertThrows(function () {
new semver.SemVer(v);
});
assertEquals(semver.valid(v), null);
assertEquals(semver.inc(v, "patch"), null);
});
Deno.test("parsingNullDoesNotThrow", function (): void {
assertEquals(semver.parse(null), null);
assertEquals(semver.parse({} as semver.SemVer), null);
assertEquals(semver.parse(new semver.SemVer("1.2.3"))!.version, "1.2.3");
});