-
Notifications
You must be signed in to change notification settings - Fork 1
/
test
executable file
·113 lines (92 loc) · 2.3 KB
/
test
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
#!/usr/bin/env bash
#
# Unitary tests for Ephemeral
#
# @author: Daniel Souza <me at posix dot dev dot br>
# @license: GPLv3
# Utils
# -----
reset='\e[0;0m'
italic='\e[3m'
blink='\e[5m'
red='\e[1;31m'
green='\e[1;32m'
# Lib
# ---
# Diff two strings
_diff() {
# use delta if available
if command -v delta &>/dev/null; then
delta <(echo "$expected") <(echo "$observed")
else
# FIXME: Working on shell but not on script (maybe zsh only)
# diff --color=always -d <(<<< "$expected") <(<<< "$observed")
diff --color=always -d <(echo "$expected") <(echo "$observed")
fi
}
# Print a report of the test
_report() {
local status="$1"
if [ "$status" -eq 0 ]; then
hits+=1
printf "[${green}PASS${reset}]: %s\n" "'$cmd'"
elif [ "$status" -eq 1 ]; then
misses+=1
printf "[${red}FAIL${reset}]: %s\n" "'$cmd'"
_diff
else
printf "${red}ERROR: Wrong status code for %s.${reset}\n" "$cmd"
fi
}
# Check if observations pass expectations
_test() {
local expected observed
observed="$($cmd)" # read stdout of command to variable
expected="$(< "testing/expected/${name}.txt")" # read contents of file to variable
if [ "$observed" = "$expected" ]; then
_report 0
else
_report 1
mkdir -p "testing/debug"
echo "$observed" > "testing/debug/${name}.txt"
fi
}
# Tests
# -----
# Create testing environment
_populate() {
local _program_dir="$EPHEMERAL_SRC/.config/program"
rm -rf "testing/src"
rm -rf "testing/dst"
mkdir -p "$_program_dir"
mkdir "$_program_dir/blob_storage/"
mkdir "$_program_dir/Cache/"
mkdir "$_program_dir/Code Cache/"
mkdir "$_program_dir/donotdelthisdir/"
touch "$_program_dir/donotdelthisfile"
}
_testEphemeral() {
local name="./ephemeral"
local cmd="./ephemeral 1>/dev/null && ls $EPHEMERAL_DST"
_test
}
# Main
# ----
# check if in the desired dir
[ -a "ephemeral" ] || exit 1
export EPHEMERAL_SRC="testing/src" EPHEMERAL_DST="testing/dst"
_populate
declare -i hits misses total
hits=0
misses=0
total=0
printf "${italic}%s${blink}%s${reset}\n\n" "Running tests" "..."
_testEphemeral
# Show summary
total="$(( hits + misses ))"
printf "\nHIT: ${green}%i${reset} of %i\n" "$hits" "$total"
printf "MISS: ${red}%i${reset} of %i\n" "$misses" "$total"
# Exit w/ error if there are any misses
if [ $misses -gt 0 ]; then
exit 1
fi