1616 *
1717*/
1818
19- const assert = require ( 'assert' ) ;
20- const fs = require ( 'fs' ) ;
21- const file = require ( '../../lib/utils/file' ) ;
19+ const { existsSync, readdirSync, rmSync, symlinkSync, unlinkSync, writeFileSync } = require ( 'fs' ) ;
2220const { assertFileMode, testRequiresWindowsElevation, ensureDirs } = require ( './helpers' ) ;
2321const { suite, setup, test } = require ( 'mocha' ) ;
22+ const assert = require ( 'assert' ) ;
23+ const file = require ( '../../lib/utils/file' ) ;
24+
25+ // Shim to support older node versions
26+ const rmF = rmSync
27+ ? ( path ) => rmSync ( path , { force : true } )
28+ : ( path ) => {
29+ if ( existsSync ( path ) ) {
30+ unlinkSync ( path ) ;
31+ }
32+ } ;
2433
2534suite ( 'fileUtils' , function ( ) {
2635
@@ -33,13 +42,13 @@ suite('fileUtils', function () {
3342
3443 test ( 'with dir' , function ( ) {
3544 file . mkdirP ( 'foo' ) ;
36- assert . ok ( fs . existsSync ( 'foo' ) ) ;
45+ assert . ok ( existsSync ( 'foo' ) ) ;
3746 ensureDirs ( false , 'foo' ) ;
3847 } ) ;
3948
4049 test ( 'with subdir' , function ( ) {
4150 file . mkdirP ( 'foo/bar/baz/qux' ) ;
42- assert . ok ( fs . existsSync ( 'foo/bar/baz/qux' ) ) ;
51+ assert . ok ( existsSync ( 'foo/bar/baz/qux' ) ) ;
4352 ensureDirs ( false , 'foo' ) ;
4453 } ) ;
4554
@@ -50,48 +59,48 @@ suite('fileUtils', function () {
5059 test ( 'with subdir' , function ( ) {
5160 ensureDirs ( 'foo/bar/baz/qux' ) ;
5261 file . rmRf ( 'foo/bar' ) ;
53- assert . deepStrictEqual ( fs . readdirSync ( 'foo' ) , [ ] ) ;
54- assert . strictEqual ( fs . existsSync ( 'foo/bar' ) , false ) ;
62+ assert . deepStrictEqual ( readdirSync ( 'foo' ) , [ ] ) ;
63+ assert . strictEqual ( existsSync ( 'foo/bar' ) , false ) ;
5564 ensureDirs ( false , 'foo' ) ;
5665 } ) ;
5766
5867 test ( 'with symlink subdir' , function ( ) {
5968 testRequiresWindowsElevation ( this ) ;
6069
6170 ensureDirs ( 'foo' , 'bar' ) ;
62- fs . writeFileSync ( 'foo/hello.txt' , 'hello, it\'s me' ) ;
63- fs . symlinkSync ( '../foo' , 'bar/foo' ) ;
71+ writeFileSync ( 'foo/hello.txt' , 'hello, it\'s me' ) ;
72+ symlinkSync ( '../foo' , 'bar/foo' ) ;
6473
6574 file . rmRf ( 'bar' ) ;
6675
6776 // Make sure the bar directory was successfully deleted
68- assert . strictEqual ( fs . existsSync ( 'bar' ) , false ) ;
77+ assert . strictEqual ( existsSync ( 'bar' ) , false ) ;
6978
7079 // Make sure that the file inside the linked folder wasn't deleted
71- assert . strictEqual ( fs . existsSync ( 'foo/hello.txt' ) , true ) ;
80+ assert . strictEqual ( existsSync ( 'foo/hello.txt' ) , true ) ;
7281
7382 // Cleanup
74- fs . unlinkSync ( 'foo/hello.txt' ) ;
83+ unlinkSync ( 'foo/hello.txt' ) ;
7584 ensureDirs ( false , 'foo' , 'bar' ) ;
7685 } ) ;
7786
7887 test ( 'with symlinked dir' , function ( ) {
7988 testRequiresWindowsElevation ( this ) ;
8089
8190 ensureDirs ( 'foo' ) ;
82- fs . writeFileSync ( 'foo/hello.txt' , 'hello!' ) ;
83- fs . symlinkSync ( 'foo' , 'bar' ) ;
91+ writeFileSync ( 'foo/hello.txt' , 'hello!' ) ;
92+ symlinkSync ( 'foo' , 'bar' ) ;
8493
8594 file . rmRf ( 'bar' ) ;
8695
8796 // Make sure the bar directory was successfully deleted
88- assert . strictEqual ( fs . existsSync ( 'bar' ) , false ) ;
97+ assert . strictEqual ( existsSync ( 'bar' ) , false ) ;
8998
9099 // Make sure that the file inside the linked folder wasn't deleted
91- assert . strictEqual ( fs . existsSync ( 'foo/hello.txt' ) , true ) ;
100+ assert . strictEqual ( existsSync ( 'foo/hello.txt' ) , true ) ;
92101
93102 // Cleanup
94- fs . unlinkSync ( 'foo/hello.txt' ) ;
103+ unlinkSync ( 'foo/hello.txt' ) ;
95104 ensureDirs ( false , 'foo' , 'bar' ) ;
96105 } ) ;
97106
@@ -101,10 +110,10 @@ suite('fileUtils', function () {
101110
102111 test ( 'with same name and different directory' , function ( ) {
103112 ensureDirs ( 'foo' , false , 'bar' ) ;
104- fs . writeFileSync ( 'foo/bar.txt' , 'w00t' ) ;
113+ writeFileSync ( 'foo/bar.txt' , 'w00t' ) ;
105114
106115 file . cpR ( 'foo' , 'bar' ) ;
107- assert . ok ( fs . existsSync ( 'bar/bar.txt' ) ) ;
116+ assert . ok ( existsSync ( 'bar/bar.txt' ) ) ;
108117
109118 ensureDirs ( false , 'foo' , 'bar' ) ;
110119 } ) ;
@@ -117,61 +126,61 @@ suite('fileUtils', function () {
117126
118127 test ( 'rename via copy in directory' , function ( ) {
119128 ensureDirs ( 'foo' ) ;
120- fs . writeFileSync ( 'foo/bar.txt' , 'w00t' ) ;
129+ writeFileSync ( 'foo/bar.txt' , 'w00t' ) ;
121130
122131 file . cpR ( 'foo/bar.txt' , 'foo/baz.txt' ) ;
123- assert . ok ( fs . existsSync ( 'foo/baz.txt' ) ) ;
132+ assert . ok ( existsSync ( 'foo/baz.txt' ) ) ;
124133
125134 ensureDirs ( false , 'foo' ) ;
126135 } ) ;
127136
128137 test ( 'rename via copy in base' , function ( ) {
129- fs . writeFileSync ( 'bar.txt' , 'w00t' ) ;
138+ writeFileSync ( 'bar.txt' , 'w00t' ) ;
130139
131140 file . cpR ( 'bar.txt' , 'baz.txt' ) ;
132- assert . ok ( fs . existsSync ( 'baz.txt' ) ) ;
141+ assert . ok ( existsSync ( 'baz.txt' ) ) ;
133142
134- fs . rmSync ( 'bar.txt' ) ;
135- fs . rmSync ( 'baz.txt' ) ;
143+ rmF ( 'bar.txt' ) ;
144+ rmF ( 'baz.txt' ) ;
136145 } ) ;
137146
138147 test ( 'keeps file mode' , function ( ) {
139148 [ 0o750 , 0o744 ] . forEach ( ( mode ) => {
140- fs . writeFileSync ( 'bar.txt' , 'w00t' , { mode : mode } ) ;
149+ writeFileSync ( 'bar.txt' , 'w00t' , { mode : mode } ) ;
141150
142151 file . cpR ( 'bar.txt' , 'baz.txt' ) ;
143152 assertFileMode ( 'baz.txt' , mode ) ;
144153
145- fs . rmSync ( 'bar.txt' ) ;
146- fs . rmSync ( 'baz.txt' ) ;
154+ rmF ( 'bar.txt' ) ;
155+ rmF ( 'baz.txt' ) ;
147156 } ) ;
148157 } ) ;
149158
150159 test ( 'keeps file mode when overwriting with preserveMode' , function ( ) {
151- fs . writeFileSync ( 'bar.txt' , 'w00t' , { mode : 0o755 } ) ;
152- fs . writeFileSync ( 'baz.txt' , 'w00t!' , { mode : 0o744 } ) ;
160+ writeFileSync ( 'bar.txt' , 'w00t' , { mode : 0o755 } ) ;
161+ writeFileSync ( 'baz.txt' , 'w00t!' , { mode : 0o744 } ) ;
153162
154163 file . cpR ( 'bar.txt' , 'baz.txt' , { silent : true , preserveMode : true } ) ;
155164 assertFileMode ( 'baz.txt' , 0o755 ) ;
156165
157- fs . rmSync ( 'bar.txt' ) ;
158- fs . rmSync ( 'baz.txt' ) ;
166+ rmF ( 'bar.txt' ) ;
167+ rmF ( 'baz.txt' ) ;
159168 } ) ;
160169
161170 test ( 'does not keep file mode when overwriting' , function ( ) {
162- fs . writeFileSync ( 'bar.txt' , 'w00t' , { mode : 0o766 } ) ;
163- fs . writeFileSync ( 'baz.txt' , 'w00t!' , { mode : 0o744 } ) ;
171+ writeFileSync ( 'bar.txt' , 'w00t' , { mode : 0o766 } ) ;
172+ writeFileSync ( 'baz.txt' , 'w00t!' , { mode : 0o744 } ) ;
164173
165174 file . cpR ( 'bar.txt' , 'baz.txt' ) ;
166175 assertFileMode ( 'baz.txt' , 0o744 ) ;
167176
168- fs . rmSync ( 'bar.txt' ) ;
169- fs . rmSync ( 'baz.txt' ) ;
177+ rmF ( 'bar.txt' ) ;
178+ rmF ( 'baz.txt' ) ;
170179 } ) ;
171180
172181 test ( 'copies file mode recursively' , function ( ) {
173182 ensureDirs ( 'foo' , false , 'baz' ) ;
174- fs . writeFileSync ( 'foo/bar.txt' , 'w00t' , { mode : 0o740 } ) ;
183+ writeFileSync ( 'foo/bar.txt' , 'w00t' , { mode : 0o740 } ) ;
175184
176185 file . cpR ( 'foo' , 'baz' ) ;
177186 assertFileMode ( 'baz/bar.txt' , 0o740 ) ;
@@ -181,8 +190,8 @@ suite('fileUtils', function () {
181190
182191 test ( 'keeps file mode recursively' , function ( ) {
183192 ensureDirs ( 'foo' , 'baz/foo' ) ;
184- fs . writeFileSync ( 'foo/bar.txt' , 'w00t' , { mode : 0o740 } ) ;
185- fs . writeFileSync ( 'baz/foo/bar.txt' , 'w00t!' , { mode : 0o755 } ) ;
193+ writeFileSync ( 'foo/bar.txt' , 'w00t' , { mode : 0o740 } ) ;
194+ writeFileSync ( 'baz/foo/bar.txt' , 'w00t!' , { mode : 0o755 } ) ;
186195
187196 file . cpR ( 'foo' , 'baz' , { silent : true , preserveMode : true } ) ;
188197 assertFileMode ( 'baz/foo/bar.txt' , 0o740 ) ;
0 commit comments