Skip to content

Commit

Permalink
add tests for cacheManager removeCache
Browse files Browse the repository at this point in the history
  • Loading branch information
TMisiukiewicz committed Nov 27, 2023
1 parent 8ffddb4 commit 366172c
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
34 changes: 34 additions & 0 deletions packages/cli-tools/src/__tests__/cacheManager.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});
17 changes: 14 additions & 3 deletions packages/cli-tools/src/cacheManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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.`,
);
}
}

Expand All @@ -76,4 +86,5 @@ export default {
get,
set,
removeProjectCache,
getCacheRootPath,
};
6 changes: 3 additions & 3 deletions packages/cli/src/commands/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'});
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 366172c

Please sign in to comment.