Skip to content

Commit

Permalink
Fix tests on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
juanjoDiaz committed Feb 11, 2021
1 parent 1ebd53b commit b600c05
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 62 deletions.
7 changes: 1 addition & 6 deletions .github/workflows/on-push.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,8 @@ jobs:

strategy:
matrix:
os: [ubuntu-latest]
os: [ubuntu-latest, windows-latest] # macos-latest is too slow
node-version: [10.x, 12.x, 13.x, 14.x, 15.x]
include:
- os: windows-latest
node-version: 14.x
- os: macos-latest
node-version: 14.x

steps:
- uses: actions/checkout@v2
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class Middleware {
this.serverless.cli.log(`Middleware: setting ${handlers.length} middlewares for function ${fn.name}`);

const middlewareBuilder = this.middlewareBuilders[extension];
const handlerPath = `${this.middlewareOpts.pathFolder}/${fn.name}.${extension}`;
const handlerPath = path.join(this.middlewareOpts.pathFolder, `${fn.name}.${extension}`);
const handler = middlewareBuilder(handlers, this.middlewareOpts.pathToRoot);
await fsAsync.writeFile(handlerPath, handler);
// eslint-disable-next-line no-param-reassign
Expand Down
6 changes: 3 additions & 3 deletions test/cleanupHooks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe.each([
await plugin.hooks[hook]();

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

Expand Down Expand Up @@ -76,7 +76,7 @@ describe.each([
await plugin.hooks[hook]();

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

Expand Down Expand Up @@ -109,7 +109,7 @@ describe.each([
await plugin.hooks[hook]();

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

Expand Down
59 changes: 30 additions & 29 deletions test/processAllFunctionsHooks.node.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ jest.mock('fs', () => ({
},
}));
const fs = require('fs');
const path = require('path');

const fsAsync = fs.promises;
const Middleware = require('../src/index');
Expand All @@ -29,7 +30,7 @@ describe.each([
});

describe('error cases', () => {
beforeEach(() => fs.existsSync.mockImplementation((path) => path.endsWith('.js')));
beforeEach(() => fs.existsSync.mockImplementation((filePath) => filePath.endsWith('.js')));

it('should error on unsupported runtimes', async () => {
const serverless = getServerlessConfig({
Expand All @@ -55,7 +56,7 @@ describe.each([
});

it('should error on unsupported node extensions', async () => {
fs.existsSync.mockImplementation((path) => !path.startsWith('middleware1'));
fs.existsSync.mockImplementation((filePath) => !filePath.startsWith('middleware1'));
const serverless = getServerlessConfig({
service: {
functions: {
Expand All @@ -78,7 +79,7 @@ describe.each([
});

it('should error on invalid handler', async () => {
fs.existsSync.mockImplementation((path) => !path.startsWith('middleware1'));
fs.existsSync.mockImplementation((filePath) => !filePath.startsWith('middleware1'));
const serverless = getServerlessConfig({
service: {
functions: {
Expand All @@ -102,7 +103,7 @@ describe.each([
});

describe('without functions', () => {
beforeEach(() => fs.existsSync.mockImplementation((path) => path.endsWith('.js')));
beforeEach(() => fs.existsSync.mockImplementation((filePath) => filePath.endsWith('.js')));
it('should process handlers that contain arrays and do nothing with standard handlers', async () => {
const serverless = getServerlessConfig({
service: {
Expand All @@ -122,7 +123,7 @@ describe.each([
['js', GeneratedFunctionTester.fromJavaScript],
['ts', GeneratedFunctionTester.fromTypeScript],
])('Node.js extension: %s', (extension, functionTesterFrom) => {
beforeEach(() => fs.existsSync.mockImplementation((path) => path.endsWith(`.${extension}`)));
beforeEach(() => fs.existsSync.mockImplementation((filePath) => filePath.endsWith(`.${extension}`)));

describe('without pre/pos', () => {
it('should process handlers that contain arrays and do nothing with standard handlers', async () => {
Expand All @@ -147,9 +148,9 @@ describe.each([
expect(plugin.serverless.service.functions.someFunc1.handler).toEqual('.middleware/someFunc1.handler');
expect(plugin.serverless.service.functions.someFunc2.handler).toEqual('someFunc2.handler');
expect(fsAsync.mkdir).toHaveBeenCalledTimes(1);
expect(fsAsync.mkdir).toHaveBeenNthCalledWith(1, 'testPath/.middleware', { recursive: true });
expect(fsAsync.mkdir).toHaveBeenNthCalledWith(1, path.join('testPath', '.middleware'), { recursive: true });
expect(fsAsync.writeFile).toHaveBeenCalledTimes(1);
expect(fsAsync.writeFile).toHaveBeenNthCalledWith(1, `testPath/.middleware/someFunc1.${extension}`, expect.any(String));
expect(fsAsync.writeFile).toHaveBeenNthCalledWith(1, path.join('testPath', '.middleware', `someFunc1.${extension}`), expect.any(String));

const event = {};
const context = {};
Expand Down Expand Up @@ -204,9 +205,9 @@ describe.each([
expect(plugin.serverless.service.functions.someFunc1.handler).toEqual('.middleware/someFunc1.handler');
expect(plugin.serverless.service.functions.someFunc2.handler).toEqual('someFunc2.handler');
expect(fsAsync.mkdir).toHaveBeenCalledTimes(1);
expect(fsAsync.mkdir).toHaveBeenNthCalledWith(1, 'testPath/.middleware', { recursive: true });
expect(fsAsync.mkdir).toHaveBeenNthCalledWith(1, path.join('testPath', '.middleware'), { recursive: true });
expect(fsAsync.writeFile).toHaveBeenCalledTimes(1);
expect(fsAsync.writeFile).toHaveBeenNthCalledWith(1, `testPath/.middleware/someFunc1.${extension}`, expect.any(String));
expect(fsAsync.writeFile).toHaveBeenNthCalledWith(1, path.join('testPath', '.middleware', `someFunc1.${extension}`), expect.any(String));

const event = {};
const context = {};
Expand Down Expand Up @@ -277,9 +278,9 @@ describe.each([
expect(plugin.serverless.service.functions.someFunc1.handler).toEqual('.middleware/someFunc1.handler');
expect(plugin.serverless.service.functions.someFunc2.handler).toEqual('someFunc2.handler');
expect(fsAsync.mkdir).toHaveBeenCalledTimes(1);
expect(fsAsync.mkdir).toHaveBeenNthCalledWith(1, 'testPath/.middleware', { recursive: true });
expect(fsAsync.mkdir).toHaveBeenNthCalledWith(1, path.join('testPath', '.middleware'), { recursive: true });
expect(fsAsync.writeFile).toHaveBeenCalledTimes(1);
expect(fsAsync.writeFile).toHaveBeenNthCalledWith(1, `testPath/.middleware/someFunc1.${extension}`, expect.any(String));
expect(fsAsync.writeFile).toHaveBeenNthCalledWith(1, path.join('testPath', '.middleware', `someFunc1.${extension}`), expect.any(String));

const event = {};
const context = {};
Expand Down Expand Up @@ -345,10 +346,10 @@ describe.each([
expect(plugin.serverless.service.functions.someFunc1.handler).toEqual('.middleware/someFunc1.handler');
expect(plugin.serverless.service.functions.someFunc2.handler).toEqual('.middleware/someFunc2.handler');
expect(fsAsync.mkdir).toHaveBeenCalledTimes(1);
expect(fsAsync.mkdir).toHaveBeenNthCalledWith(1, 'testPath/.middleware', { recursive: true });
expect(fsAsync.mkdir).toHaveBeenNthCalledWith(1, path.join('testPath', '.middleware'), { recursive: true });
expect(fsAsync.writeFile).toHaveBeenCalledTimes(2);
expect(fsAsync.writeFile).toHaveBeenNthCalledWith(1, `testPath/.middleware/someFunc1.${extension}`, expect.any(String));
expect(fsAsync.writeFile).toHaveBeenNthCalledWith(2, `testPath/.middleware/someFunc2.${extension}`, expect.any(String));
expect(fsAsync.writeFile).toHaveBeenNthCalledWith(1, path.join('testPath', '.middleware', `someFunc1.${extension}`), expect.any(String));
expect(fsAsync.writeFile).toHaveBeenNthCalledWith(2, path.join('testPath', '.middleware', `someFunc2.${extension}`), expect.any(String));

const event = {};
const context = {};
Expand Down Expand Up @@ -439,10 +440,10 @@ describe.each([
expect(plugin.serverless.service.functions.someFunc1.handler).toEqual('.middleware/someFunc1.handler');
expect(plugin.serverless.service.functions.someFunc2.handler).toEqual('.middleware/someFunc2.handler');
expect(fsAsync.mkdir).toHaveBeenCalledTimes(1);
expect(fsAsync.mkdir).toHaveBeenNthCalledWith(1, 'testPath/.middleware', { recursive: true });
expect(fsAsync.mkdir).toHaveBeenNthCalledWith(1, path.join('testPath', '.middleware'), { recursive: true });
expect(fsAsync.writeFile).toHaveBeenCalledTimes(2);
expect(fsAsync.writeFile).toHaveBeenNthCalledWith(1, `testPath/.middleware/someFunc1.${extension}`, expect.any(String));
expect(fsAsync.writeFile).toHaveBeenNthCalledWith(2, `testPath/.middleware/someFunc2.${extension}`, expect.any(String));
expect(fsAsync.writeFile).toHaveBeenNthCalledWith(1, path.join('testPath', '.middleware', `someFunc1.${extension}`), expect.any(String));
expect(fsAsync.writeFile).toHaveBeenNthCalledWith(2, path.join('testPath', '.middleware', `someFunc2.${extension}`), expect.any(String));

const event = {};
const context = {};
Expand Down Expand Up @@ -560,10 +561,10 @@ describe.each([
expect(plugin.serverless.service.functions.someFunc1.handler).toEqual('.middleware/someFunc1.handler');
expect(plugin.serverless.service.functions.someFunc2.handler).toEqual('.middleware/someFunc2.handler');
expect(fsAsync.mkdir).toHaveBeenCalledTimes(1);
expect(fsAsync.mkdir).toHaveBeenNthCalledWith(1, 'testPath/.middleware', { recursive: true });
expect(fsAsync.mkdir).toHaveBeenNthCalledWith(1, path.join('testPath', '.middleware'), { recursive: true });
expect(fsAsync.writeFile).toHaveBeenCalledTimes(2);
expect(fsAsync.writeFile).toHaveBeenNthCalledWith(1, `testPath/.middleware/someFunc1.${extension}`, expect.any(String));
expect(fsAsync.writeFile).toHaveBeenNthCalledWith(2, `testPath/.middleware/someFunc2.${extension}`, expect.any(String));
expect(fsAsync.writeFile).toHaveBeenNthCalledWith(1, path.join('testPath', '.middleware', `someFunc1.${extension}`), expect.any(String));
expect(fsAsync.writeFile).toHaveBeenNthCalledWith(2, path.join('testPath', '.middleware', `someFunc2.${extension}`), expect.any(String));

const event = {};
const context = {};
Expand Down Expand Up @@ -667,10 +668,10 @@ describe.each([
expect(plugin.serverless.service.functions.someFunc1.handler).toEqual('.middleware/someFunc1.handler');
expect(plugin.serverless.service.functions.someFunc2.handler).toEqual('.middleware/someFunc2.handler');
expect(fsAsync.mkdir).toHaveBeenCalledTimes(1);
expect(fsAsync.mkdir).toHaveBeenNthCalledWith(1, 'testPath/.middleware', { recursive: true });
expect(fsAsync.mkdir).toHaveBeenNthCalledWith(1, path.join('testPath', '.middleware'), { recursive: true });
expect(fsAsync.writeFile).toHaveBeenCalledTimes(2);
expect(fsAsync.writeFile).toHaveBeenNthCalledWith(1, `testPath/.middleware/someFunc1.${extension}`, expect.any(String));
expect(fsAsync.writeFile).toHaveBeenNthCalledWith(2, `testPath/.middleware/someFunc2.${extension}`, expect.any(String));
expect(fsAsync.writeFile).toHaveBeenNthCalledWith(1, path.join('testPath', '.middleware', `someFunc1.${extension}`), expect.any(String));
expect(fsAsync.writeFile).toHaveBeenNthCalledWith(2, path.join('testPath', '.middleware', `someFunc2.${extension}`), expect.any(String));

const event = {};
const context = {};
Expand Down Expand Up @@ -759,10 +760,10 @@ describe.each([
expect(plugin.serverless.service.functions.someFunc1.handler).toEqual('.middleware/someFunc1.handler');
expect(plugin.serverless.service.functions.someFunc2.handler).toEqual('.middleware/someFunc2.handler');
expect(fsAsync.mkdir).toHaveBeenCalledTimes(1);
expect(fsAsync.mkdir).toHaveBeenNthCalledWith(1, 'testPath/.middleware', { recursive: true });
expect(fsAsync.mkdir).toHaveBeenNthCalledWith(1, path.join('testPath', '.middleware'), { recursive: true });
expect(fsAsync.writeFile).toHaveBeenCalledTimes(2);
expect(fsAsync.writeFile).toHaveBeenNthCalledWith(1, `testPath/.middleware/someFunc1.${extension}`, expect.any(String));
expect(fsAsync.writeFile).toHaveBeenNthCalledWith(2, `testPath/.middleware/someFunc2.${extension}`, expect.any(String));
expect(fsAsync.writeFile).toHaveBeenNthCalledWith(1, path.join('testPath', '.middleware', `someFunc1.${extension}`), expect.any(String));
expect(fsAsync.writeFile).toHaveBeenNthCalledWith(2, path.join('testPath', '.middleware', `someFunc2.${extension}`), expect.any(String));

const event = {};
const context = {};
Expand Down Expand Up @@ -879,10 +880,10 @@ describe.each([
expect(plugin.serverless.service.functions.someFunc1.handler).toEqual('.middleware/someFunc1.handler');
expect(plugin.serverless.service.functions.someFunc2.handler).toEqual('.middleware/someFunc2.handler');
expect(fsAsync.mkdir).toHaveBeenCalledTimes(1);
expect(fsAsync.mkdir).toHaveBeenNthCalledWith(1, 'testPath/.middleware', { recursive: true });
expect(fsAsync.mkdir).toHaveBeenNthCalledWith(1, path.join('testPath', '.middleware'), { recursive: true });
expect(fsAsync.writeFile).toHaveBeenCalledTimes(2);
expect(fsAsync.writeFile).toHaveBeenNthCalledWith(1, `testPath/.middleware/someFunc1.${extension}`, expect.any(String));
expect(fsAsync.writeFile).toHaveBeenNthCalledWith(2, `testPath/.middleware/someFunc2.${extension}`, expect.any(String));
expect(fsAsync.writeFile).toHaveBeenNthCalledWith(1, path.join('testPath', '.middleware', `someFunc1.${extension}`), expect.any(String));
expect(fsAsync.writeFile).toHaveBeenNthCalledWith(2, path.join('testPath', '.middleware', `someFunc2.${extension}`), expect.any(String));

const event = {};
const context = {};
Expand Down
Loading

0 comments on commit b600c05

Please sign in to comment.