qunitjs / node-qunit

QUnit runner for Node.js.
MIT License
172 stars 53 forks source link

[Feature Request] Propagation of command line arguments to child processes required #105

Closed micellius closed 10 years ago

micellius commented 10 years ago

Using node-qunit for integration tests scenarios requires passing configuration options via command line to tests (like back-end host, etc.). Since test runner spawns new processes (child.js), process.argv of the parent process is no longer available in tests (because each spawened process has it's own process.argv). This issue may be solved by propagating command line arguments (passed to cli.js) to child processes via process.env and used as follows:

shell

$ ./node_modules/qunit/bin/cli.js -c src/code.js -t test/test.js --host test.domain.com

test.js

var host = process.env['qunit.args.host']; // <-- will be "test.domain.com"

For example jasmine-node allows to propagate command line arguments passed to cli.js using --config argument_name argument_value syntax.

kof commented 10 years ago

Have you tried process.env in the child?

kof commented 10 years ago

I am actually passing env to the child https://github.com/kof/node-qunit/blob/master/lib/testrunner.js#L66

kof commented 10 years ago

If it doesn't work, any ideas?

micellius commented 10 years ago

I have seen that you pass process.env in the child process, but it does not contain command line arguments out of the box, so I utilised this existing mechanism and submitted PR #106 to resolve the issue with minimum changes.

kof commented 10 years ago

http://nodejs.org/docs/latest/api/child_process.html#child_process_child_process_spawn_command_args_options

If I understand correctly the documentation, passed env object should be accessible in the child as it is in the parent, no?

kof commented 10 years ago

Oh its process.argv, not process.env.argv ...

kof commented 10 years ago

I have a better solution for this. Now you can use process.argv in the child.

kof commented 10 years ago

check it out, reopen if something is wrong.

micellius commented 10 years ago

Yes, you understand correctly: passed env is accessible in the child as in the parent, however in order to receive command line arguments of parent process you need process.argv or something that is called args in cli.js. Since cli.js passes to test runner only limited set of arguments it is familiar with (code, tests, deps and log), other arguments are lost and are not propagated further and therefore not accessible via process.argv or process.env of the child process. To solve this, I placed process.argv arguments one by one into process.env (which is already accessible in the child process) with qunit.args. prefix.