Skip to content

Commit 926d42c

Browse files
committed
test: update to expect promises instead of streams
1 parent 0cbfa42 commit 926d42c

File tree

4 files changed

+148
-106
lines changed

4 files changed

+148
-106
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
"istanbul": "^0.4.5",
5454
"mocha": "^10.0.0",
5555
"prettier": "2.8.1",
56-
"rimraf": "^2.6.3",
56+
"rimraf": "^5.0.1",
5757
"semantic-release": "^19.0.3"
5858
},
5959
"engines": {

test/helpers/index.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
const { dirname, join } = require('node:path');
2+
const { mkdtemp, readFile } = require('node:fs/promises');
3+
const { tmpdir } = require('node:os');
4+
const execa = require('execa');
5+
const { rimraf } = require('rimraf');
6+
7+
/**
8+
* Run the lcov-result-merger command in a child process. If no glob pattern
9+
* is provided, the "basic" test fixtures are targeted.
10+
*
11+
* @param {string[]} [commands]
12+
* @param {string} [pattern]
13+
*
14+
* @returns {Promise<string>}
15+
*/
16+
async function runCli(commands, pattern) {
17+
const executable = join(__dirname, '../../bin/lcov-result-merger.js');
18+
const sourceGlob =
19+
pattern || `"${join(__dirname, '..', 'fixtures/basic/*/lcov.info')}"`;
20+
const args = [executable, sourceGlob, ...(commands || [])];
21+
22+
return (await execa('node', args)).stdout.trim();
23+
}
24+
25+
/**
26+
* Read the contents from the relevant "expected" fixture file.
27+
*
28+
* @param {'basic'|'prepended'|'prepended-path-fix'} type
29+
*
30+
* @returns {Promise<string>}
31+
*/
32+
async function getExpected(type) {
33+
const filePath = join(__dirname, '..', 'expected', type, 'lcov.info');
34+
return (await readFile(filePath, 'utf-8')).toString().trim();
35+
}
36+
37+
/**
38+
* Returns the contents of the provided file as UTF-8 encoded text.
39+
*
40+
* @param {string} filePath
41+
*
42+
* @return {Promise<string>}
43+
*/
44+
async function getActual(filePath) {
45+
return (await readFile(filePath, 'utf-8')).toString().trim();
46+
}
47+
48+
/**
49+
* Create a temporary directory to output lcov content into, and
50+
* return a full string path to the lcov file that will be written.
51+
*
52+
* @returns {Promise<string>}
53+
*/
54+
async function getTempLcovFilePath() {
55+
const filePath = await mkdtemp(join(tmpdir(), 'lcov-result-merger-'));
56+
return join(filePath, 'lcov.info');
57+
}
58+
59+
/**
60+
* Empties the directory that the given file is in.
61+
*
62+
* @param {string} filePath
63+
*
64+
* @returns {Promise<boolean>}
65+
*/
66+
async function cleanFileDirectory(filePath) {
67+
return rimraf(dirname(filePath));
68+
}
69+
70+
module.exports = {
71+
runCli,
72+
getExpected,
73+
getActual,
74+
getTempLcovFilePath,
75+
cleanFileDirectory,
76+
};
Lines changed: 66 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,117 +1,97 @@
11
/* eslint-env mocha */
22

3-
const fs = require('fs');
4-
const os = require('os');
5-
const path = require('path');
63
const chai = require('chai');
7-
const execa = require('execa');
8-
const rimraf = require('rimraf');
94

10-
chai.should();
5+
const {
6+
runCli,
7+
getExpected,
8+
getActual,
9+
getTempLcovFilePath,
10+
cleanFileDirectory,
11+
} = require('./helpers');
1112

12-
/**
13-
* Run the lcov-result-merger command in a child process.
14-
*
15-
* @param {string[]} [commands]
16-
* @returns {Promise<string>}
17-
*/
18-
async function runCli(commands, sourceFiles) {
19-
const args = [
20-
'./bin/lcov-result-merger.js',
21-
sourceFiles || '"./test/fixtures/basic/*/lcov.info"',
22-
].concat(commands || []);
23-
24-
const { stdout } = await execa('node', args);
25-
return stdout.trim();
26-
}
27-
28-
/**
29-
* Read the contents from the relevant "expected" fixture file.
30-
*
31-
* @param {'basic'|'prepended'|'prepended-path-fix'} type
32-
* @returns {string}
33-
*/
34-
function getExpected(type) {
35-
return fs.readFileSync(`./test/expected/${type}/lcov.info`, 'utf-8').trim();
36-
}
37-
38-
/**
39-
* Create a temporary directory to output lcov content into, and
40-
* return a full string path to the lcov file that will be written.
41-
*
42-
* @returns {string}
43-
*/
44-
function makeTmpFilePath() {
45-
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'lcov-result-merger-'));
46-
return path.join(tmpDir, 'lcov.info');
47-
}
48-
49-
/**
50-
* Clean up the temporary directory.
51-
*
52-
* @param {string} tmpFilePath
53-
*/
54-
function cleanTmpDirectory(tmpFilePath) {
55-
rimraf.sync(path.dirname(tmpFilePath));
56-
}
13+
chai.should();
5714

