From 366172ceccb392f9fbb9a179f59321ae5360d33e Mon Sep 17 00:00:00 2001 From: Tomasz Misiukiewicz Date: Mon, 27 Nov 2023 17:36:45 +0100 Subject: [PATCH] add tests for cacheManager removeCache --- .../src/__tests__/cacheManager.test.ts | 34 +++++++++++++++++++ packages/cli-tools/src/cacheManager.ts | 17 ++++++++-- packages/cli/src/commands/init/init.ts | 6 ++-- 3 files changed, 51 insertions(+), 6 deletions(-) create mode 100644 packages/cli-tools/src/__tests__/cacheManager.test.ts diff --git a/packages/cli-tools/src/__tests__/cacheManager.test.ts b/packages/cli-tools/src/__tests__/cacheManager.test.ts new file mode 100644 index 0000000000..533299d97f --- /dev/null +++ b/packages/cli-tools/src/__tests__/cacheManager.test.ts @@ -0,0 +1,34 @@ +import fs from 'fs'; +import path from 'path'; +import cacheManager from '../cacheManager'; + +const projectName = 'Project1'; +const cachePath = '.react-native-cli/cache'; +const fullPath = path.join(cachePath, projectName); + +describe('cacheManager', () => { + beforeEach(() => { + jest.restoreAllMocks(); + }); + + test('should remove cache if it exists', () => { + jest.spyOn(fs, 'existsSync').mockReturnValue(true); + jest.spyOn(fs, 'rmSync').mockImplementation(() => {}); + jest + .spyOn(path, 'resolve') + .mockReturnValue(path.join(cachePath, projectName)); + + cacheManager.removeProjectCache(projectName); + + expect(fs.rmSync).toHaveBeenCalledWith(fullPath, {recursive: true}); + }); + + test('should not remove cache if it does not exist', () => { + jest.spyOn(fs, 'existsSync').mockReturnValue(false); + jest.spyOn(fs, 'rmSync').mockImplementation(() => {}); + + cacheManager.removeProjectCache(projectName); + + expect(fs.rmSync).not.toHaveBeenCalled(); + }); +}); diff --git a/packages/cli-tools/src/cacheManager.ts b/packages/cli-tools/src/cacheManager.ts index 21587dc91f..f227445eea 100644 --- a/packages/cli-tools/src/cacheManager.ts +++ b/packages/cli-tools/src/cacheManager.ts @@ -2,6 +2,7 @@ import path from 'path'; import fs from 'fs'; import os from 'os'; import appDirs from 'appdirsjs'; +import chalk from 'chalk'; import logger from './logger'; type CacheKey = 'eTag' | 'lastChecked' | 'latestVersion' | 'dependencies'; @@ -49,10 +50,19 @@ function getCacheRootPath() { } function removeProjectCache(name: string) { - const fullPath = path.resolve(getCacheRootPath(), name); + const cacheRootPath = getCacheRootPath(); + try { + const fullPath = path.resolve(cacheRootPath, name); - if (fs.existsSync(fullPath)) { - fs.rmSync(fullPath, {recursive: true}); + if (fs.existsSync(fullPath)) { + fs.rmSync(fullPath, {recursive: true}); + } + } catch { + logger.error( + `Failed to remove cache for ${name}. If you experience any issues when running freshly initialized project, please remove ${chalk.underline( + path.join(cacheRootPath, name), + )} folder manually.`, + ); } } @@ -76,4 +86,5 @@ export default { get, set, removeProjectCache, + getCacheRootPath, }; diff --git a/packages/cli/src/commands/init/init.ts b/packages/cli/src/commands/init/init.ts index 701696240c..c772bde01f 100644 --- a/packages/cli/src/commands/init/init.ts +++ b/packages/cli/src/commands/init/init.ts @@ -136,6 +136,9 @@ async function createFromTemplate({ packageManager = 'npm'; } + // if the project with the name already has cache, remove the cache to avoid problems with pods installation + cacheManager.removeProjectCache(projectName); + const projectDirectory = await setProjectDirectory(directory); const loader = getLoader({text: 'Downloading template'}); @@ -394,9 +397,6 @@ export default (async function initialize( projectName = projName; } - // if the project with the name already has cache, remove the cache to avoid problems with pods installation - cacheManager.removeProjectCache(projectName); - validateProjectName(projectName); if (!!options.template && !!options.version) {