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

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

Open
regseb opened this issue Nov 28, 2023 · 0 comments
Open

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

regseb opened this issue Nov 28, 2023 · 0 comments

Comments

@regseb
Copy link
Contributor

regseb commented Nov 28, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant