From ab747db1cc68d143ac55794202276b5a4a0b3eb4 Mon Sep 17 00:00:00 2001 From: Project516 <138796702+Project516@users.noreply.github.com> Date: Mon, 2 Nov 2026 18:57:18 -0600 Subject: [PATCH] Reject renaming a file over a directory --- src/fs.ts | 2 +- test/fs.test.ts | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 test/fs.test.ts diff --git a/src/fs.ts b/src/fs.ts index fb352e0..287840a 100644 --- a/src/fs.ts +++ b/src/fs.ts @@ -256,7 +256,7 @@ export class MemoryBackend implements Backend { rename(from: string, to: string) { const f = this.files.get(from); - if (!f || !this.dirs.has(parentOf(to))) return false; + if (!f || this.dirs.has(to) || !this.dirs.has(parentOf(to))) return false; this.files.delete(from); this.files.set(to, f); this.changed(from, null); diff --git a/test/fs.test.ts b/test/fs.test.ts new file mode 100644 index 0000000..f1ad084 --- /dev/null +++ b/test/fs.test.ts @@ -0,0 +1,19 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { MemoryBackend } from "../src/fs.ts"; + +test("renaming a file onto a directory leaves both intact", () => { + const fs = new MemoryBackend(); + fs.mkdir("/dir"); + fs.write("/file", new Uint8Array([1])); + fs.write("/dir/child", new Uint8Array([2])); + + assert.equal(fs.rename("/file", "/dir"), false); + assert.deepEqual(fs.read("/file"), new Uint8Array([1])); + assert.equal(fs.stat("/dir")?.dir, true); + assert.deepEqual(fs.list("/dir"), ["child"]); + + fs.write("/other", new Uint8Array([3])); + assert.equal(fs.rename("/file", "/other"), true); + assert.deepEqual(fs.read("/other"), new Uint8Array([1])); +});