Skip to content

Commit

Permalink
Handle check_license JUnit report generation
Browse files Browse the repository at this point in the history
  • Loading branch information
benbraou committed Jan 10, 2018
1 parent 6c95a0d commit 613e0ae
Show file tree
Hide file tree
Showing 12 changed files with 200 additions and 119 deletions.
2 changes: 2 additions & 0 deletions .circleci/config.yml
Expand Up @@ -41,6 +41,8 @@ jobs:
ESLINT_OUTPUT: "reports/junit/eslint-results.xml"
FLOW_FORMATTER: "junit"
FLOW_OUTPUT: "reports/junit/flow-results.xml"
CHECK_LICENSE_FORMATTER: "junit"
CHECK_LICENSE_OUTPUT: "reports/junit/check_license-results.xml"

- store_test_results:
path: reports/junit
Expand Down
11 changes: 9 additions & 2 deletions scripts/circleci/check_license.sh
Expand Up @@ -6,8 +6,15 @@ set -e
EXPECTED='scripts/circleci/check_license.sh'
ACTUAL=$(git grep -l PATENTS)

ANNOUNCEMENT=$(echo "PATENTS crept into some new files?")
DIFF=$(diff -u <(echo "$EXPECTED") <(echo "$ACTUAL") || true)
DATA=$ANNOUNCEMENT$DIFF

FORMATTER=$1
OUTPUT=$2

if [ "$EXPECTED" != "$ACTUAL" ]; then
echo "PATENTS crept into some new files?"
diff -u <(echo "$EXPECTED") <(echo "$ACTUAL") || true
echo "$DATA"
./scripts/circleci/write_junit_report.sh "$FORMATTER" "$DATA" "check_license" "$OUTPUT"
exit 1
fi
2 changes: 1 addition & 1 deletion scripts/circleci/test_entry_point.sh
Expand Up @@ -11,7 +11,7 @@ if [ $((0 % CIRCLE_NODE_TOTAL)) -eq "$CIRCLE_NODE_INDEX" ]; then
COMMANDS_TO_RUN+=("node ./scripts/tasks/flow --formatter=${FLOW_FORMATTER} --output=${FLOW_OUTPUT}")
COMMANDS_TO_RUN+=("node ./scripts/tasks/eslint --formatter=${ESLINT_FORMATTER} --output=${ESLINT_OUTPUT}")
COMMANDS_TO_RUN+=("yarn test --runInBand --testResultsProcessor=${JEST_PROCESSOR}")
COMMANDS_TO_RUN+=('./scripts/circleci/check_license.sh')
COMMANDS_TO_RUN+=("./scripts/circleci/check_license.sh ${CHECK_LICENSE_FORMATTER} ${CHECK_LICENSE_OUTPUT}")
COMMANDS_TO_RUN+=('./scripts/circleci/check_modules.sh')
COMMANDS_TO_RUN+=('./scripts/circleci/test_print_warnings.sh')
COMMANDS_TO_RUN+=('./scripts/circleci/track_stats.sh')
Expand Down
7 changes: 7 additions & 0 deletions scripts/circleci/write_junit_report.sh
@@ -0,0 +1,7 @@
#!/bin/bash

set -e

node ./scripts/tasks/junit --formatter="$1" --data="$2" --package="$3" --output="$4"


20 changes: 11 additions & 9 deletions scripts/eslint/formatterUtils.js
Expand Up @@ -11,21 +11,23 @@
const path = require('path');
const fs = require('fs');
const {execSync} = require('child_process');
const formatterConfig = require('../shared/formatterConfig');
const {createDirectoriesIfMissing} = require('../shared/junitReporting');
const {
defaultFormatterConfig,
configFromProcessArgs,
} = require('../shared/formatterConfig');
const {createDirectoriesIfMissing} = require('../shared/junitReport');

const defaultFormatConfig = {
name: 'stylish',
outputFile: '',
formatterFlag: '--formatter',
outputFlag: '--output',
};
const defaultESLintFormatterConfig = Object.assign(
{},
defaultFormatterConfig(),
{name: 'stylish'}
);

/**
* Returns the formatter configuration based on the default configuration and process args
*/
function getFormatterConfig() {
return formatterConfig(defaultFormatConfig, process.argv);
return configFromProcessArgs(defaultESLintFormatterConfig, process.argv);
}

