rooibos-roku
Version:
simple, flexible, fun brightscript test framework for roku scenegraph apps - roku brighterscript plugin
133 lines (132 loc) • 6.6 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.FileFactory = void 0;
const brighterscript_1 = require("brighterscript");
const path = require("path");
const fs = require("fs");
const fse = require("fs-extra");
const fastGlob = require("fast-glob");
class FileFactory {
constructor() {
this.sourceFilesToAutoImport = [];
this.addedFrameworkFiles = [];
if (__filename.endsWith('.ts')) {
//load the files directly from their source location. (i.e. the plugin is running as a typescript file from within ts-node)
this.frameworkSourcePath = (0, brighterscript_1.standardizePath) `${__dirname}/../../../framework/src`;
}
else {
//load the framework files from the dist folder (i.e. the plugin is running as a node_module)
this.frameworkSourcePath = (0, brighterscript_1.standardizePath) `${__dirname}/../framework`;
}
this.coverageComponentXmlTemplate = fs.readFileSync(path.join(this.frameworkSourcePath, '/components/rooibos/CodeCoverage.xml'), 'utf8');
this.coverageComponentBrsTemplate = fs.readFileSync(path.join(this.frameworkSourcePath, '/source/rooibos/CodeCoverage.brs'), 'utf8');
}
addFrameworkFiles(program) {
this.addedFrameworkFiles = [];
let globedFiles = fastGlob.sync([
'**/*.{bs,brs,xml}',
'!**/bslib.brs',
'!**/manifest',
'!**/CodeCoverage.{brs,xml}',
'!**/RooibosScene.xml'
], {
cwd: this.frameworkSourcePath,
absolute: false,
followSymbolicLinks: true,
onlyFiles: true
});
for (let filePath of globedFiles) {
if (this.shouldAddFileToImportList(filePath)) {
// Save a list of all source files added to the program
// to be imported by node test components
this.sourceFilesToAutoImport.push(filePath);
}
let sourcePath = path.resolve(this.frameworkSourcePath, filePath);
let fileContents = fs.readFileSync(sourcePath, 'utf8').toString();
let entry = { src: sourcePath, dest: filePath };
this.addedFrameworkFiles.push(program.setFile(entry, fileContents));
}
let entry = {
src: (0, brighterscript_1.standardizePath) `${this.frameworkSourcePath}/components/RooibosScene.xml`,
dest: (0, brighterscript_1.standardizePath) `components/rooibos/RooibosScene.xml`
};
this.addedFrameworkFiles.push(program.setFile(entry, this.createTestXML('RooibosScene', 'Scene')));
}
createTestXML(name, baseName, suite) {
let scriptImports = [];
for (let filePath of this.sourceFilesToAutoImport) {
scriptImports.push(`<script type="text/brighterscript" uri="pkg:/${filePath}" />`);
}
// Add the test spec file rather then relying on auto imports
if (suite) {
scriptImports.push(`<script type="text/brighterscript" uri="pkg:/${suite.file.pkgPath.replace(/\\/g, '/')}" />`);
}
let contents = `<?xml version="1.0" encoding="UTF-8" ?>
<component name="${name}" extends="${baseName}">
${scriptImports.join('\n')}
<interface>
<field id="rooibosTestResult" type="assocarray"/>
<field id="testText" type="string" alias="statusLabel.text" />
<field id="failedText" type="string" alias="failedLabel.text" />
<field id="statusColor" type="string" alias="statusBackground.color" />
<function name='Rooibos_CreateTestNode' />
</interface>
<children>
<Rectangle id="statusBackground" color="#444444" width="1920" height="1080" />
<LayoutGroup translation="[960, 540]" vertAlignment="center" horizAlignment="center">
<Label id="statusLabel" text='Rooibos is running tests' width="1800" />
<Label id="failedLabel" text="" translation="[0, 100]" width="1800" wrap="true" maxLines="15"/>
</LayoutGroup>
</children>
</component>
`;
return contents;
}
createCoverageComponent(program, coverageMap, filepathMap) {
let template = this.coverageComponentBrsTemplate;
template = template.replace(/\"\#EXPECTED_MAP\#\"/g, JSON.stringify(coverageMap !== null && coverageMap !== void 0 ? coverageMap : {}));
template = template.replace(/\"\#FILE_PATH_MAP\#\"/g, JSON.stringify(filepathMap !== null && filepathMap !== void 0 ? filepathMap : {}));
this.addFileToRootDir(program, path.join('components/rooibos', 'CodeCoverage.brs'), template);
this.addFileToRootDir(program, path.join('components/rooibos', 'CodeCoverage.xml'), this.coverageComponentXmlTemplate);
}
isIgnoredFile(file) {
let name = file.pkgPath.toLowerCase();
let result = this.addedFrameworkFiles.find((f) => {
return name === f.pkgPath.toLowerCase();
});
return result !== undefined;
}
shouldAddFileToImportList(destFilePath) {
const pathDetails = path.parse(destFilePath);
if (pathDetails.dir === 'source' || pathDetails.dir.startsWith('source\\') || pathDetails.dir.startsWith('source/')) {
if (pathDetails.ext === '.brs' || (pathDetails.ext === '.bs' && !pathDetails.name.endsWith('.d'))) {
return true;
}
}
return false;
}
addFile(program, projectPath, contents) {
try {
const file = program.setFile({
src: path.resolve(projectPath),
dest: projectPath
}, contents);
this.addedFrameworkFiles.push(file);
return file;
}
catch (error) {
console.error(`Error adding framework file: ${projectPath} : ${error.message}`);
}
}
addFileToRootDir(program, filePath, contents) {
var _a, _b;
try {
fse.outputFileSync(path.join((_b = (_a = program.options.stagingFolderPath) !== null && _a !== void 0 ? _a : program.options.stagingDir) !== null && _b !== void 0 ? _b : program.options.sourceRoot, filePath), contents);
}
catch (error) {
console.error(`Error adding framework file: ${path} : ${error.message}`);
}
}
}
exports.FileFactory = FileFactory;
//# sourceMappingURL=FileFactory.js.map