Skip to content

Commit

Permalink
refactor: use consts, async/await, etc in rollingcache spec
Browse files Browse the repository at this point in the history
- basically using pre-defined fixture vars instead of ones defined
  inside the test
  - which is more straightforward, easier to read, and less fragile
  - shorter names too
  - left a couple of ones as is where they were only used once very
    quickly -- could make them fixture vars too but 🤷

- use async/await instead of Promises with `done()` etc
- also use more `fs-extra` functions that support Promises instead of
  synchronous `fs` functions (e.g. `existsSync` -> `pathExists`)
  - async should be a small optimization for tests too

- fix: use __temp dir for auto-generated files instead of creating
  them in a fixtures dir and breaking actual fixtures

- format: a few multi-line statements were able to be condensed to a
  single line, so do so
  - esp as multi-line was not helping readability since this is just
    irrelevant test data (may have hampered readability actually)
  • Loading branch information
agilgur5 committed May 9, 2022
1 parent c90c930 commit 16bf8df
Showing 1 changed file with 41 additions and 57 deletions.
98 changes: 41 additions & 57 deletions __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",
Expand All @@ -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
Expand All @@ -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();
});

0 comments on commit 16bf8df

Please sign in to comment.