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

Add test runner. #68

Merged
merged 7 commits into from
Feb 24, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/bats
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
script: ./bin/run-tests --tap
language: c
notifications:
email:
on_success: never
1 change: 1 addition & 0 deletions bin/ivycp
7 changes: 7 additions & 0 deletions bin/run-tests
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/sh
#

set -e
cd "$(dirname "$0")/.."
[ -d bats ] || git clone https://github.com/sstephenson/bats.git bats
./bats/bin/bats "$@" ./test
1 change: 1 addition & 0 deletions bin/sbt
151 changes: 82 additions & 69 deletions sbt
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@
# Author: Paul Phillips <paulp@typesafe.com>

# todo - make this dynamic
declare -r sbt_release_version=0.13.1
declare -r sbt_unreleased_version=0.13.2-SNAPSHOT # -sbt-dev doesn't work at present
declare -r buildProps=project/build.properties
declare -r sbt_release_version="0.13.1"
declare -r sbt_unreleased_version="0.13.2-SNAPSHOT" # -sbt-dev doesn't work at present
declare -r buildProps="project/build.properties"

declare sbt_jar sbt_dir sbt_create sbt_launch_dir
declare scala_version java_home sbt_explicit_version
declare verbose debug quiet noshare batch trace_level log_level
declare sbt_saved_stty

echoerr () { [[ -z $quiet ]] && echo "$@" >&2; }
echoerr () { [[ -z "$quiet" ]] && echo "$@" >&2; }
vlog () { [[ -n "$verbose$debug" ]] && echoerr "$@"; }
dlog () { [[ -n $debug ]] && echoerr "$@"; }
dlog () { [[ -n "$debug" ]] && echoerr "$@"; }

# we'd like these set before we get around to properly processing arguments
for arg in "$@"; do
case $arg in
case "$arg" in
-q|-quiet) quiet=true ;;
-d|-debug) debug=true ;;
-v|-verbose) verbose=true ;;
Expand All @@ -29,31 +29,31 @@ done

# spaces are possible, e.g. sbt.version = 0.13.0
build_props_sbt () {
[[ -r $buildProps ]] && \
grep ^sbt\.version $buildProps | tr '=' ' ' | awk '{ print $2; }'
[[ -r "$buildProps" ]] && \
grep '^sbt\.version' "$buildProps" | tr '=' ' ' | awk '{ print $2; }'
}

