-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathrun_sql_tests.sh
executable file
·69 lines (51 loc) · 1.68 KB
/
run_sql_tests.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
#!/bin/bash
set -o pipefail
set -o nounset
ROOT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )/../
function psql_input() {
cat<<EOF
-- Turn off echo and keep things quiet.
\unset ECHO
\set QUIET 1
-- Format the output for nice TAP.
\pset format unaligned
\pset tuples_only true
\pset pager off
begin;
create extension pgtap;
create schema tests;
EOF
if [ "$1" -eq 1 ]; then
cat<<EOF
create function tests.shutdown_exit_on_error()
returns void as \$\$ begin
ASSERT num_failed() = 0;
end
\$\$ language plpgsql;
EOF
fi
cat "${ROOT_DIR}"/supabase/tests/*.test.sql
cat<<EOF
\set QUIET 0
select * from runtests('tests'::name);
\set QUIET 1
drop extension pgtap;
rollback;
EOF
}
# Cause `psql` to return a non-zero exit code when there's a test failure
# by injecting a shutdown function that checks pgTAP for any errors and
# raises an exception if there is one. Combined with `ON_ERROR_STOP`, this
# will cause `psql` to error.
psql_input 1 | psql --set ON_ERROR_STOP=1 postgres://postgres:postgres@localhost:5432/postgres
test_exit_code=$?
# Normally when pgTAP detects a test failure, it records the failure and prints
# it nicely so you can see what happened. By throwing an exception above, we
# pre-empt that pretty printing in order to detect the failure. Now, if there is
# a failure, let's re-run the tests without the exception to get the pretty output.
if [ $test_exit_code -ne 0 ]; then
echo "There was a test failure, re-running to show meaningful output";
psql_input 0 | psql --set ON_ERROR_STOP=1 postgres://postgres:postgres@localhost:5432/postgres;
# Lastly, make sure to indicate that there was a failure so we can fail the CI run.
exit 1
fi