-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.sh
More file actions
executable file
·79 lines (69 loc) · 3.27 KB
/
Copy pathtest.sh
File metadata and controls
executable file
·79 lines (69 loc) · 3.27 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
#!/bin/bash
#
# Tests for xsh-lib/xsql. Plain assertions, no external test framework.
#
# Usage:
# xsh load xsh-lib/core xsh-lib/xsql # one-time
# bash test.sh # or: zsh test.sh
#
# Make the `xsh` function available when run as a child process. Under bash it
# is inherited as an exported function (no-op here); zsh cannot export functions,
# so a child `zsh test.sh` sources ~/.xshrc to define xsh as a real zsh function.
if ! type xsh 2>/dev/null | grep -q 'function'; then
# shellcheck source=/dev/null
. ~/.xshrc
fi
# No `set -e`: query ends in a getopts/return path that can yield a non-zero
# status on success, which zsh's ERR_EXIT would trip inside `$()`. Tally
# failures explicitly instead. Array assertions use `[*]` (joins regardless of
# index base) so they're identical under bash (0-indexed) and zsh (1-indexed).
__fails=0
assert_eq () { # <desc> <expected> <actual>
if [ "$2" = "$3" ]; then printf 'ok - %s\n' "$1"
else printf 'FAIL - %s: expected [%s], got [%s]\n' "$1" "$2" "$3" >&2; __fails=$((__fails + 1)); fi
}
assert_rc_nonzero () { # <desc> <cmd...>
if "${@:2}" >/dev/null 2>&1; then
printf 'FAIL - %s: expected non-zero exit\n' "$1" >&2; __fails=$((__fails + 1))
else printf 'ok - %s\n' "$1"; fi
}
xsh log info "xsh list xsql/"
xsh list 'xsql/*' >/dev/null
xsh log info "import-smoke: all xsql function utilities"
while read -r __lpue; do
[ -z "$__lpue" ] && continue
xsh import "$__lpue" || { printf 'FAIL - import %s\n' "$__lpue" >&2; __fails=$((__fails + 1)); }
done < <(xsh list 'xsql/*' | awk '$1 == "[functions]" {print $2}')
printf 'ok - import-smoke\n'
# ----------------------------------------------------------------------------
# xsql/query/parser — parses a SQL expression into XSQL_QUERY_* globals
# (exercises the comma/space field-list split ported off bash-only `read -a`)
# ----------------------------------------------------------------------------
xsh log info "xsql/query/parser"
xsh xsql/query/parser select f1,f2 from A where f1 = x
assert_eq "parser TABLE" "A" "${XSQL_QUERY_TABLE}"
assert_eq "parser FIELDS" "f1 f2" "${XSQL_QUERY_SELECTED_FIELDS[*]}"
assert_eq "parser WHERE" "f1 = x" "${XSQL_QUERY_WHERE[*]}"
assert_rc_nonzero "parser errors without a FROM table" xsh xsql/query/parser select f1
# ----------------------------------------------------------------------------
# xsql/query — query a text-file "table" (exercises the ${!varname} array
# indirection ported to portable eval)
# ----------------------------------------------------------------------------
xsh log info "xsql/query"
__tbl=$(mktemp "${TMPDIR:-/tmp}/xsh-xsql-test.XXXXXXXX")
printf 'id name age\n1 alice 30\n2 bob 25\n' > "$__tbl"
assert_eq "query select+where (-O ,)" "name,age
bob,25" "$(xsh xsql/query -H -O , select name,age from "$__tbl" where id = 2 2>/dev/null || true)"
assert_eq "query select all rows (-O ,)" "alice
bob" "$(xsh xsql/query -O , select name from "$__tbl" 2>/dev/null || true)"
assert_eq "query multi-field row order" "bob,25" \
"$(xsh xsql/query -O , select name,age from "$__tbl" where name = bob 2>/dev/null || true)"
rm -f "$__tbl"
echo
if [ "$__fails" -eq 0 ]; then
xsh log info "xsql tests: all passed"
exit 0
else
xsh log error "xsql tests: ${__fails} failure(s)"
exit 1
fi