From 5e866fcaa039cd03d5797b4999e191fd9df6c669 Mon Sep 17 00:00:00 2001 From: Allison Karlitskaya Date: Tue, 20 Aug 2024 11:07:23 +0200 Subject: [PATCH] pkg: add qunit tests for modify() "" vs null Add some extra tests to make sure that `null` returned from a modify() callback will result in a deleted file whereas "" will result in an empty file. --- pkg/base1/test-file.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/pkg/base1/test-file.js b/pkg/base1/test-file.js index 68cf918c0c2c..cbbad9e7fc17 100644 --- a/pkg/base1/test-file.js +++ b/pkg/base1/test-file.js @@ -199,6 +199,28 @@ QUnit.test("modify", async assert => { assert.equal(n, 1, "callback called once"); assert.equal(await cockpit.spawn(["cat", dir + "/quux"]), "dcba\n", "correct content"); + + // make sure that writing "" results in an empty file, not a deleted one + n = 0; + await file.modify(old => { + n += 1; + assert.equal(old, "dcba\n", "correct old content"); + return ""; + }); + assert.equal(n, 1, "callback called once"); + + assert.equal(await cockpit.spawn(["cat", dir + "/quux"]), "", "correct content"); + + // make sure that writing null deletes the file + n = 0; + await file.modify(old => { + n += 1; + assert.equal(old, "", "correct old content"); + return null; + }); + assert.equal(n, 1, "callback called once"); + + assert.rejects(cockpit.spawn(["cat", dir + "/quux"]), /No such file or directory/, "file deleted"); }); QUnit.test("modify with conflict", async assert => {