|
10 | 10 | //------------------------------------------------------------------------------ |
11 | 11 |
|
12 | 12 | const assert = require("node:assert"); |
| 13 | +const fs = require("node:fs"); |
| 14 | +const Module = require("node:module"); |
| 15 | +const os = require("node:os"); |
13 | 16 | const path = require("node:path"); |
| 17 | +const vm = require("node:vm"); |
14 | 18 | const sinon = require("sinon"); |
15 | 19 | const { ConfigLoader } = require("../../../lib/config/config-loader"); |
16 | 20 | const { WarningService } = require("../../../lib/services/warning-service"); |
@@ -68,6 +72,63 @@ describe("ConfigLoader", () => { |
68 | 72 | }); |
69 | 73 |
|
70 | 74 | describe("loadConfigArrayForFile()", () => { |
| 75 | + it("should not error when require.cache is unavailable", async () => { |
| 76 | + const cwd = path.resolve(fixtureDir, "simple-valid-project-2"); |
| 77 | + const configLoaderPath = path.resolve( |
| 78 | + __dirname, |
| 79 | + "../../../lib/config/config-loader.js", |
| 80 | + ); |
| 81 | + const configLoaderSource = fs.readFileSync( |
| 82 | + configLoaderPath, |
| 83 | + "utf8", |
| 84 | + ); |
| 85 | + const module = { exports: {} }; |
| 86 | + const requireWithoutCache = |
| 87 | + Module.createRequire(configLoaderPath); |
| 88 | + |
| 89 | + requireWithoutCache.cache = void 0; |
| 90 | + |
| 91 | + const compiledWrapper = vm.runInThisContext( |
| 92 | + Module.wrap(configLoaderSource), |
| 93 | + { |
| 94 | + filename: path.join( |
| 95 | + os.tmpdir(), |
| 96 | + "eslint-config-loader-without-require-cache.js", |
| 97 | + ), |
| 98 | + importModuleDynamically: |
| 99 | + vm.constants.USE_MAIN_CONTEXT_DEFAULT_LOADER, |
| 100 | + }, |
| 101 | + ); |
| 102 | + |
| 103 | + compiledWrapper.call( |
| 104 | + module.exports, |
| 105 | + module.exports, |
| 106 | + requireWithoutCache, |
| 107 | + module, |
| 108 | + configLoaderPath, |
| 109 | + path.dirname(configLoaderPath), |
| 110 | + ); |
| 111 | + |
| 112 | + const { ConfigLoader: ConfigLoaderWithoutRequireCache } = |
| 113 | + module.exports; |
| 114 | + |
| 115 | + const configArray = |
| 116 | + await ConfigLoaderWithoutRequireCache.calculateConfigArray( |
| 117 | + path.resolve(cwd, "eslint.config.js"), |
| 118 | + cwd, |
| 119 | + { |
| 120 | + cwd, |
| 121 | + ignoreEnabled: true, |
| 122 | + warningService: new WarningService(), |
| 123 | + }, |
| 124 | + ); |
| 125 | + |
| 126 | + assert( |
| 127 | + Array.isArray(configArray), |
| 128 | + "Expected `calculateConfigArray()` to return a config array", |
| 129 | + ); |
| 130 | + }); |
| 131 | + |
71 | 132 | // https://github.com/eslint/eslint/issues/19025 |
72 | 133 | it("should lookup config file only once and create config array only once for multiple files in same directory", async () => { |
73 | 134 | const cwd = path.resolve(fixtureDir, "simple-valid-project-2"); |
|
0 commit comments