Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds a CLI to Rooibos (current version) #295

Merged
merged 5 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 37 additions & 5 deletions bsc-plugin/.vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,48 @@
"smartStep": false,
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
"sourceMaps": true,
"args": [
"--timeout",
"0"
],
"args": ["--timeout", "0"],
"skipFiles": [
"${workspaceFolder}/node_modules/**/*.js",
"<node_internals>/**/*.js"
],
"cwd": "${workspaceRoot}",
"internalConsoleOptions": "openOnSessionStart"
},
{
"name": "Run Cli",
"type": "node",
"request": "launch",
"smartStep": false,
"sourceMaps": true,
"runtimeArgs": ["--nolazy", "-r", "ts-node/register"],
"args": [
"src/cli.ts",
"--project=../tests/bsconfig.json",
"--host=${input:roku_host}",
"--password=${input:roku_password}"
],
"resolveSourceMapLocations": [
"${workspaceFolder}/**",
"!**/node_modules/typescript/**",
"!**/node_modules/vscode-languageserver/**"
],
"skipFiles": ["<node_internals>/**/*.js"],
"internalConsoleOptions": "openOnSessionStart"
}
],
"inputs": [
{
"id": "roku_host",
"type": "promptString",
"description": "Enter the IP address of your Roku device",
"default": ""
},
{
"id": "roku_password",
"type": "promptString",
"description": "Enter the password for your Roku device",
"default": ""
}
]
}
}
6,367 changes: 2,509 additions & 3,858 deletions bsc-plugin/package-lock.json

Large diffs are not rendered by default.

