Drop the lock file: too much machinery for a personal CLI
Concurrent runs of the CLI can again lose an update; saves stay atomic. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
dab492da1e
commit
130ffe0d13
3 files changed
+2
-50
No files matched your search
+2
-26
@@ -1,7 +1,7 @@
|
||||
import { test } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { spawnSync, spawn } from "node:child_process";
|
||||
import { mkdtempSync, writeFileSync, readFileSync, existsSync, chmodSync, utimesSync } from "node:fs";
|
||||
import { spawnSync } from "node:child_process";
|
||||
import { mkdtempSync, writeFileSync, readFileSync, existsSync, chmodSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join, dirname } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
@@ -49,30 +49,6 @@ test("add fails readably when no ids are left, without saving", () => {
|
||||
assert.equal(readFileSync(file, "utf8"), before);
|
||||
});
|
||||
|
||||
test("concurrent adds don't lose updates or reuse ids", async () => {
|
||||
const file = tempFile();
|
||||
const runs = Array.from({ length: 12 }, (_, i) =>
|
||||
new Promise((done) => {
|
||||
const child = spawn(process.execPath, [CLI, "add", `t${i}`], { env: { ...process.env, TODO_FILE: file } });
|
||||
child.on("exit", done);
|
||||
}));
|
||||
assert.deepEqual(await Promise.all(runs), Array(12).fill(0));
|
||||
const todos = JSON.parse(readFileSync(file, "utf8"));
|
||||
assert.equal(todos.length, 12);
|
||||
assert.deepEqual(todos.map((t) => t.id).sort((a, b) => a - b), Array.from({ length: 12 }, (_, i) => i + 1));
|
||||
assert.equal(existsSync(`${file}.lock`), false);
|
||||
});
|
||||
|
||||
test("a stale lock from a crashed run is taken over", () => {
|
||||
const file = tempFile();
|
||||
writeFileSync(`${file}.lock`, "");
|
||||
const old = new Date(Date.now() - 60_000);
|
||||
utimesSync(`${file}.lock`, old, old);
|
||||
const res = run(file, "add", "a");
|
||||
assert.equal(res.status, 0, res.stderr);
|
||||
assert.equal(existsSync(`${file}.lock`), false);
|
||||
});
|
||||
|
||||
test("rm on a file with duplicate ids removes only the first of them", () => {
|
||||
const file = tempFile();
|
||||
writeFileSync(file, JSON.stringify([{ id: 2, text: "b", done: false }, { id: 2, text: "c", done: false }]));
|
||||
|
||||
Reference in new issue
Block a user