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

Add mocking of commands - closes #2 and #17 #20

Open
wants to merge 11 commits into
base: master
Choose a base branch
from

Conversation

mh182
Copy link

@mh182 mh182 commented Mar 14, 2023

Add an optional parameter to mock_create [command] to mock commands.

Adds following new functions:

  • mock_chroot to create a deterministic system setup. It does not use chown - if someone comes up with a better name, suggestions are welcome.
  • path_override and path_rm to manipulate PATH
  • mock_teardown for cleanup mock objects

I updated the documentation with some examples how those functions can be used.

[[ -x "${output}/unlink" ]]
[[ -x "${output}/wc" ]]
[[ -x "${output}/which" ]]
[[ -x "${output}/xargs" ]]

Choose a reason for hiding this comment

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

I guess this list is specific to your machine, so perhaps a reduced set of commands would be enough to provide a decent test, so that this will pass even if run inside a plain Alpine docker image.

suggestion: date, ln, ls, sh, rm

# Globals:
# BATS_TMPDIR
mock_teardown() {
rm -rf "${BATS_TMPDIR}"/bats-mock.$$.*

Choose a reason for hiding this comment

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

I could not find a unit test for mock_teardown. I think it would be really easy to provide one

Copy link

@jesperronn jesperronn left a comment

Choose a reason for hiding this comment

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

I took the liberty of reviewing the basic code changes of this pull-request, although I am in no way affilliated with grayhemp/bats-mock project.

Would be fine if @grayhemp or another maintainer could review.

I might be misunderstanding, but for me it seems as I have to use this changeset to make mocks available inside each file under test.

(altough there may be easier workarounds, which I have not yet discovered.)

I like the approach that mock_create injects a symlink to the mock into the path loaded for the file under test.

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.

None yet

2 participants