From 286db737944566d379339ae4769bdce42bc5088e Mon Sep 17 00:00:00 2001 From: Piotr Grzesik Date: Wed, 22 Dec 2021 14:32:58 +0100 Subject: [PATCH 1/5] feat: Adapt to `serverless@3` logging interface --- package.json | 2 +- src/index.js | 15 ++++++-- test/index.js | 99 +++++++++++++++++++++++++++++++++++++++++++-------- 3 files changed, 98 insertions(+), 18 deletions(-) diff --git a/package.json b/package.json index 4a473e9..8828841 100644 --- a/package.json +++ b/package.json @@ -46,6 +46,6 @@ "sinon-chai": "^3.7.0" }, "peerDependencies": { - "serverless": "1 || 2" + "serverless": "1 || 2 || 3" } } diff --git a/src/index.js b/src/index.js index abcaf0b..23c9fac 100644 --- a/src/index.js +++ b/src/index.js @@ -11,7 +11,7 @@ const errorTypes = { }; class ServerlessPlugin { - constructor(serverless, options) { + constructor(serverless, options, v3Utils) { this.serverless = serverless; this.serverless.service.provider.environment = this.serverless.service.provider.environment || {}; @@ -37,12 +37,21 @@ class ServerlessPlugin { ); } + this.v3Utils = v3Utils; this.loadEnv(this.getEnvironment(options)); } - log(...args) { + log(msg) { if (this.config.logging) { - this.serverless.cli.log(...args); + if (this.v3Utils) { + if (msg.includes('WARNING')) { + this.v3Utils.log.warning(msg); + } else { + this.v3Utils.log.notice(msg); + } + } else { + this.serverless.cli.log(msg); + } } } diff --git a/test/index.js b/test/index.js index 2951955..7f86977 100644 --- a/test/index.js +++ b/test/index.js @@ -47,9 +47,24 @@ describe('ServerlessPlugin', function () { }, }; this.options = {}; + this.v3Utils = { + log: { + notice: this.sandbox.stub(), + warning: this.sandbox.stub(), + }, + }; - this.createPlugin = () => - new this.ServerlessPlugin(this.serverless, this.options); + this.createPlugin = ({ withV3Utils } = {}) => { + if (withV3Utils) { + return new this.ServerlessPlugin( + this.serverless, + this.options, + this.v3Utils, + ); + } else { + return new this.ServerlessPlugin(this.serverless, this.options); + } + }; }); afterEach(function () { @@ -82,22 +97,51 @@ describe('ServerlessPlugin', function () { }); describe('log()', function () { - it('logs by default', function () { - const msg = 'msg'; - this.createPlugin().log(msg); - this.serverless.cli.log.should.be.calledWith(msg); + describe('without v3Utils', function () { + it('logs by default', function () { + const msg = 'msg'; + this.createPlugin().log(msg); + this.serverless.cli.log.should.be.calledWith(msg); + }); + + it('does nothing if logging is disabled', function () { + this.serverless.service.custom = { + dotenv: { + logging: false, + }, + }; + + this.createPlugin().log('msg'); + this.serverless.cli.log.should.not.be.called; + }); }); - it('does nothing if logging is disabled', function () { - this.serverless.service.custom = { - dotenv: { - logging: false, - }, - }; + describe('with v3Utils', function () { + it('logs by default', function () { + const msg = 'msg'; + this.createPlugin({ withV3Utils: true }).log(msg); + this.serverless.cli.log.should.not.be.called; + this.v3Utils.log.notice.should.be.calledWith(msg); + }); - this.createPlugin().log('msg'); + it('logs by default and uses warning method if needed', function () { + const msg = 'WARNING: msg'; + this.createPlugin({ withV3Utils: true }).log(msg); + this.serverless.cli.log.should.not.be.called; + this.v3Utils.log.warning.should.be.calledWith(msg); + }); + + it('does nothing if logging is disabled', function () { + this.serverless.service.custom = { + dotenv: { + logging: false, + }, + }; - this.serverless.cli.log.should.not.be.called; + this.createPlugin({ withV3Utils: true }).log('msg'); + this.serverless.cli.log.should.not.be.called; + this.v3Utils.log.notice.should.not.be.called; + }); }); }); @@ -164,6 +208,23 @@ describe('ServerlessPlugin', function () { sinon.match(/basePath/), ); }); + + it('logs an error with v3Utils if basePath is also set', function () { + const path = '.env.unittest'; + this.serverless.service.custom = { + dotenv: { + basePath: 'base/path/', + path, + }, + }; + + this.createPlugin({ withV3Utils: true }) + .resolveEnvFileNames('env') + .should.deep.equal([path]); + this.v3Utils.log.warning.should.have.been.calledWith( + sinon.match(/basePath/), + ); + }); }); describe('with default dotenv paths', function () { @@ -344,6 +405,16 @@ describe('ServerlessPlugin', function () { log.should.have.been.calledWith('DOTENV: Could not find .env file.'); }); + it('logs an error with V3Utils if no .env files are required and none are found', function () { + const resolveEnvFileNames = this.setupResolveEnvFileNames(); + resolveEnvFileNames.withArgs(this.env).returns([]); + + this.createPlugin({ withV3Utils: true }); + this.v3Utils.log.notice.should.have.been.calledWith( + 'DOTENV: Could not find .env file.', + ); + }); + it('throws an error if no .env files are found but at least one is required', function () { const resolveEnvFileNames = this.setupResolveEnvFileNames(); resolveEnvFileNames.withArgs(this.env).returns([]); From 0d16f477f8ebea1213e4c258cee6cddbd94d85da Mon Sep 17 00:00:00 2001 From: Mark Tse Date: Thu, 23 Dec 2021 22:42:20 -0500 Subject: [PATCH 2/5] refactor(test/index): switch to test cases. --- test/index.js | 153 ++++++++++++++++++++++++-------------------------- 1 file changed, 74 insertions(+), 79 deletions(-) diff --git a/test/index.js b/test/index.js index 7f86977..6cccf09 100644 --- a/test/index.js +++ b/test/index.js @@ -97,50 +97,44 @@ describe('ServerlessPlugin', function () { }); describe('log()', function () { - describe('without v3Utils', function () { - it('logs by default', function () { - const msg = 'msg'; - this.createPlugin().log(msg); - this.serverless.cli.log.should.be.calledWith(msg); - }); - - it('does nothing if logging is disabled', function () { - this.serverless.service.custom = { - dotenv: { - logging: false, - }, - }; - - this.createPlugin().log('msg'); - this.serverless.cli.log.should.not.be.called; - }); - }); + [true, false].forEach((withV3Utils) => { + describe(JSON.stringify({ withV3Utils }), function () { + it('logs by default', function () { + const msg = 'msg'; + this.createPlugin({ withV3Utils }).log(msg); + if (withV3Utils) { + this.serverless.cli.log.should.not.be.called; + this.v3Utils.log.notice.should.be.calledWith(msg); + } else { + this.serverless.cli.log.should.be.calledWith(msg); + this.v3Utils.log.notice.should.not.be.called; + } + }); - describe('with v3Utils', function () { - it('logs by default', function () { - const msg = 'msg'; - this.createPlugin({ withV3Utils: true }).log(msg); - this.serverless.cli.log.should.not.be.called; - this.v3Utils.log.notice.should.be.calledWith(msg); - }); + it('logs by default and uses warning method if needed', function () { + const msg = 'WARNING: msg'; + this.createPlugin({ withV3Utils }).log(msg); - it('logs by default and uses warning method if needed', function () { - const msg = 'WARNING: msg'; - this.createPlugin({ withV3Utils: true }).log(msg); - this.serverless.cli.log.should.not.be.called; - this.v3Utils.log.warning.should.be.calledWith(msg); - }); + if (withV3Utils) { + this.serverless.cli.log.should.not.be.called; + this.v3Utils.log.warning.should.be.calledWith(msg); + } else { + this.serverless.cli.log.should.be.calledWith(msg); + this.v3Utils.log.notice.should.not.be.called; + } + }); - it('does nothing if logging is disabled', function () { - this.serverless.service.custom = { - dotenv: { - logging: false, - }, - }; + it('does nothing if logging is disabled', function () { + this.serverless.service.custom = { + dotenv: { + logging: false, + }, + }; - this.createPlugin({ withV3Utils: true }).log('msg'); - this.serverless.cli.log.should.not.be.called; - this.v3Utils.log.notice.should.not.be.called; + this.createPlugin({ withV3Utils }).log('msg'); + this.serverless.cli.log.should.not.be.called; + this.v3Utils.log.notice.should.not.be.called; + }); }); }); }); @@ -192,38 +186,32 @@ describe('ServerlessPlugin', function () { this.createPlugin().resolveEnvFileNames('env').should.deep.equal(path); }); - it('logs an error if basePath is also set', function () { - const path = '.env.unittest'; - this.serverless.service.custom = { - dotenv: { - basePath: 'base/path/', - path, - }, - }; - - this.createPlugin() - .resolveEnvFileNames('env') - .should.deep.equal([path]); - this.serverless.cli.log.should.have.been.calledWith( - sinon.match(/basePath/), - ); - }); - - it('logs an error with v3Utils if basePath is also set', function () { - const path = '.env.unittest'; - this.serverless.service.custom = { - dotenv: { - basePath: 'base/path/', - path, - }, - }; - - this.createPlugin({ withV3Utils: true }) - .resolveEnvFileNames('env') - .should.deep.equal([path]); - this.v3Utils.log.warning.should.have.been.calledWith( - sinon.match(/basePath/), - ); + [true, false].forEach((withV3Utils) => { + describe(JSON.stringify({ withV3Utils }), function () { + it('logs an error if basePath is also set', function () { + const path = '.env.unittest'; + this.serverless.service.custom = { + dotenv: { + basePath: 'base/path/', + path, + }, + }; + + this.createPlugin({ withV3Utils }) + .resolveEnvFileNames('env') + .should.deep.equal([path]); + + if (withV3Utils) { + this.v3Utils.log.warning.should.have.been.calledWith( + sinon.match(/basePath/), + ); + } else { + this.serverless.cli.log.should.have.been.calledWith( + sinon.match(/basePath/), + ); + } + }); + }); }); }); @@ -395,16 +383,23 @@ describe('ServerlessPlugin', function () { }); }); - it('logs an error if no .env files are required and none are found', function () { - const log = this.sandbox.stub(this.ServerlessPlugin.prototype, 'log'); + [true, false].forEach((withV3Utils) => { + describe(JSON.stringify({ withV3Utils }), function () { + it('logs an error if no .env files are required and none are found', function () { + const resolveEnvFileNames = this.setupResolveEnvFileNames(); + resolveEnvFileNames.withArgs(this.env).returns([]); - const resolveEnvFileNames = this.setupResolveEnvFileNames(); - resolveEnvFileNames.withArgs(this.env).returns([]); + this.createPlugin({ withV3Utils }); - this.createPlugin(); - log.should.have.been.calledWith('DOTENV: Could not find .env file.'); + const expectedMsg = 'DOTENV: Could not find .env file.'; + if (withV3Utils) { + this.v3Utils.log.notice.should.have.been.calledWith(expectedMsg); + } else { + this.serverless.cli.log.should.have.been.calledWith(expectedMsg); + } + }); + }); }); - it('logs an error with V3Utils if no .env files are required and none are found', function () { const resolveEnvFileNames = this.setupResolveEnvFileNames(); resolveEnvFileNames.withArgs(this.env).returns([]); From da028e5ad04a76c12cd87bda6fd9baec2e667be9 Mon Sep 17 00:00:00 2001 From: Mark Tse Date: Thu, 23 Dec 2021 22:47:47 -0500 Subject: [PATCH 3/5] chore: changelog + CI updates. --- .github/workflows/ci.yaml | 2 +- CHANGELOG.md | 4 ++++ package-lock.json | 2 +- package.json | 2 +- 4 files changed, 7 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 8e56249..ba96df5 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -12,7 +12,7 @@ jobs: strategy: matrix: # Serverless supports Node 10 and onward - serverless: ['1', '2'] + serverless: ['1', '2', '3'] node: [ '10', '12', '14', '15', '16' ] name: CI (Node v${{ matrix.node }}, serverless@${{ matrix.serverless }}) steps: diff --git a/CHANGELOG.md b/CHANGELOG.md index dca1d63..553dc5f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,10 @@ Breaking changes are introduced when going from version 3.x.x to 4.x.x: * This is because your environment variables might not be stored in dotenv files in all environments. * Setting `required.file` to `true` will continue to cause the plugin to halt if no dotenv files are found. +## 3.11.x + +* feat: Adapt to `serverless@3` logging interface ([#174](https://github.com/neverendingqs/serverless-dotenv-plugin/pull/174)) + ## 3.10.x * chore(package.json): register `serverless` as peer dependency. ([#159](https://github.com/neverendingqs/serverless-dotenv-plugin/pull/159)) diff --git a/package-lock.json b/package-lock.json index 3a7a3c4..ac7b739 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "serverless-dotenv-plugin", - "version": "3.10.0", + "version": "3.11.0", "lockfileVersion": 2, "requires": true, "packages": { diff --git a/package.json b/package.json index 8828841..06727e2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "serverless-dotenv-plugin", - "version": "3.10.0", + "version": "3.11.0", "description": "Preload environment variables with dotenv into serverless.", "main": "src/index.js", "files": [ From 5ae3bea27beb0a76b19eb779ff724b3f4625e950 Mon Sep 17 00:00:00 2001 From: Mark Tse Date: Thu, 23 Dec 2021 22:50:59 -0500 Subject: [PATCH 4/5] fix: sls@3 tag. --- .github/workflows/ci.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index ba96df5..f3119d5 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -12,7 +12,7 @@ jobs: strategy: matrix: # Serverless supports Node 10 and onward - serverless: ['1', '2', '3'] + serverless: ['1', '2', 'pre-3'] node: [ '10', '12', '14', '15', '16' ] name: CI (Node v${{ matrix.node }}, serverless@${{ matrix.serverless }}) steps: From 6d54b7a9d59b1740efac679b66e784df2cdb13ae Mon Sep 17 00:00:00 2001 From: Mark Tse Date: Thu, 23 Dec 2021 22:53:49 -0500 Subject: [PATCH 5/5] fix: don't run int tests for sls 3 on Node 10. --- .github/workflows/ci.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index f3119d5..93a321c 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -25,6 +25,7 @@ jobs: - run: npm ci - run: npm test - name: Integration Tests + if: ${{ !(matrix.node == 10 && matrix.serverless == 'pre-3') }} run: | npm ci npx serverless@${{ matrix.serverless }} package --env production