Skip to content

Instantly share code, notes, and snippets.

@jzacsh
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jzacsh/65fb4df01e3dbf23a2a4 to your computer and use it in GitHub Desktop.
Save jzacsh/65fb4df01e3dbf23a2a4 to your computer and use it in GitHub Desktop.
bats-exec

Bash script to exec BATS script on local file(s)

Status

This code has zero automated test coverage, though I do plan to add that. In the mean time, use this script at your own risk. I have had 100% success with it in my two other github pages projects, so far.

Usage

Once you've installed this repo as a dependency (see below), you can call this script via your package.json scripts dictionary, eg:

{
  "scripts": {
    "test": "bats-easy-exec tests/"
  }
}

Installation

You'll want to add this repository to your dependencies, eg: add this to your package.json:

{
  ...
  "dependencies": {
    "bats-easy-exec": "git+https://gist.github.com/65fb4df01e3dbf23a2a4.git#master",
  }
}
{
"name": "bats-easy-exec",
"description": "bats as a portable npm dependency to run tests against an sh/ directory or single script",
"repository": "https://gist.github.com/65fb4df01e3dbf23a2a4.git",
"version": "0.0.1",
"bin": {
"bats-easy-exec": "./test.sh"
},
"license": "Apache-2.0"
}
#!/usr/bin/env bash
#
# NOTE: COPY/PASTED from github.com/jzacsh/yabashlib
specDir="$(readlink -f "$1")"; shift
baseDir="$(dirname "$specDir")"
[ -d "$specDir" ] || exit 99
specSuite="$baseDir/"*.bats
tmpDir="$specDir/.tmp/"; mkdir -p "$tmpDir"
batsDir="$tmpDir/.bats"
batsBld="$batsDir/local"
batsExec="$batsBld/bin/bats"
# Ensure bats unit testing framework is in place
[ ! -x "$batsExec" ] && {
src_dir="$batsDir/src"
exec 4>/dev/null
[[ "$@" =~ --tap ]] && safefd=4 || safefd=2
{
echo 'Could not find local bats installation, installing now...' >&$safefd
[ -d "$batsDir" ] && rmdir "$batsDir"
{
[ ! -d "$batsDir" ] &&
git clone --quiet https://github.com/sstephenson/bats.git "$src_dir" &&
mkdir "$batsBld" &&
"$src_dir/install.sh" "$batsBld" &&
[ -x "$batsExec" ]
} >&$safefd
[ -d "$batsDir" ] || {
echo 'Failed to automatically install bats test framework' >&2
exit 1
}
} || {
echo 'ERROR: Failed to find or install bats testing framework' >&2
exit 1
}
}
# Default to pointing at spec/suite/ if it doesn't look like there are file
# args intended for bats to read directly
bats_target="$specSuite"
for (( i = 1; i <= $#; ++i ));do
[ -r ${!i} ] && {
unset bats_target
break
}
done
mkdir -p "$tmpDir/mktmp"
# Execute all bats unit tests
SRC_DIR="$baseDir" \
TMPDIR="$tmpDir/mktmp" \
FAKE_BACKUPS="$specDir/backups" \
FAKE_DATA="$specDir/precious-content" \
"$batsExec" $@ $bats_target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment