Build dry run test (#1129)

* Update testMain to support function args

* Add support for dry run and basic validation

* Don't log progress if not a TTY

* Ignore errors from fs.unlinkSync

* Add --dry-run-build-zip option to build zip in memory

* Add test-build
This commit is contained in:
toasted-nutbread 2020-12-18 20:51:22 -05:00 committed by GitHub
parent 2fa9b91515
commit e54a2297e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 82 additions and 41 deletions

View File

@ -17,18 +17,25 @@
const fs = require('fs');
const path = require('path');
const assert = require('assert');
const readline = require('readline');
const childProcess = require('child_process');
const util = require('./util');
const {getAllFiles, getDefaultManifestAndVariants, createManifestString, getArgs} = util;
const {getAllFiles, getDefaultManifestAndVariants, createManifestString, getArgs, testMain} = util;
function clone(value) {
return JSON.parse(JSON.stringify(value));
}
async function createZip(directory, excludeFiles, outputFileName, sevenZipExes=[], onUpdate=null) {
async function createZip(directory, excludeFiles, outputFileName, sevenZipExes, onUpdate, dryRun) {
try {
fs.unlinkSync(outputFileName);
} catch (e) {
// NOP
}
if (!dryRun) {
for (const exe of sevenZipExes) {
try {
const excludeArguments = excludeFiles.map((excludeFilePath) => `-x!${excludeFilePath}`);
@ -49,10 +56,11 @@ async function createZip(directory, excludeFiles, outputFileName, sevenZipExes=[
// NOP
}
}
return await createJSZip(directory, excludeFiles, outputFileName, onUpdate);
}
return await createJSZip(directory, excludeFiles, outputFileName, onUpdate, dryRun);
}
async function createJSZip(directory, excludeFiles, outputFileName, onUpdate) {
async function createJSZip(directory, excludeFiles, outputFileName, onUpdate, dryRun) {
const JSZip = util.JSZip;
const files = getAllFiles(directory, directory);
removeItemsFromArray(files, excludeFiles);
@ -76,8 +84,10 @@ async function createJSZip(directory, excludeFiles, outputFileName, onUpdate) {
}, onUpdate);
process.stdout.write('\n');
if (!dryRun) {
fs.writeFileSync(outputFileName, data, {encoding: null, flag: 'w'});
}
}
function removeItemsFromArray(array, removeItems) {
for (const item of removeItems) {
@ -272,15 +282,18 @@ function createVariantManifest(manifest, variant, variantMap) {
return modifiedManifest;
}
async function build(manifest, buildDir, extDir, manifestPath, variantMap, variantNames) {
async function build(manifest, buildDir, extDir, manifestPath, variantMap, variantNames, dryRun, dryRunBuildZip) {
const sevenZipExes = ['7za', '7z'];
// Create build directory
if (!fs.existsSync(buildDir)) {
if (!fs.existsSync(buildDir) && !dryRun) {
fs.mkdirSync(buildDir, {recursive: true});
}
const dontLogOnUpdate = !process.stdout.isTTY;
const onUpdate = (metadata) => {
if (dontLogOnUpdate) { return; }
let message = `Progress: ${metadata.percent.toFixed(2)}%`;
if (metadata.currentFile) {
message += ` (${metadata.currentFile})`;
@ -305,30 +318,48 @@ async function build(manifest, buildDir, extDir, manifestPath, variantMap, varia
const fileNameSafe = path.basename(fileName);
const fullFileName = path.join(buildDir, fileNameSafe);
ensureFilesExist(extDir, excludeFiles);
if (!dryRun) {
fs.writeFileSync(manifestPath, createManifestString(modifiedManifest));
await createZip(extDir, excludeFiles, fullFileName, sevenZipExes, onUpdate);
}
if (!dryRun || dryRunBuildZip) {
await createZip(extDir, excludeFiles, fullFileName, sevenZipExes, onUpdate, dryRun);
}
if (!dryRun) {
if (Array.isArray(fileCopies)) {
for (const fileName2 of fileCopies) {
const fileName2Safe = path.basename(fileName2);
fs.copyFileSync(fullFileName, path.join(buildDir, fileName2Safe));
}
}
}
process.stdout.write('\n');
}
}
function ensureFilesExist(directory, files) {
for (const file of files) {
assert.ok(fs.existsSync(path.join(directory, file)));
}
}
async function main() {
const argv = process.argv.slice(2);
async function main(argv) {
const args = getArgs(argv, new Map([
['all', false],
['default', false],
['manifest', null],
['dry-run', false],
['dry-run-build-zip', false],
[null, []]
]));
const dryRun = args.get('dry-run');
const dryRunBuildZip = args.get('dry-run-build-zip');
const {manifest, variants} = getDefaultManifestAndVariants();
const rootDir = path.join(__dirname, '..');
@ -343,7 +374,7 @@ async function main() {
try {
const variantNames = (argv.length === 0 || args.get('all') ? variants.map(({name}) => name) : args.get(null));
await build(manifest, buildDir, extDir, manifestPath, variantMap, variantNames);
await build(manifest, buildDir, extDir, manifestPath, variantMap, variantNames, dryRun, dryRunBuildZip);
} finally {
// Restore manifest
let restoreManifest = manifest;
@ -354,9 +385,18 @@ async function main() {
}
}
process.stdout.write('Restoring manifest...\n');
if (!dryRun) {
fs.writeFileSync(manifestPath, createManifestString(restoreManifest));
}
}
}
if (require.main === module) { main(); }
if (require.main === module) {
testMain(main, process.argv.slice(2));
}
module.exports = {
main
};

View File

@ -137,9 +137,9 @@ function createDictionaryArchive(dictionaryDirectory, dictionaryName) {
}
async function testMain(func) {
async function testMain(func, ...args) {
try {
await func();
await func(...args);
} catch (e) {
console.log(e);
process.exit(-1);

View File

@ -7,11 +7,12 @@
},
"scripts": {
"build": "node ./dev/build.js",
"test": "npm run test-lint && npm run test-code && npm run test-manifest",
"test": "npm run test-lint && npm run test-code && npm run test-manifest && npm run test-build",
"test-lint": "eslint . && node ./dev/lint/global-declarations.js && node ./dev/lint/html-scripts.js",
"test-lint-web-ext": "npx web-ext lint",
"test-code": "node ./test/test-all.js ./test --skip ./test/test-manifest.js",
"test-manifest": "node ./test/test-manifest.js"
"test-manifest": "node ./test/test-manifest.js",
"test-build": "node ./dev/build.js --dry-run --all"
},
"repository": {
"type": "git",