diff --git a/__tests__/rollingcache.spec.ts b/__tests__/rollingcache.spec.ts index 75e58166..0af34d8f 100644 --- a/__tests__/rollingcache.spec.ts +++ b/__tests__/rollingcache.spec.ts @@ -1,41 +1,35 @@ import { beforeEach, afterAll, test, expect } from "@jest/globals"; import * as path from "path"; -import * as fs from "fs"; -import { remove, ensureDir } from "fs-extra"; +import { remove, ensureDir, writeFile, pathExists } from "fs-extra"; import { RollingCache } from "../src/rollingcache"; -const defaultTestFileShape = { - a: 1, - b: 2, - c: 3, -}; const local = (x: string) => path.resolve(__dirname, x); +const testDir = local("__temp/rollingcache"); +const oldCacheDir = `${testDir}/cache`; +const newCacheDir = `${testDir}/cache_`; +const testFile = "file.json"; +const oldTestFile = `${oldCacheDir}/${testFile}`; -beforeEach(() => { - // pre-create oldCacheRoot - return ensureDir(local("fixtures/cache")).then(() => { - // and newCacheRoot - return ensureDir(local("fixtures/cache_")).then(() => { - const testfile = local("fixtures/cache/testfile.json"); - fs.writeFileSync(testfile, JSON.stringify(defaultTestFileShape), "utf8"); - }); - }); +const nonExistentFile = "this-does-not-exist.json"; +const emptyCacheRoot = `${testDir}/empty-cache-root`; + +const testFileShape = { a: 1, b: 2, c: 3 }; + + +beforeEach(async () => { + await ensureDir(oldCacheDir); + await ensureDir(newCacheDir); + await writeFile(oldTestFile, JSON.stringify(testFileShape), "utf8"); }); +afterAll(() => remove(testDir)); -afterAll( - () => Promise.all([ - remove(local("fixtures")), - remove(local("not-real")), - ]), -); -test("RollingCache", () => { +test("RollingCache", async () => { expect(RollingCache).toBeTruthy(); - const cacheRoot = local("fixtures"); - const cache = new RollingCache(cacheRoot, true); + const cache = new RollingCache(testDir, true); expect(Object.keys(cache)).toEqual([ "cacheRoot", "checkNewCache", @@ -44,35 +38,30 @@ test("RollingCache", () => { "newCacheRoot", ]); - expect(cache.exists("fake-file.json")).toBeFalsy(); - expect(cache.exists("testfile.json")).toBeTruthy(); - expect(cache.path("x")).toEqual(local("fixtures/cache/x")); - expect(cache.match(["fake-file.json"])).toBeFalsy(); - expect(cache.match(["testfile.json"])).toBeTruthy(); - expect(cache.read("testfile.json")).toEqual(defaultTestFileShape); + expect(cache.exists(nonExistentFile)).toBeFalsy(); + expect(cache.exists(testFile)).toBeTruthy(); + expect(cache.path("x")).toEqual(`${oldCacheDir}/x`); + expect(cache.match([nonExistentFile])).toBeFalsy(); + expect(cache.match([testFile])).toBeTruthy(); + expect(cache.read(testFile)).toEqual(testFileShape); cache.write("write-test.json", {a: 2, b: 2, c: 2}); - expect( - cache.read("write-test.json"), - ).toEqual({a: 2, b: 2, c: 2}); + expect(cache.read("write-test.json")).toEqual({a: 2, b: 2, c: 2}); cache.write("write-fail.json", (undefined as any)); - expect( - cache.read("write-fail.json"), - ).toBeFalsy(); + expect(cache.read("write-fail.json")).toBeFalsy(); cache.touch("touched.json"); - expect(fs.existsSync(local("fixtures/cache_/touched.json"))).toBeTruthy(); + expect(await pathExists(`${newCacheDir}/touched.json`)).toBeTruthy(); expect((cache as any).rolled).toBeFalsy(); cache.roll(); expect((cache as any).rolled).toBeTruthy(); - expect(fs.existsSync(local("fixtures/cache_"))).toBeFalsy(); + expect(await pathExists(newCacheDir)).toBeFalsy(); }); -test("RollingCache, rolled", () => { - const cacheRoot = local("fixtures"); - const cache = new RollingCache(cacheRoot, true); +test("RollingCache, rolled", async () => { + const cache = new RollingCache(testDir, true); // roll the cache cache.roll(); // rolling again hits coverage for this.rolled being true already @@ -81,28 +70,23 @@ test("RollingCache, rolled", () => { expect(cache.match([])).toBeFalsy(); cache.write("whatever.json", {whatever: true}); - expect(fs.existsSync(local("fixtures/cache/whatever.json"))).toBeFalsy(); + expect(await pathExists(`${oldCacheDir}/whatever.json`)).toBeFalsy(); cache.touch("touched.json"); - expect(fs.existsSync(local("fixtures/cache/touched.json"))).toBeFalsy(); + expect(await pathExists(`${oldCacheDir}/touched.json`)).toBeFalsy(); }); -test("RollingCache, test checkNewCache", (done) => { - const cacheRoot = local("fixtures"); - const cache = new RollingCache(cacheRoot, true); +test("RollingCache, test checkNewCache", async () => { + const cache = new RollingCache(testDir, true); - const preExistingTestFile = local("fixtures/cache_/pre-existing.json"); - fs.writeFile(preExistingTestFile, JSON.stringify({}), "utf8", (e?: Error) => { - if (e) console.log(e); - expect(cache.exists("pre-existing.json")).toBeTruthy(); - done(); - }); + const preExistingFile = `${newCacheDir}/pre-existing.json`; + await writeFile(preExistingFile, JSON.stringify({})); + expect(cache.exists("pre-existing.json")).toBeTruthy(); }); -test("RollingCache, test match when oldCacheRoot is empty", () => { - const cacheRoot = local("not-real"); - const cache = new RollingCache(cacheRoot, true); +test("RollingCache, test match when oldCacheDir is empty", () => { + const cache = new RollingCache(emptyCacheRoot, true); expect(cache.match([])).toBeTruthy(); - expect(cache.match(["file.json"])).toBeFalsy(); + expect(cache.match([testFile])).toBeFalsy(); });