From e36fe60b626ddd652a39cdf1fea30fcdfd2af030 Mon Sep 17 00:00:00 2001 From: kaorun343 <5625395+kaorun343@users.noreply.github.com> Date: Thu, 22 Sep 2022 20:18:42 +0900 Subject: [PATCH 01/19] Add cacheStrategy option --- lib/__tests__/standalone-cache.test.js | 21 +++++++++++++++++++++ lib/cli.js | 13 +++++++++++++ lib/standalone.js | 5 +++-- lib/utils/FileCache.js | 5 ++++- types/stylelint/index.d.ts | 1 + 5 files changed, 42 insertions(+), 3 deletions(-) diff --git a/lib/__tests__/standalone-cache.test.js b/lib/__tests__/standalone-cache.test.js index e199fe0697..d7393ac6a9 100644 --- a/lib/__tests__/standalone-cache.test.js +++ b/lib/__tests__/standalone-cache.test.js @@ -194,3 +194,24 @@ describe('standalone cache uses cacheLocation', () => { expect(cache.getKey(validFile)).toBeTruthy(); }); }); + +describe('standalone cache uses cacheStrategy', () => { + const expectedCacheFilePath = path.join(cwd, '.stylelintcache'); + + afterEach(async () => { + // clean up after each test + await removeFile(expectedCacheFilePath); + }); + + it('cacheStrategy is "contenet"', async () => { + await standalone(getConfig({ cacheStrategy: 'content' })); + + // Ensure cache file is created + expect(existsSync(expectedCacheFilePath)).toBe(true); + + const { cache } = fCache.createFromFile(expectedCacheFilePath, undefined, true); + + expect(typeof cache.getKey(validFile)).toBe('object'); + expect(cache.getKey(validFile)).toBeTruthy(); + }); +}); diff --git a/lib/cli.js b/lib/cli.js index 906ac6573e..430cb3f9b9 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -21,6 +21,7 @@ const EXIT_CODE_ERROR = 2; * @typedef {object} CLIFlags * @property {boolean} [cache] * @property {string} [cacheLocation] + * @property {string} [cacheStrategy] * @property {string | false} config * @property {string} [configBasedir] * @property {string} [customSyntax] @@ -64,6 +65,7 @@ const EXIT_CODE_ERROR = 2; * @property {boolean} [cache] * @property {string} [configFile] * @property {string} [cacheLocation] + * @property {string} [cacheStrategy] * @property {string} [customSyntax] * @property {string} [codeFilename] * @property {string} [configBasedir] @@ -173,6 +175,10 @@ const meowOptions = { If the directory for the cache does not exist, make sure you add a trailing "/" on *nix systems or "\\" on Windows. Otherwise the path will be assumed to be a file. + --cache-strategy [default: 'metadata'] + + // TODO + --formatter, -f [default: "string"] The output formatter: ${getFormatterOptionsText({ useOr: true })}. @@ -236,6 +242,9 @@ const meowOptions = { cacheLocation: { type: 'string', }, + cacheStrategy: { + type: 'string', + }, color: { type: 'boolean', }, @@ -410,6 +419,10 @@ module.exports = async (argv) => { optionsBase.cacheLocation = cli.flags.cacheLocation; } + if (cli.flags.cacheStrategy) { + optionsBase.cacheStrategy = cli.flags.cacheStrategy; + } + if (cli.flags.fix) { optionsBase.fix = cli.flags.fix; } diff --git a/lib/standalone.js b/lib/standalone.js index 6d4e45067f..7c903cabdf 100644 --- a/lib/standalone.js +++ b/lib/standalone.js @@ -39,6 +39,7 @@ async function standalone({ allowEmptyInput = false, cache: useCache = false, cacheLocation, + cacheStrategy, code, codeFilename, config, @@ -179,10 +180,10 @@ async function standalone({ const stylelintVersion = pkg.version; const hashOfConfig = hash(`${stylelintVersion}_${JSON.stringify(config || {})}`); - fileCache = new FileCache(cacheLocation, cwd, hashOfConfig); + fileCache = new FileCache(cacheLocation, cacheStrategy, cwd, hashOfConfig); } else { // No need to calculate hash here, we just want to delete cache file. - fileCache = new FileCache(cacheLocation, cwd); + fileCache = new FileCache(cacheLocation, cacheStrategy, cwd); // Remove cache file if cache option is disabled fileCache.destroy(); } diff --git a/lib/utils/FileCache.js b/lib/utils/FileCache.js index 698b3105f1..8ec45bb4bc 100644 --- a/lib/utils/FileCache.js +++ b/lib/utils/FileCache.js @@ -6,6 +6,7 @@ const getCacheFile = require('./getCacheFile'); const path = require('path'); const DEFAULT_CACHE_LOCATION = './.stylelintcache'; +const DEFAULT_CACHE_STRATEGY = 'metadata'; const DEFAULT_HASH = ''; /** @typedef {import('file-entry-cache').FileDescriptor["meta"] & { hashOfConfig?: string }} CacheMetadata */ @@ -18,13 +19,15 @@ const DEFAULT_HASH = ''; class FileCache { constructor( cacheLocation = DEFAULT_CACHE_LOCATION, + cacheStrategy = DEFAULT_CACHE_STRATEGY, cwd = process.cwd(), hashOfConfig = DEFAULT_HASH, ) { const cacheFile = path.resolve(getCacheFile(cacheLocation, cwd)); + const useCheckSum = cacheStrategy !== DEFAULT_CACHE_STRATEGY; debug(`Cache file is created at ${cacheFile}`); - this._fileCache = fileEntryCache.create(cacheFile); + this._fileCache = fileEntryCache.create(cacheFile, undefined, useCheckSum); this._hashOfConfig = hashOfConfig; } diff --git a/types/stylelint/index.d.ts b/types/stylelint/index.d.ts index 2afb65d3ff..32108df57d 100644 --- a/types/stylelint/index.d.ts +++ b/types/stylelint/index.d.ts @@ -212,6 +212,7 @@ declare module 'stylelint' { globbyOptions?: GlobbyOptions; cache?: boolean; cacheLocation?: string; + cacheStrategy?: string; code?: string; codeFilename?: string; config?: Config; From 2d4e0aa46d24beee49cc8aeb7e2454507c148a05 Mon Sep 17 00:00:00 2001 From: Richard Hallows Date: Thu, 22 Sep 2022 13:51:33 +0100 Subject: [PATCH 02/19] Create mighty-apricots-cheat.md --- .changeset/mighty-apricots-cheat.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/mighty-apricots-cheat.md diff --git a/.changeset/mighty-apricots-cheat.md b/.changeset/mighty-apricots-cheat.md new file mode 100644 index 0000000000..c96ce14872 --- /dev/null +++ b/.changeset/mighty-apricots-cheat.md @@ -0,0 +1,5 @@ +--- +"stylelint": minor +--- + +Added: `cacheStrategy` option From 89ea1cb852314c3e96245fe687bee705f76dec07 Mon Sep 17 00:00:00 2001 From: kaorun343 <5625395+kaorun343@users.noreply.github.com> Date: Fri, 23 Sep 2022 01:20:25 +0900 Subject: [PATCH 03/19] Update lib/cli.js Co-authored-by: Richard Hallows --- lib/cli.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/cli.js b/lib/cli.js index 430cb3f9b9..65c87ecd5e 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -177,7 +177,13 @@ const meowOptions = { --cache-strategy [default: 'metadata'] - // TODO + Strategy for the cache to use for detecting changed files. Can be either + "metadata" or "content". + + The "content" strategy can be useful in cases where the modification time of + your files change even if their contents have not. For example, this can happen + during git operations like "git clone" because git does not track file modification + time. --formatter, -f [default: "string"] From 64a4ea6f9d341323301439e96b10ff4e2df5a2b5 Mon Sep 17 00:00:00 2001 From: kaorun343 <5625395+kaorun343@users.noreply.github.com> Date: Fri, 23 Sep 2022 01:22:04 +0900 Subject: [PATCH 04/19] Update snapshot --- lib/__tests__/__snapshots__/cli.test.js.snap | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lib/__tests__/__snapshots__/cli.test.js.snap b/lib/__tests__/__snapshots__/cli.test.js.snap index c554de0d9e..95a70a92db 100644 --- a/lib/__tests__/__snapshots__/cli.test.js.snap +++ b/lib/__tests__/__snapshots__/cli.test.js.snap @@ -88,6 +88,16 @@ exports[`CLI --help 1`] = ` If the directory for the cache does not exist, make sure you add a trailing \\"/\\" on *nix systems or \\"\\\\\\" on Windows. Otherwise the path will be assumed to be a file. + --cache-strategy [default: 'metadata'] + + Strategy for the cache to use for detecting changed files. Can be either + \\"metadata\\" or \\"content\\". + + The \\"content\\" strategy can be useful in cases where the modification time of + your files change even if their contents have not. For example, this can happen + during git operations like \\"git clone\\" because git does not track file modification + time. + --formatter, -f [default: \\"string\\"] The output formatter: \\"compact\\", \\"github\\", \\"json\\", \\"string\\", \\"tap\\", \\"unix\\" or \\"verbose\\". From e01f83d2cf0b9b4f0e5935640a4d068137a42034 Mon Sep 17 00:00:00 2001 From: kaorun343 <5625395+kaorun343@users.noreply.github.com> Date: Fri, 23 Sep 2022 01:25:21 +0900 Subject: [PATCH 05/19] Update document --- docs/user-guide/usage/cli.md | 4 ++++ docs/user-guide/usage/options.md | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/docs/user-guide/usage/cli.md b/docs/user-guide/usage/cli.md index 54f4a8c4e0..ab4032255e 100644 --- a/docs/user-guide/usage/cli.md +++ b/docs/user-guide/usage/cli.md @@ -20,6 +20,10 @@ The process exits without throwing an error when glob pattern matches no files. Path to a file or directory for the cache location. [More info](options.md#cachelocation). +### `--cache-strategy` + +Strategy for the cache to use for detecting changed files. Can be either "metadata" or "content". [More info](options.md#cachestrategy) + ### `--cache` Store the results of processed files so that Stylelint only operates on the changed ones. By default, the cache is stored in `./.stylelintcache` in `process.cwd()`. [More info](options.md#cache). diff --git a/docs/user-guide/usage/options.md b/docs/user-guide/usage/options.md index 2a34f7bbfb..1a8d45c602 100644 --- a/docs/user-guide/usage/options.md +++ b/docs/user-guide/usage/options.md @@ -110,6 +110,14 @@ If a directory is specified, Stylelint creates a cache file inside the specified _If the directory of `cacheLocation` does not exist, make sure you add a trailing `/` on \*nix systems or `\` on Windows. Otherwise, Stylelint assumes the path to be a file._ +## `cacheStrategy` + +CLI flag: `--cache-strategy` + +Strategy for the cache to use for detecting changed files. Can be either "metadata" or "content". + +The "content" strategy can be useful in cases where the modification time of your files change even if their contents have not. For example, this can happen during git operations like "git clone" because git does not track file modification time. + ## `maxWarnings` CLI flags: `--max-warnings, --mw` From 0035fdfba3c57f2408db891515d2ceb0530da008 Mon Sep 17 00:00:00 2001 From: kaorun343 <5625395+kaorun343@users.noreply.github.com> Date: Mon, 26 Sep 2022 03:52:32 +0900 Subject: [PATCH 06/19] Update docs/user-guide/usage/cli.md Co-authored-by: Masafumi Koba <473530+ybiquitous@users.noreply.github.com> --- docs/user-guide/usage/cli.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/user-guide/usage/cli.md b/docs/user-guide/usage/cli.md index ab4032255e..e37b6c722e 100644 --- a/docs/user-guide/usage/cli.md +++ b/docs/user-guide/usage/cli.md @@ -22,7 +22,7 @@ Path to a file or directory for the cache location. [More info](options.md#cache ### `--cache-strategy` -Strategy for the cache to use for detecting changed files. Can be either "metadata" or "content". [More info](options.md#cachestrategy) +Strategy for the cache to use for detecting changed files. Can be either "metadata" or "content". [More info](options.md#cachestrategy). ### `--cache` From cb2778f82dcce9cdd9501036fd0bb56ecbee3694 Mon Sep 17 00:00:00 2001 From: kaorun343 <5625395+kaorun343@users.noreply.github.com> Date: Mon, 26 Sep 2022 03:52:45 +0900 Subject: [PATCH 07/19] Update docs/user-guide/usage/options.md Co-authored-by: Masafumi Koba <473530+ybiquitous@users.noreply.github.com> --- docs/user-guide/usage/options.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/user-guide/usage/options.md b/docs/user-guide/usage/options.md index 1a8d45c602..005b9124df 100644 --- a/docs/user-guide/usage/options.md +++ b/docs/user-guide/usage/options.md @@ -116,7 +116,7 @@ CLI flag: `--cache-strategy` Strategy for the cache to use for detecting changed files. Can be either "metadata" or "content". -The "content" strategy can be useful in cases where the modification time of your files change even if their contents have not. For example, this can happen during git operations like "git clone" because git does not track file modification time. +The "content" strategy can be useful in cases where the modification time of your files changes even if their contents have not. For example, this can happen during git operations like "git clone" because git does not track file modification time. ## `maxWarnings` From 76d064683c5785f40eeb10d4172c715a32417ccc Mon Sep 17 00:00:00 2001 From: kaorun343 <5625395+kaorun343@users.noreply.github.com> Date: Mon, 26 Sep 2022 03:53:17 +0900 Subject: [PATCH 08/19] Update lib/cli.js Co-authored-by: Masafumi Koba <473530+ybiquitous@users.noreply.github.com> --- lib/cli.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/cli.js b/lib/cli.js index 65c87ecd5e..d2d7cda8a8 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -175,15 +175,15 @@ const meowOptions = { If the directory for the cache does not exist, make sure you add a trailing "/" on *nix systems or "\\" on Windows. Otherwise the path will be assumed to be a file. - --cache-strategy [default: 'metadata'] + --cache-strategy [default: "metadata"] - Strategy for the cache to use for detecting changed files. Can be either - "metadata" or "content". + Strategy for the cache to use for detecting changed files. Can be either + "metadata" or "content". The "content" strategy can be useful in cases where the modification time of - your files change even if their contents have not. For example, this can happen + your files changes even if their contents have not. For example, this can happen during git operations like "git clone" because git does not track file modification - time. + time. --formatter, -f [default: "string"] From 35c6a65ada14e358cb92d49e416194c562bdd513 Mon Sep 17 00:00:00 2001 From: kaorun343 <5625395+kaorun343@users.noreply.github.com> Date: Mon, 26 Sep 2022 03:54:04 +0900 Subject: [PATCH 09/19] Update lib/__tests__/standalone-cache.test.js Co-authored-by: Masafumi Koba <473530+ybiquitous@users.noreply.github.com> --- lib/__tests__/standalone-cache.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/__tests__/standalone-cache.test.js b/lib/__tests__/standalone-cache.test.js index d7393ac6a9..0a8fcb6afc 100644 --- a/lib/__tests__/standalone-cache.test.js +++ b/lib/__tests__/standalone-cache.test.js @@ -203,7 +203,7 @@ describe('standalone cache uses cacheStrategy', () => { await removeFile(expectedCacheFilePath); }); - it('cacheStrategy is "contenet"', async () => { + it('cacheStrategy is "content"', async () => { await standalone(getConfig({ cacheStrategy: 'content' })); // Ensure cache file is created From 6659e7f084ec497557b3c5ceefbeeb72cd3c17c4 Mon Sep 17 00:00:00 2001 From: kaorun343 <5625395+kaorun343@users.noreply.github.com> Date: Mon, 26 Sep 2022 03:55:21 +0900 Subject: [PATCH 10/19] Update lib/utils/FileCache.js Co-authored-by: Masafumi Koba <473530+ybiquitous@users.noreply.github.com> --- lib/utils/FileCache.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/utils/FileCache.js b/lib/utils/FileCache.js index 8ec45bb4bc..297c3d7ded 100644 --- a/lib/utils/FileCache.js +++ b/lib/utils/FileCache.js @@ -23,6 +23,11 @@ class FileCache { cwd = process.cwd(), hashOfConfig = DEFAULT_HASH, ) { + if (![DEFAULT_CACHE_STRATEGY, CACHE_STRATEGY_CONTENT].includes(cacheStrategy)) { + throw new Error( + `"${cacheStrategy}" cache strategy is unsupported. Specify either "${DEFAULT_CACHE_STRATEGY}" or "${CACHE_STRATEGY_CONTENT}"`, + ); + } const cacheFile = path.resolve(getCacheFile(cacheLocation, cwd)); const useCheckSum = cacheStrategy !== DEFAULT_CACHE_STRATEGY; From 238531afd76c8b45972d5aea8fc46c0e7d0cab9a Mon Sep 17 00:00:00 2001 From: kaorun343 <5625395+kaorun343@users.noreply.github.com> Date: Mon, 26 Sep 2022 04:00:12 +0900 Subject: [PATCH 11/19] Use safeChdir --- lib/__tests__/standalone-cache.test.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/__tests__/standalone-cache.test.js b/lib/__tests__/standalone-cache.test.js index 0a8fcb6afc..1810ac5ef0 100644 --- a/lib/__tests__/standalone-cache.test.js +++ b/lib/__tests__/standalone-cache.test.js @@ -196,6 +196,10 @@ describe('standalone cache uses cacheLocation', () => { }); describe('standalone cache uses cacheStrategy', () => { + const cwd = path.join(__dirname, 'tmp', 'standalone-cache-uses-cacheStrategy'); + + safeChdir(cwd); + const expectedCacheFilePath = path.join(cwd, '.stylelintcache'); afterEach(async () => { From 539b75e5192960537feb118fb711eb4324cf32ba Mon Sep 17 00:00:00 2001 From: kaorun343 <5625395+kaorun343@users.noreply.github.com> Date: Mon, 26 Sep 2022 04:02:46 +0900 Subject: [PATCH 12/19] Add flags.cacheStrategy test --- lib/__tests__/cli.test.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/__tests__/cli.test.js b/lib/__tests__/cli.test.js index 373489384c..f726813c54 100644 --- a/lib/__tests__/cli.test.js +++ b/lib/__tests__/cli.test.js @@ -50,6 +50,11 @@ describe('buildCLI', () => { expect(buildCLI(['--cache-location=foo']).flags.cacheLocation).toBe('foo'); }); + it('flags.cacheStrategy', () => { + expect(buildCLI(['--cache-strategy=content']).flags.cacheStrategy).toBe('content'); + expect(buildCLI(['--cache-strategy=metadata']).flags.cacheStrategy).toBe('metadata'); + }); + it('flags.color', () => { expect(buildCLI(['--color']).flags.color).toBe(true); expect(buildCLI(['--no-color']).flags.color).toBe(false); From 17dac2f1389f5c6da0f6c1ab62f5b67f2b5d5b22 Mon Sep 17 00:00:00 2001 From: kaorun343 <5625395+kaorun343@users.noreply.github.com> Date: Mon, 26 Sep 2022 04:07:06 +0900 Subject: [PATCH 13/19] Update snapshot --- lib/__tests__/__snapshots__/cli.test.js.snap | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/__tests__/__snapshots__/cli.test.js.snap b/lib/__tests__/__snapshots__/cli.test.js.snap index 95a70a92db..42d39a49c4 100644 --- a/lib/__tests__/__snapshots__/cli.test.js.snap +++ b/lib/__tests__/__snapshots__/cli.test.js.snap @@ -88,15 +88,15 @@ exports[`CLI --help 1`] = ` If the directory for the cache does not exist, make sure you add a trailing \\"/\\" on *nix systems or \\"\\\\\\" on Windows. Otherwise the path will be assumed to be a file. - --cache-strategy [default: 'metadata'] + --cache-strategy [default: \\"metadata\\"] - Strategy for the cache to use for detecting changed files. Can be either - \\"metadata\\" or \\"content\\". + Strategy for the cache to use for detecting changed files. Can be either + \\"metadata\\" or \\"content\\". The \\"content\\" strategy can be useful in cases where the modification time of - your files change even if their contents have not. For example, this can happen + your files changes even if their contents have not. For example, this can happen during git operations like \\"git clone\\" because git does not track file modification - time. + time. --formatter, -f [default: \\"string\\"] From d5b47f537940fc669aa918172e0ba2cd6acb6547 Mon Sep 17 00:00:00 2001 From: kaorun343 <5625395+kaorun343@users.noreply.github.com> Date: Mon, 26 Sep 2022 04:08:03 +0900 Subject: [PATCH 14/19] Add CACHE_STRATEGY_CONTENT --- lib/utils/FileCache.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/utils/FileCache.js b/lib/utils/FileCache.js index 297c3d7ded..750655521d 100644 --- a/lib/utils/FileCache.js +++ b/lib/utils/FileCache.js @@ -5,8 +5,11 @@ const fileEntryCache = require('file-entry-cache'); const getCacheFile = require('./getCacheFile'); const path = require('path'); +const CACHE_STRATEGY_METADATA = 'metadata'; +const CACHE_STRATEGY_CONTENT = 'content'; + const DEFAULT_CACHE_LOCATION = './.stylelintcache'; -const DEFAULT_CACHE_STRATEGY = 'metadata'; +const DEFAULT_CACHE_STRATEGY = CACHE_STRATEGY_METADATA; const DEFAULT_HASH = ''; /** @typedef {import('file-entry-cache').FileDescriptor["meta"] & { hashOfConfig?: string }} CacheMetadata */ @@ -23,11 +26,12 @@ class FileCache { cwd = process.cwd(), hashOfConfig = DEFAULT_HASH, ) { - if (![DEFAULT_CACHE_STRATEGY, CACHE_STRATEGY_CONTENT].includes(cacheStrategy)) { + if (![CACHE_STRATEGY_METADATA, CACHE_STRATEGY_CONTENT].includes(cacheStrategy)) { throw new Error( - `"${cacheStrategy}" cache strategy is unsupported. Specify either "${DEFAULT_CACHE_STRATEGY}" or "${CACHE_STRATEGY_CONTENT}"`, + `"${cacheStrategy}" cache strategy is unsupported. Specify either "${CACHE_STRATEGY_METADATA}" or "${CACHE_STRATEGY_CONTENT}"`, ); } + const cacheFile = path.resolve(getCacheFile(cacheLocation, cwd)); const useCheckSum = cacheStrategy !== DEFAULT_CACHE_STRATEGY; From 4fdb5d947eb2ff9f102a53f8845adf80b78240d1 Mon Sep 17 00:00:00 2001 From: kaorun343 <5625395+kaorun343@users.noreply.github.com> Date: Mon, 26 Sep 2022 04:08:28 +0900 Subject: [PATCH 15/19] Update lib/utils/FileCache.js Co-authored-by: Masafumi Koba <473530+ybiquitous@users.noreply.github.com> --- lib/utils/FileCache.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/utils/FileCache.js b/lib/utils/FileCache.js index 750655521d..2be2c30302 100644 --- a/lib/utils/FileCache.js +++ b/lib/utils/FileCache.js @@ -33,7 +33,7 @@ class FileCache { } const cacheFile = path.resolve(getCacheFile(cacheLocation, cwd)); - const useCheckSum = cacheStrategy !== DEFAULT_CACHE_STRATEGY; + const useCheckSum = cacheStrategy === CACHE_STRATEGY_CONTENT; debug(`Cache file is created at ${cacheFile}`); this._fileCache = fileEntryCache.create(cacheFile, undefined, useCheckSum); From 5b5088f043441398bdbdfea76897f9207b5d824d Mon Sep 17 00:00:00 2001 From: kaorun343 <5625395+kaorun343@users.noreply.github.com> Date: Mon, 26 Sep 2022 04:35:59 +0900 Subject: [PATCH 16/19] Update cacheStrategy test Add metadata test Update content test --- lib/__tests__/standalone-cache.test.js | 31 +++++++++++++++++++++----- 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/lib/__tests__/standalone-cache.test.js b/lib/__tests__/standalone-cache.test.js index 1810ac5ef0..85fd2a658c 100644 --- a/lib/__tests__/standalone-cache.test.js +++ b/lib/__tests__/standalone-cache.test.js @@ -205,17 +205,36 @@ describe('standalone cache uses cacheStrategy', () => { afterEach(async () => { // clean up after each test await removeFile(expectedCacheFilePath); + await removeFile(newFileDest); + }); + + it('cacheStrategy is "metadata"', async () => { + const cacheStrategy = 'metadata'; + + await fs.copyFile(validFile, newFileDest); + const { results } = await standalone(getConfig({ cacheStrategy })); + + expect(results.some((file) => file.source === newFileDest)).toBe(true); + + // No content change, but file metadata id changed + await fs.utimes(newFileDest, Date.now(), Date.now()); + const { results: resultsCached } = await standalone(getConfig({ cacheStrategy })); + + expect(resultsCached.some((file) => file.source === newFileDest)).toBe(true); }); it('cacheStrategy is "content"', async () => { - await standalone(getConfig({ cacheStrategy: 'content' })); + const cacheStrategy = 'content'; - // Ensure cache file is created - expect(existsSync(expectedCacheFilePath)).toBe(true); + await fs.copyFile(validFile, newFileDest); + const { results } = await standalone(getConfig({ cacheStrategy })); - const { cache } = fCache.createFromFile(expectedCacheFilePath, undefined, true); + expect(results.some((file) => file.source === newFileDest)).toBe(true); - expect(typeof cache.getKey(validFile)).toBe('object'); - expect(cache.getKey(validFile)).toBeTruthy(); + // No content change, but file metadata id changed + await fs.utimes(newFileDest, Date.now(), Date.now()); + const { results: resultsCached } = await standalone(getConfig({ cacheStrategy })); + + expect(resultsCached.some((file) => file.source === newFileDest)).toBe(false); }); }); From aadd1e63944f89a8ca5b981fa66dd49fc78c4659 Mon Sep 17 00:00:00 2001 From: kaorun343 <5625395+kaorun343@users.noreply.github.com> Date: Tue, 27 Sep 2022 01:03:31 +0900 Subject: [PATCH 17/19] Update lib/__tests__/standalone-cache.test.js Co-authored-by: Masafumi Koba <473530+ybiquitous@users.noreply.github.com> --- lib/__tests__/standalone-cache.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/__tests__/standalone-cache.test.js b/lib/__tests__/standalone-cache.test.js index 85fd2a658c..f3b783a4d6 100644 --- a/lib/__tests__/standalone-cache.test.js +++ b/lib/__tests__/standalone-cache.test.js @@ -217,7 +217,7 @@ describe('standalone cache uses cacheStrategy', () => { expect(results.some((file) => file.source === newFileDest)).toBe(true); // No content change, but file metadata id changed - await fs.utimes(newFileDest, Date.now(), Date.now()); + await fs.utimes(newFileDest, new Date(), new Date()); const { results: resultsCached } = await standalone(getConfig({ cacheStrategy })); expect(resultsCached.some((file) => file.source === newFileDest)).toBe(true); From b081ddfcbf05c78d6fb0a878e7ee5d01a14e09fa Mon Sep 17 00:00:00 2001 From: kaorun343 <5625395+kaorun343@users.noreply.github.com> Date: Tue, 27 Sep 2022 01:03:36 +0900 Subject: [PATCH 18/19] Update lib/__tests__/standalone-cache.test.js Co-authored-by: Masafumi Koba <473530+ybiquitous@users.noreply.github.com> --- lib/__tests__/standalone-cache.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/__tests__/standalone-cache.test.js b/lib/__tests__/standalone-cache.test.js index f3b783a4d6..d219fc7a32 100644 --- a/lib/__tests__/standalone-cache.test.js +++ b/lib/__tests__/standalone-cache.test.js @@ -232,7 +232,7 @@ describe('standalone cache uses cacheStrategy', () => { expect(results.some((file) => file.source === newFileDest)).toBe(true); // No content change, but file metadata id changed - await fs.utimes(newFileDest, Date.now(), Date.now()); + await fs.utimes(newFileDest, new Date(), new Date()); const { results: resultsCached } = await standalone(getConfig({ cacheStrategy })); expect(resultsCached.some((file) => file.source === newFileDest)).toBe(false); From a6663ca39c74cbf9af17c062a68f03ebfa6e8d4a Mon Sep 17 00:00:00 2001 From: kaorun343 <5625395+kaorun343@users.noreply.github.com> Date: Wed, 28 Sep 2022 05:51:52 +0900 Subject: [PATCH 19/19] Add more cacheStrategy testgit --- lib/__tests__/standalone-cache.test.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/__tests__/standalone-cache.test.js b/lib/__tests__/standalone-cache.test.js index d219fc7a32..cfcc536300 100644 --- a/lib/__tests__/standalone-cache.test.js +++ b/lib/__tests__/standalone-cache.test.js @@ -208,6 +208,12 @@ describe('standalone cache uses cacheStrategy', () => { await removeFile(newFileDest); }); + it('cacheStrategy is invalid', async () => { + await expect(standalone(getConfig({ cacheStrategy: 'foo' }))).rejects.toThrow( + '"foo" cache strategy is unsupported. Specify either "metadata" or "content"', + ); + }); + it('cacheStrategy is "metadata"', async () => { const cacheStrategy = 'metadata';