Skip to content

Commit

Permalink
Fix: semver comparisons for ranges.
Browse files Browse the repository at this point in the history
Signed-off-by: dblock <[email protected]>
  • Loading branch information
dblock committed Aug 12, 2024
1 parent 9eb3732 commit 2742683
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 3 deletions.
20 changes: 20 additions & 0 deletions tools/src/_utils/semver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

import _ from 'lodash'
import * as semver from 'semver'

export function coerce(version: string) : string {
return semver.coerce(version)?.toString() ?? version
}

export function satisfies(version: string | semver.SemVer | undefined, range: string): boolean {
if (version === undefined || version === '') return true
return _.every(range.split(','), (portion) => semver.satisfies(version, portion))
}
4 changes: 2 additions & 2 deletions tools/src/merger/OpenApiVersionExtractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import _, { extend, isEmpty } from 'lodash'
import { delete_matching_keys, find_refs, write_yaml } from '../helpers'
import { Logger } from '../Logger'
import { type OpenAPIV3 } from 'openapi-types'
import semver from 'semver'
import * as semver from '../_utils/semver'

// Extract a versioned API
export default class OpenApiVersionExtractor {
Expand All @@ -22,7 +22,7 @@ export default class OpenApiVersionExtractor {

constructor(source_spec: OpenAPIV3.Document, target_version: string, logger: Logger = new Logger()) {
this._source_spec = source_spec
this._target_version = semver.coerce(target_version)?.toString() ?? target_version
this._target_version = semver.coerce(target_version)
this._logger = logger
this._spec = undefined
}
Expand Down
2 changes: 1 addition & 1 deletion tools/src/tester/StoryEvaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { overall_result } from './helpers'
import { StoryOutputs } from './StoryOutputs'
import SupplementalChapterEvaluator from './SupplementalChapterEvaluator'
import { ChapterOutput } from './ChapterOutput'
import * as semver from 'semver'
import * as semver from '../_utils/semver'
import _ from 'lodash'

export default class StoryEvaluator {
Expand Down
47 changes: 47 additions & 0 deletions tools/tests/_utils/Semver.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

import * as semver from "../../src/_utils/semver";

describe('coerce', () => {
it ('null', () => {
expect(semver.coerce('')).toEqual('')
expect(semver.coerce('1.2.3')).toEqual('1.2.3')
expect(semver.coerce('1.2')).toEqual('1.2.0')
expect(semver.coerce('1')).toEqual('1.0.0')
})
});

describe('satisfies', () => {
it ('defaults', () => {
expect(semver.satisfies('', '>= 1.3, < 99.0')).toBe(true)
})

it ('semver', () => {
expect(semver.satisfies(semver.coerce('2.17.0'), '>= 1.3, < 99.0')).toBe(true)
})

it ('~', () => {
expect(semver.satisfies('2.17.0', '~> 2.x')).toBe(true)
expect(semver.satisfies('2.17.0', '~> 2.17.0')).toBe(true)
expect(semver.satisfies('2.17.0', '~> 1.x')).toBe(false)
expect(semver.satisfies('2.17.0', '~> 2.17.0')).toBe(true)
expect(semver.satisfies('2.17.0', '~> 2.18')).toBe(false)
})

it ('> <', () => {
expect(semver.satisfies('2.17.0', '> 2.999.0')).toBe(false)
expect(semver.satisfies('2.17.0', '< 3.0')).toBe(true)
expect(semver.satisfies('2.17.0', '>= 1.3, < 99.0')).toBe(true)
})

it ('ranges', () => {
expect(semver.satisfies('2.17.0', '>= 1.3, < 99.0')).toBe(true)
})
});
19 changes: 19 additions & 0 deletions tools/tests/tester/fixtures/evals/passed.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,25 @@ chapters:
overall:
result: SKIPPED
message: Skipped because version 2.16.0 does not satisfy >= 2.999.0.
- title: This GET /_cat/health should run (>= 1.3, < 99.0).
overall:
result: PASSED
path: GET /_cat/health
request:
parameters:
format:
result: PASSED
request:
result: PASSED
response:
status:
result: PASSED
payload_body:
result: PASSED
payload_schema:
result: PASSED
output_values:
result: SKIPPED
epilogues:
- title: DELETE /books
overall:
Expand Down
6 changes: 6 additions & 0 deletions tools/tests/tester/fixtures/stories/passed.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,9 @@ chapters:
path: /_cat/health
parameters:
format: json
- synopsis: This GET /_cat/health should run (>= 1.3, < 99.0).
version: '>= 1.3, < 99.0'
method: GET
path: /_cat/health
parameters:
format: json

0 comments on commit 2742683

Please sign in to comment.