Skip to content

Commit

Permalink
Merge pull request #45 from relative-ci/validate-source
Browse files Browse the repository at this point in the history
Validate source
  • Loading branch information
vio authored Mar 29, 2020
2 parents 3863845 + b76b783 commit 3f990bb
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 25 deletions.
10 changes: 10 additions & 0 deletions bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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',
Expand Down
16 changes: 13 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -51,7 +52,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",
Expand Down
8 changes: 8 additions & 0 deletions src/webpack-plugin.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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);

Expand Down
7 changes: 7 additions & 0 deletions test/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
});
5 changes: 5 additions & 0 deletions test/cli/invalid-data/relativeci.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
webpack: {
stats: './webpack-stats.json',
},
};
1 change: 1 addition & 0 deletions test/cli/invalid-data/webpack-stats.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
94 changes: 74 additions & 20 deletions test/webpack-plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
});
});
Expand Down
4 changes: 3 additions & 1 deletion test/webpack/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ module.exports = {
mode: 'production',
context: __dirname,
plugins: [
new RelativeCiAgentWebpackPlugin(),
new RelativeCiAgentWebpackPlugin({
includeCommitMessage: false,
}),
],
};

0 comments on commit 3f990bb

Please sign in to comment.