update_build_props_sbt () {
local ver="$1"
local old="$(build_props_sbt)"

[[ -r $buildProps ]] && [[ $ver != "$old" ]] && {
perl -pi -e "s/^sbt\.version\b.*\$/sbt.version=${ver}/" $buildProps
grep -q '^sbt.version[ =]' $buildProps || printf "\nsbt.version=%s\n" "$ver" >> $buildProps
[[ -r "$buildProps" ]] && [[ "$ver" != "$old" ]] && {
perl -pi -e "s/^sbt\.version\b.*\$/sbt.version=${ver}/" "$buildProps"
grep -q '^sbt.version[ =]' "$buildProps" || printf "\nsbt.version=%s\n" "$ver" >> "$buildProps"

echoerr !!!
echoerr !!! Updated file $buildProps setting sbt.version to: "$ver"
echoerr !!! Previous value was: "$old"
echoerr !!!
echoerr "!!!"
echoerr "!!! Updated file $buildProps setting sbt.version to: $ver"
echoerr "!!! Previous value was: $old"
echoerr "!!!"
}
}

sbt_version () {
if [[ -n $sbt_explicit_version ]]; then
echo $sbt_explicit_version
if [[ -n "$sbt_explicit_version" ]]; then
echo "$sbt_explicit_version"
else
local v=$(build_props_sbt)
if [[ -n $v ]]; then
local v="$(build_props_sbt)"
if [[ -n "$v" ]]; then
echo "$v"
else
echo "$sbt_release_version"
Expand All @@ -63,16 +63,16 @@ sbt_version () {

# restore stty settings (echo in particular)
onSbtRunnerExit() {
[[ -n $sbt_saved_stty ]] || return
[[ -n "$sbt_saved_stty" ]] || return
dlog ""
dlog "restoring stty: $sbt_saved_stty"
stty $sbt_saved_stty
stty "$sbt_saved_stty"
unset sbt_saved_stty
}

# save stty and trap exit, to ensure echo is reenabled if we are interrupted.
trap onSbtRunnerExit EXIT
sbt_saved_stty=$(stty -g 2>/dev/null)
sbt_saved_stty="$(stty -g 2>/dev/null)"
dlog "Saved stty: $sbt_saved_stty"

# this seems to cover the bases on OSX, and someone will
Expand All @@ -81,7 +81,7 @@ get_script_path () {
local path="$1"
[[ -L "$path" ]] || { echo "$path" ; return; }

local target=$(readlink "$path")
local target="$(readlink "$path")"
if [[ "${target:0:1}" == "/" ]]; then
echo "$target"
else
Expand All @@ -97,7 +97,12 @@ die() {
make_url () {
version="$1"

echo "$sbt_launch_repo/org.scala-sbt/sbt-launch/$version/sbt-launch.jar"
case "$version" in
0.7.*) echo "http://simple-build-tool.googlecode.com/files/sbt-launch-0.7.7.jar" ;;
0.10.* ) echo "$sbt_launch_repo/org.scala-tools.sbt/sbt-launch/$version/sbt-launch.jar" ;;
0.11.[12]) echo "$sbt_launch_repo/org.scala-tools.sbt/sbt-launch/$version/sbt-launch.jar" ;;
*) echo "$sbt_launch_repo/org.scala-sbt/sbt-launch/$version/sbt-launch.jar" ;;
esac
}

readarr () {
Expand All @@ -107,15 +112,15 @@ readarr () {
}

init_default_option_file () {
local overriding_var=${!1}
local default_file=$2
if [[ ! -r "$default_file" && $overriding_var =~ ^@(.*)$ ]]; then
local envvar_file=${BASH_REMATCH[1]}
if [[ -r $envvar_file ]]; then
default_file=$envvar_file
local overriding_var="${!1}"
local default_file="$2"
if [[ ! -r "$default_file" && "$overriding_var" =~ ^@(.*)$ ]]; then
local envvar_file="${BASH_REMATCH[1]}"
if [[ -r "$envvar_file" ]]; then
default_file="$envvar_file"
fi
fi
echo $default_file
echo "$default_file"
}

declare -r cms_opts="-XX:+CMSClassUnloadingEnabled -XX:+UseConcMarkSweepGC"
Expand All @@ -127,13 +132,13 @@ declare -r latest_29="2.9.3"
declare -r latest_210="2.10.3"
declare -r latest_211="2.11.0-M5"

declare -r script_path=$(get_script_path "$BASH_SOURCE")
declare -r script_path="$(get_script_path "$BASH_SOURCE")"
declare -r script_name="${script_path##*/}"

# some non-read-onlies set with defaults
declare java_cmd=java
declare sbt_opts_file=$(init_default_option_file SBT_OPTS .sbtopts)
declare jvm_opts_file=$(init_default_option_file JVM_OPTS .jvmopts)
declare java_cmd="java"
declare sbt_opts_file="$(init_default_option_file SBT_OPTS .sbtopts)"
declare jvm_opts_file="$(init_default_option_file JVM_OPTS .jvmopts)"
declare sbt_launch_repo="http://typesafe.artifactoryonline.com/typesafe/ivy-releases"

# pull -J and -D options to give to java.
Expand All @@ -154,16 +159,16 @@ declare sbt_launch_dir="$HOME/.sbt/launchers"
[[ -w "$sbt_launch_dir" ]] || sbt_launch_dir="$(mktemp -d -t sbt_extras_launchers.XXXXXX)"

build_props_scala () {
if [[ -r $buildProps ]]; then
versionLine=$(grep ^build.scala.versions $buildProps)
versionString=${versionLine##build.scala.versions=}
if [[ -r "$buildProps" ]]; then
versionLine="$(grep '^build.scala.versions' "$buildProps")"
versionString="${versionLine##build.scala.versions=}"
echo "${versionString%% .*}"
fi
}

execRunner () {
# print the arguments one to a line, quoting any containing spaces
[[ $verbose || $debug ]] && echo "# Executing command line:" && {
[[ "$verbose" || "$debug" ]] && echo "# Executing command line:" && {
for arg; do
if [[ -n "$arg" ]]; then
if printf "%s\n" "$arg" | grep -q ' '; then
Expand All @@ -176,24 +181,18 @@ execRunner () {
echo ""
}

if [[ -n $batch ]]; then
if [[ -n "$batch" ]]; then
exec </dev/null
fi
exec "$@"
}

jar_url () {
case $1 in
0.13.*) make_url "$1" ;;
*) make_url "$sbt_release_version" ;;
esac
make_url "$1"
}

jar_file () {
case $1 in
0.13.*) echo "$sbt_launch_dir/$1/sbt-launch.jar" ;;
*) echo "$sbt_launch_dir/$sbt_release_version/sbt-launch.jar" ;;
esac
echo "$sbt_launch_dir/$1/sbt-launch.jar"
}

download_url () {
Expand Down Expand Up @@ -304,7 +303,7 @@ addDebugger () {
addJava "-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=$1"
}
setScalaVersion () {
[[ "$1" == *-SNAPSHOT ]] && addResolver 'Resolver.sonatypeRepo("snapshots")'
[[ "$1" == *"-SNAPSHOT" ]] && addResolver 'Resolver.sonatypeRepo("snapshots")'
addSbt "++ $1"
}

Expand All @@ -326,7 +325,7 @@ process_args ()
-d|-debug) debug=true && log_level=Debug && shift ;;
-q|-quiet) quiet=true && log_level=Error && shift ;;

