@@ -11,6 +11,7 @@ const fs = require('fs')
11
11
const glob = require ( 'glob' )
12
12
const isWindows = require ( 'is-windows' ) ( )
13
13
const rimraf = require ( 'rimraf' )
14
+ const makeDir = require ( 'make-dir' )
14
15
const spawn = require ( 'child_process' ) . spawn
15
16
const si = require ( 'strip-indent' )
16
17
@@ -622,6 +623,10 @@ describe('the nyc cli', function () {
622
623
} )
623
624
624
625
describe ( 'output folder specified' , function ( ) {
626
+ afterEach ( function ( ) {
627
+ rimraf . sync ( path . resolve ( fixturesCLI , 'output' ) )
628
+ } )
629
+
625
630
it ( 'allows a single file to be instrumented' , function ( done ) {
626
631
var args = [ bin , 'instrument' , './half-covered.js' , './output' ]
627
632
@@ -635,7 +640,6 @@ describe('the nyc cli', function () {
635
640
var files = fs . readdirSync ( path . resolve ( fixturesCLI , './output' ) )
636
641
files . length . should . equal ( 1 )
637
642
files . should . include ( 'half-covered.js' )
638
- rimraf . sync ( path . resolve ( fixturesCLI , 'output' ) )
639
643
done ( )
640
644
} )
641
645
} )
@@ -653,7 +657,6 @@ describe('the nyc cli', function () {
653
657
var files = fs . readdirSync ( path . resolve ( fixturesCLI , './output' ) )
654
658
files . should . include ( 'env.js' )
655
659
files . should . include ( 'es6.js' )
656
- rimraf . sync ( path . resolve ( fixturesCLI , 'output' ) )
657
660
done ( )
658
661
} )
659
662
} )
@@ -670,7 +673,6 @@ describe('the nyc cli', function () {
670
673
code . should . equal ( 0 )
671
674
var files = fs . readdirSync ( path . resolve ( fixturesCLI , './output' ) )
672
675
files . should . include ( 'index.js' )
673
- rimraf . sync ( path . resolve ( fixturesCLI , 'output' ) )
674
676
done ( )
675
677
} )
676
678
} )
@@ -721,6 +723,88 @@ describe('the nyc cli', function () {
721
723
} )
722
724
} )
723
725
} )
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
+ } )
724
808
} )
725
809
} )
726
810
0 commit comments