#!/usr/bin/env bash set -e proc version { echo "Bats 0.4.0" } proc usage { version echo "Usage: bats [-c] [-p | -t] [ ...]" } proc help { usage echo echo " is the path to a Bats test file, or the path to a directory" echo " containing Bats test files." echo echo " -c, --count Count the number of test cases without running any tests" echo " -h, --help Display this help message" echo " -p, --pretty Show results in pretty format (default for terminals)" echo " -t, --tap Show results in TAP format" echo " -v, --version Display the version number" echo echo " For more information, see https://github.com/sstephenson/bats" echo } proc resolve_link { $[type -p greadlink readlink | head -1] $1 } proc abs_dirname { local cwd="$[pwd]" local path="$1" while test -n $path { cd $(path%/*) local name="$(path##*/)" setglobal path = $[resolve_link $name || true] } pwd cd $cwd } proc expand_path { do { cd $[dirname $1] !2 >/dev/null local dirname="$PWD" cd $OLDPWD echo "$dirname/$[basename $1]" } || echo $1 } setglobal BATS_LIBEXEC = $[abs_dirname $0] export BATS_PREFIX="$[abs_dirname $BATS_LIBEXEC]" export BATS_CWD="$[abs_dirname .]" export PATH="$BATS_LIBEXEC:$PATH" setglobal options = ''() setglobal arguments = ''() for arg in [@Argv] { if test $(arg:0:1) = "-" { if test $(arg:1:1) = "-" { compat array-assign options '${#options[*]}' $(arg:2) } else { setglobal index = '1' while setglobal option = $(arg:$index:1) { test -n $option || break compat array-assign options '${#options[*]}' $option let index+=1 } } } else { compat array-assign arguments '${#arguments[*]}' $arg } } unset count_flag pretty test -t 0 && test -t 1 && setglobal pretty = '"1'" test -n $CI && setglobal pretty = ''"" for option in [$(options[@])] { match $option { with "h" | "help" help exit 0 with "v" | "version" version exit 0 with "c" | "count" setglobal count_flag = '"-c'" with "t" | "tap" setglobal pretty = ''"" with "p" | "pretty" setglobal pretty = '"1'" with * usage > !2 exit 1 } } if test $(#arguments[@]) -eq 0 { usage > !2 exit 1 } setglobal filenames = ''() for filename in [$(arguments[@])] { if test -d $filename { shopt -s nullglob for suite_filename in ["$[expand_path $filename]"/*.bats] { compat array-assign filenames '"${#filenames[@]}"' $suite_filename } shopt -u nullglob } else { compat array-assign filenames '"${#filenames[@]}"' $[expand_path $filename] } } if test $(#filenames[@]) -eq 1 { setglobal command = '"bats-exec-test'" } else { setglobal command = '"bats-exec-suite'" } if test -n $pretty { setglobal extended_syntax_flag = '"-x'" setglobal formatter = '"bats-format-tap-stream'" } else { setglobal extended_syntax_flag = ''"" setglobal formatter = '"cat'" } set -o pipefail execfail exec $command $count_flag $extended_syntax_flag $(filenames[@]) | $formatter