forked from ztombol/bats-assert
-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathassert_json.bash
More file actions
114 lines (107 loc) · 2.88 KB
/
assert_json.bash
File metadata and controls
114 lines (107 loc) · 2.88 KB
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
# assert_json
# ======
#
# Summary: Fail if the given JSON does not match the provided jq expressions.
#
# See jq -e for details on how jq expressions are evaluated.
#
# Usage: assert_json <json> <jq_expression> [<jq_expression> ...]
#
# Examples:
# assert_json "$json" '.name == "TestProject"' '.version == "1.0.0"'
# assert_json "$json" '.policyConditions | length == 1'
#
# # In a Bats test, using output from run command:
# assert_json_output '.name == "TestProject"' '.version == "1.0.0"'
#
# # For arrays:
# assert_json "$json" '[.[] | .name] | sort == ["A", "B"]'
#
# # For nested objects:
# assert_json "$json" '.parent.uuid == "parent-id"'
#
# IO:
# STDERR - the failed expression, on failure
# Globals:
# none
# Returns:
# 0 - if the JSON matches all expressions
# 1 - otherwise
#
assert_json() {
local json="$1" expression output
shift
for expression in "$@"
do
if ! output=$(echo "$json" | jq -e "$expression" 2>&1)
then
batslib_print_kv_single_or_multi 8 \
'expression' "$expression" \
'json' "$json" \
'output' "$output" \
| batslib_decorate 'json does not match' \
| fail
fi
done
}
# refute_json
# ========
#
# Summary: Fail if the given JSON matches any of the provided jq expressions.
#
# See jq -e for details on how jq expressions are evaluated.
#
# Usage: refute_json <json> <jq_expression> [<jq_expression> ...]
#
# Examples:
# refute_json "$json" '.name == "NonExistentProject"'
# refute_json "$json" '.policyConditions[]? | select(.uuid == "deleted-id")'
#
# # In a Bats test, using output from run command:
# refute_json_output '.name == "NonExistentProject"'
#
# IO:
# STDERR - the failed expression, on failure
# Globals:
# none
# Returns:
# 0 - if the JSON does not match any of the expressions
# 1 - otherwise, also if there is a usage or compile error in jq
#
refute_json() {
local json="$1" expression output status
shift
for expression in "$@"
do
{
output=$(echo "$json" | jq -e "$expression" 2>&1)
status=$?
} || true
if (( status == 2 || status == 3 )); then
batslib_print_kv_single_or_multi 8 \
'expression' "$expression" \
'json' "$json" \
'output' "$output" \
| batslib_decorate 'jq encountered a usage or compile error' \
| fail
fi
if (( status == 0 )); then
batslib_print_kv_single_or_multi 8 \
'expression' "$expression" \
'json' "$json" \
'output' "$output" \
| batslib_decorate 'json matches' \
| fail
fi
done
}
# Usage: assert_json_output <jq_expression> [<jq_expression> ...]
# Example: assert_json_output '.name == "TestProject"'
assert_json_output() {
assert_json "${output?}" "$@"
}
# Usage: refute_json_output <jq_expression> [<jq_expression> ...]
# Example: refute_json_output '.name == "NonExistentProject"'
refute_json_output() {
refute_json "${output?}" "$@"
}