GithubHelp home page GithubHelp logo

nodejitsu / nexpect Goto Github PK

View Code? Open in Web Editor NEW
290.0 290.0 37.0 73 KB

spawn and control child processes in node.js with ease

Home Page: http://github.com/nodejitsu/nexpect

License: Other

JavaScript 100.00%

nexpect's People

Contributors

airandfingers avatar bajtos avatar binchensjtu avatar chenglou avatar coderarity avatar indexzero avatar marak avatar rejas avatar sam-github avatar tmpvar avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

nexpect's Issues

Pulled from git, all tests fail.

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

Support for interactive programs

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.

nexpect - CLI hangs

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?

How to exit a program?

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?

How to use the sendline() method?

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("...")
...

Doesn't work with bash shells?

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();

No color in child output

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

`wait` should accept an optional timeout

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.

Is there any OR possibility?

In Expect script I can use OR condition for maybe sent another command or end session.How can I implement it with nexpect?

Is it possible to run with Jest?

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?

Test

import nexpect from 'nexpect';

nexpect
  .spawn('yarn', ['start'])
  .expect('hello')
  .run(function(err) {
    if (!err) {
      console.log('hello was echoed');
    }
  });

Output

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)

Bug: Improper pathname parsing in `nspawn` function

Bug Description:

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:

Screenshot from 2021-05-13 17-29-03

Suppose we consider our above pathname, we can clearly observe:

  • command will be: /home/user/Project
  • params will be: params OR [ "Folder/Sub" , "Folder/bin/cli" ]

This leads to improper pathanme parsing and errors in testing.

Expected Behaviour:

The pathname should be parsed properly even if it contains spaces in it.

run() got executed twice?

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?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.