|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const BbPromise = require('bluebird'); |
| 4 | +const _ = require('lodash'); |
| 5 | +const chai = require('chai'); |
| 6 | +const sinon = require('sinon'); |
| 7 | +const mockery = require('mockery'); |
| 8 | +const Serverless = require('serverless'); |
| 9 | + |
| 10 | +chai.use(require('chai-as-promised')); |
| 11 | +chai.use(require('sinon-chai')); |
| 12 | +const expect = chai.expect; |
| 13 | + |
| 14 | +const FseMock = sandbox => ({ |
| 15 | + copy: sandbox.stub(), |
| 16 | + removeSync: sandbox.stub() |
| 17 | +}); |
| 18 | + |
| 19 | +describe('cleanup', () => { |
| 20 | + let sandbox; |
| 21 | + let fseMock; |
| 22 | + let baseModule; |
| 23 | + let serverless; |
| 24 | + let module; |
| 25 | + let dirExistsSyncStub; |
| 26 | + |
| 27 | + before(() => { |
| 28 | + sandbox = sinon.sandbox.create(); |
| 29 | + sandbox.usingPromise(BbPromise); |
| 30 | + |
| 31 | + fseMock = FseMock(sandbox); |
| 32 | + |
| 33 | + mockery.enable({ warnOnUnregistered: false }); |
| 34 | + mockery.registerMock('fs-extra', fseMock); |
| 35 | + |
| 36 | + baseModule = require('../lib/cleanup'); |
| 37 | + Object.freeze(baseModule); |
| 38 | + }); |
| 39 | + |
| 40 | + after(() => { |
| 41 | + mockery.disable(); |
| 42 | + mockery.deregisterAll(); |
| 43 | + }); |
| 44 | + |
| 45 | + beforeEach(() => { |
| 46 | + serverless = new Serverless(); |
| 47 | + serverless.cli = { |
| 48 | + log: sandbox.stub(), |
| 49 | + consoleLog: sandbox.stub() |
| 50 | + }; |
| 51 | + dirExistsSyncStub = sandbox.stub(serverless.utils, 'dirExistsSync'); |
| 52 | + |
| 53 | + module = Object.assign({ |
| 54 | + serverless, |
| 55 | + options: {}, |
| 56 | + webpackOutputPath: 'my/Output/Path' |
| 57 | + }, baseModule); |
| 58 | + }); |
| 59 | + |
| 60 | + afterEach(() => { |
| 61 | + // This will reset the webpackMock too |
| 62 | + sandbox.restore(); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should do nothing if no original service path is set', () => { |
| 66 | + _.unset(module.originalServicePath); |
| 67 | + return expect(module.cleanup()).to.be.fulfilled |
| 68 | + .then(() => { |
| 69 | + expect(fseMock.copy).to.have.not.been.called; |
| 70 | + expect(fseMock.removeSync).to.not.have.been.called; |
| 71 | + }) |
| 72 | + }); |
| 73 | + |
| 74 | + it('should remove output dir if it exists', () => { |
| 75 | + dirExistsSyncStub.returns(true); |
| 76 | + _.unset(module.originalServicePath); |
| 77 | + fseMock.removeSync.reset(); |
| 78 | + |
| 79 | + return expect(module.cleanup()).to.be.fulfilled |
| 80 | + .then(() => { |
| 81 | + expect(dirExistsSyncStub).to.have.been.calledOnce; |
| 82 | + expect(dirExistsSyncStub).to.have.been.calledWith('my/Output/Path'); |
| 83 | + expect(fseMock.removeSync).to.have.been.calledOnce; |
| 84 | + }); |
| 85 | + }); |
| 86 | + |
| 87 | + it('should not call removeSync if output dir does not exists', () => { |
| 88 | + dirExistsSyncStub.returns(false); |
| 89 | + _.unset(module.originalServicePath); |
| 90 | + fseMock.removeSync.reset(); |
| 91 | + |
| 92 | + return expect(module.cleanup()).to.be.fulfilled |
| 93 | + .then(() => { |
| 94 | + expect(dirExistsSyncStub).to.have.been.calledOnce; |
| 95 | + expect(dirExistsSyncStub).to.have.been.calledWith('my/Output/Path'); |
| 96 | + expect(fseMock.removeSync).to.not.have.been.called; |
| 97 | + }); |
| 98 | + }); |
| 99 | + |
| 100 | + it('should call copy with the right parameters with a service artifact', () => { |
| 101 | + dirExistsSyncStub.returns(true); |
| 102 | + module.originalServicePath = 'my/Original/Service/Path'; |
| 103 | + fseMock.copy.reset(); |
| 104 | + fseMock.copy.yields(null, {}); |
| 105 | + serverless.service.package.artifact = 'artifact.zip'; |
| 106 | + |
| 107 | + return expect(module.cleanup()).to.be.fulfilled |
| 108 | + .then(() => { |
| 109 | + expect(serverless.config.servicePath).to.equal('my/Original/Service/Path'); |
| 110 | + expect(fseMock.copy).to.have.been.calledOnce; |
| 111 | + expect(fseMock.copy).to.have.been |
| 112 | + .calledWith('my/Output/Path/.serverless', 'my/Original/Service/Path/.serverless'); |
| 113 | + expect(serverless.service.package.artifact) |
| 114 | + .to.equal('my/Original/Service/Path/.serverless/artifact.zip'); |
| 115 | + }); |
| 116 | + }); |
| 117 | + |
| 118 | + it('should call copy with the right parameters with individual packaging', () => { |
| 119 | + dirExistsSyncStub.returns(true); |
| 120 | + module.originalServicePath = 'my/Original/Service/Path'; |
| 121 | + fseMock.copy.reset(); |
| 122 | + fseMock.copy.yields(null, {}); |
| 123 | + serverless.service.package.individually = true; |
| 124 | + |
| 125 | + const testFunctionsConfig = { |
| 126 | + func1: { |
| 127 | + handler: 'module1.func1handler', |
| 128 | + artifact: 'artifact-func1.zip', |
| 129 | + events: [{ |
| 130 | + http: { |
| 131 | + method: 'get', |
| 132 | + path: 'func1path', |
| 133 | + }, |
| 134 | + }], |
| 135 | + }, |
| 136 | + func2: { |
| 137 | + handler: 'module2.func2handler', |
| 138 | + artifact: 'artifact-func2.zip', |
| 139 | + events: [{ |
| 140 | + http: { |
| 141 | + method: 'POST', |
| 142 | + path: 'func2path', |
| 143 | + }, |
| 144 | + }, { |
| 145 | + nonhttp: 'non-http', |
| 146 | + }], |
| 147 | + }, |
| 148 | + func3: { |
| 149 | + handler: 'module2.func3handler', |
| 150 | + artifact: 'artifact-func3.zip', |
| 151 | + events: [{ |
| 152 | + nonhttp: 'non-http', |
| 153 | + }], |
| 154 | + }, |
| 155 | + }; |
| 156 | + serverless.service.functions = testFunctionsConfig; |
| 157 | + |
| 158 | + return expect(module.cleanup()).to.be.fulfilled |
| 159 | + .then(() => { |
| 160 | + expect(serverless.config.servicePath).to.equal('my/Original/Service/Path'); |
| 161 | + expect(fseMock.copy).to.have.been.calledOnce; |
| 162 | + expect(fseMock.copy).to.have.been |
| 163 | + .calledWith('my/Output/Path/.serverless', 'my/Original/Service/Path/.serverless'); |
| 164 | + _.forEach(['func1', 'func2', 'func3'], funcName => { |
| 165 | + expect(serverless.service.functions[funcName]).to.have.a.property('artifact') |
| 166 | + .that.equals(`my/Original/Service/Path/.serverless/artifact-${funcName}.zip`); |
| 167 | + }); |
| 168 | + }); |
| 169 | + }); |
| 170 | + |
| 171 | + it('should reject if the copy fails', () => { |
| 172 | + dirExistsSyncStub.returns(true); |
| 173 | + module.originalServicePath = 'my/Original/Service/Path'; |
| 174 | + fseMock.copy.yields(new Error('Failed')); |
| 175 | + serverless.service.package.artifact = 'artifact.zip'; |
| 176 | + |
| 177 | + return expect(module.cleanup()).to.be.rejectedWith('Failed'); |
| 178 | + }); |
| 179 | + |
| 180 | +}); |
0 commit comments