Skip to content

Commit ca6217f

Browse files
authored
cli: add retry to removing files after test (#259)
Co-authored-by: Sebastian Alex <[email protected]>
1 parent 8e6fd3e commit ca6217f

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

tools/cli/tests/_helpers/testFiles.ts

+23-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import crypto, { randomBytes } from 'crypto';
33
import fs from 'fs';
44
import fsExtra from 'fs-extra';
55
import path from 'path';
6+
import { promisify } from 'util';
67

78
export type TestFiles =
89
| 'no-sourcemaps'
@@ -42,10 +43,31 @@ export function withWorkingCopy(sources: TestFiles | TestFiles[], fn: (path: str
4243
try {
4344
await fn(workingCopy);
4445
} finally {
45-
fs.rmSync(workingCopy, { recursive: true, force: true });
46+
await retry(() => fs.rmSync(workingCopy, { recursive: true, force: true }));
4647
}
4748
};
4849
}
50+
51+
function retry<T>(fn: () => T | Promise<T>, retries = 5, delay = 100, factor = 2) {
52+
const wait = promisify(setTimeout);
53+
54+
const retry = async (fn: () => T | Promise<T>, retries: number, delay: number): Promise<T> => {
55+
try {
56+
return await fn();
57+
} catch (err) {
58+
if (retries === 0) {
59+
throw err;
60+
}
61+
62+
console.error(`retry failed, ${retries} retries left, waiting for ${delay}`);
63+
await wait(delay);
64+
return retry(fn, retries - 1, delay * factor);
65+
}
66+
};
67+
68+
return retry(fn, retries, delay);
69+
}
70+
4971
export async function hashFiles(files: string[]) {
5072
const hashes = await Promise.all(files.map(hashFile));
5173

0 commit comments

Comments
 (0)