Skip to content

Commit

Permalink
refactor: remove if guard that checks if dir exists
Browse files Browse the repository at this point in the history
  • Loading branch information
Ilianna Papastefanou committed Mar 26, 2021
1 parent ef0bcbb commit f74a6b5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 19 deletions.
14 changes: 6 additions & 8 deletions src/cli/commands/test/iac-local-execution/local-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,11 @@ export async function initLocalCache(): Promise<void> {
export function cleanLocalCache() {
// path to delete is hardcoded for now
const iacPath: fs.PathLike = path.join(`${process.cwd()}`, '.iac-data');
if (fs.existsSync(iacPath) && fs.lstatSync(iacPath).isDirectory()) {
try {
// when we support Node version >= 12.10.0 , we can replace rimraf
// with the native fs.rmdirSync(path, {recursive: true})
rimraf.sync(iacPath);
} catch (e) {
debug('The local cache directory could not be deleted');
}
try {
// when we support Node version >= 12.10.0 , we can replace rimraf
// with the native fs.rmdirSync(path, {recursive: true})
rimraf.sync(iacPath);
} catch (e) {
debug('The local cache directory could not be deleted');
}
}
13 changes: 2 additions & 11 deletions test/jest/unit/iac-unit-tests/local-cache.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,13 @@ describe('initLocalCache - downloads bundle successfully', () => {

it('cleans up the custom folder after finishes', () => {
const iacPath: fs.PathLike = path.join(`${process.cwd()}`, '.iac-data');
const stats: fs.Stats = new fs.Stats();
stats.isDirectory = jest.fn().mockReturnValue(true);
jest.spyOn(fs, 'lstatSync').mockReturnValueOnce(stats);
const spy = jest.spyOn(rimraf, 'sync');

localCacheModule.cleanLocalCache();

expect(spy).toHaveBeenCalledWith(iacPath);
jest.restoreAllMocks();
expect(fs.existsSync(iacPath)).toBeFalsy();
});
});

Expand All @@ -57,12 +56,4 @@ describe('initLocalCache - Missing IaC local cache data', () => {
expect(fileUtilsModule.extractBundle).not.toHaveBeenCalled();
expect(promise).rejects.toThrow(error);
});

it('does not delete the local cacheDir if it does not exist', () => {
const spy = jest.spyOn(rimraf, 'sync');

localCacheModule.cleanLocalCache();

expect(spy).not.toHaveBeenCalled();
});
});

0 comments on commit f74a6b5

Please sign in to comment.