buildkite-plugins / bats-mock

Mocking/stubbing library for BATS (Bash Automated Testing System)
MIT License
17 stars 8 forks source link

Generic stubs #15

Open toote opened 1 year ago

toote commented 1 year ago

It is quite common to have to stub commands only for them to be called multiple times with a set of arguments that are known or small.

I believe those scenarios may be better served by generic stubs. Instead of calls defining exact and sorted matches to compare against on calls, they just define a set of patterns to match against and their corresponding replacement commands. In my idea:

adrianmace commented 1 week ago

Any chance of this being added? I can't tell if it's a regression in bats-mock v4.

Here's a common use case to avoid having to actually sleep for 10 seconds each time:

stub sleep "10 : echo sleep for 10 seconds"

In our command under test, we're calling sleep 10 in a while loop to check the result of a command and break, until a max timeout of 900 seconds.

This actually worked fine in bats-mock v3, but in v4 I am required to set up 90 stub plans.

toote commented 1 week ago

Any chance of this being added?

There is a definitive non-zero chance as long as this issue is still open. Unfortunately, I can't be any more specific that that :shrug:

I can't tell if it's a regression in bats-mock v4.

I don't think it is a regression. Most likely it was unspecified behaviour that changed but would love to hear arguments either way

Here's a common use case to avoid having to actually sleep for 10 seconds each time:

stub sleep "10 : echo sleep for 10 seconds"

In our command under test, we're calling sleep 10 in a while loop to check the result of a command and break, until a max timeout of 900 seconds.

This actually worked fine in bats-mock v3, but in v4 I am required to set up 90 stub plans.

I am quite puzzled at your v3 vs v4 versioning, specially as this repository's latest release is v2.1.1 :question:

On the other hand, I do understand the annoyance of setting up 90 stub plans. Luckily, since #11 - thanks to @flamefire's efforts (see #7) - you can use incremental stub plans to setup 90 stub plans in a loop:

for ITER in seq 90; do
  stub sleep "10: echo sleep for 10 seconds #${ITER}"
done