Files
blockly/tests/migration/validate-renamings.js
Christopher Allen 3c723f0199 feat(tests): Add a test to validate scripts/migration/renamings.js (#5980)
* feat(tests): Add a test script for the renamings file

* Make scripts/migration/renamings.js a valid JSON5 file.
* Add a schema for it in tests/migration/renamings-schema.json.
* Add a script to test one against the other, as
  tests/migration/validate-renamings.  It is a node.js script
  that will exit 0 if the renamings file is valid, or 1 if it
  is not (and print a not-very-helpful error message from which
  is possible, with some effort, to deduce what the error is.)

* fix: convert to JSON5 and refactor example

* fix: convert keys to new key names

* fix: change versions to arrays instead of objects

* fix: fix version numbers

* fix: associate renames with the version where they were added

* fix: fixup example

* fix: put older renames at the bottom, and newer renames at the top

* fix: enable renamings test in run_all_tests

* fix: put newer renames back at the bottom

This reverts commit efe070e344.

* fix(tests): add missing additionalProperties: false to schema

And fix the resulting validation error in the example entry.

* chore(tests): Improve wording of example entry; add extra examples

Tweaked line wrapping of some existing entries (example and
acutal) that were otherwise untouched.

* feat: add dropDownDiv renaming to new database

* feat(tests): Check for duplicate entries for the same module

Also fixed extraneous whitespace in run_all_tests.sh

* fix(tests): Make validate-renamings.js compatible with node.js v12

Also remove success message, to adhere to usual unix convention
(silence implies success) as eslint does, and reduce unecessary
npm test output clutter.

* fix(tests): Fixes for PR #5980

* Fix typos.
* Remove redundant check.

Co-authored-by: Beka Westberg <bwestberg@google.com>
2022-03-09 11:29:43 -08:00

74 lines
1.9 KiB
JavaScript
Executable File

#!/usr/bin/env node
/**
* @license
* Copyright 2022 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @fileoverview A script to validate the renamings file
* (scripts/migration/renamings.json5) agaist the schema
* (renamings-schema.json).
*/
/* global require __dirname process */
const JsonSchema = require('@hyperjump/json-schema');
const JSON5 = require('json5');
const fs = require('fs');
const path = require('path');
/**
* Renaming schema filename.
* @type {string}
*/
const SCHEMA_FILENAME = path.join(__dirname, 'renamings-schema.json');
/**
* Renamings filename.
* @type {string}
*/
const RENAMINGS_FILENAME =
path.resolve(__dirname, '../../scripts/migration/renamings.json5');
// Can't use top-level await outside a module, and can't use require
// in a module, so use an IIAFE.
(async function() {
const schemaUrl = 'file://' + path.resolve(SCHEMA_FILENAME);
const schema = await JsonSchema.get(schemaUrl);
const renamingsJson5 = fs.readFileSync(RENAMINGS_FILENAME);
const renamings = JSON5.parse(renamingsJson5);
const output =
await JsonSchema.validate(schema, renamings, JsonSchema.DETAILED);
if (!output.valid) {
console.log('Renamings file is invalid.');
console.log('Maybe this validator output will help you find the problem:');
console.log(JSON5.stringify(output, undefined, ' '));
process.exit(1);
}
// File passed schema validation. Do some additional checks.
let ok = true;
Object.entries(renamings).forEach(([version, modules]) => {
// Scan through modules and check for duplicates.
const seen = new Set();
for (const {oldName} of modules) {
if (seen.has(oldName)) {
console.log(`Duplicate entry for module ${oldName} ` +
`in version ${version}.`);
ok = false;
}
seen.add(oldName);
}
});
if (!ok) {
console.log('Renamings file is invalid.');
process.exit(1);
}
// Default is a successful exit 0.
})();