Files
blockly/scripts/gulpfiles/test_tasks.js
Christopher Allen 52a0d525d7 chore(build): Remove build products from the Blockly repository (#6475)
* feat(build): Make build tasks invoke their prerequisites

  - Divide gulp targets into three kinds: main sequence,
    manually invokable, and script-only.  The first two categories
    automatically invoke their prerequisites.
  - Give (most of) the affected gulp targets shorter and more memorable
    names that could become their npm script names in future.

* feat(build): Make package tasks invoke their prerequisites

  Have the package task invoke the cleanBuildDir (as well as
  cleanPackageDir) and build tasks.  Remove the checkBuildDir
  task as it is now redundant since a fresh build is done every
  time.

* feat(build): Make git tasks invoke their prerequisites

* feat(build): Make cleanup, license [sic] tasks invoke their prerequisites

  Turns out they don't have any, so this commit just classifies
  their gulp targets according to the established scheme.

* feat(build): Make appengine tasks invoke their prerequisites

  In this case prepareDeployDir will eventually depend on package
  but does not for now.

* feat(build): Have npm scripts run npm ci first where applicable

  Have any npm script that have external effects (e.g. publishing an
  npm package, pushing a new version to appengine, or updating GitHub
  Pages) start by running npm ci to ensure that all dependencies are
  up-to-date with respect to package-lock.json.

  (This is done by npm and not a gulp script because gulp itself
  might need updating.  So might npm, but that is less likely to
  make any difference to what gets published/pushed.)

* chore(build): have tests use package target

  Have the tests just run the package target (with debug flags)
  since that runs the the build target automatically.

* feat(tests): Write Closure Compiler output directly to dist/

  Since they are already UMD-wrapped, have Closure Compiler write
  output chunks directly to RELEASE_DIR, i.e. dist/.

* chore(tests): Use freshly-build files in compressed mode.

  Use the freshly-built build/*_compresssed.js files when bootstrapping
  in compressed mode, rather than using the checked-in files in the
  repository root.

  This helps ensure that compressed and uncompressed mode will be
  testing (as closely as possible) the same code.

  Obsoletes #6218 (though the issues discussed there have not actually
  yet been addressed in this branch).

* chore(build): Write intermediate langfiles to build/msg

  Write the results of create_messages.py to build/msg instead of
  build/msg/js.

* fix(build): Use build/msg/en.js instead of msg/messages.js in tests

  This has no direct effect but fixes a long-standing misdesign
  where we are testing against the input to, rather than the output
  of, the language file processing pipeline.

* feat(demos): Use freshly-built files

  Use the freshly-built dist/*_compresssed.js and build/msg/* files
  rather than using the checked-in files in the repository root.

  This helps ensure that these demos are using the most recent
  version of Blockly (even in the develop branch).

* fix(build): Update appengine deployment to include built files

  Modify the prepareDemos task as follows:

  - Use the git index instead of HEAD, so that most local changes
    will be applied (without copying whatever .gitignored cruft
    might be in the local directory).
  - Run clean and build and then copy build/msg and
    dist/*_compressed.js* to the deploy directory.

  This fixes the problem created by the previous commit, wherein the
  demos relied on built files that were not being deployed to
  appengine.

* fix(build): Update GitHub Pages deployment to include built files

  Modify the updateGithubPages task to run clean and build and
  then git add build/msg dist/*_compressed.js*, so that they will
  be included in the deployed pages.

  This fixes the problem created by the previous^2 commit,
  wherein the demos relied on built files that were not being
  deployed to GitHub Pages.

* chore(build): Remove build products from repository

  Remove *_compressed.js* and msg/js/* from the blockly repository.
  Also remove the now-obsolete checkinBuilt gulp task.

* chore(build): Apply relevant changes to test_tasks.js

  Apply changes made to run_all_tests.sh and check_metadata.sh to
  the corresponding parts of their JS replacements in
  test_tasks.js.

* chore(build): Make updates suggested in PR #6475

  - Remove `clean:builddir` and `clean:releasedir` - `clean`
    is sufficient.
  - Remove duplicate `require` from `appengine_tasks.js`.

* feat(build): Use shorter npm script names

  Since scripts that run build tasks now automatically run their
  prerequisite tasks, the previous naming scheme of task `build`
  running all the `build:subtask`s no longe really makes very
  much sense.

  Additionally, following a chat discussion, there seems to be a
  rough consensus to use "messages" to refer to the .json input
  files, and "langfiles" to the generated .js output files.

  Consequently, simplify npm script names by renaming as follows:

  - "generate:langfiles" -> "messages"
  - "build:langfiles" -> "langfiles"
  - "build:js" -> "tsc"
  - "build:deps" -> "deps"
  - "build:compiled" -> "minify"
  - "build:compressed": delete this synonym for "build:compiled",

  ("minify" was chosen as agnostic to Closure Compiler vs. WebPack.)

* chores(build): Add deprecation notice for old scripts

  To reduce potential confusion/frustration, restore the previous
  npm scripts but have them display a deprecation notice instead
  (note that npm prints the script contents before running it, so
  echo is not needed).

* docs(build): Add comments distinguishing 'messages' from 'langfiles'
2022-11-03 13:15:10 +00:00

343 lines
9.0 KiB
JavaScript

/**
* @license
* Copyright 2022 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @fileoverview Gulp tasks to test.
*/
/* eslint-env node */
const gulp = require('gulp');
const gzip = require('gulp-gzip');
const fs = require('fs');
const path = require('path');
const {execSync} = require('child_process');
const rimraf = require('rimraf');
const {BUILD_DIR, RELEASE_DIR} = require('./config');
const runMochaTestsInBrowser =
require('../../tests/mocha/run_mocha_tests_in_browser.js');
const runGeneratorsInBrowser =
require('../../tests/generators/run_generators_in_browser.js');
const OUTPUT_DIR = 'build/generators';
const GOLDEN_DIR = 'tests/generators/golden';
const BOLD_GREEN = '\x1b[1;32m';
const BOLD_RED = '\x1b[1;31m';
const ANSI_RESET = '\x1b[0m';
let failerCount = 0;
/**
* Helper method for running test code block.
* @param {string} id test id
* @param {function} block test code block
* @return {Promise} asynchronous result
*/
function runTestBlock(id, block) {
return new Promise((resolve) => {
console.log('=======================================');
console.log(`== ${id}`);
if (process.env.CI) console.log('::group::');
block()
.then((result) => {
if (process.env.CI) console.log('::endgroup::');
console.log(`${BOLD_GREEN}SUCCESS:${ANSI_RESET} ${id}`);
resolve(result);
})
.catch((err) => {
failerCount++;
console.error(err.message);
if (process.env.CI) console.log('::endgroup::');
console.log(`${BOLD_RED}FAILED:${ANSI_RESET} ${id}`);
// Always continue.
resolve(err);
});
});
}
/**
* Helper method for running test command.
* @param {string} id test id
* @param {string} command command line to run
* @return {Promise} asynchronous result
*/
function runTestCommand(id, command) {
return runTestBlock(id, async() => {
return execSync(command, {stdio: 'inherit'});
}, false);
}
/**
* Lint the codebase.
* Skip for CI environments, because linting is run separately.
* @return {Promise} asynchronous result
*/
function eslint() {
if (process.env.CI) {
console.log('Skip linting.');
return Promise.resolve();
}
return runTestCommand('eslint', 'eslint .');
}
/**
* Run the full usual build and package process, checking to ensure
* there are no closure compiler warnings / errors.
* @return {Promise} asynchronous result
*/
function build() {
return runTestCommand('build + package',
'npm run package -- --verbose --debug');
}
/**
* Run renaming validation test.
* @return {Promise} asynchronous result
*/
function renamings() {
return runTestCommand('renamings', 'node tests/migration/validate-renamings.js');
}
/**
* Helper method for gzipping file.
* @param {string} file target file
* @return {Promise} asynchronous result
*/
function gzipFile(file) {
return new Promise((resolve) => {
const name = path.posix.join(RELEASE_DIR, file);
const stream = gulp.src(name)
.pipe(gzip())
.pipe(gulp.dest(RELEASE_DIR));
stream.on('end', () => {
resolve();
});
});
}
/**
* Helper method for comparing file size.
* @param {string} file target file
* @param {number} expected expected size
* @return {number} 0: success / 1: failed
*/
function compareSize(file, expected) {
const name = path.posix.join(RELEASE_DIR, file);
const compare = Math.floor(expected * 1.1);
const stat = fs.statSync(name);
const size = stat.size;
if (size > compare) {
const message = `Failed: ` +
`Size of ${name} has grown more than 10%. ${size} vs ${expected} `;
console.log(`${BOLD_RED}${message}${ANSI_RESET}`);
return 1;
} else {
const message =
`Size of ${name} at ${size} compared to previous ${expected}`;
console.log(`${BOLD_GREEN}${message}${ANSI_RESET}`);
return 0;
}
}
/**
* Helper method for zipping the compressed files.
* @return {Promise} asynchronous result
*/
function zippingFiles() {
// GZip them for additional size comparisons (keep originals, force
// overwite previously-gzipped copies).
console.log('Zipping the compressed files');
const gzip1 = gzipFile('blockly_compressed.js');
const gzip2 = gzipFile('blocks_compressed.js');
return Promise.all([gzip1, gzip2]);
}
/**
* Check the sizes of built files for unexpected growth.
* @return {Promise} asynchronous result
*/
function metadata() {
return runTestBlock('metadata', async() => {
// Zipping the compressed files.
await zippingFiles();
// Read expected size from script.
const contents = fs.readFileSync('tests/scripts/check_metadata.sh')
.toString();
const pattern = /^readonly (?<key>[A-Z_]+)=(?<value>\d+)$/gm;
const matches = contents.matchAll(pattern);
const expected = {};
for (const match of matches) {
expected[match.groups.key] = match.groups.value;
}
// Check the sizes of the files.
let failed = 0;
failed += compareSize('blockly_compressed.js',
expected.BLOCKLY_SIZE_EXPECTED);
failed += compareSize('blocks_compressed.js',
expected.BLOCKS_SIZE_EXPECTED);
failed += compareSize('blockly_compressed.js.gz',
expected.BLOCKLY_GZ_SIZE_EXPECTED);
failed += compareSize('blocks_compressed.js.gz',
expected.BLOCKS_GZ_SIZE_EXPECTED);
if (failed > 0) {
throw new Error('Unexpected growth was detected.');
}
});
}
/**
* Run Mocha tests inside a browser.
* @return {Promise} asynchronous result
*/
function mocha() {
return runTestBlock('mocha', async() => {
const result = await runMochaTestsInBrowser().catch(e => {
throw e;
});
if (result) {
throw new Error('Mocha tests failed');
}
console.log('Mocha tests passed');
});
}
/**
* Helper method for comparison file.
* @param {string} file1 first target file
* @param {string} file2 second target file
* @return {boolean} comparison result (true: same / false: different)
*/
function compareFile(file1, file2) {
const buf1 = fs.readFileSync(file1);
const buf2 = fs.readFileSync(file2);
// Normalize the line feed.
const code1 = buf1.toString().replace(/(?:\r\n|\r|\n)/g, '\n');
const code2 = buf2.toString().replace(/(?:\r\n|\r|\n)/g, '\n');
const result = (code1 === code2);
return result;
}
/**
* Helper method for checking the result of generator.
* @param {string} suffix target suffix
* @return {number} check result (0: success / 1: failed)
*/
function checkResult(suffix) {
const fileName = `generated.${suffix}`;
const resultFileName = path.posix.join(OUTPUT_DIR, fileName);
const SUCCESS_PREFIX = `${BOLD_GREEN}SUCCESS:${ANSI_RESET}`;
const FAILURE_PREFIX = `${BOLD_RED}FAILED:${ANSI_RESET}`;
if (fs.existsSync(resultFileName)) {
const goldenFileName = path.posix.join(GOLDEN_DIR, fileName);
if (fs.existsSync(goldenFileName)) {
if (compareFile(resultFileName, goldenFileName)) {
console.log(`${SUCCESS_PREFIX} ${suffix}: ` +
`${resultFileName} matches ${goldenFileName}`);
return 0;
} else {
console.log(
`${FAILURE_PREFIX} ${suffix}: ` +
`${resultFileName} does not match ${goldenFileName}`);
}
} else {
console.log(`File ${goldenFileName} not found!`);
}
} else {
console.log(`File ${resultFileName} not found!`);
}
return 1;
}
/**
* Run generator tests inside a browser and check the results.
* @return {Promise} asynchronous result
*/
function generators() {
return runTestBlock('generators', async() => {
// Clean up.
rimraf.sync(OUTPUT_DIR);
fs.mkdirSync(OUTPUT_DIR);
await runGeneratorsInBrowser(OUTPUT_DIR).catch(() => {});
const generatorSuffixes = ['js', 'py', 'dart', 'lua', 'php'];
let failed = 0;
generatorSuffixes.forEach((suffix) => {
failed += checkResult(suffix);
});
if (failed === 0) {
console.log(`${BOLD_GREEN}All generator tests passed.${ANSI_RESET}`);
} else {
console.log(
`${BOLD_RED}Failures in ${failed} generator tests.${ANSI_RESET}`);
throw new Error('Generator tests failed.');
}
});
}
/**
* Run Node tests.
* @return {Promise} asynchronous result
*/
function node() {
return runTestCommand('node', 'mocha tests/node --config tests/node/.mocharc.js');
}
/**
* Attempt advanced compilation of a Blockly app.
* @return {Promise} asynchronous result
*/
function advancedCompile() {
return runTestCommand('advanced_compile', 'npm run only:compile:advanced');
}
/**
* Report test result.
* @return {Promise} asynchronous result
*/
function reportTestResult() {
console.log('=======================================');
// Check result.
if (failerCount === 0) {
console.log(`${BOLD_GREEN}All tests passed.${ANSI_RESET}`);
return Promise.resolve();
} else {
console.log(`${BOLD_RED}Failures in ${failerCount} test groups.${ANSI_RESET}`);
return Promise.reject();
}
}
// Indivisual tasks.
const testTasks = [
eslint,
build,
renamings,
metadata,
mocha,
generators,
node,
advancedCompile,
reportTestResult,
];
// Run all tests in sequence.
const test = gulp.series(...testTasks);
module.exports = {
test,
generators,
};