|
1 | 1 | import path from 'path'; |
2 | 2 | import fs from 'fs'; |
3 | | -import test from 'ava'; |
| 3 | +import {serial as test} from 'ava'; |
4 | 4 | import tempy from 'tempy'; |
5 | 5 | import makeDir from 'make-dir'; |
6 | 6 | import del from '.'; |
7 | 7 |
|
| 8 | +const processCwd = process.cwd(); |
| 9 | + |
8 | 10 | function exists(t, files) { |
9 | 11 | for (const file of files) { |
10 | 12 | t.true(fs.existsSync(path.join(t.context.tmp, file))); |
@@ -67,7 +69,7 @@ test('take options into account - sync', t => { |
67 | 69 | notExists(t, ['2.tmp', '3.tmp', '4.tmp', '.dot.tmp']); |
68 | 70 | }); |
69 | 71 |
|
70 | | -test.serial('return deleted files - async', async t => { |
| 72 | +test('return deleted files - async', async t => { |
71 | 73 | t.deepEqual( |
72 | 74 | await del('1.tmp', {cwd: t.context.tmp}), |
73 | 75 | [path.join(t.context.tmp, '1.tmp')] |
@@ -109,7 +111,7 @@ test('don\'t delete files, but return them - sync', t => { |
109 | 111 |
|
110 | 112 | // Currently this only testable locally on an osx machine. |
111 | 113 | // https://github.com/sindresorhus/del/issues/68 |
112 | | -test.serial('does not throw EINVAL - async', async t => { |
| 114 | +test('does not throw EINVAL - async', async t => { |
113 | 115 | await del('**/*', { |
114 | 116 | cwd: t.context.tmp, |
115 | 117 | dot: true |
@@ -144,7 +146,7 @@ test.serial('does not throw EINVAL - async', async t => { |
144 | 146 | t.is(count, totalAttempts); |
145 | 147 | }); |
146 | 148 |
|
147 | | -test.serial('does not throw EINVAL - sync', t => { |
| 149 | +test('does not throw EINVAL - sync', t => { |
148 | 150 | del.sync('**/*', { |
149 | 151 | cwd: t.context.tmp, |
150 | 152 | dot: true |
@@ -177,3 +179,131 @@ test.serial('does not throw EINVAL - sync', t => { |
177 | 179 | notExists(t, [...fixtures, 'a']); |
178 | 180 | t.is(count, totalAttempts); |
179 | 181 | }); |
| 182 | + |
| 183 | +test('delete relative files outside of process.cwd using cwd - async', async t => { |
| 184 | + await del(['1.tmp'], {cwd: t.context.tmp}); |
| 185 | + |
| 186 | + exists(t, ['2.tmp', '3.tmp', '4.tmp', '.dot.tmp']); |
| 187 | + notExists(t, ['1.tmp']); |
| 188 | +}); |
| 189 | + |
| 190 | +test('delete relative files outside of process.cwd using cwd - sync', t => { |
| 191 | + del.sync(['1.tmp'], {cwd: t.context.tmp}); |
| 192 | + |
| 193 | + exists(t, ['2.tmp', '3.tmp', '4.tmp', '.dot.tmp']); |
| 194 | + notExists(t, ['1.tmp']); |
| 195 | +}); |
| 196 | + |
| 197 | +test('delete absolute files outside of process.cwd using cwd - async', async t => { |
| 198 | + const absolutePath = path.resolve(t.context.tmp, '1.tmp'); |
| 199 | + await del([absolutePath], {cwd: t.context.tmp}); |
| 200 | + |
| 201 | + exists(t, ['2.tmp', '3.tmp', '4.tmp', '.dot.tmp']); |
| 202 | + notExists(t, ['1.tmp']); |
| 203 | +}); |
| 204 | + |
| 205 | +test('delete absolute files outside of process.cwd using cwd - sync', t => { |
| 206 | + const absolutePath = path.resolve(t.context.tmp, '1.tmp'); |
| 207 | + del.sync([absolutePath], {cwd: t.context.tmp}); |
| 208 | + |
| 209 | + exists(t, ['2.tmp', '3.tmp', '4.tmp', '.dot.tmp']); |
| 210 | + notExists(t, ['1.tmp']); |
| 211 | +}); |
| 212 | + |
| 213 | +test('cannot delete actual working directory without force: true - async', async t => { |
| 214 | + process.chdir(t.context.tmp); |
| 215 | + |
| 216 | + await t.throwsAsync(() => del([t.context.tmp]), { |
| 217 | + instanceOf: Error, |
| 218 | + message: 'Cannot delete the current working directory. Can be overridden with the `force` option.' |
| 219 | + }); |
| 220 | + |
| 221 | + exists(t, ['', '1.tmp', '2.tmp', '3.tmp', '4.tmp', '.dot.tmp']); |
| 222 | + process.chdir(processCwd); |
| 223 | +}); |
| 224 | + |
| 225 | +test('cannot delete actual working directory without force: true - sync', t => { |
| 226 | + process.chdir(t.context.tmp); |
| 227 | + |
| 228 | + t.throws(() => del.sync([t.context.tmp]), { |
| 229 | + instanceOf: Error, |
| 230 | + message: 'Cannot delete the current working directory. Can be overridden with the `force` option.' |
| 231 | + }); |
| 232 | + |
| 233 | + exists(t, ['', '1.tmp', '2.tmp', '3.tmp', '4.tmp', '.dot.tmp']); |
| 234 | + process.chdir(processCwd); |
| 235 | +}); |
| 236 | + |
| 237 | +test('cannot delete actual working directory with cwd option without force: true - async', async t => { |
| 238 | + process.chdir(t.context.tmp); |
| 239 | + |
| 240 | + await t.throwsAsync(() => del([t.context.tmp], {cwd: __dirname}), { |
| 241 | + instanceOf: Error, |
| 242 | + message: 'Cannot delete the current working directory. Can be overridden with the `force` option.' |
| 243 | + }); |
| 244 | + |
| 245 | + exists(t, ['', '1.tmp', '2.tmp', '3.tmp', '4.tmp', '.dot.tmp']); |
| 246 | + process.chdir(processCwd); |
| 247 | +}); |
| 248 | + |
| 249 | +test('cannot delete actual working directory with cwd option without force: true - sync', t => { |
| 250 | + process.chdir(t.context.tmp); |
| 251 | + |
| 252 | + t.throws(() => del.sync([t.context.tmp], {cwd: __dirname}), { |
| 253 | + instanceOf: Error, |
| 254 | + message: 'Cannot delete the current working directory. Can be overridden with the `force` option.' |
| 255 | + }); |
| 256 | + |
| 257 | + exists(t, ['', '1.tmp', '2.tmp', '3.tmp', '4.tmp', '.dot.tmp']); |
| 258 | + process.chdir(processCwd); |
| 259 | +}); |
| 260 | + |
| 261 | +test('cannot delete files outside cwd without force: true - async', async t => { |
| 262 | + const absolutePath = path.resolve(t.context.tmp, '1.tmp'); |
| 263 | + |
| 264 | + await t.throwsAsync(() => del([absolutePath]), { |
| 265 | + instanceOf: Error, |
| 266 | + message: 'Cannot delete files/directories outside the current working directory. Can be overridden with the `force` option.' |
| 267 | + }); |
| 268 | + |
| 269 | + exists(t, ['1.tmp', '2.tmp', '3.tmp', '4.tmp', '.dot.tmp']); |
| 270 | +}); |
| 271 | + |
| 272 | +test('cannot delete files outside cwd without force: true - sync', t => { |
| 273 | + const absolutePath = path.resolve(t.context.tmp, '1.tmp'); |
| 274 | + |
| 275 | + t.throws(() => del.sync([absolutePath]), { |
| 276 | + instanceOf: Error, |
| 277 | + message: 'Cannot delete files/directories outside the current working directory. Can be overridden with the `force` option.' |
| 278 | + }); |
| 279 | + |
| 280 | + exists(t, ['', '1.tmp', '2.tmp', '3.tmp', '4.tmp', '.dot.tmp']); |
| 281 | +}); |
| 282 | + |
| 283 | +test('cannot delete files inside process.cwd when outside cwd without force: true - async', async t => { |
| 284 | + process.chdir(t.context.tmp); |
| 285 | + const removeFile = path.resolve(t.context.tmp, '2.tmp'); |
| 286 | + const cwd = path.resolve(t.context.tmp, '1.tmp'); |
| 287 | + |
| 288 | + await t.throwsAsync(() => del([removeFile], {cwd}), { |
| 289 | + instanceOf: Error, |
| 290 | + message: 'Cannot delete files/directories outside the current working directory. Can be overridden with the `force` option.' |
| 291 | + }); |
| 292 | + |
| 293 | + exists(t, ['1.tmp', '2.tmp', '3.tmp', '4.tmp', '.dot.tmp']); |
| 294 | + process.chdir(processCwd); |
| 295 | +}); |
| 296 | + |
| 297 | +test('cannot delete files inside process.cwd when outside cwd without force: true - sync', t => { |
| 298 | + process.chdir(t.context.tmp); |
| 299 | + const removeFile = path.resolve(t.context.tmp, '2.tmp'); |
| 300 | + const cwd = path.resolve(t.context.tmp, '1.tmp'); |
| 301 | + |
| 302 | + t.throws(() => del.sync([removeFile], {cwd}), { |
| 303 | + instanceOf: Error, |
| 304 | + message: 'Cannot delete files/directories outside the current working directory. Can be overridden with the `force` option.' |
| 305 | + }); |
| 306 | + |
| 307 | + exists(t, ['1.tmp', '2.tmp', '3.tmp', '4.tmp', '.dot.tmp']); |
| 308 | + process.chdir(processCwd); |
| 309 | +}); |
0 commit comments