|
1 | 1 | /* eslint-env mocha */ |
2 | 2 |
|
3 | | -const fs = require('fs'); |
4 | | -const os = require('os'); |
5 | | -const path = require('path'); |
6 | 3 | const chai = require('chai'); |
7 | | -const execa = require('execa'); |
8 | | -const rimraf = require('rimraf'); |
9 | 4 |
|
10 | | -chai.should(); |
| 5 | +const { |
| 6 | + runCli, |
| 7 | + getExpected, |
| 8 | + getActual, |
| 9 | + getTempLcovFilePath, |
| 10 | + cleanFileDirectory, |
| 11 | +} = require('./helpers'); |
11 | 12 |
|
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(); |
57 | 14 |
|
58 | 15 | describe('lcovResultMerger CLI', function () { |
59 | 16 | it('should combine the given records into one', async function () { |
60 | 17 | const actual = await runCli(); |
61 | | - actual.should.equal(getExpected('basic')); |
| 18 | + const expect = await getExpected('basic'); |
| 19 | + |
| 20 | + actual.should.equal(expect); |
62 | 21 | }); |
63 | 22 |
|
64 | 23 | 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); |
70 | 31 | }); |
71 | 32 |
|
72 | 33 | 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); |
78 | 40 | }); |
79 | 41 |
|
80 | 42 | 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); |
86 | 50 | }); |
87 | 51 |
|
88 | 52 | 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]); |
92 | 56 |
|
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); |
95 | 63 | }); |
96 | 64 |
|
97 | 65 | 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'); |
101 | 77 |
|
102 | | - actual.trim().should.equal(getExpected('prepended')); |
103 | | - cleanTmpDirectory(tmpFile); |
| 78 | + await cleanFileDirectory(outfile); |
| 79 | + |
| 80 | + actual.should.equal(expect); |
104 | 81 | }); |
105 | 82 |
|
106 | 83 | 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); |
116 | 96 | }); |
117 | 97 | }); |
0 commit comments