From 9f3f840120099f9427b1e91d18c459bbc76d8089 Mon Sep 17 00:00:00 2001 From: Vallie Joseph Date: Tue, 14 Mar 2023 03:35:11 +0000 Subject: [PATCH] adding tests --- packages/io/__tests__/io.test.ts | 32 +++++++++++++++++++------------- packages/io/src/io-util.ts | 3 +-- packages/io/src/io.ts | 5 +++-- 3 files changed, 23 insertions(+), 17 deletions(-) diff --git a/packages/io/__tests__/io.test.ts b/packages/io/__tests__/io.test.ts index e897b39b..3fd1c64a 100644 --- a/packages/io/__tests__/io.test.ts +++ b/packages/io/__tests__/io.test.ts @@ -3,7 +3,7 @@ import {promises as fs} from 'fs' import * as os from 'os' import * as path from 'path' import * as io from '../src/io' -// import * as ioUtil from '../src/io-util' +import * as ioUtil from '../src/io-util' describe('cp', () => { beforeEach(async () => { @@ -343,20 +343,26 @@ describe('rmRF', () => { await fs.appendFile(filePath, 'some data') await assertExists(filePath) - const fd = await fs.open(filePath, 'r') + // we need to open the file with an explicit executive lock + // otherwise node will allow the file to be deleted even though it's open + // https://github.com/nodejs/node/blob/c2e4b1fa9ad0b744616c4e4c13a5017772a630c4/deps/uv/src/win/fs.c#L499-L513 + const fd = await fs.open( + filePath, + fs.constants.O_RDONLY | ioUtil.UV_FS_O_EXLOCK + ) // // can't remove folder with locked file on windows - // if (ioUtil.IS_WINDOWS) { - // try { - // // additionally, can't stat an open file on Windows without getting EPERM - // await io.rmRF(testPath) - // } catch (err) { - // expect(err.code).toBe('EPERM') - // } - // } else { - await io.rmRF(testPath) - await assertNotExists(testPath) - // } + if (ioUtil.IS_WINDOWS) { + try { + // additionally, can't stat an open file on Windows without getting EPERM + await io.rmRF(testPath) + } catch (err) { + expect(err.code).toBe('EPERM') + } + } else { + await io.rmRF(testPath) + await assertNotExists(testPath) + } await fd.close() await io.rmRF(testPath) diff --git a/packages/io/src/io-util.ts b/packages/io/src/io-util.ts index 50a8d6c6..cb4a2c46 100644 --- a/packages/io/src/io-util.ts +++ b/packages/io/src/io-util.ts @@ -17,9 +17,8 @@ export const { unlink } = fs.promises -export const {rmSync} = fs - export const IS_WINDOWS = process.platform === 'win32' +export const UV_FS_O_EXLOCK = 0x10000000 export async function exists(fsPath: string): Promise { try { diff --git a/packages/io/src/io.ts b/packages/io/src/io.ts index 077c973c..29353aa2 100644 --- a/packages/io/src/io.ts +++ b/packages/io/src/io.ts @@ -119,8 +119,9 @@ export async function rmRF(inputPath: string): Promise { ) } } - - ioUtil.rmSync(inputPath, { + // eslint-disable-next-line no-console + console.debug(`Version: ${process.version}`) + ioUtil.rm(inputPath, { force: true, maxRetries: 3, recursive: true,