Skip to content

Commit

Permalink
Add test for js1 build viewconfigs
Browse files Browse the repository at this point in the history
Summary:
This diff adds a new `--test` option to `js1 build viewconfigs` which will only check that the configs have not changed instead of writing the new/updated files. This will allow us to run sandcastle checks on the view configs

I also improved the output of the script to give better feedback during normal runs including an additional message and a summary of generated files

Reviewed By: TheSavior

Differential Revision: D15372843

fbshipit-source-id: 4988fc2405cc03137b540817e08d4365cb31fc34
  • Loading branch information
rickhanlonii authored and facebook-github-bot committed May 20, 2019
1 parent 531f11f commit 3ccfbd6
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,33 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @emails oncall+react_native
* @flow
* @format
*/

'use strict';

const generate = require('./generate-view-configs');
const yargs = require('yargs');

const [fileList] = process.argv.slice(2);
const yargv = yargs.strict().option('t', {
alias: 'test',
describe: 'Test the changes and do not write files',
requiresArg: false,
type: 'boolean',
});

const CURRENT_VIEW_CONFIG_SCHEMAS = ['SliderSchema.js'];
const argv = yargv.argv;
const fileList = argv._[0].split('\n');

const CURRENT_VIEW_CONFIG_SCHEMAS = [''];

generate(
fileList
.split('\n')
.filter(fileName =>
CURRENT_VIEW_CONFIG_SCHEMAS.find(supportedFileName =>
fileName.endsWith(supportedFileName),
),
fileList.filter(fileName =>
CURRENT_VIEW_CONFIG_SCHEMAS.find(supportedFileName =>
fileName.endsWith(supportedFileName),
),
),
// $FlowFixMe Type argv
argv.test,
);
46 changes: 40 additions & 6 deletions packages/react-native-codegen/buck_tests/generate-view-configs.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,54 @@ const RNParser = require('../src/generators/RNParser.js');

const path = require('path');

function generate(files: Array<string>): void {
files.forEach(filename => {
type Result = $ReadOnly<{|
libraryName: string,
success: boolean,
|}>;

function generateFilesWithResults(
files: Array<string>,
test: boolean,
): Array<Result> {
return files.reduce((aggregated, filename) => {
const schema = RNParser.parse(filename);
if (schema && schema.modules) {
RNCodegen.generate(
const libraryName = path.basename(filename).replace('Schema.js', '');
const success = RNCodegen.generate(
{
schema,
libraryName,
outputDirectory: path.dirname(filename),
libraryName: path.basename(filename).replace('Schema.js', ''),
},
{generators: ['view-configs']},
{generators: ['view-configs'], test},
);

aggregated.push({
libraryName,
success,
});
}
return aggregated;
}, []);
}

function generate(files: Array<string>, test: boolean): void {
console.log(`${test ? 'Testing' : 'Generating'} view configs`);

const results = generateFilesWithResults(files, test);

const failed = results.filter(result => !result.success);
const totalCount = results.length;

console.log(`\n${test ? 'Tested' : 'Generated'} ${totalCount} view configs`);

if (failed.length) {
if (test === true) {
console.error(`${failed.length} configs changed`);
console.error("Please re-run 'js1 build viewconfigs'");
}
});
process.exit(1);
}
}

module.exports = generate;
46 changes: 40 additions & 6 deletions packages/react-native-codegen/src/generators/RNCodegen.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type Generators =

type Config = $ReadOnly<{|
generators: Array<Generators>,
test?: boolean,
|}>;

const GENERATORS = {
Expand All @@ -61,18 +62,45 @@ const GENERATORS = {
'view-configs': [generateViewConfigJs.generate],
};

function writeMapToFiles(map: Map<string, string>, outputDirectory: string) {
function writeMapToFiles(map: Map<string, string>, outputDir: string) {
let success = true;
map.forEach((contents: string, fileName: string) => {
const location = path.join(outputDirectory, fileName);
fs.writeFileSync(location, contents);
try {
const location = path.join(outputDir, fileName);
fs.writeFileSync(location, contents);
} catch (error) {
success = false;
console.error(`Failed to write ${fileName} to ${outputDir}`, error);
}
});

return success;
}

function checkFilesForChanges(
map: Map<string, string>,
outputDir: string,
): boolean {
let hasChanged = false;

map.forEach((contents: string, fileName: string) => {
const location = path.join(outputDir, fileName);
const currentContents = fs.readFileSync(location, 'utf8');
if (currentContents !== contents) {
console.error(`- ${fileName} has changed`);

hasChanged = true;
}
});

return !hasChanged;
}

module.exports = {
generate(
{libraryName, schema, outputDirectory}: Options,
{generators}: Config,
) {
{generators, test}: Config,
): boolean {
schemaValidator.validate(schema);

const generatedFiles = [];
Expand All @@ -82,6 +110,12 @@ module.exports = {
}
}

writeMapToFiles(new Map([...generatedFiles]), outputDirectory);
const filesToUpdate = new Map([...generatedFiles]);

if (test === true) {
return checkFilesForChanges(filesToUpdate, outputDirectory);
}

return writeMapToFiles(filesToUpdate, outputDirectory);
},
};

0 comments on commit 3ccfbd6

Please sign in to comment.