1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
'use strict';
const fs = require('fs');
const path = require('path');
const LANGUAGES_DIR = path.join(__dirname, '..', 'languages');
module.exports = {
/**
* Loads the list of all available tests
*
* @param {string} [rootDir]
* @returns {Map<string, string[]>}
*/
loadAllTests(rootDir) {
rootDir = rootDir || LANGUAGES_DIR;
return new Map(this.getAllDirectories(rootDir).map(language => {
return [language, this.getAllFiles(path.join(rootDir, language))];
}));
},
/**
* Loads the list of available tests that match the given languages
*
* @param {string|string[]} languages
* @param {string} [rootDir]
* @returns {Map<string, string[]>}
*/
loadSomeTests(languages, rootDir) {
rootDir = rootDir || LANGUAGES_DIR;
return new Map(this.getSomeDirectories(rootDir, languages).map(language => {
return [language, this.getAllFiles(path.join(rootDir, language))];
}));
},
/**
* Returns a list of all (sub)directories (just the directory names, not full paths)
* in the given src directory
*
* @param {string} src
* @returns {string[]}
*/
getAllDirectories(src) {
return fs.readdirSync(src).filter(file => {
return fs.statSync(path.join(src, file)).isDirectory();
});
},
/**
* Returns a list of all (sub)directories (just the directory names, not full paths)
* in the given src directory, matching the given languages
*
* @param {string} src
* @param {string|string[]} languages
* @returns {string[]}
*/
getSomeDirectories(src, languages) {
return fs.readdirSync(src).filter(file => {
return fs.statSync(path.join(src, file)).isDirectory() && this.directoryMatches(file, languages);
});
},
/**
* Returns whether a directory matches one of the given languages.
*
* @param {string} directory
* @param {string|string[]} languages
*/
directoryMatches(directory, languages) {
if (!Array.isArray(languages)) {
languages = [languages];
}
const dirLanguages = directory.split(/!?\+!?/);
return dirLanguages.some(lang => languages.indexOf(lang) >= 0);
},
/**
* Returns a list of all full file paths to all files in the given src directory
*
* @private
* @param {string} src
* @returns {string[]}
*/
getAllFiles(src) {
return fs.readdirSync(src)
.filter(fileName => {
return path.extname(fileName) === '.test'
&& fs.statSync(path.join(src, fileName)).isFile();
})
.map(fileName => {
return path.join(src, fileName);
});
}
};