Skip to content

Commit 4f6a513

Browse files
committed
Re-design deleteFiles
1 parent a9a77e0 commit 4f6a513

File tree

1 file changed

+41
-32
lines changed

1 file changed

+41
-32
lines changed

test/suite/extension.test.ts

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,21 @@ function getHaskellConfig() {
2323
return vscode.workspace.getConfiguration('haskell');
2424
}
2525

26-
function getWorkspaceRoot() {
26+
function getWorkspaceRoot(): vscode.WorkspaceFolder {
2727
return vscode.workspace.workspaceFolders![0];
2828
}
2929

30-
function getWorkspaceFile(name: string) {
30+
function getWorkspaceFile(name: string): vscode.Uri {
3131
const wsroot = getWorkspaceRoot().uri;
3232
return wsroot.with({ path: path.posix.join(wsroot.path, name) });
3333
}
3434

35-
// function getWorkspaceGhcupCacheDirectory() {
36-
// const wsroot = getWorkspaceRoot().uri;
37-
// return wsroot.with({ path: path.posix.join(wsroot.path, 'bin/.ghcup/cache') });
38-
// }
35+
function joinUri(root: vscode.Uri, ...pathSegments: string[]): vscode.Uri {
36+
return root.with({ path: path.posix.join(root.path, ...pathSegments) });
37+
}
3938

40-
async function deleteWorkspaceFiles(pred?: (fileType: [string, vscode.FileType]) => boolean) {
41-
await deleteFiles(getWorkspaceRoot().uri, pred);
39+
async function deleteWorkspaceFiles(keepDirs: vscode.Uri[], pred?: (fileName: string) => boolean): Promise<void> {
40+
await deleteFiles(getWorkspaceRoot().uri, keepDirs, pred);
4241
}
4342

4443
function getExtensionLogContent(): string | undefined {
@@ -52,17 +51,37 @@ function getExtensionLogContent(): string | undefined {
5251
}
5352
}
5453

55-
async function deleteFiles(dir: vscode.Uri, pred?: (fileType: [string, vscode.FileType]) => boolean) {
54+
async function deleteFiles(dir: vscode.Uri, keepDirs: vscode.Uri[], pred?: (fileType: string) => boolean) {
55+
if (keepDirs.includes(dir)) {
56+
console.log(`Keeping ${dir}`);
57+
return;
58+
};
5659
const dirContents = await vscode.workspace.fs.readDirectory(dir);
5760
console.log(`Deleting ${dir} contents: ${dirContents}`);
5861
dirContents.forEach(async ([name, type]) => {
59-
const uri: vscode.Uri = getWorkspaceFile(name);
60-
if (!pred || pred([name, type])) {
61-
console.log(`Deleting ${uri}`);
62-
await vscode.workspace.fs.delete(getWorkspaceFile(name), {
63-
recursive: true,
64-
useTrash: false,
65-
});
62+
const uri: vscode.Uri = joinUri(dir, name);
63+
if (type === vscode.FileType.File) {
64+
if (!pred || pred(name)) {
65+
console.log(`Deleting ${uri}`);
66+
await vscode.workspace.fs.delete(joinUri(dir, name), {
67+
recursive: false,
68+
useTrash: false,
69+
});
70+
}
71+
} else if (type === vscode.FileType.Directory) {
72+
const subDirectory = joinUri(dir, name);
73+
console.log(`Recursing into ${subDirectory}`);
74+
await deleteFiles(subDirectory, keepDirs, pred);
75+
76+
// remove directory if it is empty now
77+
const isEmptyNow = await vscode.workspace.fs.readDirectory(subDirectory)
78+
.then((contents) => Promise.resolve(contents.length === 0));
79+
if (isEmptyNow) {
80+
await vscode.workspace.fs.delete(subDirectory, {
81+
recursive: false,
82+
useTrash: false,
83+
});
84+
}
6685
}
6786
});
6887
}
@@ -89,21 +108,11 @@ suite('Extension Test Suite', () => {
89108
vscode.window.showInformationMessage('Start all tests.');
90109

91110
suiteSetup(async () => {
92-
await deleteWorkspaceFiles(([fp, type]) => {
93-
if (type === vscode.FileType.Directory && fp === '.vscode') {
94-
return false;
95-
}
96-
if (type === vscode.FileType.Directory && fp === 'bin') {
97-
return false;
98-
}
99-
if (type === vscode.FileType.Directory && fp === (process.platform === 'win32' ? 'ghcup' : '.ghcup')) {
100-
return false;
101-
}
102-
if (type === vscode.FileType.Directory && fp === 'cache') {
103-
return false;
104-
}
105-
return true;
106-
});
111+
await deleteWorkspaceFiles(
112+
[ joinUri(getWorkspaceRoot().uri, '.vscode')
113+
, joinUri(getWorkspaceRoot().uri, 'bin', process.platform === 'win32' ? 'ghcup' : '.ghcup', 'cache')
114+
]
115+
);
107116
await getHaskellConfig().update('logFile', 'hls.log');
108117
await getHaskellConfig().update('trace.server', 'messages');
109118
await getHaskellConfig().update('releasesDownloadStoragePath', path.normalize(getWorkspaceFile('bin').fsPath));
@@ -180,6 +189,6 @@ suite('Extension Test Suite', () => {
180189
console.log(logContent);
181190
}
182191
console.log('Deleting test workspace contents');
183-
await deleteWorkspaceFiles(([name, type]) => !name.includes('.log'));
192+
await deleteWorkspaceFiles([], (name) => !name.includes('.log'));
184193
});
185194
});

0 commit comments

Comments
 (0)