refactor: extract config cache into a dedicated module with clear/reset support#292
Conversation
…et support Replaces the bare module-level `let earlyConfig` in cli.ts with a dedicated `src/config/cache.ts` module that exports getCachedConfig, setCachedConfig, and clearCachedConfig. This gives tests a safe reset hook (clearCachedConfig) to prevent stale state from leaking across test cases if the caching pattern spreads to library modules. Closes #210
37589c5 to
4c72492
Compare
✅MegaLinter analysis: Success
Notices📣 MegaLinter 9.5.0 is out! Discover the new features and security recommendations in the release announcement. (Skip this info by defining See detailed reports in MegaLinter artifacts MegaLinter is graciously provided by OX Security |
JoshMock
left a comment
There was a problem hiding this comment.
A simpler implementation might be to update loadConfig to take an optional cached option, which is true by default (or a refresh option that's false by default). That way, config/loader.ts can handle its own caching internally and any place that needs to ensure it gets a fresh config every time doesn't have to import a separate module.
Per JoshMock's suggestion: delete cache.ts and handle caching inside
loadConfig itself via a refresh option (default false = use cache).
- loadConfig() returns the cached result on repeat calls
- loadConfig({ refresh: true }) bypasses cache and loads fresh
- clearConfigCache() exported from loader.ts for test cleanup
- cli.ts drops getCachedConfig/setCachedConfig imports; preAction hook
passes refresh: true when CLI flags override the defaults
- cache.test.ts rewired to test caching through loadConfig directly
|
Good suggestion, done. Deleted
|
Closes #210
Summary
src/cli.tsused a bare module-levellet earlyConfigto cache the early config load result and share it with thepreActionhook. This works fine today sincecli.tsis a top-level entry point, but if the same caching pattern were used in a library module there would be no way for tests to reset the cached state between runs — stale values could leak across test cases.This PR extracts the cache into
src/config/cache.tswith a clear public API:getCachedConfig()— returns the cachedLoadConfigResultorundefinedsetCachedConfig(result)— stores a result in the cacheclearCachedConfig()— resets the cache; intended for test cleanupcli.tsis updated to call these instead of reading/writing the bare variable directly. Runtime behaviour is unchanged.Changes
src/config/cache.ts(new) — dedicated cache modulesrc/cli.ts— replace barelet earlyConfigwithgetCachedConfig/setCachedConfigcallstest/config/cache.test.ts(new) — 7 unit tests covering all three exports (100 % line/branch/function coverage)