5815
describe('lcovResultMerger CLI', function () {
5916
it('should combine the given records into one', async function () {
6017
const actual = await runCli();
61-
actual.should.equal(getExpected('basic'));
18+
const expect = await getExpected('basic');
19+
20+
actual.should.equal(expect);
6221
});
6322

6423
it('should ignore paths with the --ignore option', async function () {
65-
const actual = await runCli(
66-
['--ignore="**/extra.info"'],
67-
'"./test/fixtures/ignore/*/*.info"'
68-
);
69-
actual.should.equal(getExpected('basic'));
24+
const options = ['--ignore="**/extra.info"'];
25+
const pattern = '"./test/fixtures/ignore/*/*.info"';
26+
27+
const actual = await runCli(options, pattern);
28+
const expect = await getExpected('basic');
29+
30+
actual.should.equal(expect);
7031
});
7132

7233
it('should optionally prepend source file lines', async function () {
73-
const actual = await runCli([
74-
'--prepend-source-files',
75-
'--prepend-path-fix=""',
76-
]);
77-
actual.should.equal(getExpected('prepended'));
34+
const options = ['--prepend-source-files', '--prepend-path-fix=""'];
35+
36+
const actual = await runCli(options);
37+
const expect = await getExpected('prepended');
38+
39+
actual.should.equal(expect);
7840
});
7941

8042
it('should optionally prepend source file lines with corrected pathing', async function () {
81-
const actual = await runCli(
82-
['--prepend-source-files'],
83-
'"./test/fixtures/coverage-subfolder/*/coverage/lcov.info"'
84-
);
85-
actual.should.equal(getExpected('prepended-path-fix'));
43+
const options = ['--prepend-source-files'];
44+
const pattern = '"./test/fixtures/coverage-subfolder/*/coverage/lcov.info"';
45+
46+
const actual = await runCli(options, pattern);
47+
const expect = await getExpected('prepended-path-fix');
48+
49+
actual.should.equal(expect);
8650
});
8751

8852
it('should combine to given records into one output file', async function () {
89-
const tmpFile = makeTmpFilePath();
90-
await runCli([tmpFile]);
91-
const actual = fs.readFileSync(tmpFile, 'utf-8');
53+
const outfile = await getTempLcovFilePath();
54+
55+
await runCli([outfile]);
9256

93-
actual.trim().should.equal(getExpected('basic'));
94-
cleanTmpDirectory(tmpFile);
57+
const actual = await getActual(outfile);
58+
const expect = await getExpected('basic');
59+
60+
await cleanFileDirectory(outfile);
61+
62+
actual.should.equal(expect);
9563
});
9664

9765
it('should optionally prepend source file lines into one output file', async function () {
98-
const tmpFile = makeTmpFilePath();
99-
await runCli([tmpFile, '--prepend-source-files', '--prepend-path-fix=""']);
100-
const actual = fs.readFileSync(tmpFile, 'utf-8');
66+
const outfile = await getTempLcovFilePath();
67+
const options = [
68+
outfile,
69+
'--prepend-source-files',
70+
'--prepend-path-fix=""',
71+
];
72+
73+
await runCli(options);
74+
75+
const actual = await getActual(outfile);
76+
const expect = await getExpected('prepended');
10177

102-
actual.trim().should.equal(getExpected('prepended'));
103-
cleanTmpDirectory(tmpFile);
78+
await cleanFileDirectory(outfile);
79+
80+
actual.should.equal(expect);
10481
});
10582

10683
it('should optionally prepend source file lines into one output file with corrected pathing', async function () {
107-
const tmpFile = makeTmpFilePath();
108-
await runCli(
109-
[tmpFile, '--prepend-source-files'],
110-
'"./test/fixtures/coverage-subfolder/*/coverage/lcov.info"'
111-
);
112-
const actual = fs.readFileSync(tmpFile, 'utf-8');
113-
114-
actual.trim().should.equal(getExpected('prepended-path-fix'));
115-
cleanTmpDirectory(tmpFile);
84+
const outfile = await getTempLcovFilePath();
85+
const options = [outfile, '--prepend-source-files'];
86+
const pattern = '"./test/fixtures/coverage-subfolder/*/coverage/lcov.info"';
87+
88+
await runCli(options, pattern);
89+
90+
const actual = await getActual(outfile);
91+
const expect = await getExpected('prepended-path-fix');
92+
93+
await cleanFileDirectory(outfile);
94+
95+
actual.should.equal(expect);
11696
});
11797
});

