Skip to content

Commit fafbbd7

Browse files
committed
test: add integration test
1 parent a3fdda0 commit fafbbd7

File tree

7 files changed

+136
-132
lines changed

7 files changed

+136
-132
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,13 @@
4646
"gulp": "4.0.2",
4747
"gulp-babel": "8.0.0",
4848
"gulp-bump": "3.1.3",
49-
"gulp-conventional-changelog": "^2.0.29",
49+
"gulp-conventional-changelog": "2.0.29",
5050
"gulp-eslint": "6.0.0",
5151
"gulp-git": "2.10.1",
5252
"gulp-jasmine": "4.0.0",
5353
"jasmine-core": "3.5.0",
5454
"rimraf": "3.0.2",
55+
"tmp": "0.2.1",
5556
"vinyl": "2.2.0"
5657
}
5758
}

src/index.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const _ = require('lodash');
3434
const moment = require('moment');
3535
const commenting = require('commenting');
3636
const through = require('through2');
37-
const read = require('./read');
37+
const Q = require('q');
3838

3939
module.exports = function gulpHeaderComment(options = {}) {
4040
const separator = _.isObject(options) && _.isString(options.separator) ? options.separator : '\n';
@@ -265,3 +265,25 @@ function maySkipFirstLine(type) {
265265
function shouldSkipFirstLine(type, line) {
266266
return prologCheckers[type](line);
267267
}
268+
269+
/**
270+
* Read file specified by given options.
271+
*
272+
* @param {Object} options Read options.
273+
* @return {Promise<string>} A promise resolved with file content.
274+
*/
275+
function read(options) {
276+
if (_.isString(options)) {
277+
return Q.when(options);
278+
}
279+
280+
const file = options.file;
281+
const encoding = options.encoding || 'utf-8';
282+
const deferred = Q.defer();
283+
284+
fs.readFile(file, {encoding}, (err, data) => {
285+
return err ? deferred.reject(err) : deferred.resolve(data);
286+
});
287+
288+
return deferred.promise;
289+
}

test/index.spec.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ const Vinyl = require('vinyl');
3030
const Stream = require('stream');
3131
const moment = require('moment');
3232
const gulpHeaderComment = require('../src/index');
33-
const EOL = '\n';
33+
const joinLines = require('./utils/join-lines');
34+
const EOL = require('./utils/eol');
3435

3536
describe('gulp-header-comment', () => {
3637
let cwd;
@@ -646,13 +647,4 @@ describe('gulp-header-comment', () => {
646647
stream.write(vinyl);
647648
stream.end();
648649
});
649-
650-
/**
651-
* Join given strings with the EOL character.
652-
* @param {Array<string>} lines Given lines to join.
653-
* @return {string} Joined lines.
654-
*/
655-
function joinLines(lines) {
656-
return lines.join(EOL);
657-
}
658650
});

test/it.spec.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/**
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2017-2019 Mickael Jeanroy
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
'use strict';
26+
27+
const path = require('path');
28+
const fs = require('fs');
29+
const gulp = require('gulp');
30+
const tmp = require('tmp');
31+
const headerComment = require('../src/index');
32+
const joinLines = require('./utils/join-lines');
33+
34+
describe('[IT] gulp-header-comment', () => {
35+
let tmpDir;
36+
37+
beforeEach(() => {
38+
tmpDir = tmp.dirSync({
39+
unsafeCleanup: true,
40+
});
41+
});
42+
43+
afterEach(() => {
44+
tmpDir.removeCallback();
45+
});
46+
47+
it('should prepend header', (done) => {
48+
const fName = 'test.txt';
49+
const src = path.join(__dirname, 'fixtures', fName);
50+
const dest = path.join(tmpDir.name);
51+
52+
gulp.src(src)
53+
.pipe(headerComment('License MIT'))
54+
.pipe(gulp.dest(dest))
55+
.on('error', (err) => done.fail(err))
56+
.on('end', () => {
57+
fs.readFile(path.join(dest, fName), 'utf8', (err, data) => {
58+
if (err) {
59+
done.fail(err);
60+
return;
61+
}
62+
63+
expect(data).toEqual(joinLines([
64+
'/**',
65+
' * License MIT',
66+
' */',
67+
'',
68+
'Hello World',
69+
'',
70+
]));
71+
72+
done();
73+
});
74+
});
75+
});
76+
});

test/read.spec.js

Lines changed: 0 additions & 101 deletions
This file was deleted.

test/utils/eol.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2017-2019 Mickael Jeanroy
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
module.exports = '\n';

src/read.js renamed to test/utils/join-lines.js

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,13 @@
2222
* THE SOFTWARE.
2323
*/
2424

25-
'use strict';
25+
const EOL = require('./eol');
2626

27-
const fs = require('fs');
28-
const _ = require('lodash');
29-
const Q = require('q');
30-
31-
module.exports = function read(options) {
32-
if (_.isString(options)) {
33-
return Q.when(options);
34-
}
35-
36-
const file = options.file;
37-
const encoding = options.encoding || 'utf-8';
38-
const deferred = Q.defer();
39-
40-
fs.readFile(file, {encoding}, (err, data) => {
41-
return err ? deferred.reject(err) : deferred.resolve(data);
42-
});
43-
44-
return deferred.promise;
27+
/**
28+
* Join given strings with the EOL character.
29+
* @param {Array<string>} lines Given lines to join.
30+
* @return {string} Joined lines.
31+
*/
32+
module.exports = function joinLines(lines) {
33+
return lines.join(EOL);
4534
};

0 commit comments

Comments
 (0)