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

Stubbing only ever attempts to match against the first entry in the plan #18

Open
mwgamble opened this issue Sep 5, 2023 · 3 comments

Comments

@mwgamble
Copy link

mwgamble commented Sep 5, 2023

I think this break statement is preventing the stubber from matching on anything except the first entry in the plan:

https://github.com/buildkite-plugins/bats-mock/blob/master/binstub#L112

It always exits the loop, even if there are more entries in the plan :(

@mwgamble
Copy link
Author

mwgamble commented Sep 5, 2023

It appears that in both #17 and #18 I've fundamentally misunderstood how stubbing is supposed to work. Each entry in the plan is an expected invocation, not a list of possible invocations to expect.

I'm happy for both issues to be closed, but it would be nice if there was a way to support this kind of functionality - instead of listing out all invocations in order, it would be helpful if we could list prefixes for expected invocations (from most specific to least specific) and match against all of them every time.

Right now it isn't clear to me why it bothers performing any argument matching at all, given it expects each pattern to be used only once. It's especially not clear to me what the behaviour is when only one pattern is supplied.

@mwgamble
Copy link
Author

mwgamble commented Sep 5, 2023

Okay so I'm fundamentally confused about how stubbing is supposed to work.

For most of my tests, I have stub aws "example command" because the aws-cli is only run once.

However for some of my tests, I know that aws-cli will be run twice, and I need it to behave differently in this situation, based on the arguments its passed. Therefore I first need to run unstub aws and then stub aws "example command one" "example command two". This is done before the original stub is ever used.

However this doesn't work. For reasons I cannot fathom, it tells me the stub doesn't exist. I added this debugging code before my unstub aws line to confirm that the stub does indeed exist:

type aws
echo "${BATS_MOCK_BINDIR}"
ls -la "${BATS_MOCK_BINDIR}"

It still tells me the stub doesn't exist, even though I can see the file on disk. I have no idea why this "file exists" check isn't passing:
https://github.com/buildkite-plugins/bats-mock/blob/master/stub.bash#L61

Even if the "file exists" check was passing, it looks like there is code in the stubber to check whether or not the stub was used at all:
https://github.com/buildkite-plugins/bats-mock/blob/master/binstub#L120-L123

So I'm not sure what options I have here. I can make assumptions about the internal implementation details of the stubber by manually deleting the files it's looking for that unstub is supposed to delete itself, but that's not necessarily reliable long-term. I feel like this wouldn't be a problem if the stubber just matched prefixes rather than treating it as a plan for the entire execution of the test.

@mwgamble
Copy link
Author

mwgamble commented Sep 5, 2023

It's especially not clear to me what the behaviour is when only one pattern is supplied.

It looks like it just silently returns an exit status of 1, potentially masking other issues with the test. The problem with this approach is that maybe I wanted the command to return an exit status of 1, but for an actual codified reason rather than an accident of not having enough lines in my plan.

Would it be possible to at least emit a warning in this case that shows up even if the test was successfully run? I'm not sure I would have ever had any idea that some of my tests weren't behaving the way I expected if I hadn't looked so deeply into the stubber for this issue.

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

No branches or pull requests

1 participant