Skip to content

Commit

Permalink
isUpgradeable: Support simple, non-semver versions in the format "v1".
Browse files Browse the repository at this point in the history
  • Loading branch information
raineorshine committed Oct 23, 2020
1 parent 2912451 commit 6acaf95
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
10 changes: 7 additions & 3 deletions lib/versionmanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ function upgradeDependencies(currentDependencies, latestVersions, options = {})
const isSatisfied = semver.satisfies

/**
* Satisfy the latest, and it is not beyond the latest).
* Check if a version satisfies the latest, and is not beyond the latest). Ignores `v` prefix.
*
* @param current
* @param latest
Expand All @@ -200,15 +200,19 @@ function isUpgradeable(current, latest) {
if (!range) {
throw new Error(`"${current}" could not be parsed by semver-utils. This is probably a bug. Please file an issue at https://github.com/raineorshine/npm-check-updates.`)
}

const version = versionUtil.stringify(range)
const latestNormalized = versionUtil.isSimpleVersion(latest)
? latest.replace('v', '') + '.0.0'
: latest

// make sure it is a valid range
// not upgradeable if the latest version satisfies the current range
// not upgradeable if the specified version is newer than the latest (indicating a prerelease version)
// NOTE: When "<" is specified with a single digit version, e.g. "<7", and has the same major version as the latest, e.g. "7", isSatisfied(latest, version) will return true since it ignores the "<". In this case, test the original range (current) rather than the versionUtil output (version).
return Boolean(semver.validRange(version)) &&
!isSatisfied(latest, range.operator === '<' ? current : version) &&
!semver.ltr(latest, version)
!isSatisfied(latestNormalized, range.operator === '<' ? current : version) &&
!semver.ltr(latestNormalized, version)
}

/**
Expand Down
9 changes: 7 additions & 2 deletions test/test-versionmanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -380,8 +380,9 @@ describe('versionmanager', () => {

describe('upgradeDependencies', () => {

it('upgrade simple versions', () => {
it('upgrade simple, non-semver versions', () => {
vm.upgradeDependencies({ mongodb: '0.5' }, { mongodb: '1.4.30' }).should.eql({ mongodb: '1.4' })
vm.upgradeDependencies({ 'ncu-test-simple-tag': 'v1' }, { 'ncu-test-simple-tag': 'v3' }).should.eql({ 'ncu-test-simple-tag': 'v3' })
})

it('upgrade latest versions that already satisfy the specified version', () => {
Expand Down Expand Up @@ -508,7 +509,7 @@ describe('versionmanager', () => {
})
})

it('support the non-semver tag "v1"', async () => {
it('support simple, non-semver tags in the format "v1"', async () => {
const upgrades = await vm.queryVersions({
// this repo has tag "1.0" which is not valid semver
'ncu-test-invalid-tag': 'git+https://github.com/raineorshine/ncu-test-simple-tag#v1'
Expand Down Expand Up @@ -571,6 +572,10 @@ describe('versionmanager', () => {
vm.isUpgradeable('<7', '7.2.0').should.equal(true)
})

it('upgrade simple versions', () => {
vm.isUpgradeable('v1', 'v2').should.equal(true)
})

})

describe('getPreferredWildcard', () => {
Expand Down

0 comments on commit 6acaf95

Please sign in to comment.