Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add --cache-reset option #2677

Merged
merged 2 commits into from
Apr 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/cspell/src/__snapshots__/app.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,7 @@ Array [
" --no-must-find-files Do not error if no files are found.",
" --cache Use cache to only check changed files.",
" --no-cache Do not use cache.",
" --cache-reset Reset the cache file.",
" --cache-strategy <strategy> Strategy to use for detecting changed files.",
" (choices: \\"metadata\\", \\"content\\")",
" --cache-location <path> Path to the cache file or directory. (default:",
Expand Down
1 change: 1 addition & 0 deletions packages/cspell/src/commandLint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export function commandLint(prog: Command): Command {
.addOption(new CommanderOption('--local <local>', 'Deprecated -- Use: --locale').hideHelp())
.option('--cache', 'Use cache to only check changed files.')
.option('--no-cache', 'Do not use cache.')
.option('--cache-reset', 'Reset the cache file.')
.addOption(
new CommanderOption('--cache-strategy <strategy>', 'Strategy to use for detecting changed files.').choices([
'metadata',
Expand Down
3 changes: 3 additions & 0 deletions packages/cspell/src/lint/lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ export async function runLint(cfg: LintRequest): Promise<RunResult> {
const fileCount = files instanceof Array ? files.length : undefined;
const status: RunResult = runResult();
const cache = createCache(cacheSettings);
if (cfg.options.cacheReset) {
cache.reset();
}
const failFast = cfg.options.failFast ?? configInfo.config.failFast ?? false;

const emitProgressBegin = (filename: string, fileNum: number, fileCount: number) =>
Expand Down
4 changes: 4 additions & 0 deletions packages/cspell/src/util/cache/CSpellLintResultCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ export interface CSpellLintResultCache {
* Persists the in-memory cache to disk.
*/
reconcile(): void;
/**
* Resets the cache.
*/
reset(): void;
}
5 changes: 5 additions & 0 deletions packages/cspell/src/util/cache/CacheOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,9 @@ export interface CacheOptions {
* Strategy to use for detecting changed files, default: metadata
*/
cacheStrategy?: CacheStrategy;

/**
* Resets the cache
*/
cacheReset?: boolean;
}
13 changes: 9 additions & 4 deletions packages/cspell/src/util/cache/DiskCache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ import { CachedFileResult, DiskCache, CSpellCacheMeta, __testing__ } from './Dis

const { calcVersion } = __testing__;

jest.mock('./getConfigHash', () => ({
getConfigHash: jest.fn().mockReturnValue('TEST_CONFIG_HASH'),
}));

const mockCreateFileEntryCache = jest.spyOn(FileEntryCacheModule, 'createFromFile');
jest.mock('file-entry-cache', () => ({
createFromFile: jest.fn().mockReturnValue({
Expand All @@ -19,6 +15,7 @@ jest.mock('file-entry-cache', () => ({
notFoundFiles: [],
notChangedFiles: [],
}),
destroy: jest.fn(),
}),
}));

Expand All @@ -38,6 +35,7 @@ describe('DiskCache', () => {
getFileDescriptor: jest.Mock;
reconcile: jest.Mock;
analyzeFiles: jest.Mock;
destroy: jest.Mock;
};

beforeEach(() => {
Expand Down Expand Up @@ -163,6 +161,13 @@ describe('DiskCache', () => {
});
});

describe('reset', () => {
it('resets', () => {
diskCache.reset();
expect(fileEntryCache.destroy).toHaveBeenCalledTimes(1);
});
});

afterEach(() => {
jest.clearAllMocks();
});
Expand Down
8 changes: 8 additions & 0 deletions packages/cspell/src/util/cache/DiskCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,14 @@ export class DiskCache implements CSpellLintResultCache {
this.fileEntryCache.reconcile();
}

public reset(): void {
this.fileEntryCache.destroy();
this.dependencyCache.clear();
this.dependencyCacheTree = {};
this.objectCollection = new ShallowObjectCollection<CachedData>();
this.ocCacheFileResult = new ShallowObjectCollection<CachedFileResult>();
}

private normalizeResult(result: CachedFileResult): CachedFileResult {
const { issues, processed, errors, configErrors, ...rest } = result;
if (!Object.keys(rest).length) {
Expand Down
3 changes: 3 additions & 0 deletions packages/cspell/src/util/cache/DummyCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@ export class DummyCache implements CSpellLintResultCache {
reconcile(): void {
return;
}
reset(): void {
return;
}
}
35 changes: 0 additions & 35 deletions packages/cspell/src/util/cache/getConfigHash.test.ts

This file was deleted.

23 changes: 0 additions & 23 deletions packages/cspell/src/util/cache/getConfigHash.ts

This file was deleted.

14 changes: 0 additions & 14 deletions packages/cspell/src/util/cache/hash.test.ts

This file was deleted.

9 changes: 0 additions & 9 deletions packages/cspell/src/util/cache/hash.ts

This file was deleted.