Skip to content

Commit bc5bc30

Browse files
committed
add specs
1 parent 16ef814 commit bc5bc30

File tree

3 files changed

+70
-3
lines changed

3 files changed

+70
-3
lines changed

bin/helpers/utils.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1212,6 +1212,9 @@ exports.getMajorVersion = (version) => {
12121212

12131213
const matches = version.match(/^(\d+\.)?(\d+\.)?(\*|\d+)$/)
12141214
if(matches && matches.length >= 2) {
1215+
if (!matches[1]) {
1216+
return matches[0];
1217+
}
12151218
return matches[1].replace('.','');
12161219
} else {
12171220
return null;

test/unit/bin/helpers/packageInstaller.js

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ const chai = require("chai"),
77
cp = require('child_process');
88

99
const logger = require("../../../../bin/helpers/logger").winstonLogger,
10-
fileHelpers = require("../../../../bin/helpers/fileHelpers");
10+
fileHelpers = require("../../../../bin/helpers/fileHelpers"),
11+
utils = require('../../../../bin/helpers/utils');
1112

1213
const rewire = require("rewire");
1314

@@ -214,15 +215,47 @@ describe("packageInstaller", () => {
214215
context("packageInstall", () => {
215216
const packageInstaller = rewire("../../../../bin/helpers/packageInstaller");
216217

217-
it("should call npm install on directory and resolve if spawn is closed successfully", () => {
218+
it("should call npm install on directory with npm <= 6 and resolve if spawn is closed successfully", () => {
218219
let spawnStub = sandbox.stub(cp, 'spawn').returns({
219220
on: (_close, nodeProcessCloseCallback) => {
220221
nodeProcessCloseCallback(0);
221222
}
222223
});
224+
let getMajorVersionStub = sandbox.stub(utils, 'getMajorVersion').returns('7');
223225
packageInstaller.__set__({
224226
nodeProcess: {},
225-
spawn: spawnStub
227+
spawn: spawnStub,
228+
utils: {
229+
getMajorVersion: getMajorVersionStub
230+
}
231+
});
232+
let packageInstallrewire = packageInstaller.__get__('packageInstall');
233+
let directoryPath = "/random/path";
234+
return packageInstallrewire(directoryPath)
235+
.then((data) => {
236+
console.log(data);
237+
chai.assert.equal(data, "Packages were installed successfully.")
238+
spawnStub.restore();
239+
getMajorVersionStub.restore();
240+
})
241+
.catch((_error) => {
242+
chai.assert.fail(`Promise error ${_error}`);
243+
});
244+
});
245+
246+
it("should call npm install on directory with npm >= 7 and resolve if spawn is closed successfully", () => {
247+
let spawnStub = sandbox.stub(cp, 'spawn').returns({
248+
on: (_close, nodeProcessCloseCallback) => {
249+
nodeProcessCloseCallback(0);
250+
}
251+
});
252+
let getMajorVersionStub = sandbox.stub(utils, 'getMajorVersion').returns('7');
253+
packageInstaller.__set__({
254+
nodeProcess: {},
255+
spawn: spawnStub,
256+
utils: {
257+
getMajorVersion: getMajorVersionStub
258+
}
226259
});
227260
let packageInstallrewire = packageInstaller.__get__('packageInstall');
228261
let directoryPath = "/random/path";
@@ -231,6 +264,7 @@ describe("packageInstaller", () => {
231264
console.log(data);
232265
chai.assert.equal(data, "Packages were installed successfully.")
233266
spawnStub.restore();
267+
getMajorVersionStub.restore();
234268
})
235269
.catch((_error) => {
236270
chai.assert.fail(`Promise error ${_error}`);

test/unit/bin/helpers/utils.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3222,4 +3222,34 @@ describe('utils', () => {
32223222
expect(utils.formatRequest(null, {statusCode: 500}, cricularBody)).to.be.eql({err: null, status: 500, body: '[Circular]'});
32233223
});
32243224
});
3225+
3226+
describe('getMajorVersion', () => {
3227+
it('should return null if undefined version is sent', () => {
3228+
expect(utils.getMajorVersion()).to.be.eql(null);
3229+
});
3230+
3231+
it('should return null if null version is sent', () => {
3232+
expect(utils.getMajorVersion(null)).to.be.eql(null);
3233+
});
3234+
3235+
it('should return null if improper version is sent', () => {
3236+
expect(utils.getMajorVersion('test')).to.be.eql(null);
3237+
expect(utils.getMajorVersion('a1.1.1')).to.be.eql(null);
3238+
expect(utils.getMajorVersion('1a.1.1')).to.be.eql(null);
3239+
expect(utils.getMajorVersion('1.a1.1')).to.be.eql(null);
3240+
expect(utils.getMajorVersion('1.1a.1')).to.be.eql(null);
3241+
expect(utils.getMajorVersion('1.1.a1')).to.be.eql(null);
3242+
expect(utils.getMajorVersion('1.1.1a')).to.be.eql(null);
3243+
expect(utils.getMajorVersion('.1.1.1')).to.be.eql(null);
3244+
expect(utils.getMajorVersion('1.')).to.be.eql(null);
3245+
expect(utils.getMajorVersion('$')).to.be.eql(null);
3246+
});
3247+
3248+
it('should return proper major version if proper version is sent', () => {
3249+
expect(utils.getMajorVersion('1.1.1')).to.be.eql('1');
3250+
expect(utils.getMajorVersion('2.1')).to.be.eql('2');
3251+
expect(utils.getMajorVersion('3')).to.be.eql('3');
3252+
expect(utils.getMajorVersion('4.1')).to.be.eql('4');
3253+
});
3254+
});
32253255
});

0 commit comments

Comments
 (0)