Skip to content

Commit

Permalink
fix: handle response from npm when one version (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
blakef authored Oct 29, 2024
1 parent 05b05a8 commit 32b7247
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
51 changes: 50 additions & 1 deletion scripts/__tests__/bumpTemplateVersion-test.js
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand All @@ -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');
});
});
});
2 changes: 1 addition & 1 deletion scripts/bumpedTemplateVersion.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 32b7247

Please sign in to comment.