Skip to content

Commit ed669a6

Browse files
add unit tests for semver conditions (#39)
1 parent 0f96f51 commit ed669a6

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ temp
99
yarn-error.log
1010

1111
test/data
12+
.vscode/settings.json

src/rule_evaluator.spec.ts

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,120 @@ describe('findMatchingRule', () => {
9090
expect(findMatchingRule({ version: '2.1.0' }, rules, false)).toBeNull();
9191
});
9292

93+
it('returns the rule for semver prerelease conditions', () => {
94+
/*
95+
https://semver.org/#spec-item-9
96+
97+
A pre-release version MAY be denoted by appending a hyphen and a series of dot separated identifiers immediately following the patch version.
98+
Identifiers MUST comprise only ASCII alphanumerics and hyphens [0-9A-Za-z-]. Identifiers MUST NOT be empty.
99+
Numeric identifiers MUST NOT include leading zeroes. Pre-release versions have a lower precedence than the associated normal version.
100+
A pre-release version indicates that the version is unstable and might not satisfy the intended compatibility requirements as denoted by its associated normal version.
101+
Examples: 1.0.0-alpha, 1.0.0-alpha.1, 1.0.0-0.3.7, 1.0.0-x.7.z.92, 1.0.0-x-y-z.--.
102+
103+
Pre-release versions have a lower precedence than the associated normal version.
104+
For example, 1.0.0-alpha < 1.0.0.
105+
*/
106+
const extendedSemverRule: IRule = {
107+
allocationKey: 'test',
108+
conditions: [
109+
{
110+
operator: OperatorType.GT,
111+
attribute: 'version',
112+
value: '1.2.3-alpha',
113+
},
114+
{
115+
operator: OperatorType.LTE,
116+
attribute: 'version',
117+
value: '2.0.0',
118+
},
119+
],
120+
};
121+
const rules = [extendedSemverRule];
122+
123+
// is greater than the associated alpha version
124+
expect(findMatchingRule({ version: '1.2.3' }, rules, false)).toEqual(extendedSemverRule);
125+
126+
// beta is greater than alpha (lexicographically)
127+
expect(findMatchingRule({ version: '1.2.3-beta' }, rules, false)).toEqual(extendedSemverRule);
128+
129+
// 1.2.4 is greater than 1.2.3
130+
expect(findMatchingRule({ version: '1.2.4' }, rules, false)).toEqual(extendedSemverRule);
131+
});
132+
133+
it('returns the rule for semver build numbers', () => {
134+
/*
135+
https://semver.org/#spec-item-10
136+
137+
Build metadata MAY be denoted by appending a plus sign and a series of dot separated identifiers immediately following the patch or pre-release version.
138+
Identifiers MUST comprise only ASCII alphanumerics and hyphens [0-9A-Za-z-]. Identifiers MUST NOT be empty.
139+
Build metadata MUST be ignored when determining version precedence.
140+
Thus two versions that differ only in the build metadata, have the same precedence.
141+
142+
Examples: 1.0.0-alpha+001, 1.0.0+20130313144700, 1.0.0-beta+exp.sha.5114f85, 1.0.0+21AF26D3----117B344092BD.
143+
*/
144+
145+
const extendedSemverRule: IRule = {
146+
allocationKey: 'test',
147+
conditions: [
148+
{
149+
operator: OperatorType.GT,
150+
attribute: 'version',
151+
value: '1.2.3+001',
152+
},
153+
{
154+
operator: OperatorType.LTE,
155+
attribute: 'version',
156+
value: '2.0.0',
157+
},
158+
],
159+
};
160+
const rules = [extendedSemverRule];
161+
162+
// build number is ignored therefore 1.2.3 is not greater than 1.2.3
163+
expect(findMatchingRule({ version: '1.2.3' }, rules, false)).toEqual(null);
164+
165+
// build number is ignored therefore 1.2.3 is not greater than 1.2.3
166+
expect(findMatchingRule({ version: '1.2.3+500' }, rules, false)).toEqual(null);
167+
168+
// 1.2.4 is greater than 1.2.3
169+
expect(findMatchingRule({ version: '1.2.4' }, rules, false)).toEqual(extendedSemverRule);
170+
});
171+
172+
it('returns the rule for semver mixed prerelease and build numbers', () => {
173+
/*
174+
When a version in Semantic Versioning (SemVer) includes both pre-release identifiers and build metadata,
175+
the version's precedence is determined first by the pre-release version,
176+
with the build metadata being ignored in terms of precedence.
177+
178+
The format for such a version would look something like this: MAJOR.MINOR.PATCH-prerelease+build
179+
*/
180+
const extendedSemverRule: IRule = {
181+
allocationKey: 'test',
182+
conditions: [
183+
{
184+
operator: OperatorType.GT,
185+
attribute: 'version',
186+
value: '1.2.3-beta+001',
187+
},
188+
{
189+
operator: OperatorType.LTE,
190+
attribute: 'version',
191+
value: '2.0.0',
192+
},
193+
],
194+
};
195+
const rules = [extendedSemverRule];
196+
197+
// gamma is greater than beta (lexicographically)
198+
expect(findMatchingRule({ version: '1.2.3-gamma' }, rules, false)).toEqual(extendedSemverRule);
199+
200+
// 1.2.3 is greater than 1.2.3-beta; this is a stable release
201+
expect(findMatchingRule({ version: '1.2.3+500' }, rules, false)).toEqual(extendedSemverRule);
202+
203+
// 1.2.4 is greater than 1.2.3
204+
expect(findMatchingRule({ version: '1.2.4' }, rules, false)).toEqual(extendedSemverRule);
205+
});
206+
93207
it('returns null if there is no attribute for the condition', () => {
94208
const rules = [numericRule];
95209
expect(findMatchingRule({ unknown: 'test' }, rules, false)).toEqual(null);

0 commit comments

Comments
 (0)