Skip to content

Commit

Permalink
fix: fallback to lower bound of range for engines check
Browse files Browse the repository at this point in the history
  • Loading branch information
lukekarrys committed Oct 4, 2022
1 parent 651ffb5 commit 6ecdea4
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 5 deletions.
20 changes: 17 additions & 3 deletions lib/config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { relative, dirname, join, extname, posix, win32 } = require('path')
const { defaults, pick, omit, uniq } = require('lodash')
const semver = require('semver')
const parseCIVersions = require('./util/parse-ci-versions.js')
const getGitUrl = require('./util/get-git-url.js')
const gitignore = require('./util/gitignore.js')
Expand Down Expand Up @@ -222,9 +223,22 @@ const getFullConfig = async ({
versions = defaultVersions.ciVersions.slice(-1)
}

const parsed = parseCIVersions(versions)
derived.ciVersions = parsed.targets
derived.engines = pkgConfig.engines || parsed.engines
const { targets, engines } = parseCIVersions(versions)

// get just a list of the target versions (not ranges)
// these are used for the node version when doing engines checks
// since we want to test in the lowest version of each major
let targetVersions = targets.filter(t => semver.valid(t))
// if the versions are all ranges then convert them to the lower bound of each range
if (!targetVersions.length) {
targetVersions = targets.filter(t => semver.validRange(t)).map(t => {
return new semver.Range(t).set[0][0].semver.version
})
}

derived.ciVersions = targets
derived.baseCiVersions = targetVersions
derived.engines = pkgConfig.engines || engines
}

const gitUrl = await getGitUrl(root)
Expand Down
2 changes: 1 addition & 1 deletion lib/content/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
jobDepFlags="--engines-strict"
macCI=false
windowsCI=false
ciVersions=(reject ciVersions '\.x$')
ciVersions=baseCiVersions
}}

lint:
Expand Down
1 change: 0 additions & 1 deletion lib/util/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ const setupHandlebars = (baseDir, ...otherDirs) => {
Handlebars.registerHelper('obj', ({ hash }) => Object.fromEntries(safeValues(hash)))
Handlebars.registerHelper('join', (arr, sep) => arr.join(typeof sep === 'string' ? sep : ', '))
Handlebars.registerHelper('pluck', (arr, key) => arr.map(a => a[key]))
Handlebars.registerHelper('reject', (arr, re) => arr.filter(a => !new RegExp(re).test(a)))
Handlebars.registerHelper('quote', (arr) => arr.map(a => `'${a}'`))
Handlebars.registerHelper('last', (arr) => arr[arr.length - 1])
Handlebars.registerHelper('json', (c) => JSON.stringify(c))
Expand Down
12 changes: 12 additions & 0 deletions test/apply/engines.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
const t = require('tap')
const { join } = require('path')
const yaml = require('yaml')
const setup = require('../setup.js')

const getEngines = (ci) => yaml.parse(ci).jobs.engines.strategy.matrix['node-version']

t.test('can set engines and ci separately', async (t) => {
const s = await setup(t, {
package: {
Expand Down Expand Up @@ -31,7 +34,10 @@ t.test('latest ci versions', async (t) => {
await s.apply()

const pkg = await s.readJson('package.json')
const ci = await s.readFile('.github/workflows/ci.yml')

t.equal(pkg.engines.node, '>=18.0.0')
t.strictSame(getEngines(ci), ['18.0.0'])
})

t.test('latest ci versions in workspace', async (t) => {
Expand Down Expand Up @@ -61,9 +67,15 @@ t.test('latest ci versions in workspace', async (t) => {
})
await s.apply()

const ci = await s.readFile('.github/workflows/ci.yml')
const ciWorkspace = await s.readFile('.github/workflows/ci-a.yml')

const root = await s.readJson('target.json')
const workspace = await s.readJson('target-a.json')

t.equal(root.node, '^12.0.0 || ^14.0.0 || >=16.0.0')
t.equal(workspace.node, '>=16.0.0')

t.strictSame(getEngines(ci), ['12.0.0', '14.0.0', '16.0.0'])
t.strictSame(getEngines(ciWorkspace), ['16.0.0'])
})

0 comments on commit 6ecdea4

Please sign in to comment.