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

perf(node/fs): faster existsSync when not exists #21458

Merged
merged 7 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add tests.
  • Loading branch information
dsherret committed Dec 4, 2023
commit ac0cdf3a29af2b04568158741923ef4ae114732b
41 changes: 40 additions & 1 deletion cli/tests/unit_node/fs_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import { assert, assertThrows } from "../../../test_util/std/assert/mod.ts";
import { join } from "node:path";
import { tmpdir } from "node:os";
import { mkdtempSync, readFileSync, writeFileSync } from "node:fs";
import { existsSync, mkdtempSync, readFileSync, writeFileSync } from "node:fs";
import { pathToAbsoluteFileUrl } from "../unit/test_util.ts";

Deno.test(
"[node/fs writeFileSync] write file without option",
Expand Down Expand Up @@ -43,3 +44,41 @@ Deno.test(
);
},
);

Deno.test(
"[node/fs existsSync] path",
{ permissions: { read: true } },
function () {
assert(existsSync("cli/tests/testdata/assets/fixture.json"));
},
);

Deno.test(
"[node/fs existsSync] url",
{ permissions: { read: true } },
function () {
assert(existsSync(
pathToAbsoluteFileUrl("cli/tests/testdata/assets/fixture.json"),
));
},
);

Deno.test(
"[node/fs existsSync] no permission",
{ permissions: { read: false } },
function () {
assertThrows(() => {
existsSync("cli/tests/testdata/assets/fixture.json");
}, Deno.errors.PermissionDenied);
},
);

Deno.test(
"[node/fs existsSync] not exists",
{ permissions: { read: true } },
function () {
assertThrows(() => {
Deno.readTextFileSync("bad_filename");
dsherret marked this conversation as resolved.
Show resolved Hide resolved
}, Deno.errors.NotFound);
},
);
3 changes: 2 additions & 1 deletion ext/fs/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,8 @@ where
let path = PathBuf::from(path);
state
.borrow_mut::<P>()
.check_read(&path, "Deno.existsSync()")?;
// currently only used by node:fs
.check_read(&path, "node:fs.existsSync()")?;
dsherret marked this conversation as resolved.
Show resolved Hide resolved
let fs = state.borrow::<FileSystemRc>();
Ok(fs.lstat_sync(&path).is_ok())
}
Expand Down