test/lcov-result-merger-spec.js

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const fg = require('fast-glob');
44
const fs = require('fs');
55
const chai = require('chai');
6-
const lcovResultMerger = require('../index.js');
6+
const { mergeCoverageReportFilesStream } = require('../index.js');
77

88
chai.should();
99

@@ -14,7 +14,7 @@ describe('lcovResultMerger', function () {
1414

1515
await new Promise((res) => {
1616
fg.stream('./test/fixtures/basic/*/lcov.info')
17-
.pipe(lcovResultMerger())
17+
.pipe(mergeCoverageReportFilesStream())
1818
.on('data', (tmpFile) => {
1919
tmpFilePath = tmpFile;
2020
})
@@ -25,20 +25,6 @@ describe('lcovResultMerger', function () {
2525
return actual.should.equal(expected);
2626
});
2727

28-
it('should ignore null files', function (callback) {
29-
const stream = lcovResultMerger();
30-
stream.on('data', function (file) {
31-
const fileContentStr = fs.readFileSync(file, 'utf8');
32-
fileContentStr.should.equal('');
33-
callback();
34-
});
35-
stream.write({
36-
cwd: './',
37-
path: '/meow.html',
38-
});
39-
stream._flush();
40-
});
41-
4228
it('should handle a record with : in the name', async function () {
4329
const expected = fs.readFileSync(
4430
'./test/expected/windows/lcov.info',
@@ -48,7 +34,7 @@ describe('lcovResultMerger', function () {
4834

4935
await new Promise((res) => {
5036
fg.stream('./test/fixtures/windows/lcov.info')
51-
.pipe(lcovResultMerger())
37+
.pipe(mergeCoverageReportFilesStream())
5238
.on('data', (tmpFile) => {
5339
tmpFilePath = tmpFile;
5440
})
@@ -69,7 +55,7 @@ describe('lcovResultMerger', function () {
6955
await new Promise((res) => {
7056
fg.stream('./test/fixtures/basic/*/lcov.info')
7157
.pipe(
72-
lcovResultMerger({
58+
mergeCoverageReportFilesStream({
7359
'prepend-source-files': true,
7460
'prepend-path-fix': '',
7561
})
@@ -93,7 +79,7 @@ describe('lcovResultMerger', function () {
9379

9480
await new Promise((res) => {
9581
fg.stream('./test/fixtures/coverage-subfolder/*/coverage/lcov.info')
96-
.pipe(lcovResultMerger({ 'prepend-source-files': true }))
82+
.pipe(mergeCoverageReportFilesStream({ 'prepend-source-files': true }))
9783
.on('data', (tmpFile) => {
9884
tmpFilePath = tmpFile;
9985
})

0 commit comments

Comments
 (0)