goyave.dev/goyave/v4@v4.4.11/run_test.sh (about) 1 #!/bin/bash 2 3 # This script DOESN'T remove the container when completed to avoid needing to 4 # setup a brand new database for every run, as the first initialization takes a 5 # long time. 6 7 # This script requires: 8 # - docker 9 # - gotest: https://github.com/rakyll/gotest 10 # - gcc (for race detection, strongly recommended but optional) 11 12 container=goyave-mariadb 13 14 echo -e "\033[1mStarting database containers...\033[0m" 15 if [ ! "$(docker ps -a | grep $container)" ]; then 16 docker run --name $container -p 3306:3306 -e MYSQL_ROOT_PASSWORD=secret -e MYSQL_USER=goyave -e MYSQL_PASSWORD=secret -e MYSQL_DATABASE=goyave -d mariadb:latest >/dev/null 17 else 18 docker start $container >/dev/null 19 fi 20 21 if [ $? -ne 0 ]; then 22 echo -e "\033[31mError: couldn't start database container.\033[0m" 23 exit $? 24 fi 25 26 health=1 27 tries=0 28 while [ $health -ne 0 ]; do 29 docker exec -it $container mysqladmin status -u goyave --password=secret >/dev/null 2>&1 30 health=$? 31 ((tries++)) 32 if [ $tries -gt 100 ]; then 33 docker stop $container 34 echo -e "\033[31mError: couldn't connect to container database after 100 retries.\033[0m" 35 exit 2 36 fi 37 echo -e "\033[33mCouldn't connect to container database. Retrying in 5 seconds...\033[0m" 38 sleep 5 39 done 40 41 echo -e "\033[92m\033[1mDatabase ready. Running tests...\033[0m" 42 gcc --version >/dev/null 2>&1 43 if [ $? -ne 0 ]; then 44 echo -e "\033[33mgcc is missing. Running tests without data race checking.\033[0m" 45 gotest -v -p 1 -coverprofile=c.out -coverpkg=./... ./... ; go tool cover -html=c.out -o=coverage.html ; go tool cover -func=c.out | grep total ; rm c.out 46 else 47 gotest -v -p 1 -race -coverprofile=c.out -coverpkg=./... ./... ; go tool cover -html=c.out -o=coverage.html ; go tool cover -func=c.out | grep total ; rm c.out 48 fi 49 test_result=$? 50 51 echo -e "\033[1mStopping database container...\033[0m" 52 docker stop $container >/dev/null 53 54 exit $test_result