Skip to content

Commit 865a77f

Browse files
authored
Reject renaming a file over a directory (#23)
1 parent cf6854f commit 865a77f

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

‎src/fs.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ export class MemoryBackend implements Backend {
256256

257257
rename(from: string, to: string) {
258258
const f = this.files.get(from);
259-
if (!f || !this.dirs.has(parentOf(to))) return false;
259+
if (!f || this.dirs.has(to) || !this.dirs.has(parentOf(to))) return false;
260260
this.files.delete(from);
261261
this.files.set(to, f);
262262
this.changed(from, null);

‎test/fs.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { test } from "node:test";
2+
import assert from "node:assert/strict";
3+
import { MemoryBackend } from "../src/fs.ts";
4+
5+
test("renaming a file onto a directory leaves both intact", () => {
6+
const fs = new MemoryBackend();
7+
fs.mkdir("/dir");
8+
fs.write("/file", new Uint8Array([1]));
9+
fs.write("/dir/child", new Uint8Array([2]));
10+
11+
assert.equal(fs.rename("/file", "/dir"), false);
12+
assert.deepEqual(fs.read("/file"), new Uint8Array([1]));
13+
assert.equal(fs.stat("/dir")?.dir, true);
14+
assert.deepEqual(fs.list("/dir"), ["child"]);
15+
16+
fs.write("/other", new Uint8Array([3]));
17+
assert.equal(fs.rename("/file", "/other"), true);
18+
assert.deepEqual(fs.read("/other"), new Uint8Array([1]));
19+
});

0 commit comments

Comments
 (0)