diff --git a/lib/temp-directory.js b/lib/temp-directory.js index 91266885..6b00490d 100644 --- a/lib/temp-directory.js +++ b/lib/temp-directory.js @@ -3,6 +3,8 @@ import { promises as fs } from 'fs'; import { randomUUID } from 'crypto'; import { tmpdir } from 'os'; +import { removeDirectory } from './utils.js'; + export async function create(context) { if (context.options && context.options.tmpDir) { context.path = join(context.options.tmpDir, randomUUID()); @@ -33,12 +35,7 @@ export let remove = async function remove(context) { `${context.module.name} rm.tempdir`, context.path ); - await fs.rm(context.path, { - recursive: true, - force: true, - maxRetries: 10, - retryDelay: 10 - }); + await removeDirectory(context.path); }; // Used in tests to simulate errors in rimraf. diff --git a/lib/utils.js b/lib/utils.js new file mode 100644 index 00000000..4d28d20b --- /dev/null +++ b/lib/utils.js @@ -0,0 +1,14 @@ +import fs from 'node:fs/promises'; + +/** + * Remove directory recursively with retries for Windows. + * @param path + */ +export async function removeDirectory(path) { + await fs.rm(path, { + recursive: true, + force: true, + maxRetries: 10, + retryDelay: 50 + }); +} diff --git a/test/npm/test-npm-install.js b/test/npm/test-npm-install.js index f3f6bb28..a8d55f53 100644 --- a/test/npm/test-npm-install.js +++ b/test/npm/test-npm-install.js @@ -7,6 +7,7 @@ import tap from 'tap'; import { getPackageManagers } from '../../lib/package-manager/index.js'; import packageManagerInstall from '../../lib/package-manager/install.js'; +import { removeDirectory } from '../../lib/utils.js'; import { npmContext } from '../helpers/make-context.js'; const { test } = tap; @@ -103,10 +104,5 @@ test('npm-install: failed install', async (t) => { }); tap.teardown(async () => { - await fs.rm(sandbox, { - recursive: true, - force: true, - maxRetries: 10, - retryDelay: 10 - }); + await removeDirectory(sandbox); }); diff --git a/test/npm/test-npm-test.js b/test/npm/test-npm-test.js index 4a2938d0..096cd865 100644 --- a/test/npm/test-npm-test.js +++ b/test/npm/test-npm-test.js @@ -5,9 +5,10 @@ import { existsSync, promises as fs } from 'fs'; import tap from 'tap'; -import { npmContext } from '../helpers/make-context.js'; import { getPackageManagers } from '../../lib/package-manager/index.js'; import { test as packageManagerTest } from '../../lib/package-manager/test.js'; +import { removeDirectory } from '../../lib/utils.js'; +import { npmContext } from '../helpers/make-context.js'; const { test } = tap; const __dirname = dirname(fileURLToPath(import.meta.url)); @@ -185,10 +186,5 @@ test('npm-test: tmpdir is redirected', async (t) => { }); tap.teardown(async () => { - await fs.rm(sandbox, { - recursive: true, - force: true, - maxRetries: 10, - retryDelay: 10 - }); + await removeDirectory(sandbox); }); diff --git a/test/pnpm/test-pnpm-install.js b/test/pnpm/test-pnpm-install.js index 70d74c54..c1f5a768 100644 --- a/test/pnpm/test-pnpm-install.js +++ b/test/pnpm/test-pnpm-install.js @@ -7,6 +7,7 @@ import tap from 'tap'; import { getPackageManagers } from '../../lib/package-manager/index.js'; import packageManagerInstall from '../../lib/package-manager/install.js'; +import { removeDirectory } from '../../lib/utils.js'; import { npmContext } from '../helpers/make-context.js'; const { test } = tap; @@ -79,10 +80,5 @@ test('pnpm-install: failed install', async (t) => { }); tap.teardown(async () => { - await fs.rm(sandbox, { - recursive: true, - force: true, - maxRetries: 10, - retryDelay: 10 - }); + await removeDirectory(sandbox); }); diff --git a/test/pnpm/test-pnpm-test.js b/test/pnpm/test-pnpm-test.js index b7860d9c..6dadd54b 100644 --- a/test/pnpm/test-pnpm-test.js +++ b/test/pnpm/test-pnpm-test.js @@ -5,9 +5,10 @@ import { fileURLToPath } from 'url'; import tap, { test } from 'tap'; -import { npmContext } from '../helpers/make-context.js'; import { getPackageManagers } from '../../lib/package-manager/index.js'; import { test as packageManagerTest } from '../../lib/package-manager/test.js'; +import { removeDirectory } from '../../lib/utils.js'; +import { npmContext } from '../helpers/make-context.js'; const __dirname = dirname(fileURLToPath(import.meta.url)); @@ -181,10 +182,5 @@ test('pnpm-test: tmpdir is redirected', async (t) => { }); tap.teardown(async () => { - await fs.rm(sandbox, { - recursive: true, - force: true, - maxRetries: 10, - retryDelay: 10 - }); + await removeDirectory(sandbox); }); diff --git a/test/reporter/test-reporter-junit.js b/test/reporter/test-reporter-junit.js index 2ceaeaf8..d9d5ac7b 100644 --- a/test/reporter/test-reporter-junit.js +++ b/test/reporter/test-reporter-junit.js @@ -9,6 +9,7 @@ import _ from 'lodash'; import xml2js from 'xml2js'; import junitReporter from '../../lib/reporter/junit.js'; +import { removeDirectory } from '../../lib/utils.js'; const { test } = tap; const parseString = promisify(xml2js.parseString); @@ -154,10 +155,5 @@ test('reporter.junit(): append to disk', (t) => { }); tap.teardown(async () => { - await fs.rm(sandbox, { - recursive: true, - force: true, - maxRetries: 10, - retryDelay: 10 - }); + await removeDirectory(sandbox); }); diff --git a/test/reporter/test-reporter-tap.js b/test/reporter/test-reporter-tap.js index 594fd253..8f033910 100644 --- a/test/reporter/test-reporter-tap.js +++ b/test/reporter/test-reporter-tap.js @@ -10,6 +10,7 @@ import { Parser } from 'tap-parser'; import str from 'string-to-stream'; import tapReporter from '../../lib/reporter/tap.js'; +import { removeDirectory } from '../../lib/utils.js'; const fixtures = JSON.parse( readFileSync(new URL('../fixtures/reporter-fixtures.json', import.meta.url)) @@ -132,10 +133,5 @@ test('reporter.tap(): append to disk when file does not exist', (t) => { }); tap.teardown(async () => { - await fs.rm(sandbox, { - recursive: true, - force: true, - maxRetries: 10, - retryDelay: 10 - }); + await removeDirectory(sandbox); }); diff --git a/test/test-grab-project.js b/test/test-grab-project.js index 6d05345e..4f6f6199 100644 --- a/test/test-grab-project.js +++ b/test/test-grab-project.js @@ -10,6 +10,7 @@ import { promises as fs } from 'fs'; import tap from 'tap'; import { grabProject } from '../lib/grab-project.js'; +import { removeDirectory } from '../lib/utils.js'; const { test } = tap; @@ -170,10 +171,5 @@ test('grab-project: timeout', async (t) => { }); tap.teardown(async () => { - await fs.rm(sandbox, { - recursive: true, - force: true, - maxRetries: 10, - retryDelay: 10 - }); + await removeDirectory(sandbox); }); diff --git a/test/yarn/test-yarn-install.js b/test/yarn/test-yarn-install.js index 399d8a76..6158fd8a 100644 --- a/test/yarn/test-yarn-install.js +++ b/test/yarn/test-yarn-install.js @@ -7,6 +7,7 @@ import tap from 'tap'; import { getPackageManagers } from '../../lib/package-manager/index.js'; import packageManagerInstall from '../../lib/package-manager/install.js'; +import { removeDirectory } from '../../lib/utils.js'; import { npmContext } from '../helpers/make-context.js'; const { test } = tap; @@ -81,10 +82,5 @@ test('yarn-install: failed install', async (t) => { }); tap.teardown(async () => { - await fs.rm(sandbox, { - recursive: true, - force: true, - maxRetries: 10, - retryDelay: 10 - }); + await removeDirectory(sandbox); }); diff --git a/test/yarn/test-yarn-test.js b/test/yarn/test-yarn-test.js index deded643..31ae93c9 100644 --- a/test/yarn/test-yarn-test.js +++ b/test/yarn/test-yarn-test.js @@ -8,6 +8,7 @@ import tap, { test } from 'tap'; import { npmContext } from '../helpers/make-context.js'; import { getPackageManagers } from '../../lib/package-manager/index.js'; import { test as packageManagerTest } from '../../lib/package-manager/test.js'; +import { removeDirectory } from '../../lib/utils.js'; const __dirname = dirname(fileURLToPath(import.meta.url)); @@ -181,10 +182,5 @@ test('yarn-test: tmpdir is redirected', async (t) => { }); tap.teardown(async () => { - await fs.rm(sandbox, { - recursive: true, - force: true, - maxRetries: 10, - retryDelay: 10 - }); + await removeDirectory(sandbox); });