diff --git a/src/vs/base/node/extfs.ts b/src/vs/base/node/extfs.ts index 4da39cd32778b80f426bc9b401a7ba4e556cee88..58710111885bcb55448ee50f205016519d5c4e87 100644 --- a/src/vs/base/node/extfs.ts +++ b/src/vs/base/node/extfs.ts @@ -245,6 +245,24 @@ function rmRecursive(path: string, callback: (error: Error) => void): void { }); } +export function delSync(path: string): void { + try { + const stat = fs.lstatSync(path); + if (stat.isDirectory() && !stat.isSymbolicLink()) { + readdirSync(path).forEach(child => delSync(paths.join(path, child))); + fs.rmdirSync(path); + } else { + fs.unlinkSync(path); + } + } catch (err) { + if (err.code === 'ENOENT') { + return; // not found + } + + throw err; + } +} + export function mv(source: string, target: string, callback: (error: Error) => void): void { if (source === target) { return callback(null); diff --git a/src/vs/base/test/node/extfs/extfs.test.ts b/src/vs/base/test/node/extfs/extfs.test.ts index a487a8d9edb3ba157f31749b720ddb34c4b10168..fbcf7e4fe826d55acefabbc2b3ace16eafb2a644 100644 --- a/src/vs/base/test/node/extfs/extfs.test.ts +++ b/src/vs/base/test/node/extfs/extfs.test.ts @@ -34,6 +34,59 @@ suite('Extfs', () => { }); // 493 = 0755 }); + test('delSync - swallows file not found error', function () { + const id = uuid.generateUuid(); + const parentDir = path.join(os.tmpdir(), 'vsctests', id); + const newDir = path.join(parentDir, 'extfs', id); + + extfs.delSync(newDir); + + assert.ok(!fs.existsSync(newDir)); + }); + + test('delSync - simple', function (done: () => void) { + const id = uuid.generateUuid(); + const parentDir = path.join(os.tmpdir(), 'vsctests', id); + const newDir = path.join(parentDir, 'extfs', id); + + extfs.mkdirp(newDir, 493, (error) => { + if (error) { + return onError(error, done); + } + + fs.writeFileSync(path.join(newDir, 'somefile.txt'), 'Contents'); + fs.writeFileSync(path.join(newDir, 'someOtherFile.txt'), 'Contents'); + + extfs.delSync(newDir); + + assert.ok(!fs.existsSync(newDir)); + done(); + }); // 493 = 0755 + }); + + test('delSync - recursive folder structure', function (done: () => void) { + const id = uuid.generateUuid(); + const parentDir = path.join(os.tmpdir(), 'vsctests', id); + const newDir = path.join(parentDir, 'extfs', id); + + extfs.mkdirp(newDir, 493, (error) => { + if (error) { + return onError(error, done); + } + + fs.writeFileSync(path.join(newDir, 'somefile.txt'), 'Contents'); + fs.writeFileSync(path.join(newDir, 'someOtherFile.txt'), 'Contents'); + + fs.mkdirSync(path.join(newDir, 'somefolder')); + fs.writeFileSync(path.join(newDir, 'somefolder', 'somefile.txt'), 'Contents'); + + extfs.delSync(newDir); + + assert.ok(!fs.existsSync(newDir)); + done(); + }); // 493 = 0755 + }); + test('copy, move and delete', function (done: () => void) { const id = uuid.generateUuid(); const id2 = uuid.generateUuid(); diff --git a/src/vs/base/test/node/pfs.test.ts b/src/vs/base/test/node/pfs.test.ts index 3e48a052e87ee8ec84db4aa2f65daaa97e90e864..3963c7eea49e49e4c8d5b54d23402e639bf1a0a3 100644 --- a/src/vs/base/test/node/pfs.test.ts +++ b/src/vs/base/test/node/pfs.test.ts @@ -101,4 +101,47 @@ suite('PFS', () => { }, error => onError(error, done)); }); }); + + test('rimraf - simple', function (done: () => void) { + const id = uuid.generateUuid(); + const parentDir = path.join(os.tmpdir(), 'vsctests', id); + const newDir = path.join(parentDir, 'extfs', id); + + extfs.mkdirp(newDir, 493, (error) => { + if (error) { + return onError(error, done); + } + + fs.writeFileSync(path.join(newDir, 'somefile.txt'), 'Contents'); + fs.writeFileSync(path.join(newDir, 'someOtherFile.txt'), 'Contents'); + + pfs.rimraf(newDir).then(() => { + assert.ok(!fs.existsSync(newDir)); + done(); + }, error => onError(error, done)); + }); // 493 = 0755 + }); + + test('rimraf - recursive folder structure', function (done: () => void) { + const id = uuid.generateUuid(); + const parentDir = path.join(os.tmpdir(), 'vsctests', id); + const newDir = path.join(parentDir, 'extfs', id); + + extfs.mkdirp(newDir, 493, (error) => { + if (error) { + return onError(error, done); + } + + fs.writeFileSync(path.join(newDir, 'somefile.txt'), 'Contents'); + fs.writeFileSync(path.join(newDir, 'someOtherFile.txt'), 'Contents'); + + fs.mkdirSync(path.join(newDir, 'somefolder')); + fs.writeFileSync(path.join(newDir, 'somefolder', 'somefile.txt'), 'Contents'); + + pfs.rimraf(newDir).then(() => { + assert.ok(!fs.existsSync(newDir)); + done(); + }, error => onError(error, done)); + }); // 493 = 0755 + }); }); \ No newline at end of file