From da875a691871e080f866b3f6fcbbc6e4a65bb910 Mon Sep 17 00:00:00 2001 From: regevbr Date: Fri, 20 Mar 2020 15:22:58 +0200 Subject: [PATCH] feature: use semver to test versions matching --- package.json | 1 + .../ciResolver/impl/targetMatcher.ts | 26 ++++++++++++------- test/common/bindingTester.ts | 6 +---- yarn.lock | 7 +++++ 4 files changed, 25 insertions(+), 15 deletions(-) diff --git a/package.json b/package.json index 70ff89f6..4b1373b0 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/resolvers/ciResolver/impl/targetMatcher.ts b/src/resolvers/ciResolver/impl/targetMatcher.ts index c478d3ad..e7204e9f 100644 --- a/src/resolvers/ciResolver/impl/targetMatcher.ts +++ b/src/resolvers/ciResolver/impl/targetMatcher.ts @@ -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 { @@ -10,6 +20,7 @@ export class TargetMatcher extends ITargetMatcher { } public async match({ targetNode, candidates, packageReleaseDate }: ITargetMatcherOptions): Promise { + const validTarget = coerce(targetNode); const resolvedCandidates: string[] = []; for (const candidate of candidates) { if (candidate === LTS_VERSION) { @@ -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; } } diff --git a/test/common/bindingTester.ts b/test/common/bindingTester.ts index fdaaea15..3f039cb8 100644 --- a/test/common/bindingTester.ts +++ b/test/common/bindingTester.ts @@ -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; @@ -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(); diff --git a/yarn.lock b/yarn.lock index 8d05f2da..7ac3716a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"