|
| 1 | +const { join } = require('path'); |
| 2 | +const { expect } = require('chai'); |
| 3 | + |
| 4 | +const BasePathPlugin = require('./base-path-plugin'); |
| 5 | + |
| 6 | +/** |
| 7 | + * Simple object copy to avoid reference copy. |
| 8 | + * |
| 9 | + * Note: only properties with primivate value will be copied, |
| 10 | + * properties with function assigned won't be copied. |
| 11 | + * |
| 12 | + * @param {Object} obj Object |
| 13 | + * @returns {Object} Copied Object |
| 14 | + */ |
| 15 | +function copyObj(obj) { |
| 16 | + return JSON.parse(JSON.stringify(obj)); |
| 17 | +} |
| 18 | + |
| 19 | +/** |
| 20 | + * Validate that the handler path rewriting match by comparing |
| 21 | + * the previous state of a set of functions and a new one. |
| 22 | + * |
| 23 | + * @param {string} basePath Bash path |
| 24 | + * @param {Object} previousFunctions Functions |
| 25 | + * @param {Object} currentFunctions Functions |
| 26 | + * @returns {void} |
| 27 | + */ |
| 28 | +function validatePath(basePath, prevFunctions, currFunctions) { |
| 29 | + Object.keys(prevFunctions).forEach(functionName => { |
| 30 | + const expectedPath = join(basePath, prevFunctions[functionName].handler); |
| 31 | + const newPath = currFunctions[functionName].handler; |
| 32 | + |
| 33 | + expect(expectedPath).to.be.equal(newPath); |
| 34 | + }); |
| 35 | +} |
| 36 | + |
| 37 | +describe('BasePathPlugin', () => { |
| 38 | + beforeEach(() => { |
| 39 | + this.serverlessMock = { |
| 40 | + providers: {}, |
| 41 | + service: { |
| 42 | + service: 'serverless-demo-functions-base-path', |
| 43 | + serviceObject: { name: 'serverless-demo-functions-base-path' }, |
| 44 | + provider: { |
| 45 | + stage: 'dev', |
| 46 | + region: 'eu-west-1', |
| 47 | + variableSyntax: '', |
| 48 | + name: 'aws', |
| 49 | + runtime: 'nodejs6.10', |
| 50 | + memorySize: 128, |
| 51 | + timemout: 10, |
| 52 | + versionFunctions: true |
| 53 | + }, |
| 54 | + custom: { functionsBasePath: 'src/handlers' }, |
| 55 | + plugins: ['serverless-functions-base-path'], |
| 56 | + functions: { |
| 57 | + hello: { |
| 58 | + handler: 'users.hello', |
| 59 | + events: [{ http: { path: 'hello', method: 'get' } }], |
| 60 | + name: 'serverless-demo-functions-base-path-dev-hello' |
| 61 | + }, |
| 62 | + bye: { |
| 63 | + handler: 'users.bye', |
| 64 | + events: [{ http: { path: 'bye', method: 'get' } }], |
| 65 | + name: 'serverless-demo-functions-base-path-dev-bye' |
| 66 | + } |
| 67 | + }, |
| 68 | + resources: undefined, |
| 69 | + package: {} |
| 70 | + } |
| 71 | + }; |
| 72 | + |
| 73 | + this.optionsMock = {}; |
| 74 | + }); |
| 75 | + |
| 76 | + it('should not rewrite the functions handler path if "basePath" is not specified', () => { |
| 77 | + this.serverlessMock.service.custom.functionsBasePath = null; |
| 78 | + |
| 79 | + const Plugin = new BasePathPlugin(this.serverlessMock, this.optionsMock); |
| 80 | + const previousFunctions = copyObj(Plugin.serverless.service.functions); |
| 81 | + |
| 82 | + Plugin.rewriteHandlersPath(); |
| 83 | + |
| 84 | + const currentFunctions = copyObj(Plugin.serverless.service.functions); |
| 85 | + |
| 86 | + validatePath(Plugin.basePath, previousFunctions, currentFunctions); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should not rewrite the functions handler path if "basePath" is something else than a string', () => { |
| 90 | + this.serverlessMock.service.custom.functionsBasePath = { prop: 'val' }; |
| 91 | + |
| 92 | + const Plugin = new BasePathPlugin(this.serverlessMock, this.optionsMock); |
| 93 | + const previousFunctions = copyObj(Plugin.serverless.service.functions); |
| 94 | + |
| 95 | + Plugin.rewriteHandlersPath(); |
| 96 | + |
| 97 | + const currentFunctions = copyObj(Plugin.serverless.service.functions); |
| 98 | + |
| 99 | + validatePath(Plugin.basePath, previousFunctions, currentFunctions); |
| 100 | + }); |
| 101 | + |
| 102 | + it('should rewrite the functions handler path if a "basePath" variable is passed', () => { |
| 103 | + const Plugin = new BasePathPlugin(this.serverlessMock, this.optionsMock); |
| 104 | + const previousFunctions = copyObj(Plugin.serverless.service.functions); |
| 105 | + |
| 106 | + Plugin.rewriteHandlersPath(); |
| 107 | + |
| 108 | + const currentFunctions = copyObj(Plugin.serverless.service.functions); |
| 109 | + |
| 110 | + validatePath(Plugin.basePath, previousFunctions, currentFunctions); |
| 111 | + }); |
| 112 | + |
| 113 | + it('should throw an error if handler path is not specified', () => { |
| 114 | + this.serverlessMock.service.functions.bye.handler = null; |
| 115 | + |
| 116 | + const Plugin = new BasePathPlugin(this.serverlessMock, this.optionsMock); |
| 117 | + const testFunc = () => { |
| 118 | + Plugin.rewriteHandlersPath(); |
| 119 | + }; |
| 120 | + |
| 121 | + expect(testFunc).to.throw( |
| 122 | + Error, |
| 123 | + /handler path is not defined for function/ |
| 124 | + ); |
| 125 | + }); |
| 126 | + |
| 127 | + it('should throw an error if handler path is something else than a string', () => { |
| 128 | + this.serverlessMock.service.functions.bye.handler = { props: 'val' }; |
| 129 | + |
| 130 | + const Plugin = new BasePathPlugin(this.serverlessMock, this.optionsMock); |
| 131 | + const testFunc = () => { |
| 132 | + Plugin.rewriteHandlersPath(); |
| 133 | + }; |
| 134 | + |
| 135 | + expect(testFunc).to.throw( |
| 136 | + Error, |
| 137 | + /handler path must be a string for function/ |
| 138 | + ); |
| 139 | + }); |
| 140 | +}); |
0 commit comments