-
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(semver): support resolving with lowest version
Closes #110
- Loading branch information
1 parent
b5ceae0
commit a17e423
Showing
14 changed files
with
336 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { z } from 'zod'; | ||
|
||
export const nonEmptyString = z.string().trim().min(1); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 44 additions & 24 deletions
68
src/get-context/get-groups/version-group/instance-group/get-highest-version.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,60 @@ | ||
import { R } from '@mobily/ts-belt'; | ||
import 'expect-more-jest'; | ||
import { shuffle } from '../../../../../test/shuffle'; | ||
import { getHighestVersion } from './get-highest-version'; | ||
|
||
const shuffle = (array: string[]): string[] => { | ||
for (let i = array.length - 1; i > 0; i--) { | ||
const j = Math.floor(Math.random() * (i + 1)); | ||
[array[i], array[j]] = [array[j], array[i]]; | ||
} | ||
return array; | ||
}; | ||
|
||
describe('getHighestVersion', () => { | ||
it('returns the newest version from an array of versions', () => { | ||
const a = ['<1.0.0']; | ||
const b = shuffle([...a, '<=1.0.0']); | ||
const c = shuffle([...b, '1']); | ||
const d = shuffle([...c, '1.0.0']); | ||
const e = shuffle([...d, '~1.0.0']); | ||
const f = shuffle([...e, '1.x.x']); | ||
const g = shuffle([...f, '^1.0.0']); | ||
const h = shuffle([...g, '>=1.0.0']); | ||
const i = shuffle([...h, '>1.0.0']); | ||
const j = shuffle([...i, '*']); | ||
// valid semver | ||
const a = ['<1.0.0']; | ||
const b = shuffle([...a, '<=1.0.0']); | ||
const c = shuffle([...b, '1']); | ||
const d = shuffle([...c, '1.0.0']); | ||
const e = shuffle([...d, '~1.0.0']); | ||
const f = shuffle([...e, '1.x.x']); | ||
const g = shuffle([...f, '^1.0.0']); | ||
const h = shuffle([...g, '>=1.0.0']); | ||
const i = shuffle([...h, '>1.0.0']); | ||
const j = shuffle([...i, '*']); | ||
|
||
// "1" and "1.0.0" are equal and first match wins | ||
const eitherFormat = expect.stringMatching(/^(1|1\.0\.0)$/); | ||
|
||
it('returns "<1.0.0" when it is the only version', () => { | ||
expect(getHighestVersion(a)).toEqual(R.Ok('<1.0.0')); | ||
}); | ||
|
||
it('returns "<=1.0.0" when added', () => { | ||
expect(getHighestVersion(b)).toEqual(R.Ok('<=1.0.0')); | ||
}); | ||
|
||
it('returns "1" when added', () => { | ||
expect(getHighestVersion(c)).toEqual(R.Ok('1')); | ||
expect(getHighestVersion(d)).toEqual( | ||
// "1" and "1.0.0" are equal and first match wins | ||
R.Ok(expect.stringMatching(/^(1|1\.0\.0)$/)), | ||
); | ||
}); | ||
|
||
it('returns "1.0.0" when added', () => { | ||
expect(getHighestVersion(d)).toEqual(R.Ok(eitherFormat)); | ||
}); | ||
|
||
it('returns "~1.0.0" when added', () => { | ||
expect(getHighestVersion(e)).toEqual(R.Ok('~1.0.0')); | ||
}); | ||
|
||
it('returns "1.x.x" when added', () => { | ||
expect(getHighestVersion(f)).toEqual(R.Ok('1.x.x')); | ||
}); | ||
|
||
it('returns "^1.0.0" when added', () => { | ||
expect(getHighestVersion(g)).toEqual(R.Ok('^1.0.0')); | ||
}); | ||
|
||
it('returns ">=1.0.0" when added', () => { | ||
expect(getHighestVersion(h)).toEqual(R.Ok('>=1.0.0')); | ||
}); | ||
|
||
it('returns ">1.0.0" when added', () => { | ||
expect(getHighestVersion(i)).toEqual(R.Ok('>1.0.0')); | ||
}); | ||
|
||
it('returns "*" when added', () => { | ||
expect(getHighestVersion(j)).toEqual(R.Ok('*')); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/get-context/get-groups/version-group/instance-group/get-lowest-version.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { R } from '@mobily/ts-belt'; | ||
import 'expect-more-jest'; | ||
import { shuffle } from '../../../../../test/shuffle'; | ||
import { getLowestVersion } from './get-lowest-version'; | ||
|
||
describe('getLowestVersion', () => { | ||
const a = ['*']; | ||
const b = shuffle([...a, '>1.0.0']); | ||
const c = shuffle([...b, '>=1.0.0']); | ||
const d = shuffle([...c, '^1.0.0']); | ||
const e = shuffle([...d, '1.x.x']); | ||
const f = shuffle([...e, '~1.0.0']); | ||
const g = shuffle([...f, '1.0.0']); | ||
const h = shuffle([...g, '1']); | ||
const i = shuffle([...h, '<=1.0.0']); | ||
const j = shuffle([...i, '<1.0.0']); | ||
|
||
// "1" and "1.0.0" are equal and first match wins | ||
const eitherFormat = expect.stringMatching(/^(1|1\.0\.0)$/); | ||
|
||
it('returns "*" when it is the only version', () => { | ||
expect(getLowestVersion(a)).toEqual(R.Ok('*')); | ||
}); | ||
|
||
it('returns ">1.0.0" when added', () => { | ||
expect(getLowestVersion(b)).toEqual(R.Ok('>1.0.0')); | ||
}); | ||
|
||
it('returns ">=1.0.0" when added', () => { | ||
expect(getLowestVersion(c)).toEqual(R.Ok('>=1.0.0')); | ||
}); | ||
|
||
it('returns "^1.0.0" when added', () => { | ||
expect(getLowestVersion(d)).toEqual(R.Ok('^1.0.0')); | ||
}); | ||
|
||
it('returns "1.x.x" when added', () => { | ||
expect(getLowestVersion(e)).toEqual(R.Ok('1.x.x')); | ||
}); | ||
|
||
it('returns "~1.0.0" when added', () => { | ||
expect(getLowestVersion(f)).toEqual(R.Ok('~1.0.0')); | ||
}); | ||
|
||
it('returns "1.0.0" when added', () => { | ||
expect(getLowestVersion(g)).toEqual(R.Ok('1.0.0')); | ||
}); | ||
|
||
it('returns "1" when added', () => { | ||
expect(getLowestVersion(h)).toEqual(R.Ok(eitherFormat)); | ||
}); | ||
|
||
it('returns "<=1.0.0" when added', () => { | ||
expect(getLowestVersion(i)).toEqual(R.Ok('<=1.0.0')); | ||
}); | ||
|
||
it('returns "<1.0.0" when added', () => { | ||
expect(getLowestVersion(j)).toEqual(R.Ok('<1.0.0')); | ||
}); | ||
}); |
49 changes: 49 additions & 0 deletions
49
src/get-context/get-groups/version-group/instance-group/get-lowest-version.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { R } from '@mobily/ts-belt'; | ||
import { BaseError } from '../../../../lib/error'; | ||
import { clean } from './lib/clean'; | ||
import { compareLt } from './lib/compare-semver'; | ||
import { getRangeScore } from './lib/get-range-score'; | ||
|
||
interface LowestVersion { | ||
withRange: string; | ||
semver: string; | ||
} | ||
|
||
export function getLowestVersion( | ||
versions: string[], | ||
): R.Result<string, BaseError> { | ||
let lowest: LowestVersion | undefined; | ||
|
||
for (const withRange of versions) { | ||
switch (compareLt(withRange, lowest?.semver)) { | ||
// lowest possible, quit early | ||
case '*': { | ||
if (!lowest) lowest = { withRange: '*', semver: '*' }; | ||
continue; | ||
} | ||
// impossible to know how the user wants to resolve unsupported versions | ||
case 'invalid': { | ||
return R.Error(new BaseError(`"${withRange}" is not supported`)); | ||
} | ||
// we found a new lowest version | ||
case 'lt': { | ||
lowest = newLowestVersion(withRange); | ||
continue; | ||
} | ||
// versions are the same, but one range might be greedier than another | ||
case 'eq': { | ||
const score = getRangeScore(withRange); | ||
const lowestScore = getRangeScore(`${lowest?.withRange}`); | ||
if (score < lowestScore) lowest = newLowestVersion(withRange); | ||
} | ||
} | ||
} | ||
|
||
return lowest && lowest.withRange | ||
? R.Ok(lowest.withRange) | ||
: R.Error(new BaseError(`getLowestVersion(): did not return a version`)); | ||
} | ||
|
||
function newLowestVersion(withRange: string): LowestVersion { | ||
return { withRange, semver: clean(withRange) }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.