Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix non recursive folder delete using file system #13361

Merged
merged 1 commit into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion packages/filesystem/src/node/disk-file-system-provider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { promises as fs } from 'fs';
import { join } from 'path';
import * as temp from 'temp';
import { v4 } from 'uuid';
import { FilePermission, FileSystemProviderError, FileSystemProviderErrorCode } from '../common/files';
import { FilePermission, FileSystemProviderCapabilities, FileSystemProviderError, FileSystemProviderErrorCode } from '../common/files';
import { DiskFileSystemProvider } from './disk-file-system-provider';
import { bindFileSystemWatcherServer } from './filesystem-backend-module';

Expand Down Expand Up @@ -93,6 +93,39 @@ describe('disk-file-system-provider', () => {
});
});

describe('delete', () => {
it('delete is able to delete folder', async () => {
const tempDirPath = tracked.mkdirSync();
const testFolder = join(tempDirPath, 'test');
const folderUri = FileUri.create(testFolder);
for (const recursive of [true, false]) {
// Note: useTrash = true fails on Linux
const useTrash = false;
if ((fsProvider.capabilities & FileSystemProviderCapabilities.Access) === 0 && useTrash) {
continue;
}
await fsProvider.mkdir(folderUri);
if (recursive) {
await fsProvider.writeFile(FileUri.create(join(testFolder, 'test.file')), Buffer.from('test'), { overwrite: false, create: true });
await fsProvider.mkdir(FileUri.create(join(testFolder, 'subFolder')));
}
await fsProvider.delete(folderUri, { recursive, useTrash });
}
});

it('delete is able to delete file', async () => {
const tempDirPath = tracked.mkdirSync();
const testFile = join(tempDirPath, 'test.file');
const testFileUri = FileUri.create(testFile);
for (const recursive of [true, false]) {
for (const useTrash of [true, false]) {
await fsProvider.writeFile(testFileUri, Buffer.from('test'), { overwrite: false, create: true });
await fsProvider.delete(testFileUri, { recursive, useTrash });
}
}
});
});

function createContainer(): Container {
const container = new Container({ defaultScope: 'Singleton' });
const module = new ContainerModule(bind => {
Expand Down
7 changes: 6 additions & 1 deletion packages/filesystem/src/node/disk-file-system-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,12 @@ export class DiskFileSystemProvider implements Disposable,
if (opts.recursive) {
await this.rimraf(filePath);
} else {
await promisify(unlink)(filePath);
const stat = await promisify(lstat)(filePath);
if (stat.isDirectory() && !stat.isSymbolicLink()) {
jfaltermeier marked this conversation as resolved.
Show resolved Hide resolved
await promisify(rmdir)(filePath);
} else {
await promisify(unlink)(filePath);
}
}
} else {
await trash(filePath);
Expand Down
Loading