github.com/safing/portbase@v0.19.5/test (about) 1 #!/bin/bash 2 3 warnings=0 4 errors=0 5 scripted=0 6 goUp="\\e[1A" 7 fullTestFlags="-short" 8 install=0 9 testonly=0 10 11 function help { 12 echo "usage: $0 [command] [options]" 13 echo "" 14 echo "commands:" 15 echo " <none> run baseline tests" 16 echo " full run full tests (ie. not short)" 17 echo " install install deps for running tests" 18 echo "" 19 echo "options:" 20 echo " --scripted don't jump console lines (still use colors)" 21 echo " --test-only run tests only, no linters" 22 echo " [package] run only on this package" 23 } 24 25 function run { 26 if [[ $scripted -eq 0 ]]; then 27 echo "[......] $*" 28 fi 29 30 # create tmpfile 31 tmpfile=$(mktemp) 32 # execute 33 $* >$tmpfile 2>&1 34 rc=$? 35 output=$(cat $tmpfile) 36 37 # check return code 38 if [[ $rc -eq 0 ]]; then 39 if [[ $output == *"[no test files]"* ]]; then 40 echo -e "${goUp}[\e[01;33mNOTEST\e[00m] $*" 41 warnings=$((warnings+1)) 42 else 43 echo -ne "${goUp}[\e[01;32m OK \e[00m] " 44 if [[ $2 == "test" ]]; then 45 echo -n $* 46 echo -n ": " 47 echo $output | cut -f "3-" -d " " 48 else 49 echo $* 50 fi 51 fi 52 else 53 if [[ $output == *"build constraints exclude all Go files"* ]]; then 54 echo -e "${goUp}[ !=OS ] $*" 55 else 56 echo -e "${goUp}[\e[01;31m FAIL \e[00m] $*" 57 cat $tmpfile 58 errors=$((errors+1)) 59 fi 60 fi 61 62 rm -f $tmpfile 63 } 64 65 # get and switch to script dir 66 baseDir="$( cd "$(dirname "$0")" && pwd )" 67 cd "$baseDir" 68 69 # args 70 while true; do 71 case "$1" in 72 "-h"|"help"|"--help") 73 help 74 exit 0 75 ;; 76 "--scripted") 77 scripted=1 78 goUp="" 79 shift 1 80 ;; 81 "--test-only") 82 testonly=1 83 shift 1 84 ;; 85 "install") 86 install=1 87 shift 1 88 ;; 89 "full") 90 fullTestFlags="" 91 shift 1 92 ;; 93 *) 94 break 95 ;; 96 esac 97 done 98 99 # check if $GOPATH/bin is in $PATH 100 if [[ $PATH != *"$GOPATH/bin"* ]]; then 101 export PATH=$GOPATH/bin:$PATH 102 fi 103 104 # install 105 if [[ $install -eq 1 ]]; then 106 echo "installing dependencies..." 107 # TODO: update golangci-lint version regularly 108 echo "$ curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.44.0" 109 curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.44.0 110 exit 0 111 fi 112 113 # check dependencies 114 if [[ $(which go) == "" ]]; then 115 echo "go command not found" 116 exit 1 117 fi 118 if [[ $testonly -eq 0 ]]; then 119 if [[ $(which gofmt) == "" ]]; then 120 echo "gofmt command not found" 121 exit 1 122 fi 123 if [[ $(which golangci-lint) == "" ]]; then 124 echo "golangci-lint command not found" 125 echo "install with: curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin vX.Y.Z" 126 echo "don't forget to specify the version you want" 127 echo "or run: ./test install" 128 echo "" 129 echo "alternatively, install the current dev version with: go get -u github.com/golangci/golangci-lint/cmd/golangci-lint" 130 exit 1 131 fi 132 fi 133 134 # target selection 135 if [[ "$1" == "" ]]; then 136 # get all packages 137 packages=$(go list -e ./...) 138 else 139 # single package testing 140 packages=$(go list -e)/$1 141 echo "note: only running tests for package $packages" 142 fi 143 144 # platform info 145 platformInfo=$(go env GOOS GOARCH) 146 echo "running tests for ${platformInfo//$'\n'/ }:" 147 148 # run vet/test on packages 149 for package in $packages; do 150 packagename=${package#github.com/safing/portbase} #TODO: could be queried with `go list .` 151 packagename=${packagename#/} 152 echo "" 153 echo $package 154 if [[ $testonly -eq 0 ]]; then 155 run go vet $package 156 run golangci-lint run $packagename 157 fi 158 run go test -cover $fullTestFlags $package 159 done 160 161 echo "" 162 if [[ $errors -gt 0 ]]; then 163 echo "failed with $errors errors and $warnings warnings" 164 exit 1 165 else 166 echo "succeeded with $warnings warnings" 167 exit 0 168 fi