UNPKG

18.2 kBJavaScriptView Raw
1var should = require('should');
2var index = require('../index');
3var getOptions = index.getOptions;
4var getBaseOptions = index.getBaseOptions;
5
6describe("getBaseOptions", function(){
7 beforeEach(function(){
8 process.env = {PATH: process.env.PATH};
9 });
10 it ("should set service_job_id if it exists", function(done){
11 testServiceJobId(getBaseOptions, done);
12 });
13 it ("should set git hash if it exists", function(done){
14 testGitHash(getBaseOptions, done);
15 });
16 it ("should set git branch if it exists", function(done){
17 testGitBranch(getBaseOptions, done);
18 });
19 it ("should detect current git hash if not passed in", function(done) {
20 testGitHashDetection(getBaseOptions, done);
21 });
22 it ("should detect current git branch if not passed in", function(done) {
23 testGitBranchDetection(getBaseOptions, done);
24 });
25 it ("should detect detached git head if no hash passed in", function(done) {
26 testGitDetachedHeadDetection(getBaseOptions, done);
27 });
28 it ("should fail local Git detection if no .git directory", function(done) {
29 testNoLocalGit(getBaseOptions, done);
30 });
31 it ("should set repo_token if it exists", function(done){
32 testRepoToken(getBaseOptions, done);
33 });
34 it ("should detect repo_token if not passed in", function(done){
35 testRepoTokenDetection(getBaseOptions, done);
36 });
37 it ("should set service_name if it exists", function(done){
38 testServiceName(getBaseOptions, done);
39 });
40 it ("should set service_name and service_job_id if it's running on travis-ci", function(done){
41 testTravisCi(getBaseOptions, done);
42 });
43 it ("should set service_name and service_job_id if it's running on jenkins", function(done){
44 testJenkins(getBaseOptions, done);
45 });
46 it ("should set service_name and service_job_id if it's running on circleci", function(done){
47 testCircleCi(getBaseOptions, done);
48 });
49 it ("should set service_name and service_job_id if it's running on codeship", function(done){
50 testCodeship(getBaseOptions, done);
51 });
52 it ("should set service_name and service_job_id if it's running on drone", function(done){
53 testDrone(getBaseOptions, done);
54 });
55 it ("should set service_name and service_job_id if it's running on wercker", function(done){
56 testWercker(getBaseOptions, done);
57 });
58});
59
60describe("getOptions", function(){
61 beforeEach(function(){
62 process.env = {PATH: process.env.PATH};
63 });
64 it ("should require a callback", function(done) {
65 (function() {
66 getOptions();
67 }).should.throw();
68 done();
69 });
70 it ("should get a filepath if there is one", function(done){
71 index.options._ = ["somepath"];
72 getOptions(function(err, options){
73 options.filepath.should.equal("somepath");
74 done();
75 });
76
77 });
78 it ("should get a filepath if there is one, even in verbose mode", function(done){
79 index.options.verbose = "true";
80 index.options._ = ["somepath"];
81 getOptions(function(err, options){
82 options.filepath.should.equal("somepath");
83 done();
84 });
85 });
86 it ("should set service_job_id if it exists", function(done){
87 testServiceJobId(getOptions, done);
88 });
89 it ("should set git hash if it exists", function(done){
90 testGitHash(getOptions, done);
91 });
92 it ("should set git branch if it exists", function(done){
93 testGitBranch(getOptions, done);
94 });
95 it ("should detect current git hash if not passed in", function(done) {
96 testGitHashDetection(getOptions, done);
97 });
98 it ("should detect current git branch if not passed in", function(done) {
99 testGitBranchDetection(getOptions, done);
100 });
101 it ("should detect detached git head if no hash passed in", function(done) {
102 testGitDetachedHeadDetection(getOptions, done);
103 });
104 it ("should fail local Git detection if no .git directory", function(done) {
105 testNoLocalGit(getOptions, done);
106 });
107 it ("should set repo_token if it exists", function(done){
108 testRepoToken(getOptions, done);
109 });
110 it ("should detect repo_token if not passed in", function(done){
111 testRepoTokenDetection(getOptions, done);
112 });
113 it ("should set paralell if env var set", function(done){
114 testParallel(getOptions, done);
115 });
116 it ("should set service_name if it exists", function(done){
117 testServiceName(getOptions, done);
118 });
119 it("should set service_pull_request if it exists", function(done){
120 testServicePullRequest(getOptions, done);
121 });
122 it ("should set service_name and service_job_id if it's running on travis-ci", function(done){
123 testTravisCi(getOptions, done);
124 });
125 it ("should set service_name and service_job_id if it's running on jenkins", function(done){
126 testJenkins(getOptions, done);
127 });
128 it ("should set service_name and service_job_id if it's running on circleci", function(done){
129 testCircleCi(getOptions, done);
130 });
131 it ("should set service_name and service_job_id if it's running on codeship", function(done){
132 testCodeship(getOptions, done);
133 });
134 it ("should set service_name and service_job_id if it's running on drone", function(done){
135 testDrone(getBaseOptions, done);
136 });
137 it ("should set service_name and service_job_id if it's running on wercker", function(done){
138 testWercker(getOptions, done);
139 });
140 it ("should set service_name and service_job_id if it's running on Gitlab", function(done){
141 testGitlab(getOptions, done);
142 });
143 it ("should set service_name and service_job_id if it's running via Surf", function(done){
144 testSurf(getOptions, done);
145 });
146 it ("should override set options with user options", function(done){
147 var userOptions = {service_name: 'OVERRIDDEN_SERVICE_NAME'};
148 process.env.COVERALLS_SERVICE_NAME = "SERVICE_NAME";
149 getOptions(function(err, options){
150 options.service_name.should.equal("OVERRIDDEN_SERVICE_NAME");
151 done();
152 }, userOptions);
153 });
154});
155
156var testServiceJobId = function(sut, done){
157 process.env.COVERALLS_SERVICE_JOB_ID = "SERVICE_JOB_ID";
158 sut(function(err, options){
159 options.service_job_id.should.equal("SERVICE_JOB_ID");
160 done();
161 });
162};
163
164var testGitHash = function(sut, done){
165 process.env.COVERALLS_GIT_COMMIT = "e3e3e3e3e3e3e3e3e";
166 sut(function(err, options){
167 options.git.head.id.should.equal("e3e3e3e3e3e3e3e3e");
168 done();
169 });
170};
171
172var testGitDetachedHeadDetection = function(sut, done){
173 var localGit = ensureLocalGitContext({ detached: true });
174 sut(function(err, options) {
175 options.git.head.id.should.equal(localGit.id);
176 localGit.wrapUp();
177 done();
178 });
179};
180
181var testGitHashDetection = function(sut, done){
182 var localGit = ensureLocalGitContext();
183 sut(function(err, options) {
184 options.git.head.id.should.equal(localGit.id);
185 localGit.wrapUp();
186 done();
187 });
188};
189
190var testGitBranch = function(sut, done){
191 process.env.COVERALLS_GIT_COMMIT = "e3e3e3e3e3e3e3e3e";
192 process.env.COVERALLS_GIT_BRANCH = "master";
193 sut(function(err, options){
194 options.git.branch.should.equal("master");
195 done();
196 });
197};
198
199var testGitBranchDetection = function(sut, done){
200 var localGit = ensureLocalGitContext();
201 sut(function(err, options) {
202 if (localGit.branch)
203 options.git.branch.should.equal(localGit.branch);
204 else
205 options.git.should.not.have.key('branch');
206 localGit.wrapUp();
207 done();
208 });
209};
210
211var testNoLocalGit = function(sut, done){
212 var localGit = ensureLocalGitContext({ noGit: true });
213 sut(function(err, options) {
214 options.should.not.have.property('git');
215 localGit.wrapUp();
216 done();
217 });
218};
219
220var testRepoToken = function(sut, done){
221 process.env.COVERALLS_REPO_TOKEN = "REPO_TOKEN";
222 sut(function(err, options){
223 options.repo_token.should.equal("REPO_TOKEN");
224 done();
225 });
226};
227
228var testParallel = function(sut, done){
229 process.env.COVERALLS_PARALLEL = "true";
230 sut(function(err, options){
231 options.parallel.should.equal(true);
232 done();
233 });
234};
235
236var testRepoTokenDetection = function(sut, done) {
237 var fs = require('fs');
238 var path = require('path');
239
240 var file = path.join(process.cwd(), '.coveralls.yml'), token, service_name, synthetic = false;
241 if (fs.exists(file)) {
242 var yaml = require('js-yaml');
243 var coveralls_yml_doc = yaml.safeLoad(fs.readFileSync(yml, 'utf8'));
244 token = coveralls_yml_doc.repo_token;
245 if(coveralls_yml_doc.service_name) {
246 service_name = coveralls_yml_doc.service_name;
247 }
248 } else {
249 token = 'REPO_TOKEN';
250 service_name = 'travis-pro';
251 fs.writeFileSync(file, 'repo_token: ' + token+'\nservice_name: ' + service_name);
252 synthetic = true;
253 }
254 sut(function(err, options) {
255 options.repo_token.should.equal(token);
256 if(service_name) {
257 options.service_name.should.equal(service_name);
258 }
259 if (synthetic)
260 fs.unlink(file);
261 done();
262 });
263};
264
265var testServiceName = function(sut, done){
266 process.env.COVERALLS_SERVICE_NAME = "SERVICE_NAME";
267 sut(function(err, options){
268 options.service_name.should.equal("SERVICE_NAME");
269 done();
270 });
271};
272
273var testServicePullRequest = function(sut, done){
274 process.env.CI_PULL_REQUEST = "https://github.com/fake/fake/pulls/123";
275 sut(function(err, options){
276 options.service_pull_request.should.equal("123");
277 done();
278 });
279};
280
281var testTravisCi = function(sut, done){
282 process.env.TRAVIS = "TRUE";
283 process.env.TRAVIS_JOB_ID = "1234";
284 process.env.TRAVIS_PULL_REQUEST = "123";
285 sut(function(err, options){
286 options.service_name.should.equal("travis-ci");
287 options.service_job_id.should.equal("1234");
288 options.service_pull_request.should.equal("123");
289 done();
290 });
291};
292
293var testJenkins = function(sut, done){
294 process.env.JENKINS_URL = "something";
295 process.env.BUILD_ID = "1234";
296 process.env.GIT_COMMIT = "a12s2d3df4f435g45g45g67h5g6";
297 process.env.GIT_BRANCH = "master";
298 sut(function(err, options){
299 options.service_name.should.equal("jenkins");
300 options.service_job_id.should.equal("1234");
301 options.git.should.eql({ head:
302 { id: 'a12s2d3df4f435g45g45g67h5g6',
303 author_name: 'Unknown Author',
304 author_email: '',
305 committer_name: 'Unknown Committer',
306 committer_email: '',
307 message: 'Unknown Commit Message' },
308 branch: 'master',
309 remotes: [] });
310 done();
311 });
312};
313
314var testCircleCi = function(sut, done){
315 process.env.CIRCLECI = true;
316 process.env.CIRCLE_BRANCH = "master";
317 process.env.CIRCLE_BUILD_NUM = "1234";
318 process.env.CIRCLE_SHA1 = "e3e3e3e3e3e3e3e3e";
319 process.env.CI_PULL_REQUEST = 'http://github.com/node-coveralls/pull/3';
320 sut(function(err, options){
321 options.service_name.should.equal("circleci");
322 options.service_job_id.should.equal("1234");
323 options.service_pull_request.should.equal('3');
324 options.git.should.eql({ head:
325 { id: 'e3e3e3e3e3e3e3e3e',
326 author_name: 'Unknown Author',
327 author_email: '',
328 committer_name: 'Unknown Committer',
329 committer_email: '',
330 message: 'Unknown Commit Message' },
331 branch: 'master',
332 remotes: [] });
333 done();
334 });
335};
336
337var testCodeship = function(sut, done) {
338 process.env.CI_NAME = 'codeship';
339 process.env.CI_BUILD_NUMBER = '1234';
340 process.env.CI_COMMIT_ID = "e3e3e3e3e3e3e3e3e";
341 process.env.CI_BRANCH = "master";
342 process.env.CI_COMMITTER_NAME = "John Doe";
343 process.env.CI_COMMITTER_EMAIL = "jd@example.com";
344 process.env.CI_COMMIT_MESSAGE = "adadadadadadadadadad";
345 sut(function(err, options){
346 options.service_name.should.equal("codeship");
347 options.service_job_id.should.equal("1234");
348 options.git.should.eql({ head:
349 { id: 'e3e3e3e3e3e3e3e3e',
350 author_name: 'Unknown Author',
351 author_email: '',
352 committer_name: 'John Doe',
353 committer_email: 'jd@example.com',
354 message: 'adadadadadadadadadad' },
355 branch: 'master',
356 remotes: [] });
357 done();
358 });
359};
360
361var testDrone = function(sut, done) {
362 process.env.DRONE = true;
363 process.env.DRONE_BUILD_NUMBER = '1234';
364 process.env.DRONE_COMMIT = "e3e3e3e3e3e3e3e3e";
365 process.env.DRONE_BRANCH = "master";
366 process.env.DRONE_PULL_REQUEST = '3';
367 process.env.DRONE_COMMIT_AUTHOR = 'john doe';
368 process.env.DRONE_COMMIT_AUTHOR_EMAIL = 'john@doe.com';
369 process.env.DRONE_COMMIT_MESSAGE = 'msgmsgmsg';
370
371 sut(function(err, options){
372 options.service_name.should.equal("drone");
373 options.service_job_id.should.equal("1234");
374 options.git.should.eql({ head:
375 { id: 'e3e3e3e3e3e3e3e3e',
376 author_name: 'Unknown Author',
377 author_email: '',
378 committer_name: 'john doe',
379 committer_email: 'john@doe.com',
380 message: 'msgmsgmsg' },
381 branch: 'master',
382 remotes: [] });
383 done();
384 });
385};
386
387var testWercker = function(sut, done) {
388 process.env.WERCKER = true;
389 process.env.WERCKER_BUILD_ID = '1234';
390 process.env.WERCKER_GIT_COMMIT = "e3e3e3e3e3e3e3e3e";
391 process.env.WERCKER_GIT_BRANCH = "master";
392 sut(function(err, options){
393 options.service_name.should.equal("wercker");
394 options.service_job_id.should.equal("1234");
395 options.git.should.eql({ head:
396 { id: 'e3e3e3e3e3e3e3e3e',
397 author_name: 'Unknown Author',
398 author_email: '',
399 committer_name: 'Unknown Committer',
400 committer_email: '',
401 message: 'Unknown Commit Message' },
402 branch: 'master',
403 remotes: [] });
404 done();
405 });
406};
407
408var testGitlab = function(sut, done) {
409 process.env.GITLAB_CI = true;
410 process.env.CI_BUILD_NAME = 'spec:one';
411 process.env.CI_BUILD_ID = "1234";
412 process.env.CI_BUILD_REF = "e3e3e3e3e3e3e3e3e";
413 process.env.CI_BUILD_REF_NAME = "feature";
414 sut(function(err, options){
415 options.service_name.should.equal("gitlab-ci");
416 options.service_job_id.should.equal("1234");
417 options.git.should.eql({ head:
418 { id: 'e3e3e3e3e3e3e3e3e',
419 author_name: 'Unknown Author',
420 author_email: '',
421 committer_name: 'Unknown Committer',
422 committer_email: '',
423 message: 'Unknown Commit Message' },
424 branch: 'feature',
425 remotes: [] });
426 done();
427 });
428};
429
430var testSurf = function(sut, done) {
431 process.env.CI_NAME = 'surf';
432 process.env.SURF_SHA1 = "e3e3e3e3e3e3e3e3e";
433 process.env.SURF_REF = "feature";
434 sut(function(err, options){
435 options.service_name.should.equal("surf");
436 options.git.should.eql({ head:
437 { id: 'e3e3e3e3e3e3e3e3e',
438 author_name: 'Unknown Author',
439 author_email: '',
440 committer_name: 'Unknown Committer',
441 committer_email: '',
442 message: 'Unknown Commit Message' },
443 branch: 'feature',
444 remotes: [] });
445 done();
446 });
447};
448
449
450function ensureLocalGitContext(options) {
451 var path = require('path');
452 var fs = require('fs');
453
454 var baseDir = process.cwd(), dir = baseDir, gitDir;
455 while (path.resolve('/') !== dir) {
456 gitDir = path.join(dir, '.git');
457 var existsSync = fs.existsSync || path.existsSync;
458 if (existsSync(path.join(gitDir, 'HEAD')))
459 break;
460
461 dir = path.dirname(dir);
462 }
463
464 options = options || {};
465 var synthetic = path.resolve('/') === dir;
466 var gitHead, content, branch, id, wrapUp = function() {};
467
468 if (synthetic) {
469 branch = 'synthetic';
470 id = '424242424242424242';
471 gitHead = path.join('.git', 'HEAD');
472 var gitBranch = path.join('.git', 'refs', 'heads', branch);
473 fs.mkdirSync('.git');
474 if (options.detached) {
475 fs.writeFileSync(gitHead, id, { encoding: 'utf8' });
476 } else {
477 fs.mkdirSync(path.join('.git', 'refs'));
478 fs.mkdirSync(path.join('.git', 'refs', 'heads'));
479 fs.writeFileSync(gitHead, "ref: refs/heads/" + branch, { encoding: 'utf8' });
480 fs.writeFileSync(gitBranch, id, { encoding: 'utf8' });
481 }
482 wrapUp = function() {
483 fs.unlinkSync(gitHead);
484 if (!options.detached) {
485 fs.unlinkSync(gitBranch);
486 fs.rmdirSync(path.join('.git', 'refs', 'heads'));
487 fs.rmdirSync(path.join('.git', 'refs'));
488 }
489 fs.rmdirSync('.git');
490 };
491 } else if (options.noGit) {
492 fs.renameSync(gitDir, gitDir + '.bak');
493 wrapUp = function() {
494 fs.renameSync(gitDir + '.bak', gitDir);
495 };
496 } else if (options.detached) {
497 gitHead = path.join(gitDir, 'HEAD');
498 content = fs.readFileSync(gitHead, 'utf8').trim();
499 var b = (content.match(/^ref: refs\/heads\/(\S+)$/) || [])[1];
500 if (!b) {
501 id = content;
502 } else {
503 id = fs.readFileSync(path.join(gitDir, 'refs', 'heads', b), 'utf8').trim();
504 fs.writeFileSync(gitHead, id, 'utf8');
505 wrapUp = function() {
506 fs.writeFileSync(gitHead, content, 'utf8');
507 };
508 }
509 } else {
510 content = fs.readFileSync(path.join(gitDir, 'HEAD'), 'utf8').trim();
511 branch = (content.match(/^ref: refs\/heads\/(\S+)$/) || [])[1];
512 id = branch ? fs.readFileSync(path.join(gitDir, 'refs', 'heads', branch), 'utf8').trim() : content;
513 }
514
515 return { id: id, branch: branch, wrapUp: wrapUp };
516}