|
1 | | -(function (handleInput, helper) { |
| 1 | +(function (handleInput, helper, Joi, request, fs, path) { |
2 | 2 | 'use strict'; |
3 | 3 |
|
4 | 4 | var expect = helper.chai.expect; |
| 5 | + var lcovData = fs.readFileSync(__dirname + '/mock/lcov.info').toString(); |
5 | 6 |
|
6 | 7 | describe('Handle Input', function () { |
7 | | - it('should return a promise', function () { |
8 | | - return expect(handleInput()).to.eventually.be.rejected(); |
| 8 | + beforeEach(function () { |
| 9 | + helper.clearEnvironmentVariables(); |
| 10 | + }); |
| 11 | + it('should be able to use the mock end-point', function () { |
| 12 | + var bodyValidator = Joi.object({ |
| 13 | + total: Joi.number().valid(50), |
| 14 | + fileReports: Joi.array().items(Joi.object({ |
| 15 | + filename: Joi.string().valid('filename'), |
| 16 | + total: Joi.number().valid(10), |
| 17 | + coverage: Joi.object({ |
| 18 | + 1: Joi.number().valid(1), |
| 19 | + 2: Joi.number().valid(3) |
| 20 | + }) |
| 21 | + }).required()) |
| 22 | + }); |
| 23 | + |
| 24 | + var sampleCoverageData = { |
| 25 | + total: 50, |
| 26 | + fileReports: [ |
| 27 | + { |
| 28 | + filename: 'filename', |
| 29 | + total: 10, |
| 30 | + coverage: { |
| 31 | + 1: 1, |
| 32 | + 2: 3 |
| 33 | + } |
| 34 | + } |
| 35 | + ] |
| 36 | + }; |
| 37 | + |
| 38 | + return helper.setupMockEndpoint('1234', '4321', bodyValidator) |
| 39 | + .then(function () { |
| 40 | + return expect(request({ |
| 41 | + url: 'https://www.codacy.com/api/coverage/1234/4321', |
| 42 | + method: 'POST', |
| 43 | + json: sampleCoverageData, |
| 44 | + resolveWithFullResponse: true |
| 45 | + }).promise()).to.eventually.satisfy(function (res) { |
| 46 | + expect(res.statusCode).to.equal(200); |
| 47 | + return true; |
| 48 | + }); |
| 49 | + }); |
| 50 | + }); |
| 51 | + it('should be able to parse lcov data', function () { |
| 52 | + var expectedCoverage = { |
| 53 | + total: 92, |
| 54 | + fileReports: Joi.array().items(Joi.compile({ |
| 55 | + filename: path.normalize('lib/reporter.js'), |
| 56 | + coverage: { |
| 57 | + 1: 1, |
| 58 | + 25: 1, |
| 59 | + 39: 1, |
| 60 | + 40: 3, |
| 61 | + 44: 3, |
| 62 | + 48: 3, |
| 63 | + 50: 3, |
| 64 | + 52: 3, |
| 65 | + 54: 3, |
| 66 | + 55: 3, |
| 67 | + 61: 3, |
| 68 | + 63: 3, |
| 69 | + 67: 3, |
| 70 | + 73: 2, |
| 71 | + 74: 1, |
| 72 | + 75: 1, |
| 73 | + 76: 1, |
| 74 | + 77: 1, |
| 75 | + 79: 2, |
| 76 | + 81: 1, |
| 77 | + 82: 1, |
| 78 | + 83: 1, |
| 79 | + 84: 1, |
| 80 | + 87: 3 |
| 81 | + }, |
| 82 | + total: 92 |
| 83 | + })) |
| 84 | + }; |
| 85 | + |
| 86 | + return helper.setupMockEndpoint('1234', '4321', Joi.compile(expectedCoverage)) |
| 87 | + .then(function () { |
| 88 | + return expect(handleInput(lcovData, { |
| 89 | + token: '1234', |
| 90 | + commit: '4321' |
| 91 | + })).to.eventually.be.fulfilled(); |
| 92 | + }); |
| 93 | + }); |
| 94 | + it('should be able to parse lcov data with path prefix', function () { |
| 95 | + var expectedCoverage = { |
| 96 | + total: 92, |
| 97 | + fileReports: Joi.array().items(Joi.compile({ |
| 98 | + filename: path.normalize('my-project/lib/reporter.js'), |
| 99 | + coverage: { |
| 100 | + 1: 1, |
| 101 | + 25: 1, |
| 102 | + 39: 1, |
| 103 | + 40: 3, |
| 104 | + 44: 3, |
| 105 | + 48: 3, |
| 106 | + 50: 3, |
| 107 | + 52: 3, |
| 108 | + 54: 3, |
| 109 | + 55: 3, |
| 110 | + 61: 3, |
| 111 | + 63: 3, |
| 112 | + 67: 3, |
| 113 | + 73: 2, |
| 114 | + 74: 1, |
| 115 | + 75: 1, |
| 116 | + 76: 1, |
| 117 | + 77: 1, |
| 118 | + 79: 2, |
| 119 | + 81: 1, |
| 120 | + 82: 1, |
| 121 | + 83: 1, |
| 122 | + 84: 1, |
| 123 | + 87: 3 |
| 124 | + }, |
| 125 | + total: 92 |
| 126 | + })) |
| 127 | + }; |
| 128 | + |
| 129 | + return helper.setupMockEndpoint('1234', '4321', Joi.compile(expectedCoverage)) |
| 130 | + .then(function () { |
| 131 | + return expect(handleInput(lcovData, { |
| 132 | + token: '1234', |
| 133 | + commit: '4321', |
| 134 | + prefix: 'my-project/' |
| 135 | + })).to.eventually.be.fulfilled(); |
| 136 | + }); |
| 137 | + }); |
| 138 | + it('shouldn\'t be able to send coverage with invalid input', function () { |
| 139 | + return expect(handleInput()).to.eventually.be.rejectedWith(Error, 'Token is required'); |
9 | 140 | }); |
10 | 141 | }); |
11 | 142 |
|
12 | | -}(require('../lib/handleInput'), require('./helper'))); |
| 143 | +}(require('../lib/handleInput'), require('./helper'), require('joi'), require('request-promise'), require('fs'), require('path'))); |
0 commit comments