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])); +});