Skip to content

Commit 3c713f1

Browse files
committed
feat!: use host system temp directory for transient lcov.info file
1 parent 5aa886d commit 3c713f1

File tree

4 files changed

+60
-6
lines changed

4 files changed

+60
-6
lines changed

bin/lcov-result-merger.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ const args = yargs(hideBin(process.argv)).command(
3838
'If using --prepend-source-files, this is needed to describe the relative path from the lcov ' +
3939
'directory to the project root.',
4040
},
41+
'legacy-temp-file': {
42+
type: 'boolean',
43+
default: false,
44+
description:
45+
'Prior to version 5, a lcov.info file containing the merged output was created in the ' +
46+
"current working directory. The operating system's temporary directory is now used by default, " +
47+
'but if you relied on prior behavior then this flag will recreate it.',
48+
},
4149
ignore: {
4250
type: 'array',
4351
default: [],

index.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
const through = require('through2');
1010
const fs = require('fs');
1111
const path = require('path');
12+
const os = require('os');
1213
const Configuration = require('./lib/Configuration');
1314
const FullReport = require('./lib/FullReport');
1415

@@ -101,12 +102,18 @@ module.exports = function mergeCoverageReportFiles(options) {
101102
},
102103

103104
function flush() {
104-
fs.writeFileSync('lcov.info', Buffer.from(report.toString()), {
105+
const tmpPath = config.legacyTempFile
106+
? ''
107+
: fs.mkdtempSync(path.join(os.tmpdir(), 'lcov-result-merger-'));
108+
109+
const tmpFile = path.join(tmpPath, 'lcov.info');
110+
111+
fs.writeFileSync(tmpFile, Buffer.from(report.toString()), {
105112
encoding: 'utf-8',
106113
flag: 'w+',
107114
});
108115

109-
this.push('lcov.info');
116+
this.push(tmpFile);
110117
this.emit('end');
111118
}
112119
);

lib/Configuration.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
* @property {boolean} [prepend-source-files=false]
88
* @property {string} [prependPathFix]
99
* @property {string} [prepend-path-fix]
10+
* @property {boolean} [legacyTempFile=false]
11+
* @property {boolean} [legacy-temp-file=false]
1012
* @property {string[]} [ignore]
1113
*/
1214

@@ -58,5 +60,19 @@ module.exports = class Configuration {
5860
: typeof partial['prepend-path-fix'] === 'string'
5961
? partial['prepend-path-fix']
6062
: '..';
63+
64+
/**
65+
* Prior to version 5, a lcov.info file containing the merged output was created in the
66+
* current working directory. The operating system's temporary directory is now used by default,
67+
* but if you relied on prior behavior then this flag will recreate it.
68+
*
69+
* @type {boolean}
70+
*/
71+
this.legacyTempFile =
72+
typeof partial.legacyTempFile === 'boolean'
73+
? partial.legacyTempFile
74+
: typeof partial['legacy-temp-file'] === 'boolean'
75+
? partial['legacy-temp-file']
76+
: false;
6177
}
6278
};

test/lcov-result-merger-spec.js

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,18 @@ chai.should();
1010
describe('lcovResultMerger', function () {
1111
it('should combine the given records into one', async function () {
1212
const expected = fs.readFileSync('./test/expected/basic/lcov.info', 'utf8');
13+
let tmpFilePath = '';
14+
1315
await new Promise((res) => {
1416
fg.stream('./test/fixtures/basic/*/lcov.info')
1517
.pipe(lcovResultMerger())
18+
.on('data', (tmpFile) => {
19+
tmpFilePath = tmpFile;
20+
})
1621
.on('end', res);
1722
});
18-
const actual = fs.readFileSync('lcov.info', 'utf8');
23+
24+
const actual = fs.readFileSync(tmpFilePath, 'utf8');
1925
return actual.should.equal(expected);
2026
});
2127

@@ -38,12 +44,17 @@ describe('lcovResultMerger', function () {
3844
'./test/expected/windows/lcov.info',
3945
'utf8'
4046
);
47+
let tmpFilePath = '';
48+
4149
await new Promise((res) => {
4250
fg.stream('./test/fixtures/windows/lcov.info')
4351
.pipe(lcovResultMerger())
52+
.on('data', (tmpFile) => {
53+
tmpFilePath = tmpFile;
54+
})
4455
.on('end', res);
4556
});
46-
const actual = fs.readFileSync('lcov.info', 'utf8');
57+
const actual = fs.readFileSync(tmpFilePath, 'utf8');
4758
return actual.should.equal(expected);
4859
});
4960

@@ -52,6 +63,9 @@ describe('lcovResultMerger', function () {
5263
'./test/expected/prepended/lcov.info',
5364
'utf8'
5465
);
66+
67+
let tmpFilePath = '';
68+
5569
await new Promise((res) => {
5670
fg.stream('./test/fixtures/basic/*/lcov.info')
5771
.pipe(
@@ -60,9 +74,12 @@ describe('lcovResultMerger', function () {
6074
'prepend-path-fix': '',
6175
})
6276
)
77+
.on('data', (tmpFile) => {
78+
tmpFilePath = tmpFile;
79+
})
6380
.on('end', res);
6481
});
65-
const actual = fs.readFileSync('lcov.info', 'utf8');
82+
const actual = fs.readFileSync(tmpFilePath, 'utf8');
6683
return actual.should.equal(expected);
6784
});
6885

@@ -71,12 +88,18 @@ describe('lcovResultMerger', function () {
7188
'./test/expected/prepended-path-fix/lcov.info',
7289
'utf8'
7390
);
91+
92+
let tmpFilePath = '';
93+
7494
await new Promise((res) => {
7595
fg.stream('./test/fixtures/coverage-subfolder/*/coverage/lcov.info')
7696
.pipe(lcovResultMerger({ 'prepend-source-files': true }))
97+
.on('data', (tmpFile) => {
98+
tmpFilePath = tmpFile;
99+
})
77100
.on('end', res);
78101
});
79-
const actual = fs.readFileSync('lcov.info', 'utf8');
102+
const actual = fs.readFileSync(tmpFilePath, 'utf8');
80103
return actual.should.equal(expected);
81104
});
82105
});

0 commit comments

Comments
 (0)