Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions packages/io/__tests__/io.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}
})
Expand Down
9 changes: 6 additions & 3 deletions packages/io/src/io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,13 @@ export async function rmRF(inputPath: string): Promise<void> {
* Will throw if it fails
*
* @param fsPath path to create
* @param options options for configuring the mkdirP call. For testing.
* @returns Promise<void>
*/
export async function mkdirP(fsPath: string): Promise<void> {
export async function mkdirP(
fsPath: string,
{loopMax}: {loopMax: number} = {loopMax: 1000}
): Promise<void> {
if (!fsPath) {
throw new Error('Parameter p is required')
}
Expand All @@ -112,8 +116,7 @@ export async function mkdirP(fsPath: string): Promise<void> {

// 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
Expand Down