You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwhisk.apache.org by dr...@apache.org on 2017/06/02 21:44:51 UTC
[incubator-openwhisk-devtools] branch master updated: Support JSON
output for node test script (#36)
This is an automated email from the ASF dual-hosted git repository.
dragos pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-openwhisk-devtools.git
The following commit(s) were added to refs/heads/master by this push:
new 5b1fd7b Support JSON output for node test script (#36)
5b1fd7b is described below
commit 5b1fd7bfe0dab5b76bfcb8d8b0192eb3bbda4221
Author: Lars Trieloff <la...@trieloff.net>
AuthorDate: Fri Jun 2 23:44:49 2017 +0200
Support JSON output for node test script (#36)
* A local test script for node.js functions
As [suggested by Carlos](https://twitter.com/csantanapr/status/861362098877739009) and [initially provided by Raymond](https://www.raymondcamden.com/2017/01/09/quick-tip-for-testing-openwhisk-actions-locally).
* #27 top-level README now includes links to subprojects
As suggested by @mrutkows
* Support for JSON output
- added new —json/-j command line parameter
- better structure for parsing command line params
- documentation
* More robust fallback
---
node-local/README.md | 3 +++
node-local/test.js | 76 +++++++++++++++++++++++++++++++++++++---------------
2 files changed, 58 insertions(+), 21 deletions(-)
diff --git a/node-local/README.md b/node-local/README.md
index 2de3234..1ec15b5 100644
--- a/node-local/README.md
+++ b/node-local/README.md
@@ -22,3 +22,6 @@ echo '{"boolean": true}' | node test.js ./path-to-function.js
cat input.json | node test.js ./path-to-function.js
```
+If you intend to post-process the result, for instance with `jq`, add the parameter `--json`,
+which will make sure `test.js` returns well-formed JSON. The default is off, which means you
+will get a slightly more readable output.
\ No newline at end of file
diff --git a/node-local/test.js b/node-local/test.js
index 16041f1..63f4bda 100644
--- a/node-local/test.js
+++ b/node-local/test.js
@@ -1,8 +1,36 @@
process.stdin.setEncoding('utf8');
-if (process.argv[2]=="--help"||process.argv[2]=="-h") {
- help();
-} else if (!process.stdin.isTTY) {
+var args = process.argv;
+args.shift();
+args.shift();
+
+//collect parameters
+var params = {};
+//output JSON
+var json = false;
+
+var action;
+
+//parse all parameters
+while(arg = args.shift()) {
+ if (arg=="--help"||arg=="-h") {
+ help();
+ process.exit();
+ } else if (arg=="--json"||arg=="-j") {
+ json = true;
+ } else if (process.stdin.isTTY&&arg.indexOf("=")>1) {
+ let [name,value] = arg.split('=');
+ params[name] = value;
+ } else {
+ action = arg;
+ }
+}
+
+//if no stdin, run with command line params
+if (process.stdin.isTTY) {
+ run(action, params, json);
+} else {
+ //if stdin, read input and parse as JSON
var input = "";
process.stdin.on('readable', () => {
@@ -15,16 +43,8 @@ if (process.argv[2]=="--help"||process.argv[2]=="-h") {
process.stdin.on('end', () => {
const params = JSON.parse(input);
- run(params);
+ run(action, params, json);
});
-
-} else {
- let params = {};
- for(var i=3;i<process.argv.length;i++) {
- let [name,value] = process.argv[i].split('=');
- params[name] = value;
- }
- run(params);
}
function help() {
@@ -32,28 +52,42 @@ function help() {
console.log("Usage:");
console.log(" node test.js ./main.js foo=bar");
console.log(" echo '{\"foo\":\"bar\"}' | node test.js ./main.js");
+ console.log("");
+ console.log("Optional parameters");
+ console.log(" --help -h print this help");
+ console.log(" --json -j format result as JSON")
+ process.exit();
}
-function run(params) {
- const actionToRun = process.argv[2];
-
- if (!actionToRun) {
+function fallback(action) {
+ eval(require("fs").readFileSync(action, "utf-8"));
+ if (this.main) {
+ return main;
+ } else {
+ console.error(action + " has no function main or no exports.main");
+ process.exit(1);
+ }
+}
+
+function run(action, params, outputJSON) {
+ if (!action) {
console.error("./test.js: Missing argument <action-to-run>");
help();
process.exit(1);
}
- const imports = require(actionToRun);
+ const imports = require(action);
+
//support a non-exported main function as a fallback
- const action = imports.main ? imports.main : main;
+ const mainfunct = imports.main ? imports.main : fallback(action);
- let result = action(params);
+ let result = mainfunct(params);
if (result.then) {
Promise.resolve(result)
- .then(result => console.log(result.toString("utf-8")))
+ .then(result => console.log(outputJSON ? JSON.stringify(result) : result))
.catch(error => console.error(error));
} else {
- console.log(result);
+ console.log(outputJSON ? JSON.stringify(result) : result);
}
}
\ No newline at end of file
--
To stop receiving notification emails like this one, please contact
['"commits@openwhisk.apache.org" <co...@openwhisk.apache.org>'].