Skip to content

Commit f87a8dc

Browse files
Adds a CLI to Rooibos (current version) (#295)
* Added the cli * Fixed issues with running bsconfig.json from different directory * Changed vscode-languageserver-protocol package version * Update bsc-plugin/src/cli.ts --------- Co-authored-by: Bronley Plumb <[email protected]>
1 parent 6c56e29 commit f87a8dc

File tree

10 files changed

+4033
-4218
lines changed

10 files changed

+4033
-4218
lines changed

bsc-plugin/.vscode/launch.json

+37-5
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,48 @@
88
"smartStep": false,
99
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
1010
"sourceMaps": true,
11-
"args": [
12-
"--timeout",
13-
"0"
14-
],
11+
"args": ["--timeout", "0"],
1512
"skipFiles": [
1613
"${workspaceFolder}/node_modules/**/*.js",
1714
"<node_internals>/**/*.js"
1815
],
1916
"cwd": "${workspaceRoot}",
2017
"internalConsoleOptions": "openOnSessionStart"
18+
},
19+
{
20+
"name": "Run Cli",
21+
"type": "node",
22+
"request": "launch",
23+
"smartStep": false,
24+
"sourceMaps": true,
25+
"runtimeArgs": ["--nolazy", "-r", "ts-node/register"],
26+
"args": [
27+
"src/cli.ts",
28+
"--project=../tests/bsconfig.json",
29+
"--host=${input:roku_host}",
30+
"--password=${input:roku_password}"
31+
],
32+
"resolveSourceMapLocations": [
33+
"${workspaceFolder}/**",
34+
"!**/node_modules/typescript/**",
35+
"!**/node_modules/vscode-languageserver/**"
36+
],
37+
"skipFiles": ["<node_internals>/**/*.js"],
38+
"internalConsoleOptions": "openOnSessionStart"
39+
}
40+
],
41+
"inputs": [
42+
{
43+
"id": "roku_host",
44+
"type": "promptString",
45+
"description": "Enter the IP address of your Roku device",
46+
"default": ""
47+
},
48+
{
49+
"id": "roku_password",
50+
"type": "promptString",
51+
"description": "Enter the password for your Roku device",
52+
"default": ""
2153
}
2254
]
23-
}
55+
}

bsc-plugin/package-lock.json

+2,509-3,858
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bsc-plugin/package.json

+12-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
"publish-npm": "npm run test && npm publish",
1616
"publish-npm:beta": "npm run test && npm publish --tag=beta",
1717
"local": "ts-node scripts/install-local.js",
18-
"remote": "ts-node scripts/install-npm.js"
18+
"remote": "ts-node scripts/install-npm.js",
19+
"cli": "ts-node src/cli.ts"
1920
},
2021
"repository": {
2122
"type": "git",
@@ -25,24 +26,31 @@
2526
"dist/**/!(*.spec.*)*"
2627
],
2728
"main": "dist/plugin.js",
29+
"bin": {
30+
"rooibos": "dist/cli.js"
31+
},
2832
"directories": {
2933
"test": "test"
3034
},
3135
"dependencies": {
36+
"roku-debug": "^0.21.10",
37+
"roku-deploy": "^3.12.1",
3238
"source-map": "^0.7.3",
3339
"undent": "^0.1.0",
3440
"vscode-languageserver": "~6.1.1",
35-
"vscode-languageserver-protocol": "~3.15.3"
41+
"vscode-languageserver-protocol": "~3.17.5",
42+
"yargs": "^16.2.0"
3643
},
3744
"devDependencies": {
3845
"@types/chai": "^4.1.2",
3946
"@types/events": "^3.0.0",
4047
"@types/fs-extra": "^5.0.1",
4148
"@types/mocha": "^9.1.1",
4249
"@types/node": "^14.18.41",
50+
"@types/yargs": "^15.0.5",
4351
"@typescript-eslint/eslint-plugin": "^5.27.0",
4452
"@typescript-eslint/parser": "^5.27.0",
45-
"brighterscript": "^0.65.22",
53+
"brighterscript": "^0.67.4",
4654
"chai": "^4.2.0",
4755
"chai-subset": "^1.6.0",
4856
"coveralls": "^3.0.0",
@@ -53,7 +61,7 @@
5361
"minimatch": "^3.0.4",
5462
"mocha": "^9.1.3",
5563
"nyc": "^15.1.0",
56-
"release-it": "^15.10.3",
64+
"release-it": "^17.6.0",
5765
"source-map-support": "^0.5.13",
5866
"trim-whitespace": "^1.3.3",
5967
"ts-node": "^9.0.0",

bsc-plugin/src/cli.ts

+133
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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+
});

