• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

streetsidesoftware / cspell / 16377100492

18 Jul 2025 05:55PM UTC coverage: 92.433% (-0.001%) from 92.434%
16377100492

Pull #7639

github

web-flow
Merge eae71ecf1 into 64931c3c9
Pull Request #7639: fix: Support url based cache entries

13114 of 15508 branches covered (84.56%)

6 of 6 new or added lines in 3 files covered. (100.0%)

14 existing lines in 4 files now uncovered.

16112 of 17431 relevant lines covered (92.43%)

29576.87 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.77
/packages/cspell/src/util/cache/createCache.ts
1
import assert from 'node:assert';
2
import { stat } from 'node:fs/promises';
3
import path from 'node:path';
4

5
import type { CacheSettings, CSpellSettings } from '@cspell/cspell-types';
6

7
import { isErrorLike } from '../errors.js';
8
import type { CacheOptions } from './CacheOptions.js';
9
import type { CSpellLintResultCache } from './CSpellLintResultCache.js';
10
import { createDiskCache } from './DiskCache.js';
11
import { DummyCache } from './DummyCache.js';
12

13
// cspell:word cspellcache
14
export const DEFAULT_CACHE_LOCATION = '.cspellcache';
6✔
15

16
export interface CreateCacheSettings extends Required<CacheSettings> {
17
    /**
18
     * cspell version used to validate cache entries.
19
     */
20
    version: string;
21

22
    /**
23
     * When true, causes the cache to be reset, removing any entries
24
     * or cache files.
25
     */
26
    reset?: true;
27
}
28

29
const versionSuffix = '';
6✔
30

31
/**
32
 * Creates CSpellLintResultCache (disk cache if caching is enabled in config or dummy otherwise)
33
 */
34
export async function createCache(options: CreateCacheSettings): Promise<CSpellLintResultCache> {
35
    const { useCache, cacheLocation, cacheStrategy, reset } = options;
142✔
36
    const location = path.resolve(cacheLocation);
142✔
37
    const useChecksum = cacheStrategy === 'content';
142✔
38
    const version = normalizeVersion(options.version);
142✔
39
    const useUniversal = options.cacheFormat === 'universal';
142✔
40
    const cache = useCache ? await createDiskCache(location, useChecksum, version, useUniversal) : new DummyCache();
142✔
41
    if (reset) {
142✔
42
        await cache.reset();
4✔
43
    }
44
    return cache;
142✔
45
}
46

47
export async function calcCacheSettings(
48
    config: CSpellSettings,
49
    cacheOptions: CacheOptions,
50
    root: string,
51
): Promise<CreateCacheSettings> {
52
    const cs = config.cache ?? {};
154✔
53
    const useCache = cacheOptions.cache ?? cs.useCache ?? false;
154✔
54
    const cacheLocation = await resolveCacheLocation(
154✔
55
        path.resolve(root, cacheOptions.cacheLocation ?? cs.cacheLocation ?? DEFAULT_CACHE_LOCATION),
399✔
56
    );
57

58
    const cacheStrategy = cacheOptions.cacheStrategy ?? cs.cacheStrategy ?? 'content';
154✔
59
    const cacheFormat = cacheOptions.cacheFormat ?? cs.cacheFormat ?? 'universal';
154✔
60
    const optionals: Partial<CreateCacheSettings> = {};
154✔
61
    if (cacheOptions.cacheReset) {
154✔
62
        optionals.reset = true;
4✔
63
    }
64
    return {
154✔
65
        ...optionals,
66
        useCache,
67
        cacheLocation,
68
        cacheStrategy,
69
        version: cacheOptions.version,
70
        cacheFormat,
71
    };
72
}
73

74
async function resolveCacheLocation(cacheLocation: string): Promise<string> {
75
    try {
154✔
76
        const s = await stat(cacheLocation);
154✔
77
        if (s.isFile()) return cacheLocation;
17✔
78
        return path.join(cacheLocation, DEFAULT_CACHE_LOCATION);
1✔
79
    } catch (err) {
80
        if (isErrorLike(err) && err.code === 'ENOENT') {
137!
81
            return cacheLocation;
137✔
82
        }
UNCOV
83
        throw err;
×
84
    }
85
}
86

87
/**
88
 * Normalizes the version and return only `major.minor + versionSuffix`
89
 * @param version The cspell semantic version.
90
 */
91
function normalizeVersion(version: string): string {
92
    const parts = version.split('.').slice(0, 2);
149✔
93
    assert(parts.length === 2);
149✔
94
    return parts.join('.') + versionSuffix;
147✔
95
}
96

97
export const __testing__: {
98
    normalizeVersion: typeof normalizeVersion;
99
    versionSuffix: string;
100
} = {
6✔
101
    normalizeVersion,
102
    versionSuffix,
103
};
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc