Skip to content

Commit cee768e

Browse files
authored
fix(mongodb-constants): fix regexp that picks valid part of semver version (#168)
* fix(mongodb-constants): fix regexp that picks valid part of semver version * chore(mongodb-constants): add test for version specified in constants
1 parent 96f27ef commit cee768e

File tree

2 files changed

+46
-7
lines changed

2 files changed

+46
-7
lines changed

packages/mongodb-constants/src/filter.spec.ts

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { expect } from 'chai';
22
import type { Completion } from './filter';
3-
import { wrapField, getFilteredCompletions } from './filter';
3+
import { wrapField, getFilteredCompletions, ALL_CONSTANTS } from './filter';
4+
import semver from 'semver';
5+
import util from 'util';
46

57
describe('completer', function () {
68
const simpleConstants: Completion[] = [
@@ -14,6 +16,16 @@ describe('completer', function () {
1416
version: '>=2.0.0 <3.0.0 || >=3.5.0',
1517
meta: 'expr:set',
1618
},
19+
{
20+
value: 'woof',
21+
version: '>=0.200.300 <0.200.301',
22+
meta: 'accumulator',
23+
},
24+
{
25+
value: 'honk',
26+
version: '999.999.999',
27+
meta: 'variable:system',
28+
},
1729
];
1830

1931
function getFilteredValues(
@@ -30,7 +42,13 @@ describe('completer', function () {
3042
'Foo',
3143
'bar',
3244
]);
33-
expect(getFilteredValues({ serverVersion: '0.0.1-alpha0' })).to.deep.eq([
45+
});
46+
47+
it('should clean up version before comparing', function () {
48+
expect(getFilteredValues({ serverVersion: '0.200.300-alpha0' })).to.deep.eq(
49+
['foo', 'Foo', 'woof']
50+
);
51+
expect(getFilteredValues({ serverVersion: '0.0.0---what???' })).to.deep.eq([
3452
'foo',
3553
'Foo',
3654
]);
@@ -44,14 +62,18 @@ describe('completer', function () {
4462
);
4563
});
4664

47-
it('should ignore version when version is not valid', function () {
48-
expect(getFilteredValues({ serverVersion: '1' })).to.deep.eq([
65+
it('should use default version when provided version is not valid', function () {
66+
expect(
67+
getFilteredValues({ serverVersion: 'one dot one dot zero' })
68+
).to.deep.eq(['foo', 'Foo', 'bar', 'buz', 'barbar', 'meow', 'honk']);
69+
expect(getFilteredValues({ serverVersion: '1.2' })).to.deep.eq([
4970
'foo',
5071
'Foo',
5172
'bar',
5273
'buz',
5374
'barbar',
5475
'meow',
76+
'honk',
5577
]);
5678
});
5779

@@ -170,4 +192,21 @@ describe('completer', function () {
170192
);
171193
});
172194
});
195+
196+
describe('all completions', function () {
197+
it('should have valid semver version or range specified', function () {
198+
ALL_CONSTANTS.forEach(({ name, version }) => {
199+
const errMessage = `Expected completion ${util.inspect({
200+
name,
201+
version,
202+
})} to have valid version`;
203+
204+
try {
205+
expect(semver.valid(version)).to.not.eq(null, errMessage);
206+
} catch {
207+
expect(semver.validRange(version)).to.not.eq(null, errMessage);
208+
}
209+
});
210+
});
211+
});
173212
});

packages/mongodb-constants/src/filter.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { QUERY_OPERATORS } from './query-operators';
99
import { STAGE_OPERATORS } from './stage-operators';
1010
import { SYSTEM_VARIABLES } from './system-variables';
1111

12-
const ALL_CONSTANTS = [
12+
export const ALL_CONSTANTS = [
1313
...ACCUMULATORS,
1414
...BSON_TYPES,
1515
...BSON_TYPE_ALIASES,
@@ -111,7 +111,7 @@ function isIn<T extends string | number>(
111111
* @param v2 Either a single version number or a range to match against
112112
*/
113113
function satisfiesVersion(v1: string, v2: string): boolean {
114-
const isGTECheck = /^\d+?\.\d+?\.\d+?$/.test(v2);
114+
const isGTECheck = /^\d+\.\d+\.\d+$/.test(v2);
115115
return satisfies(v1, isGTECheck ? `>=${v2}` : v2);
116116
}
117117

@@ -123,7 +123,7 @@ export function createConstantFilter({
123123
completion: Completion
124124
) => boolean {
125125
const currentServerVersion =
126-
/^(?<version>\d+?\.\d+?\.\d+?)/.exec(serverVersion)?.groups?.version ??
126+
/^(?<version>\d+\.\d+\.\d+)/.exec(serverVersion)?.groups?.version ??
127127
// Fallback to default server version if provided version doesn't match
128128
// regex so that semver doesn't throw when checking
129129
DEFAULT_SERVER_VERSION;

0 commit comments

Comments
 (0)