fix(build): build/test on windows (#6431)

* build: build/test on windows

* chore(deps): bump @hyperjump/json-schema from 0.18.4 to 0.18.5
* chore(deps): add gulp-gzip 1.4.2
* build: migrate test scripts to gulp task (test_tasks.js)
* build: not to use the grep command
* build: normalize path

* fix: Modified based on review suggestions.
* Add JSDoc comment
* Line length <= 80 characters.
* Formatting test output as previously.
* Always continue even if a test unit fails.
* Suppress the gulp messages.
* Fix test_tasks.js to pass eslint.

* fix: Modified based on review suggestions.
* Change generator test output directory.
* Formatting test output as previously.

* fix: Formatting test output as previously.

* fix: Modified based on review suggestions.
This commit is contained in:
YAMADA Yutaka
2022-10-28 05:02:50 +09:00
committed by GitHub
parent c1fbcc5bed
commit 52879dd953
11 changed files with 604 additions and 209 deletions

View File

@@ -28,6 +28,8 @@ var rimraf = require('rimraf');
var {BUILD_DIR, DEPS_FILE, TEST_DEPS_FILE, TSC_OUTPUT_DIR, TYPINGS_BUILD_DIR} = require('./config');
var {getPackageJson} = require('./helper_tasks');
var {posixPath} = require('../helpers');
////////////////////////////////////////////////////////////
// Build //
////////////////////////////////////////////////////////////
@@ -102,7 +104,9 @@ const NAMESPACE_PROPERTY = '__namespace__';
const chunks = [
{
name: 'blockly',
entry: path.join(CORE_DIR, 'main.js'),
entry: posixPath((argv.compileTs) ?
path.join(TSC_OUTPUT_DIR, CORE_DIR, 'main.js') :
path.join(CORE_DIR, 'main.js')),
exports: 'module$build$src$core$blockly',
reexport: 'Blockly',
},
@@ -337,6 +341,18 @@ function buildDeps(done) {
'tests/mocha'
];
/**
* Extracts lines that contain the specified keyword.
* @param {string} text output text
* @param {string} keyword extract lines with this keyword
* @returns {string} modified text
*/
function extractOutputs(text, keyword) {
return text.split('\n')
.filter((line) => line.includes(keyword))
.join('\n');
}
function filterErrors(text) {
return text.split('\n')
.filter(
@@ -349,29 +365,29 @@ function buildDeps(done) {
new Promise((resolve, reject) => {
const args = roots.map(root => `--root '${root}' `).join('');
exec(
`closure-make-deps ${args} >'${DEPS_FILE}'`,
{stdio: ['inherit', 'inherit', 'pipe']},
`closure-make-deps ${args}`,
(error, stdout, stderr) => {
console.warn(filterErrors(stderr));
if (error) {
reject(error);
} else {
fs.writeFileSync(DEPS_FILE, stdout);
resolve();
}
});
}).then(() => new Promise((resolve, reject) => {
// Use grep to filter out the entries that are already in deps.js.
// Filter out the entries that are already in deps.js.
const testArgs =
testRoots.map(root => `--root '${root}' `).join('');
exec(
`closure-make-deps ${testArgs} 2>/dev/null\
| grep 'tests/mocha' > '${TEST_DEPS_FILE}'`,
{stdio: ['inherit', 'inherit', 'pipe']},
`closure-make-deps ${testArgs}`,
(error, stdout, stderr) => {
console.warn(filterErrors(stderr));
if (error) {
reject(error);
} else {
fs.writeFileSync(TEST_DEPS_FILE,
extractOutputs(stdout, 'tests/mocha'));
resolve();
}
});
@@ -520,9 +536,6 @@ return ${chunk.exports};
* closure-calculate-chunks.
*/
function getChunkOptions() {
if (argv.compileTs) {
chunks[0].entry = path.join(TSC_OUTPUT_DIR, chunks[0].entry);
}
const basePath =
path.join(TSC_OUTPUT_DIR, 'closure', 'goog', 'base_minimal.js');
const cccArgs = [
@@ -560,7 +573,9 @@ function getChunkOptions() {
// chunk depends on any chunk but the first), so we look for
// one of the entrypoints amongst the files in each chunk.
const chunkByNickname = Object.create(null);
const jsFiles = rawOptions.js.slice(); // Will be modified via .splice!
// Copy and convert to posix js file paths.
// Result will be modified via `.splice`!
const jsFiles = rawOptions.js.map(p => posixPath(p));
const chunkList = rawOptions.chunk.map((element) => {
const [nickname, numJsFiles, parentNick] = element.split(':');

View File

@@ -0,0 +1,350 @@
/**
* @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} = 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 process, checking to ensure there are no
* closure compiler warnings / errors.
* @return {Promise} asynchronous result
*/
function buildDebug() {
return runTestCommand('build-debug', 'npm run build-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('build', file);
const stream = gulp.src(name)
.pipe(gzip())
.pipe(gulp.dest('build'));
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(BUILD_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 the package build process, as Node tests depend on it.
* @return {Promise} asynchronous result
*/
function package() {
return runTestCommand('package', 'npm run package');
}
/**
* 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 test: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,
buildDebug,
renamings,
metadata,
mocha,
generators,
package,
node,
advancedCompile,
reportTestResult,
];
// Run all tests in sequence.
const test = gulp.series(...testTasks);
module.exports = {
test,
generators,
};