nodejitsu / nexpect Goto Github PK
View Code? Open in Web Editor NEWspawn and control child processes in node.js with ease
Home Page: http://github.com/nodejitsu/nexpect
License: Other
spawn and control child processes in node.js with ease
Home Page: http://github.com/nodejitsu/nexpect
License: Other
With the following bash shell nexpect totally fails (Never makes it past the first expect) on node v0.10.21 using the strongloop branch:
#! /bin/bash
echo "Welcome to expect test."
while true; do
read -p "Name of node:" RESP
case $RESP in
1234 ) echo "Excellent! Test 1 passed.";;
[Qq]* ) echo "Quitting!"; exit;;
[Nn]* ) echo "No is not an option"; break;;
* ) echo "Please enter node, or Q for quit, N for loser.";;
esac
done
At first I thought it was just because there was an extra line before the expected result, but no, removing it just causes an infinite loop and 100% cpu usage.
The Node.js script:
var nexpect = require('nexpect').spawn;
var rick = function() {
nexpect('/tmp/test.sh', { verbose: true, ignoreCase: true })
.expect("node")
.sendline('1234')
.expect("passed.")
.sendline('q')
.expect("Quitting!")
.run(function(err) {
if (err) {
console.log("Password change failed on controller");
console.log(err);
return;
}
console.log("Password changed succesfully.");
});
};
rick();
I edit the example node.js like this:
var nexpect = require('../lib/nexpect');
nexpect.spawn("node")
.expect(">")
.sendline("console.log('testing')")
.expect("testing")
.sendline("process.exit()")
.run(function (err) {
if (!err) {
console.log("node process started, console logged, process exited");
}
else {
console.log(err);
}
});
but the output is nothong!
I don't understanding why use the "node --interactive" instead of "node" ?
and I want to know How to use the nexpect.spawn() method about python?
For example:
var nexpect = require('../lib/nexpect');
nexpect.spawn("python")
.expect("...")
...
ATT
Nodejs Version: 0.6.11
Running Debian Squeeze
Trying to run something very similar to the example code provided but my CLI just hangs until I exit:
18 nexpect.spawn("node",[],["verbose"])
19 .expect(">")
20 .sendline("console.log('testing')")
21 .expect("testing")
22 .sendline("process.exit()")
23 .run(function (err) {
24 if (!err) {
25 console.log("node process started, console logged, process exited");
26 } else {
27 console.log(err)
28 }
29 });
Nothing gets logged, and it remains open until I kill it. Any ideas?
Unfortunately nexpect seems to be incompatible with interactive cli programs that require user input, e.g. Yeoman. (sendline
is not an option here, as I don't know what choices the user will make).
Could this possibly be implemented with Readline?
Also: somewhat relevant SO.
The version in the public registry is outdated. Could you please publish a new version with the recent changes (i.e. exit code passed to run
callback).
Some terminal programs (like file watchers) just run indefinitely until you do ctrl+c
to send a SIGINT to exit the program.
How would I do this from nexpect? Is there a control character I could send through .sendline()
to get the same effect as ctrl+c
?
If the command fails, (Non-empty queue etc.) the reason may be in the stdout/stderr (whatever "stream" is being monitored) output.
The data is not available via the callback params (only "err" is passed), or as a property of the error object.
ATT.
In Expect script I can use OR condition for maybe sent another command or end session.How can I implement it with nexpect?
So i clone the repo, and edit the example echo.js like this:
nexpect.spawn("echo", ["xxx"]) // echo something wrong
.expect("hello") // this should fail
.run(function (err) {
console.log(arguments); // here
if (!err) {
console.log("hello was echoed");
}
});
However i still got hello was echoed
. The output is:
{ '0': [Error: xxx was not expected..] }
{ '0': null, '1': [ 'xxx' ], '2': 0 }
hello was echoed
This is wrong, right?
Thanks for the tool, I'm running all my other test with jest and was wondering if it is possible to use it for nexpect
test?
import nexpect from 'nexpect';
nexpect
.spawn('yarn', ['start'])
.expect('hello')
.run(function(err) {
if (!err) {
console.log('hello was echoed');
}
});
Currently I got
❯ node_modules/.bin/jest --verbose --watch ./src/__tests__/cli.test.ts
FAIL src/__tests__/cli.test.ts
● Test suite failed to run
Your test suite must contain at least one test.
at onResult (node_modules/jest-cli/node_modules/@jest/core/build/TestScheduler.js:175:18)
at node_modules/emittery/index.js:260:13
at Array.map (<anonymous>)
at Emittery.emit (node_modules/emittery/index.js:258:23)
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 1.132 s
Ran all test suites matching /.\/src\/__tests__\/cli.test.ts/i.
Active Filters: filename /./src/__tests__/cli.test.ts/
› Press c to clear filters.
Watch Usage
› Press a to run all tests.
› Press f to run only failed tests.
› Press o to only run tests related to changed files.
› Press p to filter by a filename regex pattern.
› Press t to filter by a test name regex pattern.
› Press q to quit watch mode.
› Press Enter to trigger a test run.
● Cannot log after tests are done. Did you forget to wait for something async in your test?
Attempted to log "hello was echoed".
10 | });
11 |
at console.log (node_modules/jest-cli/node_modules/@jest/console/build/CustomConsole.js:187:10)
at src/__tests__/cli.test.ts:12:17
at ChildProcess.<anonymous> (node_modules/nexpect/lib/nexpect.js:303:9)
When running nexpect with the verbose
option the output of the child output looses its color.
MacOSX 10.9
iTerm 2
Bash 4.2.45
The pathname parsing in nspawn
function does not take into consideration spaces in the pathname.
Consider the following pathname of a CLI application: /home/user/Project Folder/Sub Folder/bin/cli
(note the spaces in the pathname)
In the lines 360-362 in nexpect.js
, we can see the following code:
/home/user/Project
params
OR [ "Folder/Sub" , "Folder/bin/cli" ]
This leads to improper pathanme parsing and errors in testing.
The pathname should be parsed properly even if it contains spaces in it.
Did a git clone, npm install, then test in node v0.10.20:
nexpect$: npm test
[email protected] test /home/mowgli/nexpect/nexpect
vows --spec
♢ nexpect
When using the nexpect module
✓ it should have the correct methods defined
When using the nexpect module spawning echo hello
✗ should respond with no error
» expected true, got false // nexpect-test.js:20
When using the nexpect module spawning ls -l /tmp/undefined
✗ should respond with no error
» expected true, got false // nexpect-test.js:20
✗ Errored » callback not fired
in When using the nexpect module spawning a command that does not exist
in nexpect
in test/nexpect-test.js
✗ Errored » callback not fired
in When using the nexpect module spawning and using the sendline() method
in nexpect
in test/nexpect-test.js
✗ Errored » callback not fired
in When using the nexpect module spawning when options.stripColors is set
in nexpect
in test/nexpect-test.js
✗ Errored » callback not fired
in When using the nexpect module spawning when options.ignoreCase is set
in nexpect
in test/nexpect-test.js
✗ Errored » callback not fired
in When using the nexpect module spawning when options.cwd is set
in nexpect
in test/nexpect-test.js
✗ Errored » callback not fired
in When using the nexpect module spawning and using the wait() method when assertions are met
in nexpect
in test/nexpect-test.js
✗ Errored » callback not fired
in When using the nexpect module spawning and using the wait() method when the last assertion is never met
in nexpect
in test/nexpect-test.js
✗ Errored » 1 honored ∙ 2 broken ∙ 1 errored ∙ 7 dropped
events.js:72
throw er; // Unhandled 'error' event
^
Error: spawn ENOENT
at errnoException (child_process.js:980:11)
at Process.ChildProcess._handle.onexit (child_process.js:771:34)
npm ERR! weird error 8
npm ERR! not ok code 0
mowgli@Honch:/home/mowgli/nexpect/nexpect$ node --version
v0.10.20
At the moment if you wait
for a condition that never occurs the process will hang indefinitely.
It would be nice if this instead timed out after a reasonable duration and allowed you to specify that Timeout in the arguments or options list.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.