Skip to content

Commit 824577c

Browse files
fix: mkdir path resolve (#841)
* Fix mkdir path resolve * remove unused import * Clippy
1 parent b4735e0 commit 824577c

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

modules/llrt_fs/src/mkdir.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33
use crate::chmod::{set_mode, set_mode_sync};
4+
5+
use llrt_path::resolve_path;
46
use llrt_utils::result::ResultExt;
57
use ring::rand::{SecureRandom, SystemRandom};
68
use rquickjs::{function::Opt, Ctx, Object, Result};
79
use tokio::fs;
810

911
pub async fn mkdir<'js>(ctx: Ctx<'js>, path: String, options: Opt<Object<'js>>) -> Result<String> {
10-
let (recursive, mode) = get_params(options);
12+
let (recursive, mode, path) = get_params(&path, options)?;
1113

1214
if recursive {
1315
fs::create_dir_all(&path).await
@@ -22,7 +24,7 @@ pub async fn mkdir<'js>(ctx: Ctx<'js>, path: String, options: Opt<Object<'js>>)
2224
}
2325

2426
pub fn mkdir_sync<'js>(ctx: Ctx<'js>, path: String, options: Opt<Object<'js>>) -> Result<String> {
25-
let (recursive, mode) = get_params(options);
27+
let (recursive, mode, path) = get_params(&path, options)?;
2628

2729
if recursive {
2830
std::fs::create_dir_all(&path)
@@ -36,15 +38,16 @@ pub fn mkdir_sync<'js>(ctx: Ctx<'js>, path: String, options: Opt<Object<'js>>) -
3638
Ok(path)
3739
}
3840

39-
fn get_params(options: Opt<Object>) -> (bool, u32) {
41+
fn get_params(path: &str, options: Opt<Object>) -> Result<(bool, u32, String)> {
4042
let mut recursive = false;
4143
let mut mode = 0o777;
4244

4345
if let Some(options) = options.0 {
4446
recursive = options.get("recursive").unwrap_or_default();
4547
mode = options.get("mode").unwrap_or(0o777);
4648
}
47-
(recursive, mode)
49+
let path = resolve_path([path])?;
50+
Ok((recursive, mode, path))
4851
}
4952

5053
const CHARS: &[u8] = b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

tests/unit/fs.test.ts

+17-4
Original file line numberDiff line numberDiff line change
@@ -220,13 +220,26 @@ describe("mkdir", () => {
220220

221221
await fs.mkdir(dirPath, { recursive: true });
222222

223+
// Helper function to check if directory exists
224+
const checkDirExists = async (dirPath: string) => {
225+
return await fs
226+
.stat(dirPath)
227+
.then(() => true)
228+
.catch(() => false);
229+
};
230+
223231
// Check that the directory exists
224-
const dirExists = await fs
225-
.stat(dirPath)
226-
.then(() => true)
227-
.catch(() => false);
232+
const dirExists = await checkDirExists(dirPath);
228233
expect(dirExists).toBeTruthy();
229234

235+
await fs.rmdir(dirPath, { recursive: true });
236+
237+
await fs.mkdir(`${dirPath}/./`, { recursive: true });
238+
239+
// Check that the directory exists
240+
const dirExists2 = await checkDirExists(dirPath);
241+
expect(dirExists2).toBeTruthy();
242+
230243
// Clean up the directory
231244
await fs.rmdir(dirPath, { recursive: true });
232245
});

0 commit comments

Comments
 (0)