Skip to content

Commit 87b4fce

Browse files
committed
Add unit tests for zipUpload, usageReporting and build creation
1 parent 1b315d2 commit 87b4fce

File tree

12 files changed

+862
-79
lines changed

12 files changed

+862
-79
lines changed

.nycrc.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
3+
# cover all files (even if a file isn't touched by any test)
4+
all: true
5+
6+
# check coverage percentage and fail test if below threshold
7+
check-coverage: true
8+
9+
# thresholds
10+
branches: 60
11+
lines: 60
12+
functions: 60
13+
statements: 60

bin/helpers/build.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const createBuild = (bsConfig, zip) => {
2121
},
2222
body: data
2323
}
24-
24+
2525
request.post(options, function (err, resp, body) {
2626
if (err) {
2727
reject(err);

bin/helpers/fileHelpers.js

Lines changed: 3 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,9 @@
11
'use strict';
22
const fs = require('fs-extra'),
3-
path = require('path'),
4-
mkdirp = require('mkdirp');
3+
path = require('path');
54

65
const logger = require('./logger').winstonLogger;
76

8-
// exports.isEmpty = function(path, cb) {
9-
// fs.readdir(path, function(err, files) {
10-
// if (err && 'ENOENT' != err.code) throw err;
11-
// cb(!files || !files.length);
12-
// });
13-
// }
14-
15-
// exports.isDirectory = function(path, cb) {
16-
// fs.stat(path, function(err, stats) {
17-
// if (err && 'ENOENT' != err.code) throw err;
18-
// if (err) {
19-
// return cb(false)
20-
// }
21-
// cb(stats.isDirectory())
22-
// })
23-
// }
24-
25-
// exports.isFile = function(path, cb) {
26-
// fs.stat(path, function(err, stats) {
27-
// if (err && 'ENOENT' != err.code) throw err;
28-
// if (err) {
29-
// return cb(false)
30-
// }
31-
// cb(stats.isFile())
32-
// })
33-
// }
34-
35-
// exports.mkdir = function(dir, cb) {
36-
// mkdirp(dir, '0755', function(err) {
37-
// if (err) throw err;
38-
// logger.info("Creating directory: ./" + path.relative(process.cwd(), dir));
39-
// cb && cb()
40-
// })
41-
// }
42-
437
exports.write = function(f, message, cb) {
448
message = message || 'Creating';
459
fs.writeFile(f.path, f.file, function() {
@@ -49,12 +13,11 @@ exports.write = function(f, message, cb) {
4913
}
5014

5115
exports.fileExists = function(filePath, cb) {
16+
let exists = true;
5217
fs.access(filePath, fs.F_OK, (err) => {
53-
let exists = true;
5418
if (err) {
5519
exists = false;
5620
}
57-
58-
cb && cb(exists)
5921
})
22+
cb && cb(exists);
6023
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"mocha": "^7.1.2",
3535
"nyc": "^15.0.1",
3636
"proxyquire": "^2.1.3",
37+
"rewire": "^5.0.0",
3738
"sinon": "^9.0.2"
3839
}
3940
}

test/unit/bin/commands/info.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ describe("buildInfo", () => {
304304

305305
return info(args)
306306
.then(function (_bsConfig) {
307-
chai.assert.isNotOk(error, "Promise error");
307+
chai.assert.fail("Promise error");
308308
}).catch((error) => {
309309
sinon.assert.calledOnceWithExactly(sendUsageReportStub, null, args, "reject error", Constants.messageTypes.ERROR, "random-error");
310310
});

test/unit/bin/commands/stop.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ describe("buildStop", () => {
316316

317317
return stop(args)
318318
.then(function (_bsConfig) {
319-
chai.assert.isNotOk(error, "Promise error");
319+
chai.assert.fail("Promise error");
320320
})
321321
.catch((error) => {
322322
sinon.assert.calledOnceWithExactly(

test/unit/bin/helpers/build.js

Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
const chai = require("chai"),
2+
chaiAsPromised = require("chai-as-promised"),
3+
sinon = require("sinon"),
4+
request = require("request"),
5+
fs = require("fs");
6+
7+
const Constants = require("../../../../bin/helpers/constants"),
8+
logger = require("../../../../bin/helpers/logger").winstonLogger,
9+
testObjects = require("../../support/fixtures/testObjects");
10+
11+
const proxyquire = require("proxyquire").noCallThru();
12+
13+
chai.use(chaiAsPromised);
14+
logger.transports["console.info"].silent = true;
15+
16+
describe("build", () => {
17+
let bsConfig = testObjects.sampleBsConfig;
18+
let capsData = testObjects.sampleCapsData;
19+
20+
var sandbox;
21+
22+
beforeEach(() => {
23+
sandbox = sinon.createSandbox();
24+
getUserAgentStub = sandbox.stub().returns("random user-agent");
25+
capsStub = sandbox.stub().returns(Promise.resolve(capsData));
26+
});
27+
28+
afterEach(() => {
29+
sandbox.restore();
30+
sinon.restore();
31+
});
32+
33+
it("reject with error", () => {
34+
let requestStub = sandbox
35+
.stub(request, "post")
36+
.yields(new Error("random error"), null, null);
37+
38+
const build = proxyquire("../../../../bin/helpers/build", {
39+
"../helpers/utils": {
40+
getUserAgent: getUserAgentStub,
41+
},
42+
"../helpers/capabilityHelper": {
43+
caps: capsStub,
44+
},
45+
request: { post: requestStub },
46+
});
47+
48+
return build.createBuild(bsConfig, "random_zip_file")
49+
.then(function (data) {
50+
chai.assert.fail("Promise error");
51+
})
52+
.catch((error) => {
53+
sinon.assert.calledOnce(requestStub);
54+
sinon.assert.calledOnce(getUserAgentStub);
55+
chai.assert.equal(error.message, "random error");
56+
});
57+
});
58+
59+
describe("handle API deprecated", () => {
60+
it("build is null", () => {
61+
let requestStub = sandbox
62+
.stub(request, "post")
63+
.yields(null, { statusCode: 299 }, null);
64+
65+
const build = proxyquire("../../../../bin/helpers/build", {
66+
"../helpers/utils": {
67+
getUserAgent: getUserAgentStub,
68+
},
69+
"../helpers/capabilityHelper": {
70+
caps: capsStub,
71+
},
72+
request: { post: requestStub },
73+
});
74+
75+
return build
76+
.createBuild(bsConfig, "random_zip_file")
77+
.then(function (data) {
78+
chai.assert.fail("Promise error");
79+
})
80+
.catch((error) => {
81+
sinon.assert.calledOnce(requestStub);
82+
sinon.assert.calledOnce(getUserAgentStub);
83+
chai.assert.equal(error, Constants.userMessages.API_DEPRECATED);
84+
});
85+
});
86+
87+
it("build is not null", () => {
88+
let build_message = "random message";
89+
let requestStub = sandbox
90+
.stub(request, "post")
91+
.yields(null, { statusCode: 299 }, JSON.stringify({ message: build_message }));
92+
93+
const build = proxyquire("../../../../bin/helpers/build", {
94+
"../helpers/utils": {
95+
getUserAgent: getUserAgentStub,
96+
},
97+
"../helpers/capabilityHelper": {
98+
caps: capsStub,
99+
},
100+
request: { post: requestStub },
101+
});
102+
103+
return build
104+
.createBuild(bsConfig, "random_zip_file")
105+
.then(function (data) {
106+
sinon.assert.calledOnce(requestStub);
107+
sinon.assert.calledOnce(getUserAgentStub);
108+
chai.assert.equal(data, "random message");
109+
})
110+
.catch((error) => {
111+
chai.assert.isNotOk(error, "Promise error");
112+
});
113+
});
114+
});
115+
116+
describe("handle statusCode != 201", () => {
117+
it("build is null", () => {
118+
let requestStub = sandbox
119+
.stub(request, "post")
120+
.yields(null, { statusCode: 400 }, null);
121+
122+
const build = proxyquire("../../../../bin/helpers/build", {
123+
"../helpers/utils": {
124+
getUserAgent: getUserAgentStub,
125+
},
126+
"../helpers/capabilityHelper": {
127+
caps: capsStub,
128+
},
129+
request: { post: requestStub },
130+
});
131+
132+
return build
133+
.createBuild(bsConfig, "random_zip_file")
134+
.then(function (data) {
135+
chai.assert.fail("Promise error");
136+
})
137+
.catch((error) => {
138+
sinon.assert.calledOnce(requestStub);
139+
sinon.assert.calledOnce(getUserAgentStub);
140+
chai.assert.equal(error, Constants.userMessages.BUILD_FAILED);
141+
});
142+
});
143+
144+
it("build is not null", () => {
145+
let build_message = "random message";
146+
let requestStub = sandbox
147+
.stub(request, "post")
148+
.yields(
149+
null,
150+
{ statusCode: 401 },
151+
JSON.stringify({ message: build_message })
152+
);
153+
154+
const build = proxyquire("../../../../bin/helpers/build", {
155+
"../helpers/utils": {
156+
getUserAgent: getUserAgentStub,
157+
},
158+
"../helpers/capabilityHelper": {
159+
caps: capsStub,
160+
},
161+
request: { post: requestStub },
162+
});
163+
164+
return build
165+
.createBuild(bsConfig, "random_zip_file")
166+
.then(function (data) {
167+
chai.assert.fail("Promise error");
168+
})
169+
.catch((error) => {
170+
sinon.assert.calledOnce(requestStub);
171+
sinon.assert.calledOnce(getUserAgentStub);
172+
chai.assert.equal(error, `${Constants.userMessages.BUILD_FAILED} Error: ${build_message}`);
173+
});
174+
});
175+
});
176+
177+
it("build created successfuly", () => {
178+
let build_id = "random build id";
179+
let build_message = "success"
180+
let requestStub = sandbox
181+
.stub(request, "post")
182+
.yields(
183+
null,
184+
{ statusCode: 201 },
185+
JSON.stringify({ message: build_message, build_id: build_id })
186+
);
187+
188+
const build = proxyquire("../../../../bin/helpers/build", {
189+
"../helpers/utils": {
190+
getUserAgent: getUserAgentStub,
191+
},
192+
"../helpers/capabilityHelper": {
193+
caps: capsStub,
194+
},
195+
request: { post: requestStub },
196+
});
197+
198+
return build
199+
.createBuild(bsConfig, "random_zip_file")
200+
.then(function (data) {
201+
sinon.assert.calledOnce(requestStub);
202+
sinon.assert.calledOnce(getUserAgentStub);
203+
chai.assert.equal(data, `${build_message}! ${Constants.userMessages.BUILD_CREATED} with build id: ${build_id}`);
204+
})
205+
.catch((error) => {
206+
chai.assert.isNotOk(error, "Promise error");
207+
});
208+
});
209+
});

0 commit comments

Comments
 (0)