-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbash_tap.sh
114 lines (96 loc) · 3.39 KB
/
bash_tap.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env bash
# For more information, see https://github.com/wbsch/bash_tap
# Subject to the MIT License. See LICENSE file or http://opensource.org/licenses/MIT
# Copyright (c) 2015 Wilhelm Schürmann
function bashtap_on_error {
# A command in the parent script failed, interpret this as a test failure.
# $bashtap_line contains the last executed line, or an error.
echo -n "$bashtap_output"
echo "not ok 1 - ${bashtap_line}"
bashtap_clean_tmpdir
}
function bashtap_run_testcase {
# Run each line in the parent script up to the first "exit".
bashtap_output=""
while IFS= read -r bashtap_line && [ "${bashtap_line:0:4}" != "exit" ]; do
# Skip shebang.
if [ "${bashtap_line:0:2}" == "#!" ]; then
continue
fi
# Avoid recursively sourcing this script, and any helper scripts.
if [[ "$bashtap_line" =~ ^(\.|source).*(/|[[:blank:]])bash_tap[^/]*$ ]]; then
continue
fi
# Include comments as-is.
if [ "${bashtap_line:0:1}" == "#" ]; then
bashtap_output+="$bashtap_line"
bashtap_output+=$'\n'
continue
fi
# Run file line by line.
if [ ! -z "$bashtap_line" ] && [ "${bashtap_line:0:2}" != "#!" ]; then
bashtap_output+="# $ $bashtap_line"
bashtap_output+=$'\n'
local cmd_output
local cmd_ret
eval "$bashtap_line" &> bashtap_out_tmp
cmd_ret=$?
cmd_output="$(sed 's/^/# >>> /' < bashtap_out_tmp)"
if [ ! -z "$cmd_output" ]; then
bashtap_output+="$cmd_output"
bashtap_output+=$'\n'
fi
if [ "$cmd_ret" -ne 0 ]; then
exit $cmd_ret
fi
fi
done <"$bashtap_org_script"
}
function bashtap_clean_tmpdir {
if [ ! -z "$bashtap_tmpdir" ] && [ -d "$bashtap_tmpdir" ]; then
cd "$bashtap_org_pwd"
rm -rf "$bashtap_tmpdir"
fi
if [ -f bashtap_out_tmp ]; then
rm bashtap_out_tmp
fi
}
function bashtap_get_absolute_path {
# NOTE: No actual thought put into this. Might break. Horribly.
# Using this instead of readlink/realpath for OSX compatibility.
echo $(cd "$(dirname "$1")" && pwd)/$(basename "$1")
}
bashtap_org_pwd=$(pwd)
bashtap_org_script=$(bashtap_get_absolute_path "$0")
if [ "${0:(-2)}" == ".t" ] || [ "$1" == "-t" ]; then
# Make sure any failing commands are caught.
set -e
set -o pipefail
# TAP header. Hardcoded number of tests, 1.
echo "1..1"
# Output TAP failure on early exit.
trap bashtap_on_error EXIT
# The different calls to mktemp are necessary for OSX compatibility.
bashtap_tmpdir=$(mktemp -d 2>/dev/null || mktemp -d -t 'bash_tap')
if [ ! -z "$bashtap_tmpdir" ]; then
cd "$bashtap_tmpdir"
else
bashtap_line="Unable to create temporary directory."
exit 1
fi
# Scripts sourced before bash_tap.sh may declare this function.
if declare -f bashtap_setup >/dev/null; then
bashtap_setup
fi
# Run test file interpreting failing commands as a test failure.
bashtap_run_testcase && echo "ok 1"
# Since we're in a sourced file and just ran the parent script,
# exit without running it a second time.
trap - EXIT
bashtap_clean_tmpdir
exit
else
if declare -f bashtap_setup >/dev/null; then
bashtap_setup
fi
fi