enable codecov
[friendica.git/.git] / autotest.sh
1 #!/usr/bin/env bash
2
3 DATABASENAME=${MYSQL_DATABASE:-test}
4 DATABASEUSER=${MYSQL_USERNAME:-friendica}
5 DATABASEHOST=${MYSQL_HOST:-localhost}
6 BASEDIR=$PWD
7
8 export MYSQL_DATABASE="$DATABASENAME"
9 export MYSQL_USERNAME="$DATABASEUSER"
10 export MYSQL_PASSWORD="friendica"
11
12 if [ -z "$PHP_EXE" ]; then
13   PHP_EXE=php
14 fi
15 PHP=$(which "$PHP_EXE")
16 # Use the Friendica internal composer
17 COMPOSER="$BASEDIR/bin/composer.phar"
18
19 set -e
20
21 _XDEBUG_CONFIG=$XDEBUG_CONFIG
22 unset XDEBUG_CONFIG
23
24 if [ -x "$PHP" ]; then
25   echo "Using PHP executable $PHP"
26 else
27   echo "Could not find PHP executable $PHP_EXE" >&2
28   exit 3
29 fi
30
31 echo "Installing depdendencies"
32 $PHP "$COMPOSER" install
33
34 PHPUNIT="$BASEDIR/vendor/bin/phpunit"
35
36 if [ -x "$PHPUNIT" ]; then
37   echo "Using PHPUnit executable $PHPUNIT"
38 else
39   echo "Could not find PHPUnit executable after composer $PHPUNIT" >&2
40   exit 3
41 fi
42
43 if ! [ \( -w config -a ! -f config/local.config.php \) -o \( -f config/local.config.php -a -w config/local.config.php \) ]; then
44         echo "Please enable write permissions on config and config/config.php" >&2
45         exit 1
46 fi
47
48 # Back up existing (dev) config if one exists and backup not already there
49 if [ -f config/local.config.php ] && [ ! -f config/local.config-autotest-backup.php ]; then
50   mv config/local.config.php config/local.config-autotest-backup.php
51 fi
52
53 function cleanup_config {
54
55     if [ -n "$DOCKER_CONTAINER_ID" ]; then
56       echo "Kill the docker $DOCKER_CONTAINER_ID"
57       docker stop "$DOCKER_CONTAINER_ID"
58       docker rm -f "$DOCKER_CONTAINER_ID"
59     fi
60
61     cd "$BASEDIR"
62
63     # Restore existing config
64     if [ -f config/local.config-autotest-backup.php ]; then
65       mv config/local.config-autotest-backup.php config/local.config.php
66     fi
67 }
68
69 # restore config on exit
70 trap cleanup_config EXIT
71
72 function execute_tests {
73     echo "Setup environment for MariaDB testing ..."
74     # back to root folder
75     cd "$BASEDIR"
76
77     # backup current config
78     if [ -f config/local.config.php ]; then
79       mv config/local.config.php config/local.config-autotest-backup.php
80     fi
81
82     if [ -z "$NOINSTALL" ]; then
83       if [ -n "$USEDOCKER" ]; then
84         echo "Fire up the mysql docker"
85         DOCKER_CONTAINER_ID=$(docker run \
86                 -e MYSQL_ROOT_PASSWORD=friendica \
87                 -e MYSQL_USER="$DATABASEUSER" \
88                 -e MYSQL_PASSWORD=friendica \
89                 -e MYSQL_DATABASE="$DATABASENAME" \
90                 -d mysql)
91         DATABASEHOST=$(docker inspect --format="{{.NetworkSettings.IPAddress}}" "$DOCKER_CONTAINER_ID")
92       else
93         if [ -z "$DRONE" ]; then  # no need to drop the DB when we are on CI
94           if [ "mysql" != "$(mysql --version | grep -o mysql)" ]; then
95             echo "Your mysql binary is not provided by mysql"
96             echo "To use the docker container set the USEDOCKER environment variable"
97             exit 3
98           fi
99           mysql -u "$DATABASEUSER" -pfriendica -e "DROP DATABASE IF EXISTS $DATABASENAME"
100           mysql -u "$DATABASEUSER" -pfriendica -e "CREATE DATABASE $DATABASENAME DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci"
101         else
102           DATABASEHOST=mysql
103         fi
104       fi
105
106       echo "Waiting for MySQL $DATABASEHOST initialization..."
107       if ! bin/wait-for-connection $DATABASEHOST 3306 300; then
108         echo "[ERROR] Waited 300 seconds, no response" >&2
109         exit 1
110       fi
111
112       if [ -n "$USEDOCKER" ]; then
113         echo "Initialize database..."
114         docker exec $DOCKER_CONTAINER_ID mysql -u root -pfriendica -e 'CREATE DATABASE IF NOT EXISTS $DATABASENAME;'
115       fi
116
117       export MYSQL_HOST="$DATABASEHOST"
118
119       #call installer
120       echo "Installing Friendica..."
121       "$PHP" ./bin/console.php autoinstall --dbuser="$DATABASEUSER" --dbpass=friendica --dbdata="$DATABASENAME" --dbhost="$DATABASEHOST" --url=https://friendica.local --admin=admin@friendica.local
122     fi
123
124     #test execution
125     echo "Testing..."
126     rm -fr "coverage-html"
127     mkdir "coverage-html"
128     if [[ "$_XDEBUG_CONFIG" ]]; then
129       export XDEBUG_CONFIG=$_XDEBUG_CONFIG
130     fi
131
132     COVER=''
133     if [ -z "$NOCOVERAGE" ]; then
134       COVER="--coverage-clover=tests/autotest-clover.xml --coverage-html=tests/coverage-html"
135     else
136       echo "No coverage"
137     fi
138
139     # per default, there is no cache installed
140     GROUP='--exclude-group=REDIS,MEMCACHE,MEMCACHED,APCU'
141     if [ "$TEST_SELECTION" == "REDIS" ]; then
142       GROUP="--group=REDIS"
143     fi
144     if [ "$TEST_SELECTION" == "MEMCACHE" ]; then
145       GROUP="--group=MEMCACHE"
146     fi
147     if [ "$TEST_SELECTION" == "MEMCACHED" ]; then
148       GROUP="--group=MEMCACHED"
149     fi
150     if [ "$TEST_SELECTION" == "APCU" ]; then
151       GROUP="--group=APCU"
152     fi
153     if [ "$TEST_SELECTION" == "NODB" ]; then
154       GROUP="--exclude-group=DB,SLOWDB"
155     fi
156
157     INPUT="$BASEDIR/tests"
158     if [ -n "$1" ]; then
159       INPUT="$INPUT/$1"
160     fi
161
162     echo "${PHPUNIT[@]}" --configuration tests/phpunit.xml "$GROUP" "$COVER" --log-junit "autotest-results.xml" "$INPUT" "$2"
163     "${PHPUNIT[@]}" --configuration tests/phpunit.xml "$GROUP" "$COVER" --log-junit "autotest-results.xml" "$INPUT" "$2"
164     RESULT=$?
165
166     if [ -n "$DOCKER_CONTAINER_ID" ]; then
167       echo "Kill the docker $DOCKER_CONTAINER_ID"
168       docker stop $DOCKER_CONTAINER_ID
169       docker rm -f $DOCKER_CONTAINER_ID
170       unset $DOCKER_CONTAINER_ID
171     fi
172 }
173
174 #
175 # Start the test execution
176 #
177 if [ -n "$1" ] && [ ! -f "tests/$FILENAME" ] && [ "${FILENAME:0:2}" != "--" ]; then
178   execute_tests "$FILENAME" "$2"
179 else
180   execute_tests
181 fi