From b1c0949f99631f925b768d2bced4983b9752f290 Mon Sep 17 00:00:00 2001 From: Shinnosuke Watanabe Date: Sat, 4 May 2019 07:32:45 +0900 Subject: [PATCH 1/3] fix: remove the unmaintained mkdirp dependency using fs.promises feature detection --- bin/c8.js | 6 ++++-- package-lock.json | 1 + package.json | 1 - test/fixtures/disable-fs-promises.js | 4 ++++ test/legacy-node.js | 20 ++++++++++++++++++++ 5 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 test/fixtures/disable-fs-promises.js create mode 100644 test/legacy-node.js diff --git a/bin/c8.js b/bin/c8.js index 21db5452..d1454f44 100755 --- a/bin/c8.js +++ b/bin/c8.js @@ -3,7 +3,7 @@ const foreground = require('foreground-child') const { outputReport } = require('../lib/commands/report') -const mkdirp = require('mkdirp') +const { promises } = require('fs') const { promisify } = require('util') const rimraf = require('rimraf') const { @@ -25,7 +25,9 @@ async function run () { await promisify(rimraf)(argv.tempDirectory) } // allow c8 to run on Node 8 (coverage just won't work). - await promisify(mkdirp)(argv.tempDirectory) + if (promises) { + await promises.mkdir(argv.tempDirectory, { recursive: true }) + } process.env.NODE_V8_COVERAGE = argv.tempDirectory foreground(hideInstrumenterArgs(argv), async (done) => { diff --git a/package-lock.json b/package-lock.json index b0cb175b..fac64c8c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2505,6 +2505,7 @@ "version": "0.5.1", "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.1.tgz", "integrity": "sha1-MAV0OOrGz3+MR2fzhkjWaX11yQM=", + "dev": true, "requires": { "minimist": "0.0.8" } diff --git a/package.json b/package.json index 581edac6..e016a06f 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,6 @@ "istanbul-lib-coverage": "^2.0.1", "istanbul-lib-report": "^2.0.1", "istanbul-reports": "^2.0.0", - "mkdirp": "^0.5.1", "rimraf": "^2.6.2", "test-exclude": "^5.0.0", "v8-to-istanbul": "^3.1.1", diff --git a/test/fixtures/disable-fs-promises.js b/test/fixtures/disable-fs-promises.js new file mode 100644 index 00000000..0bbb0bd6 --- /dev/null +++ b/test/fixtures/disable-fs-promises.js @@ -0,0 +1,4 @@ +const fs = require('fs'); + +Object.defineProperty(fs, 'promises', {value: undefined}); +require('../../bin/c8.js'); diff --git a/test/legacy-node.js b/test/legacy-node.js new file mode 100644 index 00000000..08c64999 --- /dev/null +++ b/test/legacy-node.js @@ -0,0 +1,20 @@ +/* global describe, it */ + +const { execFile } = require('child_process') +const { mkdir } = require('fs').promises +const { join } = require('path') +const { promisify } = require('util') +const c8Path = require.resolve('./fixtures/disable-fs-promises') +const nodePath = process.execPath + +describe('c8 on Node.js < 10', () => { + it('doesn\'t crash', async () => { + await mkdir(join(__dirname, '..', 'tmp', 'legacy-nodejs')) + await promisify(execFile)(nodePath, [ + c8Path, + '--temp-directory=tmp/legacy-nodejs', + 'node', + '--version' + ]) + }) +}) From f9bdcb44ae9c695ec59995f98db343b5295ad77c Mon Sep 17 00:00:00 2001 From: Shinnosuke Watanabe Date: Sun, 5 May 2019 06:26:54 +0900 Subject: [PATCH 2/3] do not try creating reports on legacy Node.js --- bin/c8.js | 11 +++++++---- test/legacy-node.js | 16 +++++++++------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/bin/c8.js b/bin/c8.js index d1454f44..abf0d69f 100755 --- a/bin/c8.js +++ b/bin/c8.js @@ -21,14 +21,17 @@ async function run () { ].indexOf(argv._[0]) !== -1) { argv = buildYargs(true).parse(process.argv.slice(2)) } else { + // allow c8 to run on Node 8 (coverage just won't work). + if (!promises) { + foreground(hideInstrumenterArgs(argv)) + return + } + if (argv.clean) { await promisify(rimraf)(argv.tempDirectory) } - // allow c8 to run on Node 8 (coverage just won't work). - if (promises) { - await promises.mkdir(argv.tempDirectory, { recursive: true }) - } + await promises.mkdir(argv.tempDirectory, { recursive: true }) process.env.NODE_V8_COVERAGE = argv.tempDirectory foreground(hideInstrumenterArgs(argv), async (done) => { try { diff --git a/test/legacy-node.js b/test/legacy-node.js index 08c64999..c1daeca5 100644 --- a/test/legacy-node.js +++ b/test/legacy-node.js @@ -1,20 +1,22 @@ /* global describe, it */ const { execFile } = require('child_process') -const { mkdir } = require('fs').promises +const { existsSync } = require('fs') const { join } = require('path') const { promisify } = require('util') const c8Path = require.resolve('./fixtures/disable-fs-promises') -const nodePath = process.execPath describe('c8 on Node.js < 10', () => { - it('doesn\'t crash', async () => { - await mkdir(join(__dirname, '..', 'tmp', 'legacy-nodejs')) - await promisify(execFile)(nodePath, [ + it('skip coverage', async () => { + const tmp = join(__dirname, '..', 'tmp', 'legacy-nodejs') + + await promisify(execFile)(process.execPath, [ c8Path, - '--temp-directory=tmp/legacy-nodejs', - 'node', + `--temp-directory=${__filename}`, + process.execPath, '--version' ]) + + existsSync(tmp).should.equal(false) }) }) From 938a46391105b4f56bc1c07144dba362da74b4b1 Mon Sep 17 00:00:00 2001 From: Shinnosuke Watanabe Date: Sun, 5 May 2019 10:42:25 +0900 Subject: [PATCH 3/3] update the comment ref. https://github.com/bcoe/c8/pull/91#discussion_r281000192 --- bin/c8.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bin/c8.js b/bin/c8.js index abf0d69f..a00da7fe 100755 --- a/bin/c8.js +++ b/bin/c8.js @@ -21,7 +21,8 @@ async function run () { ].indexOf(argv._[0]) !== -1) { argv = buildYargs(true).parse(process.argv.slice(2)) } else { - // allow c8 to run on Node 8 (coverage just won't work). + // fs.promises was not added until Node.js v10.0.0, if it doesn't + // exist, assume we're Node.js v8.x and skip coverage. if (!promises) { foreground(hideInstrumenterArgs(argv)) return