Skip to content

[fs-monkey] Doesn't patch readFile promise #970

Description

@regseb

With this code:

import fs, { readFile, readFileSync } from "node:fs";
import fsPromises, { readFile as readFilePromise } from "node:fs/promises";
import { vol } from "memfs";
import { patchFs } from "fs-monkey";

vol.fromJSON({ "foo": "bar" });
patchFs(vol);

const test = (fn) => {
    fn((err, data) => {
        if (err) {
            console.log(fn.toString().slice(8), "ERR");
        } else {
            console.log(fn.toString().slice(8), "OK");
        }
    });
};

const testSync = (fn) => {
    try {
        const data = fn();
        console.log(fn.toString().slice(6), "OK");
    } catch (err) {
        console.log(fn.toString().slice(6), "ERR");
    }
};

const testAsync = async (fn) => {
    try {
        const data = await fn();
        console.log(fn.toString().slice(6), "OK");
    } catch (err) {
        console.log(fn.toString().slice(6), "ERR");
    }
};

test((cb) => fs.readFile("foo", "utf8", cb));
test((cb) => fs.readFile("foo", cb));
test((cb) => readFile("foo", "utf8", cb));
test((cb) => readFile("foo", cb));
testSync(() => fs.readFileSync("foo", "utf8"));
testSync(() => fs.readFileSync("foo"));
testSync(() => readFileSync("foo", "utf8"));
testSync(() => readFileSync("foo"));
await testAsync(() => fs.promises.readFile("foo", "utf8"));
await testAsync(() => fs.promises.readFile("foo"));
await testAsync(() => fsPromises.readFile("foo", "utf8"));
await testAsync(() => fsPromises.readFile("foo"));
await testAsync(() => readFilePromise("foo", "utf8"));
await testAsync(() => readFilePromise("foo"));

Only fs.readFile() and fs.readFileSync() work.

Function Result
fs.readFile("foo", "utf8", cb)
fs.readFile("foo", cb)
readFile("foo", "utf8", cb)
readFile("foo", cb)
fs.readFileSync("foo", "utf8")
fs.readFileSync("foo")
readFileSync("foo", "utf8")
readFileSync("foo")
fs.promises.readFile("foo", "utf8")
fs.promises.readFile("foo")
fsPromises.readFile("foo", "utf8")
fsPromises.readFile("foo")
readFilePromise("foo", "utf8")
readFilePromise("foo")

I tested with and without "utf8" because the behavior can be different: nodejs/node#48658

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions