bats icon indicating copy to clipboard operation
bats copied to clipboard

Helpful test script

Open lukechilds opened this issue 6 years ago • 2 comments

Great job on bats, it's really nice.

I was kind of confused on how to get my tests running everywhere.

  • On CI I was cloning bats then running it.
  • Locally I had bats installed with my package manager so could just call it directly
  • For contributors I had to point them to where to install bats to be able to run tests

It was kind of a pain.

Anyway, I just thew together this super simple script that pulls bats down into your tests directory, then runs your tests with it. Next time you run the script it won't pull bats down again if it's still there. tests/bats should be added to .gitignore so it's never included in the actual repo.

That means I can just run ./test everywhere; CI, locally and for end users. And it just works. Thought it might be worth sharing. Happy to submit a PR if you think it would be useful.

Script is just:

#!/bin/bash
set -euo pipefail

# Pull down bats
if [ ! -d "tests/bats" ] ; then
    echo "Pulling down github.com/sstephenson/bats test framework..."
    git clone --quiet https://github.com/sstephenson/bats.git tests/bats
fi

# Run tests
echo "Running tests..."
tests/bats/bin/bats tests

And means anyone can just do:

$ ./test
Pulling down github.com/sstephenson/bats test framework...
Running tests...
 ✓ gpg is in PATH
 ✓ CHEST_DIR gets auto created
 ✓ Running with no commands returns usage with error status
 ✓ Option -e encrypts a file and sends it to the chest
 ✓ Option -d decrypts a file from the chest
 ✓ Option -z compresses data before/after sending to chest
 ✓ Option -r removes the original file after sending to the chest
 ✓ Option -l lists items in chest
 ✓ Option -k sets a custom key
 ✓ Directories with children can be added/retrieved form the chest

10 tests, 0 failures

This is the project I created it for if you're interested: https://github.com/lukechilds/chest

lukechilds avatar Jan 11 '18 02:01 lukechilds

This project is no longer being actively maintained by its original auther (see https://github.com/sstephenson/bats/issues/236).

You might want to migrate to the community maintained fork bats-core and report your issue there.

Potherca avatar Apr 25 '18 21:04 Potherca

https://medium.com/@pimterry/testing-your-shell-scripts-with-bats-abfca9bdc5b9

030 avatar May 09 '18 05:05 030