-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun-tests
executable file
·121 lines (107 loc) · 3.31 KB
/
run-tests
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
115
116
117
118
119
120
121
#!/usr/bin/env bash
set -e
function usage() {
echo "Usage: $0 [-c] [-U <USERNAME>] [-h <HOST>] [-d <DBNAME>]"
echo ""
echo "Options:"
echo "-U <USERNAME>, --username <USERNAME>"
echo " pg user to connect to the db"
echo " May also be specified by setting the PGUSER env var"
echo " Default: postgres"
echo ""
echo "-h <HOST>, --host <HOST>"
echo " database host to connect to"
echo " May also be specified by setting the PGHOST env var"
echo " Required to be specified either as an option of env var"
echo ""
echo "-d <DBNAME>, --dbname <DBNAME>"
echo " name of the db to connect to"
echo " May also be specified by setting the PGDATABASE env var"
echo " Required to be specified either as an option of env var"
echo ""
echo "-c, --createdb"
echo " whether the script should attempt to create the db"
echo " If not set, it's assumed that the schema files will ensure that the db is created"
echo " Default: false"
echo ""
echo "--help"
echo " show help and exit successfully"
echo ""
}
data_dir=/tmp/tapestry-data
pghost=
pguser=
pgdatabase=
is_create_db=0
display_help=0
while [ "$1" != "" ]; do
case $1 in
-U | --username ) shift
pguser=$1
;;
-h | --host ) shift
pghost=$1
;;
-d | --dbname ) shift
pgdatabase=$1
;;
-c | --create-db ) is_create_db=1
;;
--help ) display_help=1
;;
esac
shift
done
if [ $display_help -eq 1 ]; then
usage
exit 0
fi
if [ -z "$pghost" ]; then
if [ -n "$PGHOST" ]; then
pghost=$PGHOST
else
echo "Either --host option or env var PGHOST must be set" >&2
exit 1
fi
fi
if [ -z "$pguser" ]; then
pguser=${PGUSER:-postgres}
fi
if [ -z "$pgdatabase" ]; then
if [ -n "$PGDATABASE" ]; then
pgdatabase=$PGDATABASE
else
echo "Either --dbname option or env var PGDATABASE must be set" >&2
exit 1
fi
fi
# Create the database if required
if [ $is_create_db -eq 1 ]; then
echo "==== Creating database '$pgdatabase' ===="
createdb $pgdatabase
fi
# Execute sql in all schema files in alphabetical order
echo "==== Executing commands in schema files ===="
schema_files=$(ls $data_dir/schema/*.sql | sort)
for f in $schema_files; do
if grep -iq "create database $pgdatabase;" $f; then
psql -h $pghost -U $pguser -f $f -q
else
psql -h $pghost -U $pguser -d $pgdatabase -f $f -q
fi
done
# Create pgtap extension
echo "==== Creating pgtap extension ===="
psql -h $pghost -U $pguser -d $pgdatabase -c "create extension pgtap;"
# Execute sql in fixture files (if any) in alphabetical order
if [ -d $data_dir/fixtures ]; then
echo "Executing commands in fixtures files"
fixture_files=$(ls $data_dir/fixtures/*.sql | sort)
for f in $fixture_files; do
psql -h $pghost -U $pguser -d $pgdatabase -f $f
done
fi
echo "==== Running pg_prove ===="
set -x
pg_prove -h $pghost -U $pguser -d $pgdatabase --verbose $data_dir/tests/*.sql
set +x