16 changes: 12 additions & 4 deletions bsc-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"publish-npm": "npm run test && npm publish",
"publish-npm:beta": "npm run test && npm publish --tag=beta",
"local": "ts-node scripts/install-local.js",
"remote": "ts-node scripts/install-npm.js"
"remote": "ts-node scripts/install-npm.js",
"cli": "ts-node src/cli.ts"
},
"repository": {
"type": "git",
Expand All @@ -25,24 +26,31 @@
"dist/**/!(*.spec.*)*"
],
"main": "dist/plugin.js",
"bin": {
"rooibos": "dist/cli.js"
},
"directories": {
"test": "test"
},
"dependencies": {
"roku-debug": "^0.21.10",
"roku-deploy": "^3.12.1",
"source-map": "^0.7.3",
"undent": "^0.1.0",
"vscode-languageserver": "~6.1.1",
"vscode-languageserver-protocol": "~3.15.3"
"vscode-languageserver-protocol": "~3.17.5",
"yargs": "^16.2.0"
},
"devDependencies": {
"@types/chai": "^4.1.2",
"@types/events": "^3.0.0",
"@types/fs-extra": "^5.0.1",
"@types/mocha": "^9.1.1",
"@types/node": "^14.18.41",
"@types/yargs": "^15.0.5",
"@typescript-eslint/eslint-plugin": "^5.27.0",
"@typescript-eslint/parser": "^5.27.0",
"brighterscript": "^0.65.22",
"brighterscript": "^0.67.4",
"chai": "^4.2.0",
"chai-subset": "^1.6.0",
"coveralls": "^3.0.0",
Expand All @@ -53,7 +61,7 @@
"minimatch": "^3.0.4",
"mocha": "^9.1.3",
"nyc": "^15.1.0",
"release-it": "^15.10.3",
"release-it": "^17.6.0",
"source-map-support": "^0.5.13",
"trim-whitespace": "^1.3.3",
"ts-node": "^9.0.0",
Expand Down
133 changes: 133 additions & 0 deletions bsc-plugin/src/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
#!/usr/bin/env node

import { RendezvousTracker, TelnetAdapter } from 'roku-debug';
import type { BsConfig } from 'brighterscript';
import { LogLevel, util, ProgramBuilder } from 'brighterscript';
import * as yargs from 'yargs';
import { RokuDeploy } from 'roku-deploy';
import * as fs from 'fs';
import * as path from 'path';
import { create } from 'domain';

let options = yargs
.usage('$0', 'Rooibos: a simple, flexible, fun Brightscript test framework for Roku Scenegraph apps')
.help('help', 'View help information about this tool.')
.option('project', { type: 'string', description: 'Path to a bsconfig.json project file.' })
.option('host', { type: 'string', description: 'Host of the Roku device to connect to. Overrides value in bsconfig file.' })
.option('password', { type: 'string', description: 'Password of the Roku device to connect to. Overrides value in bsconfig file.' })
.option('log-level', { type: 'string', defaultDescription: '"log"', description: 'The log level. Value can be "error", "warn", "log", "info", "debug".' })
.check((argv) => {
if (!argv.host) {
return new Error('You must provide a host. (--host)');
}
if (!argv.password) {
return new Error('You must provide a password. (--password)');
}
if (!argv.project) {
console.log('No project file specified. Using "./bsconfig.json"');

}
let bsconfigPath = argv.project || './bsconfig.json';

if (!fs.existsSync(bsconfigPath)) {
return new Error(`Unable to load ${bsconfigPath}`);
}
return true;
})
.argv;


async function main() {
let currentErrorCode = 0;
let bsconfigPath = options.project ?? 'bsconfig.json';
console.log(`Using bsconfig: ${bsconfigPath}`);

const rawConfig: BsConfig = util.loadConfigFile(bsconfigPath);
const bsConfig = util.normalizeConfig(rawConfig);

const host = options.host ?? bsConfig.host;
const password = options.password ?? bsConfig.password;

const logLevel = LogLevel[options['log-level']] ?? bsConfig.logLevel;
const builder = new ProgramBuilder();

builder.logger.logLevel = logLevel;


await builder.run(<any>{ ...options, retainStagingDir: true, createPackage: true });

const rokuDeploy = new RokuDeploy();
const deviceInfo = await rokuDeploy.getDeviceInfo({ host: host });
const rendezvousTracker = new RendezvousTracker({ softwareVersion: deviceInfo['software-version'] }, { host: host, remotePort: 8085 } as any);
const telnet = new TelnetAdapter({ host: options.host }, rendezvousTracker);

telnet.logger.logLevel = logLevel;
await telnet.activate();
await telnet.connect();

const failRegex = /RESULT: Fail/g;
const endRegex = /\[END TEST REPORT\]/g;

async function doExit(emitAppExit = false) {
if (emitAppExit) {
(telnet as any).beginAppExit();
}
await rokuDeploy.pressHomeButton(host); // roku-deploy v4: keyPress({ host: options.host, key: 'home' });
process.exit(currentErrorCode);
}

telnet.on('console-output', (output) => {
console.log(output);

//check for Fails or Crashes
let failMatches = failRegex.exec(output);
if (failMatches && failMatches.length > 0) {
currentErrorCode = 1;
}

let endMatches = endRegex.exec(output);
if (endMatches && endMatches.length > 0) {
doExit(true).catch(e => {
console.error(e);
process.exit(1);
});
}
});

telnet.on('runtime-error', (error) => {
console.error(`Runtime Error: ${error.errorCode} - ${error.message}`);
currentErrorCode = 1;
doExit(true).catch(e => {
console.error(e);
process.exit(1);
});
});

telnet.on('app-exit', () => {
doExit(false).catch(e => {
console.error(e);
process.exit(1);
});
});

// Actually start the unit tests

//deploy a .zip package of your project to a roku device
async function deployBuiltFiles() {
const outFile = bsConfig.outFile;
console.log(`Deploying ${outFile} to ${host}`);
await rokuDeploy.publish({ // roku-deploy v4: .sideload({...})
password: password,
host: host,
outFile: outFile,
outDir: process.cwd()
});
}

await deployBuiltFiles();
}

main().catch(e => {
console.error(e);
process.exit(1);
});
34 changes: 31 additions & 3 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Simple, mocha-inspired, flexible, fun Brightscript test framework for ROKU apps
- [Incorporate your own util methods](#incorporate-your-own-util-methods)
- [Hook into your global setup mechanisms](#hook-into-your-global-setup-mechanisms)
- [Only show output for failed tests](#only-show-output-for-failed-tests)
- [Simple Command Line Interface](#command-line-interface)
- [Easily integrate into any CI system](#easily-integrate-into-any-ci-system)
- [Generate code coverage](#generate-code-coverage)

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

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.

## Command Line Interface
<a name="simple-cli"></a>
Rooibos includes a simple CLI that can be used to run the tests on a Roku Device
from the command line.

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:

```
npx rooibos --project=<path_to_bsconfig.json> --host=<host> --password=<password>
```

The test runner CLI will:
1. build the app as defined in the given `bsconfig.json` file
2. deploy the app the Roku device specified
3. send the Roku's console output to `stdout`
4. exit with status `0` on success, or `1` on failure.


## Integrating with your CI
<a name="easily-integrate-into-any-ci-system"></a>
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.
Rooibos CLI can be used directly in your CI process.

An example make target might look like

```
continuousIntegration: build
echo "Running Rooibos Unit Tests"
npx rooibos --project=<test project bsconfig.json> --host=${ROKU_DEV_TARGET} --password=${ROKU_DEV_PASSWORD}

```

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.
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.

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.
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.

An example make target might look like

Expand Down
Loading