Skip to content

Commit

Permalink
feat: add mock jsonFile cache
Browse files Browse the repository at this point in the history
  • Loading branch information
chrismclarke committed Sep 25, 2024
1 parent 4b73f78 commit 1db2371
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { TimeLike } from "fs";
import { JsonFileCache } from "./jsonFile";
import { IContentsEntry } from "shared";

interface IContentsEntryWithValue extends IContentsEntry {
value?: any;
}

/**
* Mock implementation of JsonFileCache that only uses in-memory caching
* instead of persisting files to disk
*/
export class MockJsonFileCache extends JsonFileCache {
private mockContents: Record<string, IContentsEntryWithValue> = {};

constructor() {
super("", 0);
}

public add(data: any, entryName?: string, stats?: { mtime: TimeLike }) {
if (data) {
if (!entryName) {
entryName = this.generateCacheEntryName(data);
}
if (!this.mockContents[entryName]) {
this.mockContents[entryName] = {} as any;
}
this.mockContents[entryName].value = data;
return { filePath: entryName, entryName, data };
}
}

public clear() {
this.mockContents = {};
}

public remove(entryName: string) {
if (this.mockContents.hasOwnProperty(entryName)) {
delete this.mockContents[entryName];
}
}

public get<T = any>(entryName: string) {
return this.mockContents[entryName] as T;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ export class JsonFileCache {

constructor(folderPath: string, version: number) {
this.version = version;
this.setup(folderPath);
// HACK - support tests by avoiding setup if folderPath not provided
if (folderPath) {
this.setup(folderPath);
}
}

/**
Expand Down

0 comments on commit 1db2371

Please sign in to comment.