-trace) require_arg integer "$1" "$2" && trace_level=$2 && shift 2 ;;
-trace) require_arg integer "$1" "$2" && trace_level="$2" && shift 2 ;;
-ivy) require_arg path "$1" "$2" && addJava "-Dsbt.ivy.home=$2" && shift 2 ;;
-no-colors) addJava "-Dsbt.log.noformat=true" && shift ;;
-no-share) noshare=true && shift ;;
Expand Down Expand Up @@ -354,10 +353,10 @@ process_args ()
-D*) addJava "$1" && shift ;;
-J*) addJava "${1:2}" && shift ;;
-S*) addScalac "${1:2}" && shift ;;
-28) setScalaVersion $latest_28 && shift ;;
-29) setScalaVersion $latest_29 && shift ;;
-210) setScalaVersion $latest_210 && shift ;;
-211) setScalaVersion $latest_211 && shift ;;
-28) setScalaVersion "$latest_28" && shift ;;
-29) setScalaVersion "$latest_29" && shift ;;
-210) setScalaVersion "$latest_210" && shift ;;
-211) setScalaVersion "$latest_211" && shift ;;

*) addResidual "$1" && shift ;;
esac
Expand All @@ -377,24 +376,28 @@ readConfigFile() {
if [[ -r "$sbt_opts_file" ]]; then
vlog "Using sbt options defined in file $sbt_opts_file"
readarr extra_sbt_opts < <(readConfigFile "$sbt_opts_file")
elif [[ -n "$SBT_OPTS" && ! ($SBT_OPTS =~ ^@.*) ]]; then
elif [[ -n "$SBT_OPTS" && ! ("$SBT_OPTS" =~ ^@.*) ]]; then
vlog "Using sbt options defined in variable \$SBT_OPTS"
extra_sbt_opts=( $SBT_OPTS )
else
vlog "No extra sbt options have been defined"
fi

[[ -n $extra_sbt_opts ]] && process_args "${extra_sbt_opts[@]}"
[[ -n "$extra_sbt_opts" ]] && process_args "${extra_sbt_opts[@]}"

# reset "$@" to the residual args
set -- "${residual_args[@]}"
argumentCount=$#

# only exists in 0.12+
setTraceLevel() {
case $(sbt_version) in
0.{7,10,11}.*) echoerr "Cannot set trace level in sbt version $(sbt_version)" ;;
*) addSbt "set every traceLevel := $trace_level" ;;
case "$(sbt_version)" in
"0.7."* | "0.10."* | "0.11."* )
echoerr "Cannot set trace level in sbt version $(sbt_version)"
;;
*)
addSbt "set every traceLevel := $trace_level"
;;
esac
}

