jasonkarns / bats-mock Goto Github PK
View Code? Open in Web Editor NEWMocking/stubbing library for BATS (Bash Automated Testing System)
License: MIT License
Mocking/stubbing library for BATS (Bash Automated Testing System)
License: MIT License
Hi, what do you think about bats-core/bats-core#269 ?
Having all libraries under the same org would make them more discoverable and potentially bring more users/contributors to this project and the bats ecosystem.
When I attempt to run a test with a stub, the test runner gets stuck without throwing an error. It must be exited with CTRL + C
$ find -type f | entr ./node_modules/bats/bin/bats -r ./helpers/tests
✓ a/b matcher to ensure the asserts are working
✓ createConfigFile
✓ toLowercase
gatherConfigOptions 4/4
I need to stub read
to allow automated testing to work. But this code is causing the halt:
# tests.bats
load "batsMock/stub" # saved via git submodule into this folder
@test "gatherConfigOptions" {
# setup
stub read \
"echo 'returnValue1'" \
"echo 'returnValue2'"
# test execution & assert
local options=$(gatherConfigOptions)
assert_equal $options "wrongValue"
# teardown
unstub read
}
Also tried
unstub
statement. No effectI suspect that read
is running unstubbed and causing the infinite delay. I could be wrong though.
Anyone able to tell what the issue is and how to overcome it?
Here is the function I am attempting to stub the internals of:
# ./helpers/helpers.sh
function gatherConfigOptions {
declare -A configData
read -p "prompt text here" "configData[languageCode]"
read -p "prompt text here" "configData[numberOfRepeats]"
echo "${configData[*]}"
}
It appears that a race to write stub test plans happens when you run bats
with --jobs n
set.
Example:
load "test_helpers/bats-mock/stub"
load "test_helpers/bats-support/load"
load "test_helpers/bats-assert/load"
# 1_stub_stat_succeeding.bats
@test "Stub stat" {
stub stat "fake-dir: echo 'Yup' && exit 0"
run ./example.sh
assert_success
unstub stat
}
# 2_stub_stat_failing.bats
load "test_helpers/bats-mock/stub"
load "test_helpers/bats-support/load"
load "test_helpers/bats-assert/load"
@test "Stub stat failing" {
stub stat "fake-dir: echo 'Nope' && exit 1"
run ./example.sh
assert_failure
unstub stat
}
# example.sh
stat fake-dir
When you run this with --bats jobs 8
(for example), one of these things happens:
unstub stat
(since /tmp/stat-stub-plan
will have two entries in its plan when there should only ever be oneSorry for the toy example. I hope this makes sense.
I can't currently update bats because bats-mock has an outdated peer dependency. Please update.
Would be great if unstub
would not only return 1 but also print the missing invocations to &3 for easier error fixing.
presently, stub
just appends to the stub-plan file(s). And typical teardown
functions only cleanup TMP
, not BATS_TMPDIR
. It would be nice for stubs to have their own directory under BATS_TMPDIR
. Or alternatively keep a list of stubs created and expose a teardown function to remove them (which could then be invoked from userland teardown
)
I am currently looking into unit-testing my BASH code. The bats-core
project seems the most likely candidate to write tests in. As part of writing unit-test is mocking/stubbing, I went looking for which project offers the best functionality.
Besides this project I also came across https://github.com/jimeh/stub.sh, which looks like it has more functionality/features.
I was wondering, is there any advantage in using bats-mock
over stub.sh
?
Hello, I have some scripts that depend on hardware and hence the need for my to use mock/stubs. Can anyone please share some insight as to how to setup a bats project, do all stub files go into a certain folder?
This will fail:
@test "test calling the same mocked program multiple times" {
stub curl \
"-s -m 2 http://www.google.com : echo called_me"
run bash -c "test.sh"
unstub curl
}
test.sh:
!#/bin/bash
for i in `seq 1 10`;
do
MYVAR=$(curl -s -m 2 http://www.google.com)
echo ${MYVAR}
done
The error:
✗ test calling the same mocked program multiple times
(from function `unstub' in file ./helpers/mocks/stub.bash, line 34,
in test file test-m.bats, line 16)
`unstub curl' failed
Is there a way to support calling the same call multiple times?
presently stubs behavior is supported (creating a plan); and mock behavior is supported (via unstub, which fails if the plan wasn't followed as expected). It would be nice to be able to make limited assertions against the test run, instead of requiring full mock verification
If the arguments string includes an asterisk then the bats-mock stub appears to fail to recognise the call.
Reduced test case:
#!/usr/bin/env bats
load '../vendor/bats-mock/stub'
teardown() {
unstub aws
}
# fails
@test "test with asterisk" {
stub aws \
"ec2 describe-snapshots --query Snapshots[*].{Time:StartTime} : echo 'foo'"
run aws ec2 describe-snapshots --query Snapshots[*].{Time:StartTime}
[ "$output" = "foo" ]
}
# passes
@test "test without asterisk" {
stub aws \
"ec2 describe-snapshots --query Snapshots[].{Time:StartTime} : echo 'foo'"
run aws ec2 describe-snapshots --query Snapshots[].{Time:StartTime}
[ "$output" = "foo" ]
}
Output:
$ vendor/bats/bin/bats test/test_case.bats
✗ test with asterisk
(in test file test/test_case.bats, line 14)
`[ "$output" = "foo" ]' failed
✓ test without asterisk
2 tests, 1 failure
If you stub a function with 3 mocks e.g.
stub someFunc \
"echo 1 >> somefile.txt" \
"echo 2 >> somefile.txt" \
"echo 3 >> somefile.txt";
sut_dosomething_thatwillcall_someFunc;
unstub someFunc
This will work if [sut_dosomething_thatwillcall_someFunc] is single threated. However,
if [sut_dosomething_thatwillcall_someFunc] has 3 threads, each thread calls [someFunc] once, the 3rd mock will never be executed (the 2nd mock "echo 2..." gets executed repetitively).
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.