From 24f6db0642e4a58ba81ec958b4d9330fd4db4748 Mon Sep 17 00:00:00 2001 From: penw0lf Date: Thu, 10 Oct 2024 02:02:13 +0530 Subject: [PATCH 1/6] Added --function and -f switch to findHandler - solved #244 --- index.js | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index 3c1b279..da7f41d 100644 --- a/index.js +++ b/index.js @@ -434,13 +434,29 @@ class ServerlessWSGI { } findHandler() { - return _.findKey(this.serverless.service.functions, (fun) => - _.includes(fun.handler, "wsgi_handler.handler") - ); + const functionName = this.options.function || this.options.f; + + if (functionName) { + // If the function name is specified, return it directly + if (this.serverless.service.functions[functionName]) { + return functionName; + } else { + throw new Error(`Function "${functionName}" not found.`); + } + } else { + return _.findKey(this.serverless.service.functions, (fun) => + _.includes(fun.handler, "wsgi_handler.handler") + ); + } } invokeHandler(command, data, local) { - const handlerFunction = this.findHandler(); + let handlerFunction; + try { + handlerFunction = this.findHandler(); + } catch (error) { + return BbPromise.reject(error.message); + } if (!handlerFunction) { return BbPromise.reject( @@ -482,7 +498,7 @@ class ServerlessWSGI { // remotely, we get a string back and we want it to appear in the console as it would have // if it was invoked locally. // - // We capture stdout output in order to parse the array returned from the lambda invocation, + // We capture stdout output in order to parse the array returned from the lambda invocation, // then restore stdout. let output = ""; @@ -502,7 +518,6 @@ class ServerlessWSGI { ); /* eslint-enable no-unused-vars */ - return this.serverless.pluginManager .run(local ? ["invoke", "local"] : ["invoke"]) .then( From 41fe3abc6a31d5ab5c9a40ca316ec8c6c1f864c7 Mon Sep 17 00:00:00 2001 From: penw0lf Date: Thu, 10 Oct 2024 02:07:34 +0530 Subject: [PATCH 2/6] Revert "Added --function and -f switch to findHandler - solved #244" This reverts commit 24f6db0642e4a58ba81ec958b4d9330fd4db4748. --- index.js | 27 ++++++--------------------- 1 file changed, 6 insertions(+), 21 deletions(-) diff --git a/index.js b/index.js index da7f41d..3c1b279 100644 --- a/index.js +++ b/index.js @@ -434,29 +434,13 @@ class ServerlessWSGI { } findHandler() { - const functionName = this.options.function || this.options.f; - - if (functionName) { - // If the function name is specified, return it directly - if (this.serverless.service.functions[functionName]) { - return functionName; - } else { - throw new Error(`Function "${functionName}" not found.`); - } - } else { - return _.findKey(this.serverless.service.functions, (fun) => - _.includes(fun.handler, "wsgi_handler.handler") - ); - } + return _.findKey(this.serverless.service.functions, (fun) => + _.includes(fun.handler, "wsgi_handler.handler") + ); } invokeHandler(command, data, local) { - let handlerFunction; - try { - handlerFunction = this.findHandler(); - } catch (error) { - return BbPromise.reject(error.message); - } + const handlerFunction = this.findHandler(); if (!handlerFunction) { return BbPromise.reject( @@ -498,7 +482,7 @@ class ServerlessWSGI { // remotely, we get a string back and we want it to appear in the console as it would have // if it was invoked locally. // - // We capture stdout output in order to parse the array returned from the lambda invocation, + // We capture stdout output in order to parse the array returned from the lambda invocation, // then restore stdout. let output = ""; @@ -518,6 +502,7 @@ class ServerlessWSGI { ); /* eslint-enable no-unused-vars */ + return this.serverless.pluginManager .run(local ? ["invoke", "local"] : ["invoke"]) .then( From 1a9601ea3dcca22e82e4cc79b55bc8317ea9dc46 Mon Sep 17 00:00:00 2001 From: penw0lf Date: Thu, 10 Oct 2024 02:13:56 +0530 Subject: [PATCH 3/6] Added --function and -f switch to findHandler - solved #244 --- index.js | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 3c1b279..6d24c8e 100644 --- a/index.js +++ b/index.js @@ -434,13 +434,29 @@ class ServerlessWSGI { } findHandler() { - return _.findKey(this.serverless.service.functions, (fun) => - _.includes(fun.handler, "wsgi_handler.handler") - ); + const functionName = this.options.function || this.options.f; + + if (functionName) { + // If the function name is specified, return it directly + if (this.serverless.service.functions[functionName]) { + return functionName; + } else { + throw new Error(`Function "${functionName}" not found.`); + } + } else { + return _.findKey(this.serverless.service.functions, (fun) => + _.includes(fun.handler, "wsgi_handler.handler") + ); + } } invokeHandler(command, data, local) { - const handlerFunction = this.findHandler(); + let handlerFunction; + try { + handlerFunction = this.findHandler(); + } catch (error) { + return BbPromise.reject(error.message); + } if (!handlerFunction) { return BbPromise.reject( @@ -482,7 +498,7 @@ class ServerlessWSGI { // remotely, we get a string back and we want it to appear in the console as it would have // if it was invoked locally. // - // We capture stdout output in order to parse the array returned from the lambda invocation, + // We capture stdout output in order to parse the array returned from the lambda invocation, // then restore stdout. let output = ""; From cf4b544e4b4d4b13c7eb9518393be26f74f24d45 Mon Sep 17 00:00:00 2001 From: penw0lf Date: Thu, 10 Oct 2024 02:18:22 +0530 Subject: [PATCH 4/6] linter showing empty space removed from line 501(new) adding it --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 6d24c8e..872c75f 100644 --- a/index.js +++ b/index.js @@ -498,7 +498,7 @@ class ServerlessWSGI { // remotely, we get a string back and we want it to appear in the console as it would have // if it was invoked locally. // - // We capture stdout output in order to parse the array returned from the lambda invocation, + // We capture stdout output in order to parse the array returned from the lambda invocation, // then restore stdout. let output = ""; From caac0afa573398aabeae1b274e0f9e89738a8303 Mon Sep 17 00:00:00 2001 From: ayaan-qadri Date: Sat, 12 Oct 2024 17:46:35 +0530 Subject: [PATCH 5/6] Added test cases for PR / #261 --- index.test.js | 109 ++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 87 insertions(+), 22 deletions(-) diff --git a/index.test.js b/index.test.js index 8e20b88..f324522 100644 --- a/index.test.js +++ b/index.test.js @@ -1545,8 +1545,8 @@ describe("serverless-wsgi", () => { }); describe("exec", () => { - const mockCli = Object({log: () => {}}); - + const mockCli = Object({ log: () => {} }); + it("fails when invoked without command or file", () => { var plugin = new Plugin( { @@ -1681,7 +1681,7 @@ describe("serverless-wsgi", () => { }); describe("exec local", () => { - const mockCli = {log: () => {}}; + const mockCli = { log: () => {} }; it("fails when invoked without command or file", () => { var plugin = new Plugin( @@ -1788,7 +1788,7 @@ describe("serverless-wsgi", () => { }); describe("command", () => { - const mockCli = {log: () => {}}; + const mockCli = { log: () => {} }; it("fails when no wsgi handler is set", () => { var plugin = new Plugin( @@ -1915,7 +1915,7 @@ describe("serverless-wsgi", () => { }); describe("command local", () => { - const mockCli = {log: () => {}}; + const mockCli = { log: () => {} }; it("fails when no wsgi handler is set", () => { var plugin = new Plugin( @@ -2046,34 +2046,48 @@ describe("serverless-wsgi", () => { }); describe("manage", () => { - const mockCli = Object({log: () => {}}); + const mockCli = { + log: sinon.spy(), + }; - it("calls handler to execute manage commands remotely from argument", () => { - var plugin = new Plugin( + let plugin; + let sandbox; + + beforeEach(() => { + sandbox = sinon.createSandbox(); + plugin = new Plugin( { config: { servicePath: "/tmp" }, service: { provider: { runtime: "python2.7" }, custom: { wsgi: { app: "api.app" } }, - functions: { app: { handler: "wsgi_handler.handler" } }, + functions: { + app: { handler: "wsgi_handler.handler" }, + otherFunc: { handler: "other_handler.handler" }, + }, }, classes: { Error: Error }, cli: mockCli, pluginManager: { cliOptions: {}, - run: (command) => - new BbPromise((resolve) => { - expect(command).to.deep.equal(["invoke"]); - console.log('[0, "manage command output"]'); // eslint-disable-line no-console - resolve(); - }), + run: sandbox.stub().resolves(), }, }, { command: "check" } ); + }); + + afterEach(() => { + sandbox.restore(); + }); + + it("calls handler to execute manage commands remotely from argument", () => { + plugin.serverless.pluginManager.run.callsFake((command) => { + expect(command).to.deep.equal(["invoke"]); + console.log('[0, "manage command output"]'); + return BbPromise.resolve(); + }); - var sandbox = sinon.createSandbox(); - let loggerSpy = sandbox.spy(mockCli, "log"); return plugin.hooks["wsgi:manage:manage"]().then(() => { expect(plugin.serverless.pluginManager.cliOptions.f).to.equal("app"); expect(plugin.options.function).to.equal("app"); @@ -2083,14 +2097,65 @@ describe("serverless-wsgi", () => { expect(plugin.options.data).to.equal( '{"_serverless-wsgi":{"command":"manage","data":"check"}}' ); - expect(loggerSpy.calledWith("manage command output")).to.be.true; - sandbox.restore(); + expect(mockCli.log.calledWith("manage command output")).to.be.true; }); }); + + it("uses the function specified by --function", () => { + plugin.options.function = "otherFunc"; + + return plugin.hooks["wsgi:manage:manage"]().then(() => { + expect(plugin.serverless.pluginManager.cliOptions.f).to.equal( + "otherFunc" + ); + expect(plugin.options.function).to.equal("otherFunc"); + }); + }); + + it("uses the function specified by -f", () => { + plugin.options.f = "otherFunc"; + + return plugin.hooks["wsgi:manage:manage"]().then(() => { + expect(plugin.serverless.pluginManager.cliOptions.f).to.equal( + "otherFunc" + ); + expect(plugin.options.function).to.equal("otherFunc"); + }); + }); + + it("throws an error when specified function is not found", () => { + plugin.options.function = "nonExistentFunc"; + + return expect(plugin.hooks["wsgi:manage:manage"]()).to.be.rejectedWith( + 'Function "nonExistentFunc" not found.' + ); + }); + + it("falls back to finding wsgi handler when no function is specified", () => { + delete plugin.options.function; + delete plugin.options.f; + + return plugin.hooks["wsgi:manage:manage"]().then(() => { + expect(plugin.serverless.pluginManager.cliOptions.f).to.equal("app"); + expect(plugin.options.function).to.equal("app"); + }); + }); + + it("rejects when no wsgi handler is found and no function is specified", () => { + delete plugin.options.function; + delete plugin.options.f; + plugin.serverless.service.functions = { + someFunc: { handler: "some_handler.handler" }, + }; + + return expect(plugin.hooks["wsgi:manage:manage"]()).to.be.rejectedWith( + "No functions were found with handler: wsgi_handler.handler" + ); + }); }); describe("manage local", () => { - const mockCli = Object({log: () => {}}); + const mockCli = Object({ log: () => {} }); it("calls handler to execute manage commands locally from argument", () => { var plugin = new Plugin( @@ -2137,7 +2202,7 @@ describe("serverless-wsgi", () => { }); describe("flask", () => { - const mockCli = Object({log: () => {}}); + const mockCli = Object({ log: () => {} }); it("calls handler to execute flask commands remotely from argument", () => { var plugin = new Plugin( { @@ -2180,7 +2245,7 @@ describe("serverless-wsgi", () => { }); describe("flask local", () => { - const mockCli = Object({log: () => {}}); + const mockCli = Object({ log: () => {} }); it("calls handler to execute flask commands locally from argument", () => { var plugin = new Plugin( { From ed496931a4bb25bea96f5cc810155a8d83cb9ea2 Mon Sep 17 00:00:00 2001 From: ayaan-qadri Date: Sun, 13 Oct 2024 17:02:30 +0530 Subject: [PATCH 6/6] Enabled mocha in ESLint config --- .eslintrc.js | 1 + 1 file changed, 1 insertion(+) diff --git a/.eslintrc.js b/.eslintrc.js index 4787cf3..469f59f 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -2,6 +2,7 @@ module.exports = { env: { es6: true, node: true, + mocha: true, }, extends: "eslint:recommended", parserOptions: {