Coder Social home page Coder Social logo

bats-mock's People

Contributors

agent-0028 avatar dependabot[bot] avatar ilons avatar jasonkarns avatar kaos avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

bats-mock's Issues

Migrate to bats-core org?

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.

`read` fails to stub, halts test runner

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

  • Commenting out the unstub statement. No effect

I 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[*]}"
}

`stub` not concurrent-safe with parallel test execution (with `--jobs`)

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:

  • A failure occurs when trying to unstub stat (since /tmp/stat-stub-plan will have two entries in its plan when there should only ever be one
  • One of these tests produces an invalid result (because both plans share the same arguments)

Sorry for the toy example. I hope this makes sense.

cleanup function

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)

Question: Advantage of bats-mock over stub.sh?

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?

Support for calling the same mocked call - argument multiple times

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?

spy-like behavior

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

Stub fails to recognise call when arguments contain an asterisk character

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

Stubbing w/ more than 2 threads doesn't work

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).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.