Skip to content

fix: (fs) rm also removes signal #796

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/clean-baboons-kneel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@solid-primitives/filesystem": patch
---

fix: removing a file deletes its signal correctly
4 changes: 4 additions & 0 deletions packages/filesystem/src/reactive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ export const createSyncFileSystem = (
getTypeMap.delete(item);
}
});
if (readFileMap.has(path)) {
readFileMap.get(path)?.[1](undefined);
readFileMap.delete(path);
}
readdirMap.get(getParentDir(path))?.[1](
(items = []) => items.filter(item => item === getItemName(path)) as [] | DirEntries,
);
Expand Down
25 changes: 24 additions & 1 deletion packages/filesystem/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
makeTauriFileSystem,
rsync,
} from "../src/index.js";
import { createEffect, createRoot } from "solid-js";
import { catchError, createEffect, createRoot } from "solid-js";

describe("makeNoFileSystem", () => {
const fs = makeNoFileSystem();
Expand Down Expand Up @@ -51,6 +51,15 @@ describe("makeVirtualFileSystem", () => {
});
test("fs.readFile returns file content", () =>
expect(fs.readFile("src/test.ts")).toBe("// test"));
test("fs.readFile throws on attempting to read a directory as file", () => {
expect(() => fs.readFile("src")).toThrow('"src" is not a file');
});
test("fs.readFile throws on attempting to read a non-existent file", () => {
expect(() => fs.readFile("src/nonexistent.ts")).toThrow('"src/nonexistent.ts" is not a file');
});
test("fs.readFile throws on attempting to read from a non-existing directory", () => {
expect(() => fs.readFile("nonexistent/test.ts")).toThrow('"nonexistent" is not a directory')
});
test("fs.writeFile creates and overwrites file", () => {
expect(fs.readdir("src")).toHaveLength(1);
fs.writeFile("src/test2.ts", "// data");
Expand Down Expand Up @@ -114,6 +123,20 @@ describe("createFileSystem (sync) calls the underlying fs", () => {
});
});

describe("createFileSystem (sync) relays file system errors", () => {
test("a deleted file stored in a signal throws an error", () => new Promise<void>((done, fail) => {
setTimeout(() => fail(new Error('did not throw')), 100);
const fs = createFileSystem(makeVirtualFileSystem({ 'test.json': '{}' }));
catchError(() => {
createEffect(() => fs.readFile("test.json"));
setTimeout(() => fs.rm("test.json"), 30);
}, (error) => {
expect(error).toEqual(new Error('"test.json" is not a file'));
done();
});
}));
});

describe("createFileSystem (async) calls the underlying fs", () => {
const afs = makeNoAsyncFileSystem();
instrumentFs(afs);
Expand Down
Loading