2018-08-04 15:13:01 +00:00
|
|
|
import * as plugins from './tstest.plugins';
|
|
|
|
import * as paths from './tstest.paths';
|
2018-08-05 21:20:32 +00:00
|
|
|
import * as logPrefixes from './tstest.logprefixes';
|
|
|
|
|
2018-08-04 12:23:07 +00:00
|
|
|
import { coloredString as cs } from '@pushrocks/consolecolor';
|
|
|
|
|
2018-08-04 15:13:01 +00:00
|
|
|
import { TestDirectory } from './tstest.classes.testdirectory';
|
2018-08-13 21:58:50 +00:00
|
|
|
import { TapCombinator } from './tstest.classes.tap.combinator';
|
|
|
|
import { TapParser } from './tstest.classes.tap.parser';
|
2020-07-07 20:50:29 +00:00
|
|
|
import { threeEighths } from 'figures';
|
2018-08-03 17:18:42 +00:00
|
|
|
|
|
|
|
export class TsTest {
|
2020-07-07 20:50:29 +00:00
|
|
|
public testDir: TestDirectory;
|
|
|
|
|
|
|
|
public smartshellInstance = new plugins.smartshell.Smartshell({
|
|
|
|
executor: 'bash',
|
|
|
|
pathDirectories: [paths.binDirectory],
|
|
|
|
sourceFilePaths: [],
|
|
|
|
});
|
|
|
|
public smartbrowserInstance = new plugins.smartbrowser.SmartBrowser();
|
|
|
|
|
|
|
|
public tsbundleInstance = new plugins.tsbundle.TsBundle();
|
2018-08-03 17:18:42 +00:00
|
|
|
|
|
|
|
constructor(cwdArg: string, relativePathToTestDirectory: string) {
|
|
|
|
this.testDir = new TestDirectory(cwdArg, relativePathToTestDirectory);
|
|
|
|
}
|
|
|
|
|
|
|
|
async run() {
|
|
|
|
const fileNamesToRun: string[] = await this.testDir.getTestFilePathArray();
|
2018-08-13 21:58:50 +00:00
|
|
|
console.log(cs(plugins.figures.hamburger.repeat(80), 'cyan'));
|
|
|
|
console.log('');
|
|
|
|
console.log(`${logPrefixes.TsTestPrefix} FOUND ${fileNamesToRun.length} TESTFILE(S):`);
|
2018-08-03 17:18:42 +00:00
|
|
|
for (const fileName of fileNamesToRun) {
|
2018-08-05 21:20:32 +00:00
|
|
|
console.log(`${logPrefixes.TsTestPrefix} ${cs(fileName, 'orange')}`);
|
2018-08-03 17:18:42 +00:00
|
|
|
}
|
2018-08-13 21:58:50 +00:00
|
|
|
console.log('-'.repeat(48));
|
2018-08-04 17:23:17 +00:00
|
|
|
console.log(''); // force new line
|
2020-07-07 21:59:07 +00:00
|
|
|
|
2018-08-03 17:18:42 +00:00
|
|
|
const tapCombinator = new TapCombinator(); // lets create the TapCombinator
|
2020-07-07 20:50:29 +00:00
|
|
|
for (const fileNameArg of fileNamesToRun) {
|
|
|
|
let tapParser: TapParser;
|
2020-07-07 21:59:07 +00:00
|
|
|
switch (true) {
|
2020-07-07 20:50:29 +00:00
|
|
|
case fileNameArg.endsWith('.browser.ts'):
|
|
|
|
tapParser = await this.runInChrome(fileNameArg);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
tapParser = await this.runInNode(fileNameArg);
|
|
|
|
break;
|
2018-12-05 23:38:03 +00:00
|
|
|
}
|
2018-08-04 12:23:07 +00:00
|
|
|
tapCombinator.addTapParser(tapParser);
|
2020-07-07 23:09:17 +00:00
|
|
|
console.log(cs(`^`.repeat(16), 'cyan'));
|
|
|
|
console.log(''); // force new line
|
2018-08-03 17:18:42 +00:00
|
|
|
}
|
2018-08-04 12:23:07 +00:00
|
|
|
tapCombinator.evaluate();
|
2018-08-03 17:18:42 +00:00
|
|
|
}
|
2020-07-07 20:50:29 +00:00
|
|
|
|
|
|
|
public async runInNode(fileNameArg: string): Promise<TapParser> {
|
|
|
|
console.log(`${cs('=> ', 'blue')} Running ${cs(fileNameArg, 'orange')} in node.js runtime.`);
|
|
|
|
console.log(cs(`=`.repeat(16), 'cyan'));
|
|
|
|
const tapParser = new TapParser(fileNameArg);
|
|
|
|
|
|
|
|
// tsrun options
|
|
|
|
let tsrunOptions = '';
|
|
|
|
if (process.argv.includes('--web')) {
|
|
|
|
tsrunOptions += ' --web';
|
|
|
|
}
|
|
|
|
|
|
|
|
const execResultStreaming = await this.smartshellInstance.execStreamingSilent(
|
|
|
|
`tsrun ${fileNameArg}${tsrunOptions}`
|
|
|
|
);
|
|
|
|
await tapParser.handleTapProcess(execResultStreaming.childProcess);
|
|
|
|
return tapParser;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async runInChrome(fileNameArg: string): Promise<TapParser> {
|
|
|
|
console.log(`${cs('=> ', 'blue')} Running ${cs(fileNameArg, 'orange')} in chromium runtime.`);
|
|
|
|
console.log(cs(`=`.repeat(16), 'cyan'));
|
2020-07-07 21:59:07 +00:00
|
|
|
|
2020-07-07 20:50:29 +00:00
|
|
|
// lets get all our paths sorted
|
|
|
|
const tsbundleCacheDirPath = plugins.path.join(paths.cwd, './.nogit/tstest_cache');
|
|
|
|
const bundleFileName = fileNameArg.replace('/', '__') + '.js';
|
|
|
|
const bundleFilePath = plugins.path.join(tsbundleCacheDirPath, bundleFileName);
|
2020-07-07 21:59:07 +00:00
|
|
|
|
2020-07-07 20:50:29 +00:00
|
|
|
// lets bundle the test
|
|
|
|
await plugins.smartfile.fs.ensureDir(tsbundleCacheDirPath);
|
|
|
|
await this.tsbundleInstance.buildTest(fileNameArg, bundleFilePath, 'parcel');
|
2020-07-07 21:59:07 +00:00
|
|
|
|
2020-07-07 20:50:29 +00:00
|
|
|
// lets create a server
|
|
|
|
const server = new plugins.smartexpress.Server({
|
|
|
|
cors: true,
|
2020-07-07 21:59:07 +00:00
|
|
|
port: 3007,
|
2020-07-07 20:50:29 +00:00
|
|
|
});
|
2020-07-07 21:59:07 +00:00
|
|
|
server.addRoute(
|
|
|
|
'/test',
|
|
|
|
new plugins.smartexpress.Handler('GET', async (req, res) => {
|
|
|
|
res.type('.html');
|
|
|
|
res.write(`
|
2020-07-07 20:50:29 +00:00
|
|
|
<html>
|
2020-07-07 21:59:07 +00:00
|
|
|
<head>
|
|
|
|
<script>
|
|
|
|
globalThis.testdom = true;
|
|
|
|
</script>
|
|
|
|
</head>
|
2020-07-07 20:50:29 +00:00
|
|
|
<body></body>
|
|
|
|
</html>
|
|
|
|
`);
|
2020-07-07 21:59:07 +00:00
|
|
|
res.end();
|
|
|
|
})
|
|
|
|
);
|
2020-07-07 20:50:29 +00:00
|
|
|
server.addRoute('*', new plugins.smartexpress.HandlerStatic(tsbundleCacheDirPath));
|
|
|
|
await server.start();
|
|
|
|
|
|
|
|
// lets do the browser bit
|
|
|
|
await this.smartbrowserInstance.start();
|
2020-07-07 21:59:07 +00:00
|
|
|
const evaluation = await this.smartbrowserInstance.evaluateOnPage(
|
|
|
|
`http://localhost:3007/test?bundleName=${bundleFileName}`,
|
|
|
|
async () => {
|
|
|
|
const convertToText = (obj) => {
|
|
|
|
// create an array that will later be joined into a string.
|
|
|
|
const stringArray = [];
|
|
|
|
|
2020-07-08 02:05:51 +00:00
|
|
|
if (typeof obj === 'object' && typeof obj.toString === 'function') {
|
|
|
|
stringArray.push(obj.toString());
|
|
|
|
} else if (typeof obj === 'object' && obj.join === undefined) {
|
2020-07-07 21:59:07 +00:00
|
|
|
stringArray.push('{');
|
|
|
|
for (const prop of Object.keys(obj)) {
|
|
|
|
stringArray.push(prop, ': ', convertToText(obj[prop]), ',');
|
|
|
|
}
|
|
|
|
stringArray.push('}');
|
|
|
|
|
|
|
|
// is array
|
|
|
|
} else if (typeof obj === 'object' && !(obj.join === undefined)) {
|
|
|
|
stringArray.push('[');
|
|
|
|
for (const prop of Object.keys(obj)) {
|
|
|
|
stringArray.push(convertToText(obj[prop]), ',');
|
|
|
|
}
|
|
|
|
stringArray.push(']');
|
|
|
|
|
|
|
|
// is function
|
|
|
|
} else if (typeof obj === 'function') {
|
|
|
|
stringArray.push(obj.toString());
|
|
|
|
|
|
|
|
// all other values can be done with JSON.stringify
|
|
|
|
} else {
|
|
|
|
stringArray.push(JSON.stringify(obj));
|
|
|
|
}
|
|
|
|
|
|
|
|
return stringArray.join('');
|
|
|
|
};
|
2020-07-08 00:59:55 +00:00
|
|
|
|
|
|
|
let logStore = 'Starting log capture\n';
|
|
|
|
// tslint:disable-next-line: max-classes-per-file
|
2020-07-07 21:59:07 +00:00
|
|
|
const log = console.log.bind(console);
|
|
|
|
console.log = (...args) => {
|
|
|
|
args = args.map((argument) => {
|
|
|
|
return typeof argument !== 'string' ? convertToText(argument) : argument;
|
|
|
|
});
|
|
|
|
logStore += `${args}\n`;
|
|
|
|
log(...args);
|
|
|
|
};
|
|
|
|
const error = console.error;
|
|
|
|
console.error = (...args) => {
|
|
|
|
args = args.map((argument) => {
|
|
|
|
return typeof argument !== 'string' ? convertToText(argument) : argument;
|
|
|
|
});
|
|
|
|
logStore += `${args}\n`;
|
|
|
|
error(...args);
|
|
|
|
};
|
|
|
|
const bundle = await (await fetch('/test__test.browser.ts.js')).text();
|
|
|
|
try {
|
|
|
|
// tslint:disable-next-line: no-eval
|
|
|
|
eval(bundle);
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err);
|
|
|
|
}
|
2020-07-08 00:59:55 +00:00
|
|
|
|
|
|
|
if (globalThis.tapbundleDeferred && globalThis.tapbundleDeferred.promise) {
|
|
|
|
await globalThis.tapbundleDeferred.promise;
|
|
|
|
} else {
|
|
|
|
console.log('Error: Could not find tapbundle Deferred');
|
|
|
|
}
|
2020-07-07 21:59:07 +00:00
|
|
|
return logStore;
|
|
|
|
}
|
|
|
|
);
|
2020-07-07 20:50:29 +00:00
|
|
|
await this.smartbrowserInstance.stop();
|
|
|
|
console.log(`${cs('=> ', 'blue')} Stopped ${cs(fileNameArg, 'orange')} chromium instance.`);
|
|
|
|
await server.stop();
|
|
|
|
console.log(`${cs('=> ', 'blue')} Stopped ${cs(fileNameArg, 'orange')} server.`);
|
|
|
|
// lets create the tap parser
|
|
|
|
const tapParser = new TapParser(fileNameArg);
|
|
|
|
tapParser.handleTapLog(evaluation);
|
|
|
|
return tapParser;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async runInDeno() {}
|
2018-08-03 17:18:42 +00:00
|
|
|
}
|