From 2f17c4a7ae3401146aa132f848ba593ca6108627 Mon Sep 17 00:00:00 2001 From: Vio Date: Sun, 29 Mar 2020 17:51:24 +0200 Subject: [PATCH 1/5] chore: Update @bundle-stats/utils --- package-lock.json | 6 +++--- package.json | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/package-lock.json b/package-lock.json index b9569923..9f72639d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1158,9 +1158,9 @@ "dev": true }, "@bundle-stats/utils": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/@bundle-stats/utils/-/utils-2.0.0.tgz", - "integrity": "sha512-js4CeWoFzk2Y7Vv5QeBN1qvPBS+9zp9NwJR5UX9NQ6WClsRFg/GEAKse2zxJGnicB83+YNuqO5lrTNqMr7jZvg==", + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/@bundle-stats/utils/-/utils-2.1.1.tgz", + "integrity": "sha512-UpHVGYjGa3ce+7T31okZytUh/vC+JcyR8W5m7QrmiwFN12ETBljOEridN7ocvsDLv0mnhpuojucy67jNTSTl6A==", "requires": { "convert-units": "^2.3.4", "core-js": "^3.1.4" diff --git a/package.json b/package.json index 9a68baca..f2840f88 100644 --- a/package.json +++ b/package.json @@ -51,7 +51,7 @@ "webpack": "^4.42.1" }, "dependencies": { - "@bundle-stats/utils": "^2.0.0", + "@bundle-stats/utils": "^2.1.1", "@relative-ci/env-ci": "^5.1.0", "core-js": "^3.6.4", "cosmiconfig": "^6.0.0", From 45522034d1ce56984389b6591f66d902fb153970 Mon Sep 17 00:00:00 2001 From: Vio Date: Sun, 29 Mar 2020 18:10:21 +0200 Subject: [PATCH 2/5] chore: Add @types/jest --- package-lock.json | 10 ++++++++++ package.json | 1 + 2 files changed, 11 insertions(+) diff --git a/package-lock.json b/package-lock.json index 9f72639d..51654037 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1943,6 +1943,16 @@ "@types/istanbul-lib-report": "*" } }, + "@types/jest": { + "version": "25.1.4", + "resolved": "https://registry.npmjs.org/@types/jest/-/jest-25.1.4.tgz", + "integrity": "sha512-QDDY2uNAhCV7TMCITrxz+MRk1EizcsevzfeS6LykIlq2V1E5oO4wXG8V2ZEd9w7Snxeeagk46YbMgZ8ESHx3sw==", + "dev": true, + "requires": { + "jest-diff": "^25.1.0", + "pretty-format": "^25.1.0" + } + }, "@types/json-schema": { "version": "7.0.4", "resolved": "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.4.tgz", diff --git a/package.json b/package.json index f2840f88..22eae102 100644 --- a/package.json +++ b/package.json @@ -42,6 +42,7 @@ "@babel/cli": "^7.8.4", "@babel/core": "^7.8.6", "@babel/preset-env": "^7.8.6", + "@types/jest": "^25.1.4", "eslint": "^6.8.0", "eslint-config-airbnb-base": "^14.0.0", "eslint-plugin-import": "^2.20.1", From 401917e33fb9a7125843f35ee062d1b0049e17dc Mon Sep 17 00:00:00 2001 From: Vio Date: Sun, 29 Mar 2020 18:09:37 +0200 Subject: [PATCH 3/5] tests: Webpack-plugin payload test --- test/webpack-plugin.test.js | 94 ++++++++++++++++++++++++++-------- test/webpack/webpack.config.js | 4 +- 2 files changed, 77 insertions(+), 21 deletions(-) diff --git a/test/webpack-plugin.test.js b/test/webpack-plugin.test.js index 02233363..bf1793b4 100644 --- a/test/webpack-plugin.test.js +++ b/test/webpack-plugin.test.js @@ -6,28 +6,51 @@ const fetch = require('isomorphic-fetch'); const appConfig = require('./webpack/webpack.config'); +const ENV_DEFAULT = { + CI: 'true', + RELATIVE_CI_ENDPOINT: 'http://localhost/save', + RELATIVE_CI_KEY: '123', + CIRCLECI: 'true', + CIRCLE_SHA1: 'abcd1234', + CIRCLE_BRANCH: 'master', + CIRCLE_PROJECT_USERNAME: 'organization', + CIRCLE_PROJECT_REPONAME: 'project', +}; + +const setCustomEnv = (customEnv = {}) => { + const envVars = { ...ENV_DEFAULT, ...customEnv }; + + Object.entries(envVars).forEach(([key, value]) => { + process.env[key] = value; + }); +}; + +const clearCustomEnv = () => { + Object.keys(ENV_DEFAULT).forEach((key) => { + process.env[key] = undefined; + }); +}; describe('webpack-plugin', () => { test('should send data to the service', (done) => { - process.env.CI = 'true'; - process.env.RELATIVE_CI_ENDPOINT = 'http://localhost/save'; - process.env.RELATIVE_CI_KEY = '123'; - process.env.RELATIVE_CI_SLUG = 'organization/project'; - - fetch.mockReturnValue(Promise.resolve({ - json: () => Promise.resolve({ - res: { - job: { - internalBuildNumber: 1, + setCustomEnv(); + + fetch.mockReturnValue( + Promise.resolve({ + json: () => Promise.resolve({ + res: { + job: { + internalBuildNumber: 1, + }, }, - }, - info: { - message: { - txt: 'Hello world!', + info: { + message: { + txt: 'Hello world!', + }, }, - }, + }), }), - })); + ); const compiler = webpack(appConfig); compiler.outputFileSystem = new MemoryFS(); @@ -36,11 +59,42 @@ describe('webpack-plugin', () => { expect(error).toEqual(null); expect(stats.hasErrors()).toBe(false); expect(fetch).toHaveBeenCalledTimes(1); + expect(fetch).toHaveBeenCalledWith(ENV_DEFAULT.RELATIVE_CI_ENDPOINT, { + method: 'POST', + headers: { + 'Content-Type': 'application/json; charset=utf-8', + }, + body: JSON.stringify({ + key: '123', + project: 'organization/project', + job: { + commit: 'abcd1234', + branch: 'master', + }, + rawData: { + webpack: { + stats: { + hash: 'e11635038d1ab076ae9a', + assets: [{ name: 'main.js', size: 957 }], + entrypoints: { main: { assets: ['main.js'] } }, + chunks: [ + { + id: 0, + entry: true, + initial: true, + files: ['main.js'], + names: ['main'], + }, + ], + modules: [{ name: './src/index.js', size: 29, chunks: [0] }], + }, + }, + }, + agentVersion: '1.2.0', + }), + }); - process.env.CI = undefined; - process.env.RELATIVE_CI_ENDPOINT = undefined; - process.env.RELATIVE_CI_KEY = undefined; - process.env.RELATIVE_CI_SLUG = undefined; + clearCustomEnv(); done(); }); }); diff --git a/test/webpack/webpack.config.js b/test/webpack/webpack.config.js index 9a15e6b1..f6b9d8df 100644 --- a/test/webpack/webpack.config.js +++ b/test/webpack/webpack.config.js @@ -4,6 +4,8 @@ module.exports = { mode: 'production', context: __dirname, plugins: [ - new RelativeCiAgentWebpackPlugin(), + new RelativeCiAgentWebpackPlugin({ + includeCommitMessage: false, + }), ], }; From e951e74890f77a225013b4b0f40995f7698f8fae Mon Sep 17 00:00:00 2001 From: Vio Date: Sun, 29 Mar 2020 18:39:28 +0200 Subject: [PATCH 4/5] feature(cli): Validate webpack stats data --- bin/index.js | 10 ++++++++++ test/cli.test.js | 7 +++++++ test/cli/invalid-data/relativeci.config.js | 5 +++++ test/cli/invalid-data/webpack-stats.json | 1 + 4 files changed, 23 insertions(+) create mode 100644 test/cli/invalid-data/relativeci.config.js create mode 100644 test/cli/invalid-data/webpack-stats.json diff --git a/bin/index.js b/bin/index.js index 585298ae..b6cd4751 100755 --- a/bin/index.js +++ b/bin/index.js @@ -3,6 +3,7 @@ const { get } = require('lodash'); const { readJSONSync, pathExistsSync } = require('fs-extra'); const { cosmiconfigSync } = require('cosmiconfig'); +const { validate } = require('@bundle-stats/utils/lib/webpack'); const { agent } = require('..'); const { debug } = require('../lib/utils'); @@ -31,6 +32,15 @@ if (!pathExistsSync(webpackArtifactFilepath)) { process.exit(0); } +const data = readJSONSync(webpackArtifactFilepath); + +const invalidData = validate(data); + +if (invalidData) { + console.error(invalidData); + process.exit(0); +} + const artifactsData = [ { key: 'webpack.stats', diff --git a/test/cli.test.js b/test/cli.test.js index a8ea5bdf..3ff32af0 100644 --- a/test/cli.test.js +++ b/test/cli.test.js @@ -14,4 +14,11 @@ describe('CLI', () => { done(); }); }); + + test('should return error if webpack stats data is invalid', (done) => { + exec('cd test/cli/invalid-data && ../../../bin/index.js', (error, stdout, sterr) => { + expect(sterr).toContain('Assets property is missing'); + done(); + }); + }); }); diff --git a/test/cli/invalid-data/relativeci.config.js b/test/cli/invalid-data/relativeci.config.js new file mode 100644 index 00000000..bbbb7f17 --- /dev/null +++ b/test/cli/invalid-data/relativeci.config.js @@ -0,0 +1,5 @@ +module.exports = { + webpack: { + stats: './webpack-stats.json', + }, +}; diff --git a/test/cli/invalid-data/webpack-stats.json b/test/cli/invalid-data/webpack-stats.json new file mode 100644 index 00000000..0967ef42 --- /dev/null +++ b/test/cli/invalid-data/webpack-stats.json @@ -0,0 +1 @@ +{} From b76b7833edd06caa59efcf2d8433c9d73acbd49e Mon Sep 17 00:00:00 2001 From: Vio Date: Sun, 29 Mar 2020 18:47:30 +0200 Subject: [PATCH 5/5] feature(webpack-plugin): Validate data --- src/webpack-plugin.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/webpack-plugin.js b/src/webpack-plugin.js index 0ebdc478..58848f16 100644 --- a/src/webpack-plugin.js +++ b/src/webpack-plugin.js @@ -1,5 +1,6 @@ import process from 'process'; import { get, merge } from 'lodash'; +import { validate } from '@bundle-stats/utils/lib/webpack'; import { agent } from './agent'; import { getEnvCI } from './utils'; @@ -25,6 +26,13 @@ const getOnEmit = (options) => async (compilation, callback) => { ? compilation.getInfrastructureLogger(PLUGIN_NAME) : console; + const invalidData = validate(data); + + if (invalidData) { + logger.warn(invalidData); + return; + } + try { await agent([{ key: 'webpack.stats', data }], agentOptions, logger);