1 |
|
2 | "use strict";
|
3 |
|
4 | var fs = require("fs"),
|
5 | es = require("event-stream"),
|
6 | should = require("should"),
|
7 | sinon = require("sinon"),
|
8 | coveralls = require("coveralls");
|
9 |
|
10 | require("mocha");
|
11 |
|
12 | delete require.cache[require.resolve("../")];
|
13 |
|
14 | var gutil = require("gulp-util"),
|
15 | gulpCoveralls = require("../");
|
16 |
|
17 | describe("gulp-coveralls", function () {
|
18 |
|
19 | var convertedFile = 'CONVERTED';
|
20 |
|
21 | var expectedFile = new gutil.File({
|
22 | path: "test/expected/lcov.info",
|
23 | cwd: "test/",
|
24 | base: "test/expected",
|
25 | contents: fs.readFileSync("test/expected/lcov.info")
|
26 | });
|
27 |
|
28 | afterEach(function() {
|
29 | coveralls.getBaseOptions.restore && coveralls.getBaseOptions.restore();
|
30 | coveralls.convertLcovToCoveralls.restore && coveralls.convertLcovToCoveralls.restore();
|
31 | coveralls.sendToCoveralls.restore && coveralls.sendToCoveralls.restore();
|
32 | });
|
33 |
|
34 | describe("when successful", function() {
|
35 |
|
36 | beforeEach(function() {
|
37 |
|
38 | sinon.stub(coveralls, "getBaseOptions").callsArgWith(0, null, {});
|
39 | sinon.stub(coveralls, "convertLcovToCoveralls").callsArgWith(2, null, convertedFile);
|
40 | sinon.stub(coveralls, "sendToCoveralls").callsArgWith(1, null, {}, '');
|
41 | });
|
42 |
|
43 | it("should pass the file through via buffer", function (done) {
|
44 |
|
45 | var srcFile = new gutil.File({
|
46 | path: "test/fixtures/lcov.info",
|
47 | cwd: "test/",
|
48 | base: "test/fixtures",
|
49 | contents: fs.readFileSync("test/fixtures/lcov.info")
|
50 | });
|
51 |
|
52 | var stream = gulpCoveralls();
|
53 |
|
54 | stream.on("error", function(err) {
|
55 | should.exist(err);
|
56 | done(err);
|
57 | });
|
58 |
|
59 | stream.on("data", function (newFile) {
|
60 |
|
61 | should.exist(newFile);
|
62 | should.exist(newFile.contents);
|
63 |
|
64 | String(newFile.contents).should.equal(String(expectedFile.contents));
|
65 | done();
|
66 | });
|
67 |
|
68 | stream.write(srcFile);
|
69 | stream.end();
|
70 | });
|
71 |
|
72 | it("should send the file contents to Coveralls", function (done) {
|
73 |
|
74 | var srcFile = new gutil.File({
|
75 | path: "test/fixtures/lcov.info",
|
76 | cwd: "test/",
|
77 | base: "test/fixtures",
|
78 | contents: fs.readFileSync("test/fixtures/lcov.info")
|
79 | });
|
80 |
|
81 | var stream = gulpCoveralls();
|
82 | stream.write(srcFile);
|
83 | stream.end();
|
84 |
|
85 | stream.on("data", function () {
|
86 |
|
87 | sinon.assert.calledOnce(coveralls.getBaseOptions);
|
88 | sinon.assert.calledWith(coveralls.getBaseOptions, sinon.match.func);
|
89 |
|
90 | sinon.assert.calledOnce(coveralls.convertLcovToCoveralls);
|
91 | sinon.assert.calledWith(coveralls.convertLcovToCoveralls, srcFile.contents.toString(), { filepath: '.' }, sinon.match.func);
|
92 |
|
93 | sinon.assert.calledOnce(coveralls.sendToCoveralls);
|
94 | sinon.assert.calledWith(coveralls.sendToCoveralls, convertedFile, sinon.match.func);
|
95 | done();
|
96 | });
|
97 | });
|
98 |
|
99 | });
|
100 |
|
101 | describe("when Coveralls responds with an error", function() {
|
102 |
|
103 | beforeEach(function() {
|
104 |
|
105 | sinon.stub(coveralls, "getBaseOptions").callsArgWith(0, null, {});
|
106 | sinon.stub(coveralls, "convertLcovToCoveralls").callsArgWith(2, null, convertedFile);
|
107 | sinon.stub(coveralls, "sendToCoveralls").callsArgWith(1, null, { statusCode: 404 }, 'Blah blah blah');
|
108 | });
|
109 |
|
110 | it("should emit an error", function (done) {
|
111 |
|
112 | var srcFile = new gutil.File({
|
113 | path: "test/fixtures/lcov.info",
|
114 | cwd: "test/",
|
115 | base: "test/fixtures",
|
116 | contents: fs.readFileSync("test/fixtures/lcov.info")
|
117 | });
|
118 |
|
119 | var stream = gulpCoveralls();
|
120 |
|
121 | stream.on("error", function (error) {
|
122 | should.exist(error);
|
123 | done();
|
124 | });
|
125 |
|
126 | stream.write(srcFile);
|
127 | stream.end();
|
128 |
|
129 | });
|
130 |
|
131 | });
|
132 |
|
133 | describe("nulls", function() {
|
134 |
|
135 | beforeEach(function() {
|
136 |
|
137 | sinon.stub(coveralls, "getBaseOptions");
|
138 | sinon.stub(coveralls, "convertLcovToCoveralls");
|
139 | sinon.stub(coveralls, "sendToCoveralls");
|
140 | });
|
141 |
|
142 | it("should pass the file through when null", function(done) {
|
143 | var nullFile = new gutil.File();
|
144 |
|
145 | var stream = gulpCoveralls();
|
146 |
|
147 | stream.on("data", function (newFile) {
|
148 | should.exist(newFile);
|
149 | sinon.assert.notCalled(coveralls.getBaseOptions);
|
150 | sinon.assert.notCalled(coveralls.convertLcovToCoveralls);
|
151 | sinon.assert.notCalled(coveralls.sendToCoveralls);
|
152 | done();
|
153 | });
|
154 |
|
155 | stream.write(nullFile);
|
156 | stream.end();
|
157 | });
|
158 |
|
159 | });
|
160 |
|
161 | describe("streams", function() {
|
162 |
|
163 | it("should error on stream", function (done) {
|
164 |
|
165 | var srcFile = new gutil.File({
|
166 | path: "test/fixtures/lcov.info",
|
167 | cwd: "test/",
|
168 | base: "test/fixtures",
|
169 | contents: fs.createReadStream("test/fixtures/lcov.info")
|
170 | });
|
171 |
|
172 | var stream = gulpCoveralls();
|
173 |
|
174 | stream.on("error", function(err) {
|
175 | should.exist(err);
|
176 | done();
|
177 | });
|
178 |
|
179 | stream.on("data", function (newFile) {
|
180 | newFile.contents.pipe(es.wait(function(err, data) {
|
181 | done(err);
|
182 | }));
|
183 | });
|
184 |
|
185 | stream.write(srcFile);
|
186 | stream.end();
|
187 | });
|
188 |
|
189 | });
|
190 |
|
191 | });
|