UNPKG

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