Skip to content

Commit d6db551

Browse files
AndrewFinlaycoreyfarrell
authored andcommitted
feat: add delete option to instrument command (#1005)
1 parent 3b203c7 commit d6db551

File tree

2 files changed

+104
-3
lines changed

2 files changed

+104
-3
lines changed

lib/commands/instrument.js

+17
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
const NYC = require('../../index.js')
2+
const path = require('path')
3+
const rimraf = require('rimraf')
24

35
exports.command = 'instrument <input> [output]'
46

@@ -51,6 +53,11 @@ exports.builder = function (yargs) {
5153
type: 'boolean',
5254
description: 'tell the instrumenter to treat files as ES Modules'
5355
})
56+
.option('delete', {
57+
describe: 'should the output folder be deleted before instrumenting files?',
58+
default: false,
59+
type: 'boolean'
60+
})
5461
.example('$0 instrument ./lib ./output', 'instrument all .js files in ./lib with coverage and output in ./output')
5562
}
5663

@@ -72,6 +79,16 @@ exports.handler = function (argv) {
7279
exitOnError: argv.exitOnError
7380
})
7481

82+
if (argv.delete && argv.output && argv.output.length !== 0) {
83+
const relPath = path.relative(process.cwd(), path.resolve(argv.output))
84+
if (relPath !== '' && !relPath.startsWith('..')) {
85+
rimraf.sync(argv.output)
86+
} else {
87+
console.error(`nyc instrument failed: attempt to delete '${process.cwd()}'`)
88+
process.exit(1)
89+
}
90+
}
91+
7592
nyc.instrumentAllFiles(argv.input, argv.output, function (err) {
7693
if (err) {
7794
console.error(err.message)

test/nyc-integration.js

+87-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const fs = require('fs')
1111
const glob = require('glob')
1212
const isWindows = require('is-windows')()
1313
const rimraf = require('rimraf')
14+
const makeDir = require('make-dir')
1415
const spawn = require('child_process').spawn
1516
const si = require('strip-indent')
1617

@@ -622,6 +623,10 @@ describe('the nyc cli', function () {
622623
})
623624

624625
describe('output folder specified', function () {
626+
afterEach(function () {
627+
rimraf.sync(path.resolve(fixturesCLI, 'output'))
628+
})
629+
625630
it('allows a single file to be instrumented', function (done) {
626631
var args = [bin, 'instrument', './half-covered.js', './output']
627632

@@ -635,7 +640,6 @@ describe('the nyc cli', function () {
635640
var files = fs.readdirSync(path.resolve(fixturesCLI, './output'))
636641
files.length.should.equal(1)
637642
files.should.include('half-covered.js')
638-
rimraf.sync(path.resolve(fixturesCLI, 'output'))
639643
done()
640644
})
641645
})
@@ -653,7 +657,6 @@ describe('the nyc cli', function () {
653657
var files = fs.readdirSync(path.resolve(fixturesCLI, './output'))
654658
files.should.include('env.js')
655659
files.should.include('es6.js')
656-
rimraf.sync(path.resolve(fixturesCLI, 'output'))
657660
done()
658661
})
659662
})
@@ -670,7 +673,6 @@ describe('the nyc cli', function () {
670673
code.should.equal(0)
671674
var files = fs.readdirSync(path.resolve(fixturesCLI, './output'))
672675
files.should.include('index.js')
673-
rimraf.sync(path.resolve(fixturesCLI, 'output'))
674676
done()
675677
})
676678
})
@@ -721,6 +723,88 @@ describe('the nyc cli', function () {
721723
})
722724
})
723725
})
726+
727+
describe('delete', function () {
728+
beforeEach(function () {
729+
makeDir.sync(path.resolve(fixturesCLI, 'output', 'removed-by-clean'))
730+
})
731+
732+
it('cleans the output directory if `--delete` is specified', function (done) {
733+
const args = [bin, 'instrument', '--delete', 'true', './', './output']
734+
735+
const proc = spawn(process.execPath, args, {
736+
cwd: fixturesCLI,
737+
env: env
738+
})
739+
740+
proc.on('close', function (code) {
741+
code.should.equal(0)
742+
const subdirExists = fs.existsSync(path.resolve(fixturesCLI, './output/subdir/input-dir'))
743+
subdirExists.should.equal(true)
744+
const files = fs.readdirSync(path.resolve(fixturesCLI, './output'))
745+
files.should.not.include('removed-by-clean')
746+
done()
747+
})
748+
})
749+
750+
it('does not clean the output directory by default', function (done) {
751+
const args = [bin, 'instrument', './', './output']
752+
753+
const proc = spawn(process.execPath, args, {
754+
cwd: fixturesCLI,
755+
env: env
756+
})
757+
758+
proc.on('close', function (code) {
759+
code.should.equal(0)
760+
const subdirExists = fs.existsSync(path.resolve(fixturesCLI, './output/subdir/input-dir'))
761+
subdirExists.should.equal(true)
762+
const files = fs.readdirSync(path.resolve(fixturesCLI, './output'))
763+
files.should.include('removed-by-clean')
764+
done()
765+
})
766+
})
767+
768+
it('aborts if trying to clean process.cwd()', function (done) {
769+
const args = [bin, 'instrument', '--delete', './', './']
770+
771+
const proc = spawn(process.execPath, args, {
772+
cwd: fixturesCLI,
773+
env: env
774+
})
775+
776+
let stderr = ''
777+
proc.stderr.on('data', function (chunk) {
778+
stderr += chunk
779+
})
780+
781+
proc.on('close', function (code) {
782+
code.should.equal(1)
783+
stderr.should.include('nyc instrument failed: attempt to delete')
784+
done()
785+
})
786+
})
787+
788+
it('aborts if trying to clean outside working directory', function (done) {
789+
const args = [bin, 'instrument', '--delete', './', '../']
790+
791+
const proc = spawn(process.execPath, args, {
792+
cwd: fixturesCLI,
793+
env: env
794+
})
795+
796+
let stderr = ''
797+
proc.stderr.on('data', function (chunk) {
798+
stderr += chunk
799+
})
800+
801+
proc.on('close', function (code) {
802+
code.should.equal(1)
803+
stderr.should.include('nyc instrument failed: attempt to delete')
804+
done()
805+
})
806+
})
807+
})
724808
})
725809
})
726810

0 commit comments

Comments
 (0)