#!/bin/sh
# Based on https://github.com/mlafeldt/chef-runner/blob/34269dbb726c243dff9764007e7bd7f0fe9ee331/script/test
# Run package tests for a file/directory, or all tests if no argument is passed.
# Useful to e.g. execute package tests for the file currently open in Vim.
# Usage: script/test [path]

set -e

go_pkg_from_path() {
    path=$1
    if test -d "$path"; then
        dir="$path"
    else
        dir=$(dirname "$path")
    fi
    (cd "$dir" && go list)
}

if test $# -gt 0; then
    pkg=$(go_pkg_from_path "$1")
    verbose=-v
else
    pkg=$(go list ./... | grep -v /vendor/)
    verbose=
fi

exec go test ${GOTESTOPTS:-$verbose} $pkg