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

cleanup function #3

Open
jasonkarns opened this issue Nov 2, 2015 · 6 comments
Open

cleanup function #3

jasonkarns opened this issue Nov 2, 2015 · 6 comments

Comments

@jasonkarns
Copy link
Owner

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)

@jt-nti
Copy link

jt-nti commented Jul 18, 2018

@jasonkarns would you accept any contributions for this issue? (Btw. I noticed that you're a maintainer in the Bats github org- do you have any plans to move this repository to that org?)

@jasonkarns
Copy link
Owner Author

We are looking at pulling in a or some assertion libraries, and I would push for this one to be pulled into the org as well. Until that time, I'm hesitant to do much development on bats-mock as its architecture would need to change to work with whatever assertion library does get accepted into the org.

@jt-nti
Copy link

jt-nti commented Jul 19, 2018

That makes sense. I've also been holding off on assertions in the hope that they'll get pulled in to the org :)

@jt-nti
Copy link

jt-nti commented Jul 19, 2018

Btw. in case it helps anyone, I've created a helper function which should hopefully do some cleanup. Any comments or suggestions for improvement would be very welcome.

cleanup_stubs() {
  if stat ${BATS_TMPDIR}/*-stub-plan >/dev/null 2>&1; then
    for file in ${BATS_TMPDIR}/*-stub-plan; do
      program=$(basename $(echo "$file" | rev | cut -c 11- | rev))
      unstub $program || true
    done
  fi
}

@Flamefire
Copy link

I would very much like to see such a function probably also (or instead) in a verify_stubs fashion:

  • Your function cleans up all stubs so is suitable to be put in cleanup
  • Missing: Show message of failed unstub calls (probably just echo) and return result if any failed

-> Automatic verification of all stubs generated reducing boiler plate.

Note the this won't work:

function cleanup () {
  unstub foo # Failure here will not be registred
  unstub bar # Return of cleanup is only dependent on this
}

@Flamefire
Copy link

My current version:

_STUB_ORIG_BASENAME=$(command -v basename)
_STUB_ORIG_ECHO=$(command -v echo)

function verify_stubs {
    local result=0
    if [ -d "${BATS_MOCK_BINDIR}" ]; then
        for program in "${BATS_MOCK_BINDIR}"/*; do
            program=$("${_STUB_ORIG_BASENAME}" "${program}")
            if [ -f "${BATS_TMPDIR}/${program}-stub-plan" ] && ! unstub "${program}"; then
                "${_STUB_ORIG_ECHO}" "${program} has failed expectations"
                result=1
            fi
        done
    fi
    return $result
}

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

3 participants