Add Drone CI
[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 [ -n "$USEDOCKER" ]; then
83       echo "Fire up the mysql docker"
84       DOCKER_CONTAINER_ID=$(docker run \
85               -e MYSQL_ROOT_PASSWORD=friendica \
86               -e MYSQL_USER="$DATABASEUSER" \
87               -e MYSQL_PASSWORD=friendica \
88               -e MYSQL_DATABASE="$DATABASENAME" \
89               -d mysql)
90       DATABASEHOST=$(docker inspect --format="{{.NetworkSettings.IPAddress}}" "$DOCKER_CONTAINER_ID")
91     else
92       if [ -z "$DRONE" ]; then  # no need to drop the DB when we are on CI
93         if [ "mysql" != "$(mysql --version | grep -o mysql)" ]; then
94           echo "Your mysql binary is not provided by mysql"
95           echo "To use the docker container set the USEDOCKER environment variable"
96           exit 3
97         fi
98         mysql -u "$DATABASEUSER" -pfriendica -e "DROP DATABASE IF EXISTS $DATABASENAME"
99         mysql -u "$DATABASEUSER" -pfriendica -e "CREATE DATABASE $DATABASENAME DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci"
100       else
101         DATABASEHOST=mysql
102       fi
103     fi
104
105     echo "Waiting for MySQL $DATABASEHOST initialization..."
106     if ! bin/wait-for-connection $DATABASEHOST 3306 300; then
107       echo "[ERROR] Waited 300 seconds, no response" >&2
108       exit 1
109     fi
110
111     if [ -n "$USEDOCKER" ]; then
112       echo "Initialize database..."
113       docker exec $DOCKER_CONTAINER_ID mysql -u root -pfriendica -e 'CREATE DATABASE IF NOT EXISTS $DATABASENAME;'
114     fi
115
116     export MYSQL_HOST="$DATABASEHOST"
117
118     #call installer
119     echo "Installing Friendica..."
120     "$PHP" ./bin/console.php autoinstall --dbuser="$DATABASEUSER" --dbpass=friendica --dbdata="$DATABASENAME" --dbhost="$DATABASEHOST" --url=https://friendica.local --admin=admin@friendica.local
121
122     #test execution
123     echo "Testing..."
124     rm -fr "coverage-html"
125     mkdir "coverage-html"
126     if [[ "$_XDEBUG_CONFIG" ]]; then
127       export XDEBUG_CONFIG=$_XDEBUG_CONFIG
128     fi
129
130     COVER=''
131     if [ -z "$NOCOVERAGE" ]; then
132       COVER="--coverage-clover autotest-clover.xml --coverage-html coverage-html"
133     else
134       echo "No coverage"
135     fi
136
137     INPUT="$BASEDIR/tests"
138     if [ -n "$1" ]; then
139       INPUT="$INPUT/$1"
140     fi
141
142     echo "${PHPUNIT[@]}" --configuration tests/phpunit.xml $COVER --log-junit "autotest-results.xml" "$INPUT" "$2"
143     "${PHPUNIT[@]}" --configuration tests/phpunit.xml $COVER --log-junit "autotest-results.xml" "$INPUT" "$2"
144     RESULT=$?
145
146     if [ -n "$DOCKER_CONTAINER_ID" ]; then
147       echo "Kill the docker $DOCKER_CONTAINER_ID"
148       docker stop $DOCKER_CONTAINER_ID
149       docker rm -f $DOCKER_CONTAINER_ID
150       unset $DOCKER_CONTAINER_ID
151     fi
152 }
153
154 #
155 # Start the test execution
156 #
157 if [ -n "$1" ] && [ ! -f "tests/$FILENAME" ] && [ "${FILENAME:0:2}" != "--" ]; then
158   execute_tests "$FILENAME" "$2"
159 else
160   execute_tests
161 fi