Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement [mkdtempSync]https://nodejs.org/api/fs.html#fsmkdtempsyncprefix-options) #256

Merged
merged 1 commit into from
Mar 10, 2024
Merged
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
1 change: 1 addition & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ Available globally

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

## fs/promises
Expand Down
7 changes: 7 additions & 0 deletions src/fs/mkdir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,10 @@ pub async fn mkdtemp(ctx: Ctx<'_>, prefix: String) -> Result<String> {
.or_throw_msg(&ctx, &format!("Can't create dir \"{}\"", &path))?;
Ok(path)
}

pub fn mkdtemp_sync(ctx: Ctx<'_>, prefix: String) -> Result<String> {
let path = format!("{},{}", &prefix, &random_chars(6));
std::fs::create_dir_all(&path)
.or_throw_msg(&ctx, &format!("Can't create dir \"{}\"", &path))?;
Ok(path)
}
4 changes: 3 additions & 1 deletion src/fs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use self::read_file::read_file;
use self::rm::{rmdir, rmfile};
use self::stats::{stat_fn, Stat};
use self::write_file::write_file;
use crate::fs::mkdir::{mkdir, mkdir_sync, mkdtemp};
use crate::fs::mkdir::{mkdir, mkdir_sync, mkdtemp, mkdtemp_sync};

pub const CONSTANT_F_OK: u32 = 0;
pub const CONSTANT_R_OK: u32 = 4;
Expand Down Expand Up @@ -73,6 +73,7 @@ impl ModuleDef for FsModule {
declare.declare("promises")?;
declare.declare("accessSync")?;
declare.declare("mkdirSync")?;
declare.declare("mkdtempSync")?;
declare.declare("readdirSync")?;

declare.declare("default")?;
Expand All @@ -91,6 +92,7 @@ impl ModuleDef for FsModule {
default.set("promises", promises)?;
default.set("accessSync", Func::from(access_sync))?;
default.set("mkdirSync", Func::from(mkdir_sync))?;
default.set("mkdtempSync", Func::from(mkdtemp_sync))?;
default.set("readdirSync", Func::from(read_dir_sync))?;

Ok(())
Expand Down
50 changes: 36 additions & 14 deletions tests/unit/fs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe("readdir", () => {
it("should read a directory with types", async () => {
const dir = await fs.readdir(".cargo", { withFileTypes: true });
assert.deepEqual(dir, [{ name: "config.toml" }]);
assert.equal(dir[0].isFile(), true);
expect(dir[0].isFile()).toBeTruthy();
});

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

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

assert.ok(buf instanceof Buffer);
assert.ok(buf instanceof Uint8Array);
assert.equal(text, "hello world!");
assert.equal(base64Text, "aGVsbG8gd29ybGQh");
assert.equal(hexText, "68656c6c6f20776f726c6421");
expect(buf).toBeInstanceOf(Buffer);
expect(buf).toBeInstanceOf(Uint8Array);
expect(text).toEqual("hello world!");
expect(base64Text).toEqual("aGVsbG8gd29ybGQh");
expect(hexText).toEqual("68656c6c6f20776f726c6421");
});
});

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

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

// Clean up the temporary directory
//await fs.rmdir(dirPath);
await fs.rmdir(dirPath);
});
})

describe("mkdtempSync", () => {
it("should create a temporary directory with a given prefix synchronously", async () => {
// Create a temporary directory with the given prefix
const prefix = "test-";
const dirPath = defaultFsImport.mkdtempSync(path.join(os.tmpdir(), prefix));

// Check that the directory exists
const dirExists = await fs
.stat(dirPath)
.then(() => true)
.catch(() => false);
expect(dirExists).toBeTruthy()

// Check that the directory has the correct prefix
const dirPrefix = path.basename(dirPath).slice(0, prefix.length);
expect(dirPrefix).toStrictEqual(prefix)

// Clean up the temporary directory
await fs.rmdir(dirPath);
});
});

Expand All @@ -124,7 +146,7 @@ describe("mkdir", () => {
.stat(dirPath)
.then(() => true)
.catch(() => false);
assert.ok(dirExists);
expect(dirExists).toBeTruthy();

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

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

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

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

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

assert.equal(fileContents, contents);
expect(fileContents).toEqual(contents);

await fs.rmdir(tmpDir, { recursive: true });
});
Expand Down