-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests-funcs
72 lines (64 loc) · 1.74 KB
/
tests-funcs
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
# Created by: WestleyR
# Email: [email protected]
# Url: https://github.com/WestleyR/list-files
# Last modified date: 2020-08-20
#
# This file is licensed under the terms of
#
# The Clear BSD License
#
# Copyright (c) 2019-2020 WestleyR
# All rights reserved.
#
# This software is licensed under a Clear BSD License.
#
# usage:
# run-test <test-name> <test-command> <expect-to-contain-output> <also-expect-to-contain> <expect-exit-code>
#
run-test() {
test_name=$1
test_cmd=$2
expect_contain_out=$3
also_expect_contain_out=$4
expect_exit_code=$5
green='\033[0;32m'
red='\033[0;31m'
creset='\033[0m'
echo -n " Running: ${test_name} ... "
output=`/bin/sh -c "$test_cmd" 2>&1`
exit_code=$?
if [ $exit_code -ne $expect_exit_code ]; then
echo -e "${red}FAIL${creset} "
echo "expecting exit code: ${expect_exit_code}, got ${exit_code}"
echo "while running: ${test_cmd}"
echo "out: ${output}"
exit 1
fi
line_output=`echo "$output" | grep "$expect_contain_out"`
if [[ -n $expect_contain_out ]]; then
if [[ ! -n $line_output ]]; then
echo -e "${red}FAIL${creset} "
echo "expected outout to contain: ${expect_contain_out}, got: ${output}"
echo "while running: ${test_cmd}"
exit 1
fi
fi
if [[ -n $also_expect_contain_out ]]; then
if [[ ! -n $(echo "$line_output" | grep "$also_expect_contain_out") ]]; then
echo -e "${red}FAIL${creset} "
echo "expected outout to contain: ${also_expect_contain_out}, got: ${line_output}"
echo "while running: ${test_cmd}"
exit 1
fi
fi
echo -e "${green}PASS${creset}"
}
require_cmd() {
if ! command -v "$1"; then
echo -e "${red}WARNING:${creset} '$1' not found in PATH; skipping test"
exit 1
fi
}
#
# End tests-funcs
#