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

[Bug] Text not appended to a file in a mock-fs filesystem when writing to a WritableStream created with the a flag #411

Closed
Arc8ne opened this issue Jan 28, 2025 · 2 comments

Comments

@Arc8ne
Copy link

Arc8ne commented Jan 28, 2025

New text does not get appended to a file in a mock-fs filesystem when the write() function of a writable stream created using the following code is called:

let fileWriteStream = fs.createWriteStream(
    "file",
    {
        flags: "a"
    }
);

The following snippet of code (originally part of a Jest test script) can be used to reproduce this bug:

const fs = require("fs");

const { test, expect } = require("@jest/globals");

const mock = require("mock-fs");

function cleanUp()
{
    mock.restore();
}

test(
    "Append to a file in a 'mock-fs' filesystem",
    async () =>
    {
        mock(
            {
                "test.log": "[Block 1]"
            }
        );

        let testLogFileWriteStream = fs.createWriteStream(
            "test.log",
            {
                flags: "a"
            }
        );

        testLogFileWriteStream.write("[Block 2]");

        let textInTestLogFile = await fs.promises.readFile(
            "test.log",
            {
                encoding: "utf-8"
            }
        );

        try
        {
            expect(textInTestLogFile).toEqual("[Block 1][Block 2]"); // Here, the 'test.log' file contains its initial text (i.e. '[Block 1]') but does not contain the text appended to it (i.e. '[Block 2]') causing this assertion to fail.
        }
        finally
        {
            cleanUp();
        }
    }
);

Is there a way to resolve this bug?

@tschaub
Copy link
Owner

tschaub commented Jan 31, 2025

In #412, I added a test demonstrating the use of fs.createWriteStream() with the append flag:

it('can be used with the append flag', function (done) {
const filename = 'existing';
const output = fs.createWriteStream(filename, {flags: 'a'});
output.on('close', function () {
fs.readFile(filename, function (err, data) {
if (err) {
return done(err);
}
assert.equal(String(data), 'contentmorecontent');
done();
});
});
output.on('error', done);
output.write(Buffer.from('more'));
output.end(Buffer.from('content'));
});

One difference is that the added test calls end on the write stream. Another difference is that it doesn't use Jest. Does your test pass if you call end on the write stream?

@Arc8ne
Copy link
Author

Arc8ne commented Feb 11, 2025

Thanks for letting me know about that. The test passes once I read the content of the file after the write stream's close event is fired.

@Arc8ne Arc8ne closed this as completed Feb 12, 2025
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

2 participants