Skip to content

Commit

Permalink
refactor: replace deprecated fs.rmdir by fs.rm for recursive operations
Browse files Browse the repository at this point in the history
  • Loading branch information
juanjoDiaz committed Dec 20, 2021
1 parent b8653ea commit db92ad6
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 23 deletions.
3 changes: 2 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
coverage
coverage
test_integration
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class WarmUp {

await Promise.all(foldersToClean.map(async (folderToClean) => {
try {
await fs.rmdir(
await fs.rm(
path.join(this.serviceDir, folderToClean),
{ recursive: true },
);
Expand All @@ -133,7 +133,7 @@ class WarmUp {
foldersToClean.some((folder) => folder.startsWith('.warmup'))
&& (await fs.readdir(defaultDir)).length === 0
) {
await fs.rmdir(defaultDir, { recursive: true });
await fs.rm(defaultDir, { recursive: true });
}
} catch (err) {
if (err.code !== 'ENOENT') {
Expand Down
2 changes: 1 addition & 1 deletion test/bakcwardsCompatibility.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jest.mock('fs', () => ({
mkdir: jest.fn(),
unlink: jest.fn(),
writeFile: jest.fn(),
rmdir: jest.fn(),
rm: jest.fn(),
},
}));

Expand Down
2 changes: 1 addition & 1 deletion test/hook.warmupAddWamersAddWamers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jest.mock('fs', () => ({
mkdir: jest.fn(),
unlink: jest.fn(),
writeFile: jest.fn(),
rmdir: jest.fn(),
rm: jest.fn(),
},
}));
jest.mock('child_process', () => ({
Expand Down
36 changes: 18 additions & 18 deletions test/hook.warmupCleanupTempDirCleanup.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jest.mock('fs', () => ({
mkdir: jest.fn(),
readdir: jest.fn((file) => ((!file.endsWith('node_modules') && !file.endsWith('.warmup')) ? ['index.js', 'node_modules'] : [])),
writeFile: jest.fn(),
rmdir: jest.fn(),
rm: jest.fn(),
stat: jest.fn((file) => ({ isDirectory: () => !file.endsWith('.js') })),
},
}));
Expand All @@ -17,7 +17,7 @@ const { getServerlessConfig } = require('./utils/configUtils');
describe('Serverless warmup plugin warmup:cleanupTempDir:cleanup hook', () => {
beforeEach(() => {
fs.readdir.mockClear();
fs.rmdir.mockClear();
fs.rm.mockClear();
});

it('Should be called after package:createDeploymentArtifacts', async () => {
Expand Down Expand Up @@ -69,9 +69,9 @@ describe('Serverless warmup plugin warmup:cleanupTempDir:cleanup hook', () => {
await plugin.hooks['before:warmup:cleanupTempDir:cleanup']();
await plugin.hooks['warmup:cleanupTempDir:cleanup']();

expect(fs.rmdir).toHaveBeenCalledTimes(2);
expect(fs.rmdir).toHaveBeenNthCalledWith(1, path.join('testPath', '.warmup', 'default'), { recursive: true });
expect(fs.rmdir).toHaveBeenNthCalledWith(2, path.join('testPath', '.warmup'), { recursive: true });
expect(fs.rm).toHaveBeenCalledTimes(2);
expect(fs.rm).toHaveBeenNthCalledWith(1, path.join('testPath', '.warmup', 'default'), { recursive: true });
expect(fs.rm).toHaveBeenNthCalledWith(2, path.join('testPath', '.warmup'), { recursive: true });
expect(serverless.cli.log).not.toHaveBeenCalledWith(expect.stringMatching(/^WarmUp: Couldn't clean up temporary folder .*/));
});

Expand Down Expand Up @@ -100,13 +100,13 @@ describe('Serverless warmup plugin warmup:cleanupTempDir:cleanup hook', () => {
await plugin.hooks['before:warmup:cleanupTempDir:cleanup']();
await plugin.hooks['warmup:cleanupTempDir:cleanup']();

expect(fs.rmdir).toHaveBeenCalledTimes(1);
expect(fs.rmdir).toHaveBeenCalledWith(path.join('testPath', 'test-folder'), { recursive: true });
expect(fs.rm).toHaveBeenCalledTimes(1);
expect(fs.rm).toHaveBeenCalledWith(path.join('testPath', 'test-folder'), { recursive: true });
expect(serverless.cli.log).not.toHaveBeenCalledWith(expect.stringMatching(/^WarmUp: Couldn't clean up temporary folder .*/));
});

it('Should not error if couldn\'t clean up the custom temporary folder', async () => {
fs.rmdir.mockRejectedValueOnce(new Error('Folder couldn\'t be cleaned'));
fs.rm.mockRejectedValueOnce(new Error('Folder couldn\'t be cleaned'));
const serverless = getServerlessConfig({
service: {
custom: {
Expand All @@ -124,15 +124,15 @@ describe('Serverless warmup plugin warmup:cleanupTempDir:cleanup hook', () => {
await plugin.hooks['before:warmup:cleanupTempDir:cleanup']();
await plugin.hooks['warmup:cleanupTempDir:cleanup']();

expect(fs.rmdir).toHaveBeenCalledTimes(2);
expect(fs.rmdir).toHaveBeenCalledWith(path.join('testPath', '.warmup', 'default'), { recursive: true });
expect(fs.rmdir).toHaveBeenCalledWith(path.join('testPath', '.warmup'), { recursive: true });
expect(fs.rm).toHaveBeenCalledTimes(2);
expect(fs.rm).toHaveBeenCalledWith(path.join('testPath', '.warmup', 'default'), { recursive: true });
expect(fs.rm).toHaveBeenCalledWith(path.join('testPath', '.warmup'), { recursive: true });
});

it('Should ignore cleaning the warmer temporary folders if there was nothing to clean', async () => {
const err = new Error('Folder doesn\'t exist');
err.code = 'ENOENT';
fs.rmdir.mockRejectedValue(err);
fs.rm.mockRejectedValue(err);
const serverless = getServerlessConfig({
config: {
cli: {
Expand All @@ -155,9 +155,9 @@ describe('Serverless warmup plugin warmup:cleanupTempDir:cleanup hook', () => {
await plugin.hooks['before:warmup:cleanupTempDir:cleanup']();
await plugin.hooks['warmup:cleanupTempDir:cleanup']();

expect(fs.rmdir).toHaveBeenCalledTimes(2);
expect(fs.rmdir).toHaveBeenNthCalledWith(1, path.join('testPath', '.warmup', 'default'), { recursive: true });
expect(fs.rmdir).toHaveBeenNthCalledWith(2, path.join('testPath', '.warmup'), { recursive: true });
expect(fs.rm).toHaveBeenCalledTimes(2);
expect(fs.rm).toHaveBeenNthCalledWith(1, path.join('testPath', '.warmup', 'default'), { recursive: true });
expect(fs.rm).toHaveBeenNthCalledWith(2, path.join('testPath', '.warmup'), { recursive: true });
expect(serverless.cli.log).not.toHaveBeenCalledWith(expect.stringMatching(/^Middleware: Couldn't clean up temporary folder .*/));
});

Expand Down Expand Up @@ -185,8 +185,8 @@ describe('Serverless warmup plugin warmup:cleanupTempDir:cleanup hook', () => {
await plugin.hooks['before:warmup:cleanupTempDir:cleanup']();
await plugin.hooks['warmup:cleanupTempDir:cleanup']();

expect(fs.rmdir).toHaveBeenCalledTimes(1);
expect(fs.rmdir).toHaveBeenCalledWith(path.join('testPath', '.warmup', 'default'), { recursive: true });
expect(fs.rm).toHaveBeenCalledTimes(1);
expect(fs.rm).toHaveBeenCalledWith(path.join('testPath', '.warmup', 'default'), { recursive: true });
expect(serverless.cli.log).toHaveBeenCalledWith(expect.stringMatching(/^WarmUp: Couldn't clean up temporary folder .*/));
});

Expand Down Expand Up @@ -214,7 +214,7 @@ describe('Serverless warmup plugin warmup:cleanupTempDir:cleanup hook', () => {
await plugin.hooks['before:warmup:cleanupTempDir:cleanup']();
await plugin.hooks['warmup:cleanupTempDir:cleanup']();

expect(fs.rmdir).not.toHaveBeenCalled();
expect(fs.rm).not.toHaveBeenCalled();
expect(serverless.cli.log).not.toHaveBeenCalledWith(expect.stringMatching(/^WarmUp: Couldn't clean up temporary folder .*/));
});
});

0 comments on commit db92ad6

Please sign in to comment.