Skip to content

Commit 1878004

Browse files
committed
Make fileSystemHelpers/ensureDirectory ignore only EEXIST error
1 parent e04eab4 commit 1878004

File tree

2 files changed

+35
-20
lines changed

2 files changed

+35
-20
lines changed

src/lib/fileSystemHelpers.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@ const isDirectory = async (path) => {
2424

2525
const ensureDirectory = async (path) => {
2626
try {
27-
return await fs.mkdir(path, { recursive: true })
28-
} catch (e) { } finally {
29-
return Promise.resolve(true)
27+
await fs.mkdir(path, { recursive: true })
28+
} catch (error) {
29+
if (error.code !== 'EEXIST') {
30+
throw error
31+
}
3032
}
3133
}
3234

src/lib/fileSystemHelpers.spec.js

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,7 @@ test('ensureDirectory creates nonexistent directory', async (t) => {
198198
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'test-'))
199199
const newDir = path.join(tempDir, 'newdir')
200200

201-
const result = await ensureDirectory(newDir)
202-
203-
t.ok(result, 'returns true')
201+
await ensureDirectory(newDir)
204202

205203
const stats = await fs.stat(newDir)
206204

@@ -212,14 +210,16 @@ test('ensureDirectory creates nonexistent directory', async (t) => {
212210
await fs.rm(tempDir, { recursive: true })
213211
})
214212

215-
test('ensureDirectory with existing directory', async (t) => {
213+
test('ensureDirectory ignores if directory already exists', async (t) => {
216214
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'test-'))
217215

218-
const result = await ensureDirectory(tempDir)
216+
await ensureDirectory(tempDir)
217+
218+
const stats = await fs.stat(tempDir)
219219

220220
t.ok(
221-
result,
222-
'returns true for existing directory'
221+
stats.isDirectory(),
222+
'does not throw and directory still exists'
223223
)
224224

225225
await fs.rm(tempDir, { recursive: true })
@@ -229,9 +229,7 @@ test('ensureDirectory creates nested directories', async (t) => {
229229
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'test-'))
230230
const nestedPath = path.join(tempDir, 'a', 'b', 'c')
231231

232-
const result = await ensureDirectory(nestedPath)
233-
234-
t.ok(result, 'returns true')
232+
await ensureDirectory(nestedPath)
235233

236234
const stats = await fs.stat(nestedPath)
237235

@@ -243,11 +241,26 @@ test('ensureDirectory creates nested directories', async (t) => {
243241
await fs.rm(tempDir, { recursive: true })
244242
})
245243

246-
test('ensureDirectory returns true on error', async (t) => {
247-
const result = await ensureDirectory(null)
248-
249-
t.ok(
250-
result,
251-
'returns true even when mkdir fails'
252-
)
244+
test('ensureDirectory throws on invalid path parameter', async (t) => {
245+
try {
246+
await ensureDirectory(null)
247+
t.fail('should have thrown error')
248+
} catch (error) {
249+
t.ok(
250+
error instanceof TypeError,
251+
'throws TypeError for null path'
252+
)
253+
}
253254
})
255+
256+
test('ensureDirectory throws on undefined path parameter', async (t) => {
257+
try {
258+
await ensureDirectory(undefined)
259+
t.fail('should have thrown error')
260+
} catch (error) {
261+
t.ok(
262+
error instanceof TypeError,
263+
'throws TypeError for undefined path'
264+
)
265+
}
266+
})

0 commit comments

Comments
 (0)