Skip to content

Commit 4b1afe6

Browse files
committed
1 parent d0d4cbf commit 4b1afe6

File tree

4 files changed

+47
-15
lines changed

4 files changed

+47
-15
lines changed

API.md

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ Available globally
5757

5858
[accessSync](https://nodejs.org/api/fs.html#fsaccesssyncpath-mode)
5959
[mkdirSync](https://nodejs.org/api/fs.html#fsmkdirsyncpath-options)
60+
[mkdtempSync](https://nodejs.org/api/fs.html#fsmkdtempsyncprefix-options)
6061
[readdirSync](https://nodejs.org/api/fs.html#fsreaddirsyncpath-options)
6162

6263
## fs/promises

src/fs/mkdir.rs

+7
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,10 @@ pub async fn mkdtemp(ctx: Ctx<'_>, prefix: String) -> Result<String> {
7575
.or_throw_msg(&ctx, &format!("Can't create dir \"{}\"", &path))?;
7676
Ok(path)
7777
}
78+
79+
pub fn mkdtemp_sync(ctx: Ctx<'_>, prefix: String) -> Result<String> {
80+
let path = format!("{},{}", &prefix, &random_chars(6));
81+
std::fs::create_dir_all(&path)
82+
.or_throw_msg(&ctx, &format!("Can't create dir \"{}\"", &path))?;
83+
Ok(path)
84+
}

src/fs/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use self::read_file::read_file;
2323
use self::rm::{rmdir, rmfile};
2424
use self::stats::{stat_fn, Stat};
2525
use self::write_file::write_file;
26-
use crate::fs::mkdir::{mkdir, mkdir_sync, mkdtemp};
26+
use crate::fs::mkdir::{mkdir, mkdir_sync, mkdtemp, mkdtemp_sync};
2727

2828
pub const CONSTANT_F_OK: u32 = 0;
2929
pub const CONSTANT_R_OK: u32 = 4;
@@ -73,6 +73,7 @@ impl ModuleDef for FsModule {
7373
declare.declare("promises")?;
7474
declare.declare("accessSync")?;
7575
declare.declare("mkdirSync")?;
76+
declare.declare("mkdtempSync")?;
7677
declare.declare("readdirSync")?;
7778

7879
declare.declare("default")?;
@@ -91,6 +92,7 @@ impl ModuleDef for FsModule {
9192
default.set("promises", promises)?;
9293
default.set("accessSync", Func::from(access_sync))?;
9394
default.set("mkdirSync", Func::from(mkdir_sync))?;
95+
default.set("mkdtempSync", Func::from(mkdtemp_sync))?;
9496
default.set("readdirSync", Func::from(read_dir_sync))?;
9597

9698
Ok(())

tests/unit/fs.test.ts

+36-14
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ describe("readdir", () => {
1313
it("should read a directory with types", async () => {
1414
const dir = await fs.readdir(".cargo", { withFileTypes: true });
1515
assert.deepEqual(dir, [{ name: "config.toml" }]);
16-
assert.equal(dir[0].isFile(), true);
16+
expect(dir[0].isFile()).toBeTruthy();
1717
});
1818

1919
it("should read a directory using default import", async () => {
@@ -45,7 +45,7 @@ describe("readdirSync", () => {
4545
it("should read a directory with types synchronously", () => {
4646
const dir = defaultFsImport.readdirSync(".cargo", { withFileTypes: true });
4747
assert.deepEqual(dir, [{ name: "config.toml" }]);
48-
assert.equal(dir[0].isFile(), true);
48+
expect(dir[0].isFile()).toBeTruthy();
4949
});
5050

5151
it("should read a directory using default import synchronously", () => {
@@ -78,11 +78,11 @@ describe("readfile", () => {
7878
const base64Text = buf.toString("base64");
7979
const hexText = buf.toString("hex");
8080

81-
assert.ok(buf instanceof Buffer);
82-
assert.ok(buf instanceof Uint8Array);
83-
assert.equal(text, "hello world!");
84-
assert.equal(base64Text, "aGVsbG8gd29ybGQh");
85-
assert.equal(hexText, "68656c6c6f20776f726c6421");
81+
expect(buf).toBeInstanceOf(Buffer);
82+
expect(buf).toBeInstanceOf(Uint8Array);
83+
expect(text).toEqual("hello world!");
84+
expect(base64Text).toEqual("aGVsbG8gd29ybGQh");
85+
expect(hexText).toEqual("68656c6c6f20776f726c6421");
8686
});
8787
});
8888

@@ -97,14 +97,36 @@ describe("mkdtemp", () => {
9797
.stat(dirPath)
9898
.then(() => true)
9999
.catch(() => false);
100-
assert.ok(dirExists);
100+
expect(dirExists).toBeTruthy();
101101

102102
// Check that the directory has the correct prefix
103103
const dirPrefix = path.basename(dirPath).slice(0, prefix.length);
104-
assert.strictEqual(dirPrefix, prefix);
104+
expect(dirPrefix).toStrictEqual(prefix);
105105

106106
// Clean up the temporary directory
107-
//await fs.rmdir(dirPath);
107+
await fs.rmdir(dirPath);
108+
});
109+
})
110+
111+
describe("mkdtempSync", () => {
112+
it("should create a temporary directory with a given prefix synchronously", async () => {
113+
// Create a temporary directory with the given prefix
114+
const prefix = "test-";
115+
const dirPath = defaultFsImport.mkdtempSync(path.join(os.tmpdir(), prefix));
116+
117+
// Check that the directory exists
118+
const dirExists = await fs
119+
.stat(dirPath)
120+
.then(() => true)
121+
.catch(() => false);
122+
expect(dirExists).toBeTruthy()
123+
124+
// Check that the directory has the correct prefix
125+
const dirPrefix = path.basename(dirPath).slice(0, prefix.length);
126+
expect(dirPrefix).toStrictEqual(prefix)
127+
128+
// Clean up the temporary directory
129+
await fs.rmdir(dirPath);
108130
});
109131
});
110132

@@ -124,7 +146,7 @@ describe("mkdir", () => {
124146
.stat(dirPath)
125147
.then(() => true)
126148
.catch(() => false);
127-
assert.ok(dirExists);
149+
expect(dirExists).toBeTruthy();
128150

129151
// Clean up the directory
130152
await fs.rmdir(dirPath, { recursive: true });
@@ -133,7 +155,7 @@ describe("mkdir", () => {
133155

134156
describe("mkdirSync", () => {
135157
it("should create a directory with the given path synchronously", async () => {
136-
const dirPath = await fs.mkdtemp(path.join(os.tmpdir(), "test/test-"));
158+
const dirPath = defaultFsImport.mkdtempSync(path.join(os.tmpdir(), "test/test-"));
137159

138160
//non recursive should reject
139161
expect(() => defaultFsImport.mkdirSync(dirPath)).toThrow(/[fF]ile.*exists/);
@@ -145,7 +167,7 @@ describe("mkdirSync", () => {
145167
.stat(dirPath)
146168
.then(() => true)
147169
.catch(() => false);
148-
assert.ok(dirExists);
170+
expect(dirExists).toBeTruthy();
149171

150172
// Clean up the directory
151173
await fs.rmdir(dirPath, { recursive: true });
@@ -161,7 +183,7 @@ describe("writeFile", () => {
161183

162184
const contents = (await fs.readFile(filePath)).toString();
163185

164-
assert.equal(fileContents, contents);
186+
expect(fileContents).toEqual(contents);
165187

166188
await fs.rmdir(tmpDir, { recursive: true });
167189
});

0 commit comments

Comments
 (0)