From 9d0fe99a4e03989439ef9e87f6e4cf2eb1a04e7f Mon Sep 17 00:00:00 2001 From: "Mark J. Becker" Date: Fri, 21 Jul 2017 11:55:34 +0200 Subject: [PATCH] Optimize path extraction of action handlers --- compile/functions/runtimes/base.js | 6 +++++- compile/functions/tests/index.js | 23 +++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/compile/functions/runtimes/base.js b/compile/functions/runtimes/base.js index c1d04a7..31d9c6a 100644 --- a/compile/functions/runtimes/base.js +++ b/compile/functions/runtimes/base.js @@ -51,7 +51,11 @@ class BaseRuntime { } convertHandlerToPath(functionHandler) { - return functionHandler.replace(/\..*$/, this.extension) + const lastDot = functionHandler.lastIndexOf('.'); + if (lastDot === -1) { + return functionHandler; + } + return functionHandler.substring(0, lastDot) + this.extension; } generateActionPackage(functionObject) { diff --git a/compile/functions/tests/index.js b/compile/functions/tests/index.js index 5913a25..27824f0 100644 --- a/compile/functions/tests/index.js +++ b/compile/functions/tests/index.js @@ -7,6 +7,29 @@ require('chai').use(chaiAsPromised); const sinon = require('sinon'); const OpenWhiskCompileFunctions = require('../index'); +const BaseRuntime = require('../runtimes/base.js'); + +describe('BaseRuntime', () => { + describe('#convertHandlerToPath', () => { + const base = new BaseRuntime(); + base.extension = '.js'; + + it('should extract the path for a given file handler', () => { + const result = base.convertHandlerToPath('index.main'); + expect(result).to.equal('index.js'); + }); + + it('should return the input for a given file handler without exported function', () => { + const result = base.convertHandlerToPath('index'); + expect(result).to.equal('index'); + }); + + it('should extract the path for a given path handler', () => { + const result = base.convertHandlerToPath('myFunction@0.1.0/index.main'); + expect(result).to.equal('myFunction@0.1.0/index.js'); + }); + }); +}); describe('OpenWhiskCompileFunctions', () => { let serverless;