Skip to content

Commit 056bb0d

Browse files
authored
fix(version): ensure custom version is handled as a string (#1112)
1 parent 988f95d commit 056bb0d

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

lib/commands/install.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ class InstallCommand extends Command {
1919
const dirIsEmpty = require('../utils/dir-is-empty');
2020
const ensureStructure = require('../tasks/ensure-structure');
2121

22-
let version = argv.version;
22+
// if version is a single number (i.e. 2) yargs converts it to a number.
23+
// We convert it back to a string for consistency
24+
let version = argv.version ? `${argv.version}` : null;
2325

2426
// Check if the directory is empty
2527
if (!dirIsEmpty(process.cwd())) {

lib/commands/update.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ class UpdateCommand extends Command {
3434
);
3535
}
3636

37-
const {force, version, zip, v1} = argv;
37+
const {force, zip, v1} = argv;
38+
const version = argv.version ? `${argv.version}` : null;
3839

3940
const context = {
4041
instance,

test/unit/commands/install-spec.js

+28
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,34 @@ describe('Unit: Commands > Install', function () {
160160
});
161161
});
162162

163+
it('normalizes version to a string', function () {
164+
const dirEmptyStub = sinon.stub().returns(true);
165+
const listrStub = sinon.stub();
166+
listrStub.onFirstCall().resolves();
167+
listrStub.onSecondCall().rejects();
168+
const setEnvironmentStub = sinon.stub();
169+
170+
const InstallCommand = proxyquire(modulePath, {
171+
'../utils/dir-is-empty': dirEmptyStub
172+
});
173+
const testInstance = new InstallCommand({listr: listrStub}, {cliVersion: '1.0.0', setEnvironment: setEnvironmentStub});
174+
const runCommandStub = sinon.stub(testInstance, 'runCommand').resolves();
175+
176+
return testInstance.run({version: 2, zip: '', v1: false, _: ['install', 'local']}).then(() => {
177+
expect(false, 'run should have rejected').to.be.true;
178+
}).catch(() => {
179+
expect(dirEmptyStub.calledOnce).to.be.true;
180+
expect(runCommandStub.calledOnce).to.be.true;
181+
expect(listrStub.calledOnce).to.be.true;
182+
expect(listrStub.args[0][1]).to.deep.equal({
183+
argv: {version: '2', zip: '', v1: false, _: ['install', 'local']},
184+
cliVersion: '1.0.0'
185+
});
186+
expect(setEnvironmentStub.calledOnce).to.be.true;
187+
expect(setEnvironmentStub.calledWithExactly(true, true)).to.be.true;
188+
});
189+
});
190+
163191
it('calls all tasks and returns after tasks run if --no-setup is passed', function () {
164192
const dirEmptyStub = sinon.stub().returns(true);
165193
const yarnInstallStub = sinon.stub().resolves();

0 commit comments

Comments
 (0)