-
Notifications
You must be signed in to change notification settings - Fork 4
/
extra-assertions.sh
90 lines (78 loc) · 2.46 KB
/
extra-assertions.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
#!/usr/bin/env bash
shopt -s extglob
# @param $1 error message
# @param $2 globby pattern
# @param $3 text
function assertPatternEquals {
# failNotEquals is defined in shunit
# shellcheck disable=2053
[[ $3 == $2 ]] || failNotEquals "$1" "$2" "$3"
}
# @param $1 error message
# @param $2 text which the file at the URL should equal
# @param $3 URL
function assertURLEquals {
local curl_output
curl_output=$(curl -s "$3")
assertEquals "$1" "$2" "$curl_output"
}
# @param $1 name of a plugin
function assertGetInfoWorks {
local out exit_code
out=$("$1" get_info)
exit_code=$?
assertEquals "$1 get_info exit code" 0 "$?"
assertPatternEquals "$1 get_info has [name]" '*[name]*' "$out"
}
# internal helper
# @param $1 label of the link to retrieve
# @param $2 anypaste stdout (optional, defaults to $ap_t_upload_stdout)
# @return stdout the link in its full glory
function get_output_link {
local grep_output anypaste_stdout
[[ -n $2 ]] && anypaste_stdout=$2 || anypaste_stdout=$ap_t_upload_stdout
# TODO: BASH_REMATCH instead
grep_output=$(grep "^$1: " <<< "$anypaste_stdout")
echo "${grep_output#*: }"
exit
}
# @param $1 error message
# @param $2 file path
# @param $3 anypaste stdout (optional, defaults to $ap_t_upload_stdout)
function assertDirectLinkWorks {
local file_content direct_link
direct_link=$(get_output_link 'Direct' "$3")
assertNotNull 'direct link was outputted' "$direct_link"
file_content=$(<"$2")
assertURLEquals "$1" "$file_content" "$direct_link"
}
# @param $1 anypaste stdout
function assertURLIsAnypaste {
assertDirectLinkWorks 'URL contains Anypaste source code' './anypaste' "$1"
}
# This checks that a plugin is able to upload a certain file, then uploads it.
# it also checks get_info is correct for that plugin.
# @param $1 name of a plugin
# @param $2 file to upload
# @return $ap_t_upload_stdout stdout of the upload for further assertions
function uploadAndAssert {
local out exit_code file_content out_link
assertGetInfoWorks "$1"
ap_rel_to_abs "$2"
# shellcheck disable=2034
# shellcheck disable=2154
ap_path=$ap_rel_to_abs_return
ap_collect_file_metadata "$2"
"$1" check_eligibility
exit_code=$?
assertEquals "$1 check_eligibility exits ok" 0 "$exit_code"
ap_t_upload_stdout=$("$1" upload)
}
# @param $1 error message
# @param $2 the label we are looking at
# @param $3 the pattern to match
function assertLabelPatternEquals {
local out_link
out_link=$(get_output_link "$2" "$ap_t_upload_stdout")
assertPatternEquals "$1" "$3" "$out_link"
}