docs/index.md

+31-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Simple, mocha-inspired, flexible, fun Brightscript test framework for ROKU apps
2222
- [Incorporate your own util methods](#incorporate-your-own-util-methods)
2323
- [Hook into your global setup mechanisms](#hook-into-your-global-setup-mechanisms)
2424
- [Only show output for failed tests](#only-show-output-for-failed-tests)
25+
- [Simple Command Line Interface](#command-line-interface)
2526
- [Easily integrate into any CI system](#easily-integrate-into-any-ci-system)
2627
- [Generate code coverage](#generate-code-coverage)
2728

@@ -36,6 +37,7 @@ Simple, mocha-inspired, flexible, fun Brightscript test framework for ROKU apps
3637
- [Using mocks and stubs](#using-mocks-and-stubs)
3738
- [API reference](https://rokucommunity.github.io/rooibos)
3839
- [assertion reference](https://rokucommunity.github.io/rooibos/module-BaseTestSuite.html)
40+
- [Command Line Interface (CLI)](#command-line-interface)
3941
- [Integrating with your CI](#integrating-with-your-ci)
4042
- [Advanced Setup](#advanced-setup)
4143
- [Code coverage](#generate-code-coverage)
@@ -949,14 +951,40 @@ m.expectCalled(videoService.getVideos, someJson, true)
949951

950952
Note, you can also opt to disable the error at the whole test suite level; by setting `m.allowNonExistingMethods = true` in your test suite code.
951953

954+
## Command Line Interface
955+
<a name="simple-cli"></a>
956+
Rooibos includes a simple CLI that can be used to run the tests on a Roku Device
957+
from the command line.
958+
959+
To use the CLI, you call it with references to the `bsconfig.json` file defining your test project, and the host and password of a Roku device that is in developer mode:
960+
961+
```
962+
npx rooibos --project=<path_to_bsconfig.json> --host=<host> --password=<password>
963+
```
964+
965+
The test runner CLI will:
966+
1. build the app as defined in the given `bsconfig.json` file
967+
2. deploy the app the Roku device specified
968+
3. send the Roku's console output to `stdout`
969+
4. exit with status `0` on success, or `1` on failure.
970+
952971

953972
## Integrating with your CI
954973
<a name="easily-integrate-into-any-ci-system"></a>
955-
Rooibos does not have special test runners for outputting to files, or uploading to servers. However, that will not stop you integrating with your CI system.
974+
Rooibos CLI can be used directly in your CI process.
975+
976+
An example make target might look like
977+
978+
```
979+
continuousIntegration: build
980+
echo "Running Rooibos Unit Tests"
981+
npx rooibos --project=<test project bsconfig.json> --host=${ROKU_DEV_TARGET} --password=${ROKU_DEV_PASSWORD}
982+
983+
```
956984

957-
Because the test output has a convenient status at the end of the output, you can simply parse the last line of output from the telnet session to ascertain if your CI build's test succeeded or failed.
985+
Alternately, you can manually deploy the app after it has been built, and check the output. Because the test output has a convenient status at the end of the output, you can simply parse the last line of output from the telnet session to ascertain if your CI build's test succeeded or failed.
958986

959-
Note that rooibos doesn't need any special parameters to run. If you follow the standard setup the tests will run. Simply ensure that your build system includes, or does not include rooibosDist.brs (and better still, _all_ of your tests), depending on whether you wish to run the tests or not.
987+
Note that Rooibos doesn't need any special parameters to run. If you follow the standard setup the tests will run. Simply ensure that your build system includes, or does not include rooibosDist.brs (and better still, _all_ of your tests), depending on whether you wish to run the tests or not.
960988

961989
An example make target might look like
962990

0 commit comments

Comments
 (0)