From 051c575bd23e2c4ef44624766249fbe7ab6d362a Mon Sep 17 00:00:00 2001 From: Erik Ritter Date: Sat, 20 Apr 2019 19:41:48 -0700 Subject: [PATCH 1/4] Replace colons in transform cache filenames --- CHANGELOG.md | 1 + .../jest-transform/src/ScriptTransformer.ts | 8 ++--- .../src/__tests__/script_transformer.test.js | 29 +++++++++++++++++++ 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ad6df5adf66c..522091252f85 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ - `[jest-core]` Make `detectOpenHandles` imply `runInBand` ([#8283](https://github.com/facebook/jest/pull/8283)) - `[jest-haste-map]` Fix the `mapper` option which was incorrectly ignored ([#8299](https://github.com/facebook/jest/pull/8299)) - `[jest-jasmine2]` Fix describe return value warning being shown if the describe function throws ([#8335](https://github.com/facebook/jest/pull/8335)) +- `[jest-transform]` Replace colons in transform cache filenames to support Windows ([#8353](https://github.com/facebook/jest/pull/8353)) ### Chore & Maintenance diff --git a/packages/jest-transform/src/ScriptTransformer.ts b/packages/jest-transform/src/ScriptTransformer.ts index 3e0e7c4d8b93..dee421aab0b7 100644 --- a/packages/jest-transform/src/ScriptTransformer.ts +++ b/packages/jest-transform/src/ScriptTransformer.ts @@ -121,11 +121,11 @@ export default class ScriptTransformer { // Create sub folders based on the cacheKey to avoid creating one // directory with many files. const cacheDir = path.join(baseCacheDir, cacheKey[0] + cacheKey[1]); + const cacheFilenamePrefix = path + .basename(filename, path.extname(filename)) + .replace(/:/g, '_COLON_'); const cachePath = slash( - path.join( - cacheDir, - path.basename(filename, path.extname(filename)) + '_' + cacheKey, - ), + path.join(cacheDir, cacheFilenamePrefix + '_' + cacheKey), ); createDirectory(cacheDir); diff --git a/packages/jest-transform/src/__tests__/script_transformer.test.js b/packages/jest-transform/src/__tests__/script_transformer.test.js index 994d28d7066c..bcacc14fb8ee 100644 --- a/packages/jest-transform/src/__tests__/script_transformer.test.js +++ b/packages/jest-transform/src/__tests__/script_transformer.test.js @@ -152,6 +152,7 @@ describe('ScriptTransformer', () => { mockFs = object({ '/fruits/banana.js': ['module.exports = "banana";'].join('\n'), + '/fruits/banana:colon.js': ['module.exports = "bananaColon";'].join('\n'), '/fruits/grapefruit.js': [ 'module.exports = function () { return "grapefruit"; }', ].join('\n'), @@ -532,6 +533,34 @@ describe('ScriptTransformer', () => { expect(writeFileAtomic.sync).toBeCalled(); }); + it('reads values from the cache when the file contains colons', () => { + const transformConfig = { + ...config, + transform: [['^.+\\.js$', 'test_preprocessor']], + }; + let scriptTransformer = new ScriptTransformer(transformConfig); + scriptTransformer.transform('/fruits/banana:colon.js', {}); + + const cachePath = getCachePath(mockFs, config); + expect(writeFileAtomic.sync).toBeCalled(); + expect(writeFileAtomic.sync.mock.calls[0][0]).toBe(cachePath); + + // Cache the state in `mockFsCopy` + const mockFsCopy = mockFs; + jest.resetModuleRegistry(); + reset(); + + // Restore the cached fs + mockFs = mockFsCopy; + scriptTransformer = new ScriptTransformer(transformConfig); + scriptTransformer.transform('/fruits/banana:colon.js', {}); + + expect(fs.readFileSync).toHaveBeenCalledTimes(2); + expect(fs.readFileSync).toBeCalledWith('/fruits/banana:colon.js', 'utf8'); + expect(fs.readFileSync).toBeCalledWith(cachePath, 'utf8'); + expect(writeFileAtomic.sync).not.toBeCalled(); + }); + it('does not reuse the in-memory cache between different projects', () => { const scriptTransformer = new ScriptTransformer({ ...config, From 6f47664873f477d8fdbdee8dc60ae5cddc421c52 Mon Sep 17 00:00:00 2001 From: Erik Ritter Date: Sun, 21 Apr 2019 14:03:09 -0700 Subject: [PATCH 2/4] Include filename in cache key --- packages/jest-transform/src/ScriptTransformer.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/jest-transform/src/ScriptTransformer.ts b/packages/jest-transform/src/ScriptTransformer.ts index dee421aab0b7..d1e1c50d1aa0 100644 --- a/packages/jest-transform/src/ScriptTransformer.ts +++ b/packages/jest-transform/src/ScriptTransformer.ts @@ -94,6 +94,7 @@ export default class ScriptTransformer { rootDir: this._config.rootDir, }), ) + .update(filename) .update(CACHE_VERSION) .digest('hex'); } else { @@ -102,6 +103,7 @@ export default class ScriptTransformer { .update(fileData) .update(configString) .update(instrument ? 'instrument' : '') + .update(filename) .update(CACHE_VERSION) .digest('hex'); } From 0737f6b809278059f38db80b0f198d492030a1ea Mon Sep 17 00:00:00 2001 From: Erik Ritter Date: Mon, 22 Apr 2019 10:13:41 -0700 Subject: [PATCH 3/4] Remove all special characters from filename prefix --- CHANGELOG.md | 2 +- packages/jest-transform/src/ScriptTransformer.ts | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cb909d7841ac..23d5543773f0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,7 +19,7 @@ - `[jest-haste-map]` Fix the `mapper` option which was incorrectly ignored ([#8299](https://github.com/facebook/jest/pull/8299)) - `[jest-jasmine2]` Fix describe return value warning being shown if the describe function throws ([#8335](https://github.com/facebook/jest/pull/8335)) - `[jest-environment-jsdom]` Re-declare global prototype of JSDOMEnvironment ([#8352](https://github.com/facebook/jest/pull/8352)) -- `[jest-transform]` Replace colons in transform cache filenames to support Windows ([#8353](https://github.com/facebook/jest/pull/8353)) +- `[jest-transform]` Replace special characters in transform cache filenames to support Windows ([#8353](https://github.com/facebook/jest/pull/8353)) ### Chore & Maintenance diff --git a/packages/jest-transform/src/ScriptTransformer.ts b/packages/jest-transform/src/ScriptTransformer.ts index d1e1c50d1aa0..7748d2cb2439 100644 --- a/packages/jest-transform/src/ScriptTransformer.ts +++ b/packages/jest-transform/src/ScriptTransformer.ts @@ -94,7 +94,6 @@ export default class ScriptTransformer { rootDir: this._config.rootDir, }), ) - .update(filename) .update(CACHE_VERSION) .digest('hex'); } else { @@ -125,7 +124,7 @@ export default class ScriptTransformer { const cacheDir = path.join(baseCacheDir, cacheKey[0] + cacheKey[1]); const cacheFilenamePrefix = path .basename(filename, path.extname(filename)) - .replace(/:/g, '_COLON_'); + .replace(/\W/g, ''); const cachePath = slash( path.join(cacheDir, cacheFilenamePrefix + '_' + cacheKey), ); From 944291cac968109d2973a92b471ba013a807a45f Mon Sep 17 00:00:00 2001 From: Erik Ritter Date: Wed, 24 Apr 2019 15:15:17 -0700 Subject: [PATCH 4/4] Update CHANGELOG.md --- CHANGELOG.md | 1 - 1 file changed, 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8feadbf831cc..d658ff9b8de8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,7 +24,6 @@ - `[expect]` Extract names of async and generator functions ([#8362](https://github.com/facebook/jest/pull/8362)) - `[jest-transform]` Replace special characters in transform cache filenames to support Windows ([#8353](https://github.com/facebook/jest/pull/8353)) - ### Chore & Maintenance - `[expect]` Fix label and add opposite assertion for toEqual tests ([#8288](https://github.com/facebook/jest/pull/8288))