|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import { RendezvousTracker, TelnetAdapter } from 'roku-debug'; |
| 4 | +import type { BsConfig } from 'brighterscript'; |
| 5 | +import { LogLevel, util, ProgramBuilder } from 'brighterscript'; |
| 6 | +import * as yargs from 'yargs'; |
| 7 | +import { RokuDeploy } from 'roku-deploy'; |
| 8 | +import * as fs from 'fs'; |
| 9 | +import * as path from 'path'; |
| 10 | +import { create } from 'domain'; |
| 11 | + |
| 12 | +let options = yargs |
| 13 | + .usage('$0', 'Rooibos: a simple, flexible, fun Brightscript test framework for Roku Scenegraph apps') |
| 14 | + .help('help', 'View help information about this tool.') |
| 15 | + .option('project', { type: 'string', description: 'Path to a bsconfig.json project file.' }) |
| 16 | + .option('host', { type: 'string', description: 'Host of the Roku device to connect to. Overrides value in bsconfig file.' }) |
| 17 | + .option('password', { type: 'string', description: 'Password of the Roku device to connect to. Overrides value in bsconfig file.' }) |
| 18 | + .option('log-level', { type: 'string', defaultDescription: '"log"', description: 'The log level. Value can be "error", "warn", "log", "info", "debug".' }) |
| 19 | + .check((argv) => { |
| 20 | + if (!argv.host) { |
| 21 | + return new Error('You must provide a host. (--host)'); |
| 22 | + } |
| 23 | + if (!argv.password) { |
| 24 | + return new Error('You must provide a password. (--password)'); |
| 25 | + } |
| 26 | + if (!argv.project) { |
| 27 | + console.log('No project file specified. Using "./bsconfig.json"'); |
| 28 | + |
| 29 | + } |
| 30 | + let bsconfigPath = argv.project || './bsconfig.json'; |
| 31 | + |
| 32 | + if (!fs.existsSync(bsconfigPath)) { |
| 33 | + return new Error(`Unable to load ${bsconfigPath}`); |
| 34 | + } |
| 35 | + return true; |
| 36 | + }) |
| 37 | + .argv; |
| 38 | + |
| 39 | + |
| 40 | +async function main() { |
| 41 | + let currentErrorCode = 0; |
| 42 | + let bsconfigPath = options.project ?? 'bsconfig.json'; |
| 43 | + console.log(`Using bsconfig: ${bsconfigPath}`); |
| 44 | + |
| 45 | + const rawConfig: BsConfig = util.loadConfigFile(bsconfigPath); |
| 46 | + const bsConfig = util.normalizeConfig(rawConfig); |
| 47 | + |
| 48 | + const host = options.host ?? bsConfig.host; |
| 49 | + const password = options.password ?? bsConfig.password; |
| 50 | + |
| 51 | + const logLevel = LogLevel[options['log-level']] ?? bsConfig.logLevel; |
| 52 | + const builder = new ProgramBuilder(); |
| 53 | + |
| 54 | + builder.logger.logLevel = logLevel; |
| 55 | + |
| 56 | + |
| 57 | + await builder.run(<any>{ ...options, retainStagingDir: true, createPackage: true }); |
| 58 | + |
| 59 | + const rokuDeploy = new RokuDeploy(); |
| 60 | + const deviceInfo = await rokuDeploy.getDeviceInfo({ host: host }); |
| 61 | + const rendezvousTracker = new RendezvousTracker({ softwareVersion: deviceInfo['software-version'] }, { host: host, remotePort: 8085 } as any); |
| 62 | + const telnet = new TelnetAdapter({ host: options.host }, rendezvousTracker); |
| 63 | + |
| 64 | + telnet.logger.logLevel = logLevel; |
| 65 | + await telnet.activate(); |
| 66 | + await telnet.connect(); |
| 67 | + |
| 68 | + const failRegex = /RESULT: Fail/g; |
| 69 | + const endRegex = /\[END TEST REPORT\]/g; |
| 70 | + |
| 71 | + async function doExit(emitAppExit = false) { |
| 72 | + if (emitAppExit) { |
| 73 | + (telnet as any).beginAppExit(); |
| 74 | + } |
| 75 | + await rokuDeploy.pressHomeButton(host); // roku-deploy v4: keyPress({ host: options.host, key: 'home' }); |
| 76 | + process.exit(currentErrorCode); |
| 77 | + } |
| 78 | + |
| 79 | + telnet.on('console-output', (output) => { |
| 80 | + console.log(output); |
| 81 | + |
| 82 | + //check for Fails or Crashes |
| 83 | + let failMatches = failRegex.exec(output); |
| 84 | + if (failMatches && failMatches.length > 0) { |
| 85 | + currentErrorCode = 1; |
| 86 | + } |
| 87 | + |
| 88 | + let endMatches = endRegex.exec(output); |
| 89 | + if (endMatches && endMatches.length > 0) { |
| 90 | + doExit(true).catch(e => { |
| 91 | + console.error(e); |
| 92 | + process.exit(1); |
| 93 | + }); |
| 94 | + } |
| 95 | + }); |
| 96 | + |
| 97 | + telnet.on('runtime-error', (error) => { |
| 98 | + console.error(`Runtime Error: ${error.errorCode} - ${error.message}`); |
| 99 | + currentErrorCode = 1; |
| 100 | + doExit(true).catch(e => { |
| 101 | + console.error(e); |
| 102 | + process.exit(1); |
| 103 | + }); |
| 104 | + }); |
| 105 | + |
| 106 | + telnet.on('app-exit', () => { |
| 107 | + doExit(false).catch(e => { |
| 108 | + console.error(e); |
| 109 | + process.exit(1); |
| 110 | + }); |
| 111 | + }); |
| 112 | + |
| 113 | + // Actually start the unit tests |
| 114 | + |
| 115 | + //deploy a .zip package of your project to a roku device |
| 116 | + async function deployBuiltFiles() { |
| 117 | + const outFile = bsConfig.outFile; |
| 118 | + console.log(`Deploying ${outFile} to ${host}`); |
| 119 | + await rokuDeploy.publish({ // roku-deploy v4: .sideload({...}) |
| 120 | + password: password, |
| 121 | + host: host, |
| 122 | + outFile: outFile, |
| 123 | + outDir: process.cwd() |
| 124 | + }); |
| 125 | + } |
| 126 | + |
| 127 | + await deployBuiltFiles(); |
| 128 | +} |
| 129 | + |
| 130 | +main().catch(e => { |
| 131 | + console.error(e); |
| 132 | + process.exit(1); |
| 133 | +}); |
0 commit comments