Skip to content

Commit

Permalink
target semver: ignore explicit ranges.
Browse files Browse the repository at this point in the history
  • Loading branch information
raineorshine committed Aug 12, 2023
1 parent d52de32 commit 4cead92
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 23 deletions.
12 changes: 12 additions & 0 deletions src/package-managers/npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import sortBy from 'lodash/sortBy'
import pacote from 'pacote'
import path from 'path'
import nodeSemver from 'semver'
import { parseRange } from 'semver-utils'
import spawn from 'spawn-please'
import untildify from 'untildify'
import filterObject from '../lib/filterObject'
Expand All @@ -30,6 +31,14 @@ import { VersionResult } from '../types/VersionResult'
import { VersionSpec } from '../types/VersionSpec'
import { filterPredicate, satisfiesNodeEngine } from './filters'

const EXPLICIT_RANGE_OPS = new Set(['-', '||', '&&', '<', '<=', '>', '>='])

/** Returns true if the spec is an explicit version range (not ~ or ^). */
const isExplicitRange = (spec: VersionSpec) => {
const range = parseRange(spec)
return range.some(parsed => EXPLICIT_RANGE_OPS.has(parsed.operator || ''))
}

/** Normalizes the keys of an npm config for pacote. */
export const normalizeNpmConfig = (
npmConfig: NpmConfig,
Expand Down Expand Up @@ -821,6 +830,9 @@ export const semver: GetVersion = async (
npmConfig,
npmConfigProject,
)) as Index<Packument>
// ignore explicit version ranges
if (isExplicitRange(currentVersion)) return { version: null }

const versionsFiltered = filter(versions, filterPredicate(options)).map(o => o.version)
// TODO: Upgrading within a prerelease does not seem to work.
// { includePrerelease: true } does not help.
Expand Down
136 changes: 113 additions & 23 deletions test/target.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,26 +166,6 @@ describe('target', () => {
})

describe('semver', () => {
describe('exact', () => {
it('exact version range', async () => {
const data = await ncu({
jsonAll: true,
packageData: {
dependencies: {
'ncu-test-semver': '1.0.0',
},
},
target: 'semver',
})

data!.should.eql({
dependencies: {
'ncu-test-semver': '1.0.0',
},
})
})
})

describe('^', () => {
it('highest minor for post-1.0 version', async () => {
const data = await ncu({
Expand Down Expand Up @@ -282,8 +262,28 @@ describe('target', () => {
})
})

describe('-', () => {
it('inclusive range', async () => {
describe('exact version', () => {
it('ignore exact version range', async () => {
const data = await ncu({
jsonAll: true,
packageData: {
dependencies: {
'ncu-test-semver': '1.0.0',
},
},
target: 'semver',
})

data!.should.eql({
dependencies: {
'ncu-test-semver': '1.0.0',
},
})
})
})

describe('explicit ranges', () => {
it('ignore inclusive range', async () => {
const data = await ncu({
jsonAll: true,
packageData: {
Expand All @@ -296,7 +296,97 @@ describe('target', () => {

data!.should.eql({
dependencies: {
'ncu-test-semver': '1.2.0',
'ncu-test-semver': '1.0.0 - 1.3.0',
},
})
})

it('ignore >', async () => {
const data = await ncu({
jsonAll: true,
packageData: {
dependencies: {
'ncu-test-semver': '>1',
},
},
target: 'semver',
})

data!.should.eql({
dependencies: {
'ncu-test-semver': '>1',
},
})
})

it('ignore >=', async () => {
const data = await ncu({
jsonAll: true,
packageData: {
dependencies: {
'ncu-test-semver': '>=1',
},
},
target: 'semver',
})

data!.should.eql({
dependencies: {
'ncu-test-semver': '>=1',
},
})
})

it('ignore <', async () => {
const data = await ncu({
jsonAll: true,
packageData: {
dependencies: {
'ncu-test-semver': '<2',
},
},
target: 'semver',
})

data!.should.eql({
dependencies: {
'ncu-test-semver': '<2',
},
})
})

it('ignore <=', async () => {
const data = await ncu({
jsonAll: true,
packageData: {
dependencies: {
'ncu-test-semver': '<=2',
},
},
target: 'semver',
})

data!.should.eql({
dependencies: {
'ncu-test-semver': '<=2',
},
})
})

it('ignore ||', async () => {
const data = await ncu({
jsonAll: true,
packageData: {
dependencies: {
'ncu-test-semver': '1 || 2',
},
},
target: 'semver',
})

data!.should.eql({
dependencies: {
'ncu-test-semver': '1 || 2',
},
})
})
Expand Down

0 comments on commit 4cead92

Please sign in to comment.