Skip to content
Browse files

nspwan is functional

  • Loading branch information...
1 parent 1da6ebe commit 55837bd12d691baa844425acb15cd810ae0b93f0 @tmpvar tmpvar committed
Showing with 88 additions and 9 deletions.
  1. +3 −9 exec-test.js
  2. +85 −0 nspawn.js
View
12 exec-test.js
@@ -4,19 +4,13 @@ var sys = require('sys'),
eyes = require('eyes'),
vows = require('vows'),
exec = require('child_process').exec,
- spawn = require('child_process').spawn;
-
-
-var nexpect = {
- spawn:function(){
- return 'exec-test.js';
- }
-};
+ spawn = require('child_process').spawn,
+ nspawn = require('./nspawn').nspawn;
vows.describe('noc/terminal-exec').addBatch({
"When executing commands on the terminal": {
"and you run ls with DIRNAME": {
- topic: nexpect.spawn('ls -a'),
+ topic: nspawn.spawn('ls -a'),
"you should see the current path": function(result){
eyes.inspect(result);
assert.equal( result, 'exec-test.js');
View
85 nspawn.js
@@ -0,0 +1,85 @@
+var spawn = require("child_process").spawn,
+ chain = function(ctx) {
+ return {
+ expect : function(str) {
+ ctx.queue.push(function(data) {
+ if (data.indexOf(str) > -1) {
+ return true
+ }
+ });
+ return chain(ctx);
+ },
+ sendline : function(str) {
+ ctx.queue.push(function() {
+ ctx.process.stdin.write(str + "\n");
+ });
+ return chain(ctx);
+ },
+ run : function(fn) {
+ var err = null,
+ out = function(data) {
+ data = data.toString();
+ var expect = ctx.queue[0];
+
+ if (typeof expect !== "function" || expect(data) !== true) {
+ err = data + " was not expected..";
+ } else {
+ ctx.queue.shift();
+ var sendline = ctx.queue.shift();
+ if (typeof sendline === "function") {
+ sendline();
+ }
+ }
+ };
+
+ ctx.process = spawn(ctx.app, ctx.args);
+ ctx.process.stdout.on("data", out);
+ ctx.process.stdout.on("end", function() {
+ fn(err);
+ });
+ }
+ };
+ };
+
+var nspawn = exports.nspawn = {
+ spawn : function(cmd) {
+ var args = cmd.split(" "),
+ app = args.shift();
+
+ var context = {
+ app: app,
+ args : args,
+ queue : [],
+ };
+ return chain(context);
+ }
+};
+
+// ---- Quick tests ----
+// TODO: refactor these into vows
+if (process.ARGV[1].indexOf("nspawn.js") > -1 && process.ARGV[2] === "test") {
+ nspawn.spawn("echo hello").expect("hello").run(function(err) {
+ if (!err) {
+ console.log("#1 pass");
+ }
+ });
+
+ nspawn.spawn("ls -al /tmp/undefined").expect("No such file or directory").run(function(err) {
+ if (!err) {
+ console.log("#2 pass");
+ }
+ });
+
+ nspawn.spawn("node")
+ .expect("Type '.help' for options.")
+ .sendline("console.log('testing')")
+ .expect("testing")
+ .sendline("process.exit()")
+ .run(function(err) {
+ if (!err) {
+ console.log("#3 pass");
+ } else {
+ console.log(err)
+ }
+ });
+}

0 comments on commit 55837bd

Please sign in to comment.
Something went wrong with that request. Please try again.