Dev/test script organization (#846)
* Move lint scripts * Move dictionary-validate.js * Move schema-validate.js * Move createTestDictionaryArchive, remove yomichan-test.js * Rename yomichan-util.js to util.js * Move test/yomichan-vm.js to dev/vm.js * Move getArgs into util.js (and fix name) * Create test-all.js * Update test-code script
This commit is contained in:
parent
4293f73153
commit
51d4e5b0ff
50
dev/build.js
50
dev/build.js
@ -19,8 +19,8 @@ const fs = require('fs');
|
||||
const path = require('path');
|
||||
const readline = require('readline');
|
||||
const childProcess = require('child_process');
|
||||
const util = require('./yomichan-util');
|
||||
const {getAllFiles, getDefaultManifestAndVariants, createManifestString} = util;
|
||||
const util = require('./util');
|
||||
const {getAllFiles, getDefaultManifestAndVariants, createManifestString, getArgs} = util;
|
||||
|
||||
|
||||
function clone(value) {
|
||||
@ -208,54 +208,10 @@ async function build(manifest, buildDir, extDir, manifestPath, variantMap, varia
|
||||
}
|
||||
}
|
||||
|
||||
function getArs(args, argMap) {
|
||||
let key = null;
|
||||
let canKey = true;
|
||||
let onKey = false;
|
||||
for (const arg of args) {
|
||||
onKey = false;
|
||||
|
||||
if (canKey && arg.startsWith('--')) {
|
||||
if (arg.length === 2) {
|
||||
canKey = false;
|
||||
key = null;
|
||||
onKey = false;
|
||||
} else {
|
||||
key = arg.substring(2);
|
||||
onKey = true;
|
||||
}
|
||||
}
|
||||
|
||||
const target = argMap.get(key);
|
||||
if (typeof target === 'boolean') {
|
||||
argMap.set(key, true);
|
||||
key = null;
|
||||
} else if (typeof target === 'number') {
|
||||
argMap.set(key, target + 1);
|
||||
key = null;
|
||||
} else if (target === null || typeof target === 'string') {
|
||||
if (!onKey) {
|
||||
argMap.set(key, arg);
|
||||
key = null;
|
||||
}
|
||||
} else if (Array.isArray(target)) {
|
||||
if (!onKey) {
|
||||
target.push(arg);
|
||||
key = null;
|
||||
}
|
||||
} else {
|
||||
console.error(`Unknown argument: ${arg}`);
|
||||
key = null;
|
||||
}
|
||||
}
|
||||
|
||||
return argMap;
|
||||
}
|
||||
|
||||
|
||||
async function main() {
|
||||
const argv = process.argv.slice(2);
|
||||
const args = getArs(argv, new Map([
|
||||
const args = getArgs(argv, new Map([
|
||||
['all', false],
|
||||
['default', false],
|
||||
['manifest', null],
|
||||
|
@ -17,8 +17,8 @@
|
||||
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const {JSZip} = require('../dev/yomichan-util');
|
||||
const {VM} = require('./yomichan-vm');
|
||||
const {JSZip} = require('./util');
|
||||
const {VM} = require('./vm');
|
||||
|
||||
const vm = new VM();
|
||||
vm.execute([
|
@ -19,7 +19,7 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const assert = require('assert');
|
||||
const {getAllFiles} = require('../../dev/yomichan-util');
|
||||
const {getAllFiles} = require('../util');
|
||||
|
||||
|
||||
function countOccurences(string, pattern) {
|
@ -16,7 +16,7 @@
|
||||
*/
|
||||
|
||||
const fs = require('fs');
|
||||
const {VM} = require('./yomichan-vm');
|
||||
const {VM} = require('./vm');
|
||||
|
||||
const vm = new VM();
|
||||
vm.execute([
|
@ -32,6 +32,50 @@ function getJSZip() {
|
||||
}
|
||||
|
||||
|
||||
function getArgs(args, argMap) {
|
||||
let key = null;
|
||||
let canKey = true;
|
||||
let onKey = false;
|
||||
for (const arg of args) {
|
||||
onKey = false;
|
||||
|
||||
if (canKey && arg.startsWith('--')) {
|
||||
if (arg.length === 2) {
|
||||
canKey = false;
|
||||
key = null;
|
||||
onKey = false;
|
||||
} else {
|
||||
key = arg.substring(2);
|
||||
onKey = true;
|
||||
}
|
||||
}
|
||||
|
||||
const target = argMap.get(key);
|
||||
if (typeof target === 'boolean') {
|
||||
argMap.set(key, true);
|
||||
key = null;
|
||||
} else if (typeof target === 'number') {
|
||||
argMap.set(key, target + 1);
|
||||
key = null;
|
||||
} else if (target === null || typeof target === 'string') {
|
||||
if (!onKey) {
|
||||
argMap.set(key, arg);
|
||||
key = null;
|
||||
}
|
||||
} else if (Array.isArray(target)) {
|
||||
if (!onKey) {
|
||||
target.push(arg);
|
||||
key = null;
|
||||
}
|
||||
} else {
|
||||
console.error(`Unknown argument: ${arg}`);
|
||||
key = null;
|
||||
}
|
||||
}
|
||||
|
||||
return argMap;
|
||||
}
|
||||
|
||||
function getAllFiles(baseDirectory, relativeTo=null, predicate=null) {
|
||||
const results = [];
|
||||
const directories = [baseDirectory];
|
||||
@ -69,11 +113,36 @@ function createManifestString(manifest) {
|
||||
return JSON.stringify(manifest, null, 4) + '\n';
|
||||
}
|
||||
|
||||
function createDictionaryArchive(dictionaryDirectory, dictionaryName) {
|
||||
const fileNames = fs.readdirSync(dictionaryDirectory);
|
||||
|
||||
const JSZip2 = getJSZip();
|
||||
const archive = new JSZip2();
|
||||
|
||||
for (const fileName of fileNames) {
|
||||
if (/\.json$/.test(fileName)) {
|
||||
const content = fs.readFileSync(path.join(dictionaryDirectory, fileName), {encoding: 'utf8'});
|
||||
const json = JSON.parse(content);
|
||||
if (fileName === 'index.json' && typeof dictionaryName === 'string') {
|
||||
json.title = dictionaryName;
|
||||
}
|
||||
archive.file(fileName, JSON.stringify(json, null, 0));
|
||||
} else {
|
||||
const content = fs.readFileSync(path.join(dictionaryDirectory, fileName), {encoding: null});
|
||||
archive.file(fileName, content);
|
||||
}
|
||||
}
|
||||
|
||||
return archive;
|
||||
}
|
||||
|
||||
|
||||
module.exports = {
|
||||
get JSZip() { return getJSZip(); },
|
||||
getArgs,
|
||||
getAllFiles,
|
||||
getDefaultManifest,
|
||||
getDefaultManifestAndVariants,
|
||||
createManifestString
|
||||
createManifestString,
|
||||
createDictionaryArchive
|
||||
};
|
@ -8,8 +8,8 @@
|
||||
"scripts": {
|
||||
"build": "node ./dev/build.js",
|
||||
"test": "npm run test-lint && npm run test-code && npm run test-manifest",
|
||||
"test-lint": "eslint . && node ./test/lint/global-declarations.js",
|
||||
"test-code": "node ./test/test-schema.js && node ./test/test-dictionary.js && node ./test/test-database.js && node ./test/test-document-util.js && node ./test/test-object-property-accessor.js && node ./test/test-japanese.js && node ./test/test-text-source-map.js && node ./test/test-dom-text-scanner.js && node ./test/test-cache-map.js && node ./test/test-profile-conditions.js && node ./test/test-core.js",
|
||||
"test-lint": "eslint . && node ./dev/lint/global-declarations.js",
|
||||
"test-code": "node ./test/test-all.js ./test --skip ./test/test-manifest.js",
|
||||
"test-manifest": "node ./test/test-manifest.js"
|
||||
},
|
||||
"repository": {
|
||||
|
66
test/test-all.js
Normal file
66
test/test-all.js
Normal file
@ -0,0 +1,66 @@
|
||||
/*
|
||||
* Copyright (C) 2020 Yomichan Authors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const {spawnSync} = require('child_process');
|
||||
const {getArgs} = require('../dev/util');
|
||||
|
||||
|
||||
function main() {
|
||||
const args = getArgs(process.argv.slice(2), new Map([
|
||||
['skip', []],
|
||||
[null, []]
|
||||
]));
|
||||
const directories = args.get(null);
|
||||
const skip = new Set([__filename, ...args.get('skip')].map((value) => path.resolve(value)));
|
||||
|
||||
const node = process.execPath;
|
||||
const fileNamePattern = /\.js$/i;
|
||||
|
||||
let first = true;
|
||||
for (const directory of directories) {
|
||||
const fileNames = fs.readdirSync(directory);
|
||||
for (const fileName of fileNames) {
|
||||
if (!fileNamePattern.test(fileName)) { continue; }
|
||||
|
||||
const fullFileName = path.resolve(path.join(directory, fileName));
|
||||
if (skip.has(fullFileName)) { continue; }
|
||||
|
||||
const stats = fs.lstatSync(fullFileName);
|
||||
if (!stats.isFile()) { continue; }
|
||||
|
||||
process.stdout.write(`${first ? '' : '\n'}Running ${fileName}...\n`);
|
||||
first = false;
|
||||
|
||||
const {error, status} = spawnSync(node, [fileName], {cwd: directory, stdio: 'inherit'});
|
||||
|
||||
if (status !== null && status !== 0) {
|
||||
process.exit(status);
|
||||
return;
|
||||
}
|
||||
if (error) {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
|
||||
if (require.main === module) { main(); }
|
@ -16,7 +16,7 @@
|
||||
*/
|
||||
|
||||
const assert = require('assert');
|
||||
const {VM} = require('./yomichan-vm');
|
||||
const {VM} = require('../dev/vm');
|
||||
|
||||
const vm = new VM({console});
|
||||
vm.execute([
|
||||
|
@ -17,7 +17,7 @@
|
||||
|
||||
const assert = require('assert');
|
||||
const crypto = require('crypto');
|
||||
const {VM} = require('./yomichan-vm');
|
||||
const {VM} = require('../dev/vm');
|
||||
|
||||
const vm = new VM({
|
||||
crypto: {
|
||||
|
@ -19,9 +19,8 @@ const fs = require('fs');
|
||||
const url = require('url');
|
||||
const path = require('path');
|
||||
const assert = require('assert');
|
||||
const {JSZip} = require('../dev/yomichan-util');
|
||||
const {createTestDictionaryArchive} = require('./yomichan-test');
|
||||
const {VM} = require('./yomichan-vm');
|
||||
const {JSZip, createDictionaryArchive} = require('../dev/util');
|
||||
const {VM} = require('../dev/vm');
|
||||
require('fake-indexeddb/auto');
|
||||
|
||||
const chrome = {
|
||||
@ -125,6 +124,12 @@ const DictionaryImporter = vm.get('DictionaryImporter');
|
||||
const DictionaryDatabase = vm.get('DictionaryDatabase');
|
||||
|
||||
|
||||
function createTestDictionaryArchive(dictionary, dictionaryName) {
|
||||
const dictionaryDirectory = path.join(__dirname, 'data', 'dictionaries', dictionary);
|
||||
return createDictionaryArchive(dictionaryDirectory, dictionaryName);
|
||||
}
|
||||
|
||||
|
||||
function countTermsWithExpression(terms, expression) {
|
||||
return terms.reduce((i, v) => (i + (v.expression === expression ? 1 : 0)), 0);
|
||||
}
|
||||
|
@ -15,8 +15,15 @@
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
const {createTestDictionaryArchive} = require('./yomichan-test');
|
||||
const dictionaryValidate = require('./dictionary-validate');
|
||||
const path = require('path');
|
||||
const {createDictionaryArchive} = require('../dev/util');
|
||||
const dictionaryValidate = require('../dev/dictionary-validate');
|
||||
|
||||
|
||||
function createTestDictionaryArchive(dictionary, dictionaryName) {
|
||||
const dictionaryDirectory = path.join(__dirname, 'data', 'dictionaries', dictionary);
|
||||
return createDictionaryArchive(dictionaryDirectory, dictionaryName);
|
||||
}
|
||||
|
||||
|
||||
async function main() {
|
||||
|
@ -19,7 +19,7 @@ const fs = require('fs');
|
||||
const path = require('path');
|
||||
const assert = require('assert');
|
||||
const {JSDOM} = require('jsdom');
|
||||
const {VM} = require('./yomichan-vm');
|
||||
const {VM} = require('../dev/vm');
|
||||
|
||||
|
||||
// DOMRect class definition
|
||||
|
@ -19,7 +19,7 @@ const fs = require('fs');
|
||||
const path = require('path');
|
||||
const assert = require('assert');
|
||||
const {JSDOM} = require('jsdom');
|
||||
const {VM} = require('./yomichan-vm');
|
||||
const {VM} = require('../dev/vm');
|
||||
|
||||
|
||||
function createJSDOM(fileName) {
|
||||
|
@ -16,7 +16,7 @@
|
||||
*/
|
||||
|
||||
const assert = require('assert');
|
||||
const {VM} = require('./yomichan-vm');
|
||||
const {VM} = require('../dev/vm');
|
||||
|
||||
const vm = new VM();
|
||||
vm.execute([
|
||||
|
@ -18,7 +18,7 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const assert = require('assert');
|
||||
const {getDefaultManifest, createManifestString} = require('../dev/yomichan-util');
|
||||
const {getDefaultManifest, createManifestString} = require('../dev/util');
|
||||
|
||||
|
||||
function loadManifestString() {
|
||||
|
@ -16,7 +16,7 @@
|
||||
*/
|
||||
|
||||
const assert = require('assert');
|
||||
const {VM} = require('./yomichan-vm');
|
||||
const {VM} = require('../dev/vm');
|
||||
|
||||
const vm = new VM({});
|
||||
vm.execute('mixed/js/object-property-accessor.js');
|
||||
|
@ -16,7 +16,7 @@
|
||||
*/
|
||||
|
||||
const assert = require('assert');
|
||||
const {VM} = require('./yomichan-vm');
|
||||
const {VM} = require('../dev/vm');
|
||||
|
||||
|
||||
const vm = new VM({});
|
||||
|
@ -16,7 +16,7 @@
|
||||
*/
|
||||
|
||||
const assert = require('assert');
|
||||
const {VM} = require('./yomichan-vm');
|
||||
const {VM} = require('../dev/vm');
|
||||
|
||||
const vm = new VM();
|
||||
vm.execute([
|
||||
|
@ -16,7 +16,7 @@
|
||||
*/
|
||||
|
||||
const assert = require('assert');
|
||||
const {VM} = require('./yomichan-vm');
|
||||
const {VM} = require('../dev/vm');
|
||||
|
||||
const vm = new VM();
|
||||
vm.execute(['bg/js/text-source-map.js']);
|
||||
|
@ -1,49 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2020 Yomichan Authors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
|
||||
function createTestDictionaryArchive(dictionary, dictionaryName) {
|
||||
const dictionaryDirectory = path.join(__dirname, 'data', 'dictionaries', dictionary);
|
||||
const fileNames = fs.readdirSync(dictionaryDirectory);
|
||||
|
||||
const {JSZip} = require('../dev/yomichan-util');
|
||||
const archive = new JSZip();
|
||||
|
||||
for (const fileName of fileNames) {
|
||||
if (/\.json$/.test(fileName)) {
|
||||
const content = fs.readFileSync(path.join(dictionaryDirectory, fileName), {encoding: 'utf8'});
|
||||
const json = JSON.parse(content);
|
||||
if (fileName === 'index.json' && typeof dictionaryName === 'string') {
|
||||
json.title = dictionaryName;
|
||||
}
|
||||
archive.file(fileName, JSON.stringify(json, null, 0));
|
||||
} else {
|
||||
const content = fs.readFileSync(path.join(dictionaryDirectory, fileName), {encoding: null});
|
||||
archive.file(fileName, content);
|
||||
}
|
||||
}
|
||||
|
||||
return archive;
|
||||
}
|
||||
|
||||
|
||||
module.exports = {
|
||||
createTestDictionaryArchive
|
||||
};
|
Loading…
Reference in New Issue
Block a user