You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwhisk.apache.org by cs...@apache.org on 2017/05/10 03:26:48 UTC
[incubator-openwhisk-devtools] branch master updated: A local test
script for node.js functions (#26)
This is an automated email from the ASF dual-hosted git repository.
csantanapr 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 54b388e A local test script for node.js functions (#26)
54b388e is described below
commit 54b388e72d83ba677418c1eaf2cf03aaaf31320e
Author: Lars Trieloff <la...@trieloff.net>
AuthorDate: Wed May 10 04:26:46 2017 +0100
A local test script for node.js functions (#26)
* 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).
* We don’t need zlib
---
node-local/README.md | 24 +++++++++++++++++++++
node-local/test.js | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 83 insertions(+)
diff --git a/node-local/README.md b/node-local/README.md
new file mode 100644
index 0000000..2de3234
--- /dev/null
+++ b/node-local/README.md
@@ -0,0 +1,24 @@
+# Testing node.js functions locally
+
+Usage:
+
+```bash
+node test.js ./path-to-function.js parameter=value parameter2=value2
+```
+
+Will invoke the `main` function of `path-to-function.js` with following `params`:
+```javascript
+{
+ "parameter":"value",
+ "parameter2":"value"
+}
+```
+
+Alternatively, input can be passed on stdin, this allows the creation of more complex input
+objects that would be inconvenient to edit on the command line or passing non-string values.
+
+```bash
+echo '{"boolean": true}' | node test.js ./path-to-function.js
+cat input.json | node test.js ./path-to-function.js
+```
+
diff --git a/node-local/test.js b/node-local/test.js
new file mode 100644
index 0000000..16041f1
--- /dev/null
+++ b/node-local/test.js
@@ -0,0 +1,59 @@
+process.stdin.setEncoding('utf8');
+
+if (process.argv[2]=="--help"||process.argv[2]=="-h") {
+ help();
+} else if (!process.stdin.isTTY) {
+ var input = "";
+
+ process.stdin.on('readable', () => {
+ var chunk = process.stdin.read();
+ if (chunk !== null) {
+ input += chunk;
+ }
+ });
+
+ process.stdin.on('end', () => {
+ const params = JSON.parse(input);
+
+ run(params);
+ });
+
+} 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() {
+ console.log("");
+ console.log("Usage:");
+ console.log(" node test.js ./main.js foo=bar");
+ console.log(" echo '{\"foo\":\"bar\"}' | node test.js ./main.js");
+}
+
+function run(params) {
+ const actionToRun = process.argv[2];
+
+ if (!actionToRun) {
+ console.error("./test.js: Missing argument <action-to-run>");
+ help();
+ process.exit(1);
+ }
+
+ const imports = require(actionToRun);
+ //support a non-exported main function as a fallback
+ const action = imports.main ? imports.main : main;
+
+ let result = action(params);
+
+ if (result.then) {
+ Promise.resolve(result)
+ .then(result => console.log(result.toString("utf-8")))
+ .catch(error => console.error(error));
+ } else {
+ console.log(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>'].