@@ -11,6 +11,7 @@ const fs = require('fs')
1111const glob = require ( 'glob' )
1212const isWindows = require ( 'is-windows' ) ( )
1313const rimraf = require ( 'rimraf' )
14+ const makeDir = require ( 'make-dir' )
1415const spawn = require ( 'child_process' ) . spawn
1516const 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