Skip to content

Commit

Permalink
add Versions.prototype.(add|get), resolve tests on CI
Browse files Browse the repository at this point in the history
  • Loading branch information
balupton committed Nov 20, 2023
1 parent 818aeea commit 77bb1b9
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 35 deletions.
62 changes: 34 additions & 28 deletions source/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
/* eslint no-console:0 */

// external
const { deepEqual } = require('assert-helpers')
const versionRange = require('version-range').default
const { equal, deepEqual } = require('assert-helpers')
const nodeProcessVersion = process.versions.node

// local
const Versions = require('./versions.js')
const { runVersion, parseExitCode, uniq, lastLine } = require('./util.js')
const { parseExitCode } = require('./util.js')

async function runTests(command, serial = false) {
let versions
Expand Down Expand Up @@ -37,15 +37,15 @@ async function runTests(command, serial = false) {
}

try {
// Note
// log
console.log(
'Running tests on node version',
process.versions.node,
'with command:',
command,
)

// Create the versions (use old versions as they stay the same, new versions change)
// create the versions (use old versions as they stay the same, new versions change)
versions = new Versions(['current', 10, 8], [storeUpdate])
deepEqual(
versions.map((V) => ({ version: V.version, aliases: V.aliases })),
Expand All @@ -66,44 +66,50 @@ async function runTests(command, serial = false) {
'versions are sorted initially correctly',
)

// Load
console.log('loading versions')
// resolve/load versions
await versions.load()

// Install
console.log('installing versions')
// install missing versions
await versions.install()

// Fetch the actual exact versions
console.log('testing versions')
const nodeCurrentVersion = await runVersion('current').then((result) =>
lastLine(result.stdout),
)
const nodeEightVersion = await runVersion(8).then((result) =>
lastLine(result.stdout),
)
const nodeTenVersion = await runVersion(10).then((result) =>
lastLine(result.stdout),
// fetch the resolved versions
const nodeCurrentVersion = versions.get('current')?.version
const nodeEightVersion = versions.get(8)?.version
const nodeTenVersion = versions.get(10)?.version

// node current version is the process version
equal(
nodeCurrentVersion,
nodeProcessVersion,
'Node.js current version resolved to Node.js process version',
)

// Confirm compaction and everything occurred correctly
const latest = uniq([nodeCurrentVersion, nodeEightVersion, nodeTenVersion])
.map((v) => ({
version: v,
aliases: versionRange(nodeCurrentVersion, v) ? ['current'] : [],
}))
.sort(Versions.comparator)
// confirm version fetching, confirm compation, confirm sorting
const latest = [
{
version: nodeEightVersion,
aliases: [],
},
{
version: nodeTenVersion,
aliases: [],
},
{
version: nodeCurrentVersion,
aliases: ['current'],
},
]
deepEqual(
versions.map((V) => ({ version: V.version, aliases: V.aliases })),
latest,
'versions are sorted after load correctly',
)

// Test
// run our test on them
await versions.test(command, serial)
checkUpdates()

// Check how we did
// check how we did
if (!versions.success) {
return Promise.reject(
new Error(
Expand Down
2 changes: 1 addition & 1 deletion source/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function loadVersion(version) {

function runCommand(version, command) {
return exec(
`unset npm_config_prefix && . ~/.nvm/nvm.sh && nvm use ${version} && ${command}`,
`unset npm_config_prefix && . ~/.nvm/nvm.sh && nvm use --silent ${version} && ${command}`,
)
}

Expand Down
44 changes: 38 additions & 6 deletions source/versions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// external
const versionClean = require('version-clean').default
const versionCompare = require('version-compare').default
const versionRange = require('version-range').default

// local
const Version = require('./version.js')
Expand Down Expand Up @@ -39,13 +40,10 @@ class Versions {
* @type {Array<Version>}
* @public
*/
this.array = versions.map((v) => {
const V = new Version(v, this.listeners)
return V
})
this.array = []

// Sort
this.sort()
// Add the versions
this.add(versions)
}

/**
Expand All @@ -71,6 +69,39 @@ class Versions {
}
}

/**
* Add version(s) to the list.
* @param {Version|Array<Version>} versions
* @returns {this}
* @public
*/
add(versions) {
if (!Array.isArray(versions)) versions = [versions]
this.array.push(
...versions.map((v) => {
const V = new Version(v, this.listeners)
return V
}),
)
this.compact()
return this
}

/**
* Get a version(s) from the list.
* @param {Version} version number or alias
* @returns {this}
* @public
*/
get(version) {
return (
this.array.find((V) => V.version === version) ||
this.array.find((V) => V.aliases.includes(version)) ||
this.array.find((V) => versionRange(V.version, version)) ||
null
)
}

/**
* Run map on our versions
* @param {...*} args
Expand Down Expand Up @@ -117,6 +148,7 @@ class Versions {
}
})
this.array = Object.values(map)
this.sort()
return this
}

Expand Down

0 comments on commit 77bb1b9

Please sign in to comment.