From 431c5855c41511f2f7d6c8c7d068ec5e76567418 Mon Sep 17 00:00:00 2001 From: Zach Leatherman Date: Wed, 18 Dec 2024 10:21:40 -0600 Subject: [PATCH] Ignore setInitialCacheTimestamp if called after initial save (no error) --- src/AssetCache.js | 33 +++++++++++---------------------- test/AssetCacheTest.js | 33 ++++++++++++++++++++++++--------- 2 files changed, 35 insertions(+), 31 deletions(-) diff --git a/src/AssetCache.js b/src/AssetCache.js index 441a968..74f9938 100644 --- a/src/AssetCache.js +++ b/src/AssetCache.js @@ -11,7 +11,6 @@ class AssetCache { #source; #hash; #customFilename; - #hasSaved = false; #cache; #cacheDirectory; #cacheLocationDirty = false; @@ -42,10 +41,6 @@ class AssetCache { setInitialCacheTimestamp(timestamp) { this.initialCacheTimestamp = timestamp; - - if(this.#hasSaved) { - throw new Error("`setInitialCacheTimestamp` method must be called before the object is saved."); - } } log(message) { @@ -235,21 +230,11 @@ class AssetCache { } async save(contents, type = "buffer", metadata = {}) { - if (this.options.dryRun) { - debug("An attempt was made to save to the file system with `dryRun: true`. Skipping."); - - // Errors are still expected from this - this.#hasSaved = true; - return; - } - if(!contents) { - throw new Error("save(contents) expects contents (it was falsy)"); + throw new Error("save(contents) expects contents (was falsy)"); } - if(!this.isDirEnsured) { - this.ensureDir(); - } + this.ensureDir(); if (type === "json" || type === "parsed-xml") { contents = JSON.stringify(contents); @@ -258,9 +243,12 @@ class AssetCache { let contentPath = this.getCachedContentsPath(type); // the contents must exist before the cache metadata are saved below - fs.writeFileSync(contentPath, contents); - - debug(`Writing ${contentPath}`); + if(!this.options.dryRun) { + fs.writeFileSync(contentPath, contents); + debug(`Writing ${contentPath}`); + } else { + debug(`Dry run writing ${contentPath}`); + } this.cache.set(this.hash, { cachedAt: this.initialCacheTimestamp || Date.now(), @@ -268,8 +256,9 @@ class AssetCache { metadata, }); - this.cache.save(); - this.#hasSaved = true; + if(!this.options.dryRun) { + this.cache.save(); + } } async getCachedContents(type) { diff --git a/test/AssetCacheTest.js b/test/AssetCacheTest.js index fab82b3..c7db873 100644 --- a/test/AssetCacheTest.js +++ b/test/AssetCacheTest.js @@ -142,7 +142,7 @@ test("setInitialCacheTimestamp method (used by Eleventy Image to establish a con let cache = new AssetCache("this_is_a_test", ".cache", { dryRun: true }); - let timestamp = Date.now(); + let timestamp = (new Date(2024,1,1)).getTime(); cache.setInitialCacheTimestamp(timestamp); await cache.save("test"); @@ -150,17 +150,32 @@ test("setInitialCacheTimestamp method (used by Eleventy Image to establish a con t.is(cache.getCachedTimestamp(), timestamp); }); -test("setInitialCacheTimestamp method after save throws error", async (t) => { - let cache = new AssetCache("this_is_a_test", ".cache", { +test("setInitialCacheTimestamp method after save is ignored", async (t) => { + let cache = new AssetCache("this_is_a_test2", ".cache", { dryRun: true }); - let timestamp = Date.now(); + + let timestamp = (new Date(2024,1,1)).getTime(); await cache.save("test"); - t.throws(() => { - cache.setInitialCacheTimestamp(timestamp); - }, { - message: "`setInitialCacheTimestamp` method must be called before the object is saved." - }) + cache.setInitialCacheTimestamp(timestamp); + + t.not(cache.getCachedTimestamp(), timestamp); +}); + +test("setInitialCacheTimestamp method after second save is used", async (t) => { + let cache = new AssetCache("this_is_a_test3", ".cache", { + dryRun: true + }); + + let timestamp = (new Date(2024,1,1)).getTime(); + + await cache.save("test"); + + cache.setInitialCacheTimestamp(timestamp); + + await cache.save("test"); + + t.is(cache.getCachedTimestamp(), timestamp); });