Skip to content

Commit

Permalink
feature: use semver to test versions matching
Browse files Browse the repository at this point in the history
  • Loading branch information
regevbr committed Mar 20, 2020
1 parent 5a7e0c5 commit da875a6
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 15 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"@types/node": "^13.9.2",
"@types/nodegit": "^0.26.1",
"@types/pacote": "^9.5.0",
"@types/semver": "^7.1.0",
"@types/tmp": "^0.1.0",
"@types/yaml": "^1.2.0",
"@typescript-eslint/eslint-plugin": "^2.24.0",
Expand Down
26 changes: 16 additions & 10 deletions src/resolvers/ciResolver/impl/targetMatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@ import { ITargetMatcher, ITargetMatcherOptions } from '../interfaces/targetMatch
import { injectable } from 'inversify';
import { ILts } from '../../../utils/lts';
import { LTS_VERSION } from '..';
// eslint-disable-next-line @typescript-eslint/quotes
import semver = require('semver');

const coerce = (version: string): string => {
const coereced = semver.coerce(semver.coerce(version)?.major?.toFixed(0) || version);
if (coereced) {
return coereced.format();
}
return version;
};

@injectable()
export class TargetMatcher extends ITargetMatcher {
Expand All @@ -10,6 +20,7 @@ export class TargetMatcher extends ITargetMatcher {
}

public async match({ targetNode, candidates, packageReleaseDate }: ITargetMatcherOptions): Promise<boolean> {
const validTarget = coerce(targetNode);
const resolvedCandidates: string[] = [];
for (const candidate of candidates) {
if (candidate === LTS_VERSION) {
Expand All @@ -21,15 +32,10 @@ export class TargetMatcher extends ITargetMatcher {
resolvedCandidates.push(candidate);
}
}
const dotedTargetVersions = `${targetNode}.`;
for (const candidate of resolvedCandidates) {
if (candidate === targetNode) {
return true;
}
if (candidate.startsWith(dotedTargetVersions)) {
return true;
}
}
return false;
const matchingCandidates = resolvedCandidates.filter((candidate) => {
const validCandidate = coerce(candidate);
return semver.eq(validTarget, validCandidate);
});
return matchingCandidates.length > 0;
}
}
6 changes: 1 addition & 5 deletions test/common/bindingTester.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
/* eslint-disable jest/no-export */
import { interfaces } from 'inversify';
import Bind = interfaces.Bind;

// eslint-disable-next-line jest/no-export
export type BinderFn = (bind: Bind) => void;

// eslint-disable-next-line jest/no-export
export enum BindingTypes {
SINGELTON = `SINGELTON`,
CONSTANT = `CONSTANT`,
}

// eslint-disable-next-line jest/no-export
export interface IBindingToTest {
binder: any;
binded: any;
type: BindingTypes;
}

// eslint-disable-next-line jest/no-export
export interface IBindingTestOptions {
bindings: IBindingToTest[];
binderFn: BinderFn;
Expand All @@ -31,7 +28,6 @@ const getBindName = (bind: any): string => {
return bind.name || bind.constructor?.name || bind.toString?.() || bind;
};

// eslint-disable-next-line jest/no-export
export const testBindings = ({ binderFn, bindings, name }: IBindingTestOptions): void => {
describe(name, () => {
const inSingletonScopeMock = jest.fn();
Expand Down
7 changes: 7 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,13 @@
resolved "https://registry.yarnpkg.com/@types/parse-json/-/parse-json-4.0.0.tgz#2f8bb441434d163b35fb8ffdccd7138927ffb8c0"
integrity sha512-//oorEZjL6sbPcKUaCdIGlIUeH26mgzimjBB77G6XRgnDl/L5wOnpyBGRe/Mmf5CVW3PwEBE1NjiMZ/ssFh4wA==

"@types/semver@^7.1.0":
version "7.1.0"
resolved "https://registry.yarnpkg.com/@types/semver/-/semver-7.1.0.tgz#c8c630d4c18cd326beff77404887596f96408408"
integrity sha512-pOKLaubrAEMUItGNpgwl0HMFPrSAFic8oSVIvfu1UwcgGNmNyK9gyhBHKmBnUTwwVvpZfkzUC0GaMgnL6P86uA==
dependencies:
"@types/node" "*"

"@types/ssri@*":
version "6.0.1"
resolved "https://registry.yarnpkg.com/@types/ssri/-/ssri-6.0.1.tgz#7d15320522d8005e3a435ebf56c76f7d051fe437"
Expand Down

0 comments on commit da875a6

Please sign in to comment.