Skip to content

Commit

Permalink
Return null in Switch.readFile() and Switch.stat() if file does…
Browse files Browse the repository at this point in the history
… not exist
  • Loading branch information
TooTallNate committed Mar 5, 2024
1 parent ef38829 commit b58f783
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 32 deletions.
5 changes: 5 additions & 0 deletions .changeset/beige-eels-share.md
@@ -0,0 +1,5 @@
---
"nxjs-runtime": patch
---

Return `null` in `Switch.readFile()` and `Switch.stat()` if file does not exist
33 changes: 9 additions & 24 deletions apps/tests/src/switch.ts
Expand Up @@ -81,24 +81,14 @@ test('`Switch.readFile()` works with URL path', async () => {
assert.ok(data.byteLength > 0);
});

test('`Switch.readFile()` rejects when file does not exist', async () => {
let err: Error | undefined;
try {
await Switch.readFile('romfs:/__does_not_exist__');
} catch (_err) {
err = _err as Error;
}
assert.ok(err);
test('`Switch.readFile()` returns null when file does not exist', async () => {
const data = await Switch.readFile('romfs:/__does_not_exist__');
assert.equal(data, null);
});

test('`Switch.readFile()` rejects when attempting to read a directory', async () => {
let err: Error | undefined;
try {
await Switch.readFile('.');
} catch (_err) {
err = _err as Error;
}
assert.ok(err);
test('`Switch.readFile()` returns null when attempting to read a directory', async () => {
const data = await Switch.readFile('.');
assert.equal(data, null);
});

test('`Switch.stat()` returns file information', async () => {
Expand All @@ -107,14 +97,9 @@ test('`Switch.stat()` returns file information', async () => {
assert.ok(stat.size > 0);
});

test('`Switch.stat()` rejects when file does not exist', async () => {
let err: Error | undefined;
try {
await Switch.stat('romfs:/__does_not_exist__');
} catch (_err) {
err = _err as Error;
}
assert.ok(err);
test('`Switch.stat()` returns `null` when file does not exist', async () => {
const stat = await Switch.stat('romfs:/__does_not_exist__');
assert.equal(stat, null);
});

test("`Switch.removeSync()` doesn't throw when path does not exist", async () => {
Expand Down
23 changes: 15 additions & 8 deletions source/fs.c
Expand Up @@ -252,14 +252,18 @@ void nx_read_file_cb(JSContext *ctx, nx_work_t *req, JSValue *args)
nx_fs_read_file_async_t *data = (nx_fs_read_file_async_t *)req->data;
JS_FreeCString(ctx, data->filename);

if (data->err)
if (data->err == ENOENT)
{
args[1] = JS_NULL;
}
else if (data->err)
{
args[0] = JS_NewError(ctx);
JS_DefinePropertyValueStr(ctx, args[0], "message", JS_NewString(ctx, strerror(data->err)), JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
return;
}

args[1] = JS_NewArrayBuffer(ctx, data->result, data->size, free_array_buffer, NULL, false);
else
{
args[1] = JS_NewArrayBuffer(ctx, data->result, data->size, free_array_buffer, NULL, false);
}
}

JSValue nx_read_file(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
Expand Down Expand Up @@ -387,14 +391,17 @@ void nx_stat_cb(JSContext *ctx, nx_work_t *req, JSValue *args)
nx_fs_stat_async_t *data = (nx_fs_stat_async_t *)req->data;
JS_FreeCString(ctx, data->filename);

if (data->err)
if (data->err == ENOENT) {
args[1] = JS_NULL;
}
else if (data->err)
{
args[0] = JS_NewError(ctx);
JS_DefinePropertyValueStr(ctx, args[0], "message", JS_NewString(ctx, strerror(data->err)), JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
return;
} else {
args[1] = statToObject(ctx, &data->st);
}

args[1] = statToObject(ctx, &data->st);
}

JSValue nx_stat(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
Expand Down

0 comments on commit b58f783

Please sign in to comment.