From b40bef48631a07ac8977b090354ece203f9dd7f7 Mon Sep 17 00:00:00 2001 From: Jonathan Clem Date: Wed, 22 May 2019 12:34:57 -0400 Subject: [PATCH 1/2] Use an option for setting `mkdirP`'s max loop size - No need to dirty up the environment - Use a jest pattern for expecting the right error/assertion count --- packages/io/__tests__/io.test.ts | 10 ++++------ packages/io/src/io.ts | 8 ++++++-- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/packages/io/__tests__/io.test.ts b/packages/io/__tests__/io.test.ts index ddaedded65..2bdb5aaac2 100644 --- a/packages/io/__tests__/io.test.ts +++ b/packages/io/__tests__/io.test.ts @@ -807,14 +807,12 @@ describe('mkdirP', () => { '9', '10' ) - process.env['TEST_MKDIRP_FAILSAFE'] = '10' + + expect.assertions(1) + try { - await io.mkdirP(testPath) - throw new Error('directory should not have been created') + await io.mkdirP(testPath, {loopMax: 10}) } catch (err) { - delete process.env['TEST_MKDIRP_FAILSAFE'] - - // ENOENT is expected, all other errors are not expect(err.code).toBe('ENOENT') } }) diff --git a/packages/io/src/io.ts b/packages/io/src/io.ts index a1f44bea04..72cedda2a7 100644 --- a/packages/io/src/io.ts +++ b/packages/io/src/io.ts @@ -99,9 +99,13 @@ export async function rmRF(inputPath: string): Promise { * Will throw if it fails * * @param fsPath path to create + * @param options options for configuring the mkdirP call. For testing. * @returns Promise */ -export async function mkdirP(fsPath: string): Promise { +export async function mkdirP( + fsPath: string, + {loopMax}: {loopMax: number} = {loopMax: 1000} +): Promise { if (!fsPath) { throw new Error('Parameter p is required') } @@ -113,7 +117,7 @@ export async function mkdirP(fsPath: string): Promise { // eslint-disable-next-line no-constant-condition while (true) { // validate the loop is not out of control - if (stack.length >= (process.env['TEST_MKDIRP_FAILSAFE'] || 1000)) { + if (stack.length >= loopMax) { // let the framework throw await ioUtil.mkdir(fsPath) return From 7f1966b00807758030a2a8841fa791570c787bef Mon Sep 17 00:00:00 2001 From: Jonathan Clem Date: Wed, 22 May 2019 12:36:11 -0400 Subject: [PATCH 2/2] Comment is redundant with variable name --- packages/io/src/io.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/io/src/io.ts b/packages/io/src/io.ts index 72cedda2a7..84b7e19df9 100644 --- a/packages/io/src/io.ts +++ b/packages/io/src/io.ts @@ -116,7 +116,6 @@ export async function mkdirP( // eslint-disable-next-line no-constant-condition while (true) { - // validate the loop is not out of control if (stack.length >= loopMax) { // let the framework throw await ioUtil.mkdir(fsPath)