Skip to content

Commit 5a6448b

Browse files
committed
refactor(project): Do not create a CacheManager if cache mode is Off
1 parent 7f7fd16 commit 5a6448b

3 files changed

Lines changed: 60 additions & 1 deletion

File tree

packages/project/lib/build/cache/ProjectBuildCache.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ export default class ProjectBuildCache {
113113
* @public
114114
* @param {@ui5/project/specifications/Project} project Project instance
115115
* @param {string} buildSignature Build signature for the current build
116-
* @param {object} cacheManager Cache manager instance
116+
* @param {object|null} cacheManager Cache manager instance
117117
* @param {string} cacheMode Cache mode to use for building UI5 projects
118118
* @returns {Promise<@ui5/project/build/cache/ProjectBuildCache>} Initialized cache instance
119119
*/

packages/project/lib/build/helpers/BuildContext.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,9 @@ class BuildContext {
164164
}
165165

166166
async getCacheManager() {
167+
if (this._buildConfig.cache === Cache.Off) {
168+
return null;
169+
}
167170
if (this.#cacheManager) {
168171
return this.#cacheManager;
169172
}

packages/project/test/lib/build/helpers/BuildContext.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,62 @@ test("getProjectContext", async (t) => {
324324
t.is(projectBuildContext, projectBuildContext2);
325325
});
326326

327+
test("getCacheManager: Returns null when cache mode is 'Off'", async (t) => {
328+
const {BuildContext} = t.context;
329+
330+
const graph = {
331+
getRoot: () => ({getType: () => "library", getRootPath: () => ""}),
332+
};
333+
const buildContext = new BuildContext(graph, "taskRepository", {
334+
cache: "Off"
335+
});
336+
337+
const cacheManager = await buildContext.getCacheManager();
338+
t.is(cacheManager, null, "Returned null");
339+
t.is(t.context.CacheManagerCreate.callCount, 0,
340+
"CacheManager.create was not called");
341+
});
342+
343+
test("getCacheManager: Creates and caches CacheManager for default cache mode", async (t) => {
344+
const {BuildContext} = t.context;
345+
346+
const cacheManagerInstance = {};
347+
t.context.CacheManagerCreate.resetBehavior();
348+
t.context.CacheManagerCreate.resolves(cacheManagerInstance);
349+
350+
const graph = {
351+
getRoot: () => ({getType: () => "library", getRootPath: () => "/some/path"}),
352+
};
353+
const buildContext = new BuildContext(graph, "taskRepository");
354+
355+
const cacheManager1 = await buildContext.getCacheManager();
356+
t.is(cacheManager1, cacheManagerInstance, "Returned CacheManager instance");
357+
t.is(t.context.CacheManagerCreate.callCount, 1,
358+
"CacheManager.create was called once");
359+
360+
const cacheManager2 = await buildContext.getCacheManager();
361+
t.is(cacheManager2, cacheManagerInstance, "Returned same CacheManager instance");
362+
t.is(t.context.CacheManagerCreate.callCount, 1,
363+
"CacheManager.create was not called again on subsequent invocations");
364+
});
365+
366+
test("closeCacheManager: No-op when cache mode is 'Off'", async (t) => {
367+
const {BuildContext} = t.context;
368+
369+
const graph = {
370+
getRoot: () => ({getType: () => "library", getRootPath: () => ""}),
371+
};
372+
const buildContext = new BuildContext(graph, "taskRepository", {
373+
cache: "Off"
374+
});
375+
376+
await buildContext.getCacheManager();
377+
378+
t.notThrows(() => {
379+
buildContext.closeCacheManager();
380+
}, "closeCacheManager does not throw when no CacheManager was created");
381+
});
382+
327383
test("executeCleanupTasks", async (t) => {
328384
const {BuildContext} = t.context;
329385

0 commit comments

Comments
 (0)