Skip to content

Commit

Permalink
feat(runtime): add stat and statSync methods to Deno.File (denoland#1…
Browse files Browse the repository at this point in the history
  • Loading branch information
caspervonb committed Apr 12, 2021
1 parent 5c2a8cd commit da92193
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
2 changes: 2 additions & 0 deletions cli/dts/lib.deno.ns.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,8 @@ declare namespace Deno {
readSync(p: Uint8Array): number | null;
seek(offset: number, whence: SeekMode): Promise<number>;
seekSync(offset: number, whence: SeekMode): number;
stat(): Promise<FileInfo>;
statSync(): FileInfo;
close(): void;
}

Expand Down
32 changes: 32 additions & 0 deletions cli/tests/unit/file_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,35 @@ unitTest(function fileUsingNumberFileName(): void {
unitTest(function fileUsingEmptyStringFileName(): void {
testSecondArgument("", "");
});

unitTest({ perms: { read: true } }, function fileStatSyncSuccess(): void {
const file = Deno.openSync("README.md");
const fileInfo = file.statSync();
assert(fileInfo.isFile);
assert(!fileInfo.isSymlink);
assert(!fileInfo.isDirectory);
assert(fileInfo.size);
assert(fileInfo.atime);
assert(fileInfo.mtime);
// The `birthtime` field is not available on Linux before kernel version 4.11.
assert(fileInfo.birthtime || Deno.build.os === "linux");

file.close();
});

unitTest({ perms: { read: true } }, async function fileStatSuccess(): Promise<
void
> {
const file = await Deno.open("README.md");
const fileInfo = await file.stat();
assert(fileInfo.isFile);
assert(!fileInfo.isSymlink);
assert(!fileInfo.isDirectory);
assert(fileInfo.size);
assert(fileInfo.atime);
assert(fileInfo.mtime);
// The `birthtime` field is not available on Linux before kernel version 4.11.
assert(fileInfo.birthtime || Deno.build.os === "linux");

file.close();
});
4 changes: 2 additions & 2 deletions runtime/js/30_fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -390,8 +390,6 @@
removeSync,
renameSync,
rename,
fstatSync,
fstat,
lstat,
lstatSync,
stat,
Expand All @@ -403,6 +401,8 @@
umask,
link,
linkSync,
fstatSync,
fstat,
futime,
futimeSync,
utime,
Expand Down
9 changes: 9 additions & 0 deletions runtime/js/30_files.js → runtime/js/40_files.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
((window) => {
const core = window.Deno.core;
const { read, readSync, write, writeSync } = window.__bootstrap.io;
const { fstat, fstatSync } = window.__bootstrap.fs;
const { pathFromURL } = window.__bootstrap.util;

function seekSync(
Expand Down Expand Up @@ -103,6 +104,14 @@
return seekSync(this.rid, offset, whence);
}

stat() {
return fstat(this.rid);
}

statSync() {
return fstatSync(this.rid);
}

close() {
core.close(this.rid);
}
Expand Down

0 comments on commit da92193

Please sign in to comment.