diff --git a/scripts/__tests__/bumpTemplateVersion-test.js b/scripts/__tests__/bumpTemplateVersion-test.js index 213c3db..e459a69 100644 --- a/scripts/__tests__/bumpTemplateVersion-test.js +++ b/scripts/__tests__/bumpTemplateVersion-test.js @@ -1,10 +1,44 @@ -const {execSync} = require('child_process'); +const {execSync, exec: _exec} = require('child_process'); +const fs = require('fs'); +const {promisify} = require('util'); +const path = require('path'); const semver = require('semver'); function run(version) { return execSync(`./bumpedTemplateVersion.sh ${version}`, { cwd: 'scripts', stdio: ['ignore', 'pipe', 'ignore'] }).toString().trim(); } +const FIFO_PATH = '/tmp/_npm_fifo' + +const exec = promisify(_exec); +const writeFile = promisify(fs.writeFile); + +async function runStubbedNpm(version, response) { + cleanUpStubbedNpm(); + execSync(`mkfifo ${FIFO_PATH}`); + return Promise.all( + [ + writeFile(FIFO_PATH, JSON.stringify(response)), + exec(`./bumpedTemplateVersion.sh ${version}`, { + cwd: 'scripts', + stdio: ['ignore', 'pipe', 'ignore'], + env: { + PATH: `${__dirname}/stub:${process.env.PATH}`, + NPM_STUB_FIFO: FIFO_PATH, + } + }).then(raw => raw.stdout.toString().trim()), + ] + ).then(([, resp]) => resp); +} + +function cleanUpStubbedNpm() { + try { + fs.unlinkSync(FIFO_PATH); + } catch { + // Best attempt is OK. + } +} + describe('bumpTemplateVersion.sh', () => { it('nightlies stay the same', () => { @@ -27,4 +61,19 @@ describe('bumpTemplateVersion.sh', () => { expect(run(`${major}.${minor}.${patch}`)).toEqual(`${major}.${minor}.${patch+1}`); }); + describe('handles different npm responses', () => { + afterAll(cleanUpStubbedNpm); + it('arrays of versions', async () => { + expect(await runStubbedNpm('0.76.0', [ + { version: '0.76.0' }, + { version: '0.76.2' }, + // Expecting +1 on this: + { version: '0.76.3' }, + ])).toEqual('0.76.4'); + }); + + it('single version', async () => { + expect(await runStubbedNpm('0.76.0', { version: '0.76.0' })).toEqual('0.76.1'); + }); + }); }); diff --git a/scripts/bumpedTemplateVersion.sh b/scripts/bumpedTemplateVersion.sh index f0b9128..8e6ec44 100755 --- a/scripts/bumpedTemplateVersion.sh +++ b/scripts/bumpedTemplateVersion.sh @@ -41,7 +41,7 @@ if [[ $1 =~ [0-9]+\.[0-9]+\.[0-9]+ ]]; then exit 0; fi # Bump PATCH - PUBLISHED=$(jq -r 'last | .version' <<< $CURRENT_VERSION | awk -F'.' '{ print $1"."$2"."($3+1) }') + PUBLISHED=$(jq -r 'if . | type == "array" then last | .version else .version end' <<< $CURRENT_VERSION | awk -F'.' '{ print $1"."$2"."($3+1) }') echo $PUBLISHED; exit 0 fi