Expand Down Expand Up @@ -434,22 +437,29 @@ EOM
exit 1
}

if [[ -n $noshare ]]; then
if [[ -n "$noshare" ]]; then
for opt in ${noshare_opts}; do
addJava "$opt"
done
else
[[ -n "$sbt_dir" ]] || {
sbt_dir=~/.sbt/$(sbt_version)
vlog "Using $sbt_dir as sbt dir, -sbt-dir to override."
}
# addJava "-Dsbt.global.base=$sbt_dir"
case "$(sbt_version)" in
"0.7."* | "0.10."* | "0.11."* | "0.12."* )
[[ -n "$sbt_dir" ]] || {
sbt_dir="$HOME/.sbt/$(sbt_version)"
vlog "Using $sbt_dir as sbt dir, -sbt-dir to override."
}
;;
esac

if [[ -n "$sbt_dir" ]]; then
addJava "-Dsbt.global.base=$sbt_dir"
fi
fi

if [[ -r "$jvm_opts_file" ]]; then
vlog "Using jvm options defined in file $jvm_opts_file"
readarr extra_jvm_opts < <(readConfigFile "$jvm_opts_file")
elif [[ -n "$JVM_OPTS" && ! ($JVM_OPTS =~ ^@.*) ]]; then
elif [[ -n "$JVM_OPTS" && ! ("$JVM_OPTS" =~ ^@.*) ]]; then
vlog "Using jvm options defined in \$JVM_OPTS variable"
extra_jvm_opts=( $JVM_OPTS )
else
Expand All @@ -458,15 +468,18 @@ else
fi

# traceLevel is 0.12+
[[ -n $trace_level ]] && setTraceLevel
[[ -n "$trace_level" ]] && setTraceLevel


if [[ -n "$log_level" ]] && [[ "$log_level" != Info ]]; then
sbt_commands=("set logLevel in Global := Level.$log_level" "${sbt_commands[@]}")
fi

[[ -n $log_level ]] && [[ $log_level != Info ]] && logLevalArg="set logLevel in Global := Level.$log_level"

# run sbt
execRunner "$java_cmd" \
"${extra_jvm_opts[@]}" \
"${java_args[@]}" \
-jar "$sbt_jar" \
"$logLevalArg" \
"${sbt_commands[@]}" \
"${residual_args[@]}"
18 changes: 18 additions & 0 deletions test/completion.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env bats

load test_helper

setup() {
create_project
create_launcher "${sbt_release_version}"
}

@test "loads .sbt_completion.sh" {
cd "${sbt_project}"
echo 'echo "${BASH_SOURCE[0]} has been loaded."' > .sbt_completion.sh
stub java true
run sbt
assert_success
assert_output ".sbt_completion.sh has been loaded."
unstub java
}
25 changes: 25 additions & 0 deletions test/create.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env bats

load test_helper

setup() {
create_launcher "${sbt_release_version}"
mkdir -p "${TMP}/newproject"
cd "${TMP}/newproject"
}

@test "fails to start sbt on empty project" {
assert [ ! -f "build.sbt" ]
assert [ ! -d "project" ]
run sbt
assert_failure
}

@test "starts sbt on empty project if -sbt-create was given" {
assert [ ! -f "build.sbt" ]
assert [ ! -d "project" ]
stub java true
run sbt -sbt-create
assert_success
unstub java
}
Loading