/**
Expand Down
63 changes: 55 additions & 8 deletions scripts/shared/__tests__/formatterConfig-test.js
Expand Up @@ -7,48 +7,91 @@

'use strict';

const formatterConfig = require('../formatterConfig');
const {
defaultFormatterConfig,
configFromProcessArgs,
} = require('../formatterConfig');

const immutableDefaultConfig = {
name: 'defaultName',
outputFile: 'defaultFile',
data: '',
packageName: '',
formatterFlag: '--formatter',
dataFlag: '--data',
outputFlag: '--output',
packageNameFlag: '',
};
let defaultConfig;

describe('formatterConfig', () => {
let config;

describe('defaultFormatterConfig', () => {
it('should return an immutable default configuration', () => {
let defaultConfig = defaultFormatterConfig();
expect(defaultConfig).toEqual({
name: '',
outputFile: '',
data: '',
packageName: '',
formatterFlag: '--formatter',
outputFlag: '--output',
dataFlag: '--data',
packageNameFlag: '--package',
});
defaultConfig.name = 'test';
expect(defaultFormatterConfig()).toEqual({
name: '',
outputFile: '',
data: '',
packageName: '',
formatterFlag: '--formatter',
outputFlag: '--output',
dataFlag: '--data',
packageNameFlag: '--package',
});
});
});

describe('configFromProcessArgs', () => {
beforeEach(() => {
defaultConfig = {...immutableDefaultConfig};
config = Object.assign({}, immutableDefaultConfig);
});

it('should work with empty process arguments', () => {
expect(formatterConfig(defaultConfig)).toEqual(immutableDefaultConfig);
expect(configFromProcessArgs(config)).toEqual(immutableDefaultConfig);
});

it('should work when provided a formatter name', () => {
expect(formatterConfig(defaultConfig, ['--formatter=junit'])).toEqual({
expect(configFromProcessArgs(config, ['--formatter=junit'])).toEqual({
name: 'junit',
outputFile: 'defaultFile',
data: '',
packageName: '',
formatterFlag: '--formatter',
dataFlag: '--data',
outputFlag: '--output',
packageNameFlag: '',
});
});

it('should work when provided a file output path', () => {
expect(
formatterConfig(defaultConfig, ['Hello', '--output=some/path/result.xml'])
configFromProcessArgs(config, ['Hello', '--output=some/path/result.xml'])
).toEqual({
name: 'defaultName',
outputFile: 'some/path/result.xml',
data: '',
packageName: '',
formatterFlag: '--formatter',
dataFlag: '--data',
outputFlag: '--output',
packageNameFlag: '',
});
});

it('should work when provided a formatter name and a file output path', () => {
expect(
formatterConfig(defaultConfig, [
configFromProcessArgs(config, [
'Hi',
'--output=some/path/result.xml',
'Hello',
Expand All @@ -57,8 +100,12 @@ describe('formatterConfig', () => {
).toEqual({
name: 'junit',
outputFile: 'some/path/result.xml',
data: '',
packageName: '',
formatterFlag: '--formatter',
dataFlag: '--data',
outputFlag: '--output',
packageNameFlag: '',
});
});
});
Expand Up @@ -7,9 +7,9 @@

'use strict';

const {buildXMLOutputAsSingleTest} = require('../junitReporting');
const {buildXMLOutputAsSingleTest} = require('../junitReport');

describe('junitReporting', () => {
describe('junitReport', () => {
describe('buildXMLOutputAsSingleTest', () => {
it('should create a JUnit report containing one testsuite and one testcase', () => {
expect(
Expand Down
79 changes: 70 additions & 9 deletions scripts/shared/formatterConfig.js
Expand Up @@ -18,12 +18,48 @@
* @typedef {Object} FormatterConfig
* @property {String} name
* @property {String} outputFile
* @property {String} data
* @property {String} packageName
* @property {String} formatterFlag
* @property {String} outputFlag
* @property {String} dataFlag
* @property {String} packageNameFlag
*/

const junitMode = 'junit';

const defaultConfig = {
name: '',
outputFile: '',
data: '',
packageName: '',
formatterFlag: '--formatter',
outputFlag: '--output',
dataFlag: '--data',
packageNameFlag: '--package',
};

/**
* Returns the default configuration to be overridden by the caller
*
* @returns {FormatterConfig} The default configuration to be overridden by the caller
*/
function defaultFormatterConfig() {
return Object.assign({}, defaultConfig);
}

/**
* Returns based on a default formatter configuration and provided process arguments,the formatter
* Returns whether the formatter configuration indicates a JUnit mode
*
* @param {FormatterConfig} formatterConfig The default configuration to be overridden by the caller
* @return {boolean} Whether the formatter configuration indicates a JUnit mode
*/
function isJUnitMode(formatterConfig) {
return formatterConfig.name === junitMode;
}

/**
* Returns based on a provided formatter configuration and process arguments,the next formatter
* configuration
* @see FormatterConfig
*
Expand All @@ -32,21 +68,46 @@
*
* If an output file has been provided as a command line argument, it is returned.
*
* @param {FormatterConfig} defaultFormatConfig
* @param {FormatterConfig} formatterConfig
* @param {Array.<String>} processArgs An array of command line arguments
* @returns {FormatterConfig} the formatter configuration
*/
function formatterConfig(defaultFormatConfig, processArgs = []) {
let config = {...defaultFormatConfig};
function configFromProcessArgs(formatterConfig, processArgs = []) {
let config = Object.assign({}, formatterConfig);
for (let arg of processArgs) {
if (arg.startsWith(`${defaultFormatConfig.formatterFlag}=`)) {
config.name = arg.replace(`${defaultFormatConfig.formatterFlag}=`, '');
if (
!!formatterConfig.formatterFlag &&
arg.startsWith(`${formatterConfig.formatterFlag}=`)
) {
config.name = arg.replace(`${formatterConfig.formatterFlag}=`, '');
}
if (
!!formatterConfig.outputFlag &&
arg.startsWith(`${formatterConfig.outputFlag}=`)
) {
config.outputFile = arg.replace(`${formatterConfig.outputFlag}=`, '');
}
if (
!!formatterConfig.dataFlag &&
arg.startsWith(`${formatterConfig.dataFlag}=`)
) {
config.data = arg.replace(`${formatterConfig.dataFlag}=`, '');
}
if (arg.startsWith(`${defaultFormatConfig.outputFlag}=`)) {
config.outputFile = arg.replace(`${defaultFormatConfig.outputFlag}=`, '');
if (
!!formatterConfig.packageNameFlag &&
arg.startsWith(`${formatterConfig.packageNameFlag}=`)
) {
config.packageName = arg.replace(
`${formatterConfig.packageNameFlag}=`,
''
);
}
}
return config;
}

module.exports = formatterConfig;
module.exports = {
defaultFormatterConfig,
configFromProcessArgs,
isJUnitMode,
};
File renamed without changes.
75 changes: 0 additions & 75 deletions scripts/shared/junitReporting.js

This file was deleted.

0 comments on commit 613e0ae

Please sign in to comment.