Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw error in sync method - fixes #66 #67

Merged
merged 1 commit into from
Jan 9, 2017
Merged

Throw error in sync method - fixes #66 #67

merged 1 commit into from
Jan 9, 2017

Conversation

SamVerschueren
Copy link
Contributor

This should fix #66 by throwing an Error object if something is written to stderr. Also added a test for this.

@SamVerschueren
Copy link
Contributor Author

Oh, I totally missed test.js in the root. Let me move the test I wrote to that file.

@SamVerschueren SamVerschueren force-pushed the iss66 branch 2 times, most recently from a8a5684 to b4031c4 Compare December 20, 2016 14:21
@SamVerschueren
Copy link
Contributor Author

Odd, this works on my Mac and on Windows but apparently not on Linux...

@SamVerschueren
Copy link
Contributor Author

SamVerschueren commented Dec 21, 2016

The test doesn't seem to be correct. Is there any other way I can make sure something is printed to stderr instead?

@sindresorhus
Copy link
Owner

I think you misunderstood. It shouldn't throw when something is outputted on stderr. stderr is not just for errors, it's an alternative stream. It should throw when there's an error (.error has something).

@SamVerschueren
Copy link
Contributor Author

Oh yes, you're totally right! My bad.

I fixed the implementation but couldn't come up with a command that had an error object on Windows. I'm really not familiar with that system. Everything I tried just exited with status 1 and data in the stderr. So if you have any better ideas on which command we could use that fails on Windows, let me know and I'll fix the tests.

@sindresorhus
Copy link
Owner

Try spawning something that doesn't exist.

@SamVerschueren
Copy link
Contributor Author

Unfortunately that doesn't give an error...

$ cd foo

{ status: 1,
  signal: null,
  output: [ null, '', 'The system cannot find the path specified.\r\n' ],
  pid: 684,
  stdout: '',
  stderr: 'The system cannot find the path specified.'
}

$ foo

{ status: 1,
  signal: null,
  output:
   [ null,
     '',
     '\'foo\' is not recognized as an internal or external command,\r\noperable program or batch file.\r\n' ],
  pid: 1760,
  stdout: '',
  stderr: '\'foo\' is not recognized as an internal or external command,\r\noperable program or batch file.'
}

@sindresorhus
Copy link
Owner

That sounds like a Node.js bug. The status (exit code) is not 0, so it should be an error. You could check the status though.

@SamVerschueren
Copy link
Contributor Author

Do you mean in the code? Something like this:

if (result.error || result.status !== 0) {
    throw result.error || new Error(result.stderr);
}

@sindresorhus
Copy link
Owner

👍

@SamVerschueren
Copy link
Contributor Author

Finally got it working on both Travis and AppVeyor :p.

@@ -231,6 +231,10 @@ module.exports.sync = (cmd, args, opts) => {

const result = childProcess.spawnSync(parsed.cmd, parsed.args, parsed.opts);

if (result.error || result.status !== 0) {
throw result.error || new Error(result.stdout === '' ? result.stderr : result.stdout);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrap it in parens for clarity:

throw (result.error || new Error(result.stdout === '' ? result.stderr : result.stdout));

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to switch the stdout/stderr check to something like this

new Error(result.stderr === '' ? result.stdout : result.stderr);

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, actually, that would make sense.

@@ -95,6 +95,8 @@ Same options as [`child_process.execFileSync`](https://nodejs.org/api/child_proc

Returns the same result object as [`child_process.spawnSync`](https://nodejs.org/api/child_process.html#child_process_child_process_spawnsync_command_args_options).

This method throws an `Error` if something is written to `stderr`.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to reflect the actual code.

@LinusU
Copy link
Contributor

LinusU commented Jan 8, 2017

Can this be merged now? I would love to have it in ❤️

@sindresorhus
Copy link
Owner

It's waiting on #67 (comment)

@SamVerschueren
Copy link
Contributor Author

@sindresorhus I fixed that. Forgot to notify you.

@sindresorhus sindresorhus merged commit c8c9c7b into master Jan 9, 2017
@sindresorhus
Copy link
Owner

Cool. Thanks Sam :)

@sindresorhus sindresorhus deleted the iss66 branch January 9, 2017 09:34
@SamVerschueren
Copy link
Contributor Author

SamVerschueren commented Jan 9, 2017

You're welcome! And thank you for merging 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Should execa.sync() throw on error?
3 participants