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

[Feature idea] Execute current package's binary #281

Closed
ehmicky opened this issue Jun 10, 2019 · 5 comments · Fixed by #339
Closed

[Feature idea] Execute current package's binary #281

ehmicky opened this issue Jun 10, 2019 · 5 comments · Fixed by #339

Comments

@ehmicky
Copy link
Collaborator

ehmicky commented Jun 10, 2019

When execa is used in unit tests to test the current package's CLI, it might look like this:

const PATH_TO_BINARY = `${__dirname}/../../bin/index.js`

test('should work', async t => {
  const output = await execa(PATH_TO_BINARY, ['arguments'])
  t.is(output, 'expected value')
})

To improve this use case, should we allow the following?

test('should work', async t => {
  const output = await execa('binary-name', ['arguments'])
  t.is(output, 'expected value')
})

It could be implemented like this:

  • use readPkgUp[.sync](). This could be cached.
  • get the bin field string or object
  • if the name matches the file passed to execa, use the binary path instead

This would be behind a currentPackage: boolean option. We could default it either to false or true.

Advantages:

  • it tests that the bin field in package.json is valid. This is something that is basically never checked in unit tests, but should.
  • it decouples the binary path from the tests, which allows moving the file without rewriting the tests.

Is this a good idea?

@revelt
Copy link

revelt commented Jun 28, 2019

I tested the readPkgUp, bin is indeed recognised. Idea looks good!

@sindresorhus
Copy link
Owner

I feel like this is outside the scope of execa. I've been wanting to make child process assertion library to make it easier to assert certain stuff from child processes, especially for interactive processes. Would be cool to have child process mocking built-in too. I imagine your idea would fit better in such library. I have no time to make it though, so feel free to go with it if you have time.

@ehmicky
Copy link
Collaborator Author

ehmicky commented Jul 1, 2019

If it's as a separate library, I would imagine it to be restricted to this use case as:

const binPath = await getBinPath('binary-name')

Like this it could be used outside of execa as well. And for execa it would be:

import getBinPath from 'get-bin-path'

test('should work', async t => {
  const binPath = await getBinPath('binary-name')
  const output = await execa(binPath, ['arguments'])
  t.is(output, 'expected value')
})

What do you think? I could build that.

The only other package that does that seems to be resolve-bin but without promises, and not maintained it seems.

@sindresorhus
Copy link
Owner

sindresorhus commented Jul 1, 2019

👍 We could link to it from the readme.

@ehmicky
Copy link
Collaborator Author

ehmicky commented Jul 1, 2019

Done by #339

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

Successfully merging a pull request may close this issue.

3 participants