github.com/rigado/snapd@v2.42.5-go-mod+incompatible/run-checks (about)

     1  #!/bin/sh -eu
     2  
     3  if [ -n "${TRAVIS_BUILD_NUMBER:-}" ]; then
     4      echo travis_fold:start:env
     5      printenv | sort
     6      echo travis_fold:end:env
     7  fi
     8  
     9  export LANG=C.UTF-8
    10  export LANGUAGE=en
    11  
    12  if command -v goctest >/dev/null; then
    13      goctest="goctest"
    14  else
    15      goctest="go test"
    16  fi
    17  COVERMODE=${COVERMODE:-atomic}
    18  
    19  if [ -z "${TRAVIS_BUILD_ID:-}" ]; then
    20      # when *not* running inside travis, ensure we use go-1.10 by default
    21      export PATH=/usr/lib/go-1.10/bin:${PATH}
    22  fi
    23  
    24  # add workaround for https://github.com/golang/go/issues/24449
    25  if [ "$(uname -m)" = "s390x" ]; then
    26      if go version | grep -q go1.10; then
    27          echo "covermode 'atomic' crashes on s390x with go1.10, reseting "
    28          echo "to 'set'. see https://github.com/golang/go/issues/24449"
    29          COVERMODE="set"
    30      fi
    31  fi
    32  
    33  export GOPATH="${GOPATH:-$(realpath "$(dirname "$0")"/../../../../)}"
    34  export PATH="$PATH:${GOPATH%%:*}/bin"
    35  
    36  short=
    37  
    38  STATIC=
    39  UNIT=
    40  SPREAD=
    41  
    42  case "${1:-all}" in
    43      all)
    44          STATIC=1
    45          UNIT=1
    46          ;;
    47      --static)
    48          STATIC=1
    49          ;;
    50      --unit)
    51          UNIT=1
    52          ;;
    53      --short-unit)
    54          UNIT=1
    55          short=1
    56          ;;
    57      --spread)
    58          SPREAD=full
    59          ;;
    60      --spread-ubuntu)
    61          SPREAD=ubuntu-only
    62          ;;
    63      --spread-no-ubuntu)
    64          SPREAD=no-ubuntu
    65          ;;
    66      --spread-unstable)
    67          SPREAD=unstable
    68          ;;
    69      *)
    70          echo "Wrong flag ${1}. To run a single suite use --static, --unit, --spread."
    71          exit 1
    72  esac
    73  
    74  CURRENTTRAP="true"
    75  EXIT_CODE=99
    76  
    77  store_exit_code() {
    78      EXIT_CODE=$?
    79  }
    80  
    81  exit_with_exit_code() {
    82      exit $EXIT_CODE
    83  }
    84  
    85  addtrap() {
    86      CURRENTTRAP="$CURRENTTRAP ; $1"
    87      # shellcheck disable=SC2064
    88      trap "store_exit_code; $CURRENTTRAP ; exit_with_exit_code" EXIT
    89  }
    90  
    91  endmsg() {
    92      if [ $EXIT_CODE -eq 0 ]; then
    93          p="success.txt"
    94          m="All good, what could possibly go wrong."
    95      else
    96          p="failure.txt"
    97          m="Crushing failure and despair."
    98      fi
    99      echo
   100      if [ -t 1 ] && [ -z "$STATIC" ]; then
   101          cat "data/$p"
   102      else
   103          echo "$m"
   104      fi
   105  }
   106  addtrap endmsg
   107  
   108  # Append the coverage profile of a package to the project coverage.
   109  append_coverage() (
   110      profile="$1"
   111      if [ -f "$profile" ]; then
   112          grep -v "^mode:" -- "$profile" >> .coverage/coverage.out || true
   113          rm "$profile"
   114      fi
   115  )
   116  
   117  missing_interface_spread_test() {
   118      snap_yaml="tests/lib/snaps/test-snapd-policy-app-consumer/meta/snap.yaml"
   119      core_snap_yaml="tests/lib/snaps/test-snapd-policy-app-provider-core/meta/snap.yaml"
   120      classic_snap_yaml="tests/lib/snaps/test-snapd-policy-app-provider-classic/meta/snap.yaml"
   121      for iface in $(go run ./tests/lib/list-interfaces.go) ; do
   122          search="plugs: \\[ $iface \\]"
   123          case "$iface" in
   124              bool-file|gpio|hidraw|i2c|iio|serial-port|spi)
   125                  # skip gadget provided interfaces for now
   126                  continue
   127                  ;;
   128              dbus|content)
   129                  search="interface: $iface"
   130                  ;;
   131              autopilot)
   132                  search='plugs: \[ autopilot-introspection \]'
   133                  ;;
   134          esac
   135          if ! grep -q "$search" "$snap_yaml" ; then
   136              echo "Missing high-level test for interface '$iface'. Please add to:"
   137              echo "* $snap_yaml"
   138              echo "* $core_snap_yaml (if needed)"
   139              echo "* $classic_snap_yaml (if needed)"
   140              exit 1
   141          fi
   142      done
   143  }
   144  
   145  
   146  if [ "$STATIC" = 1 ]; then
   147      ./get-deps.sh
   148  
   149      # Run static tests.
   150      echo Checking docs
   151      ./mdlint.py ./*.md docs/*.md
   152  
   153      if [ -n "${TRAVIS_PULL_REQUEST:-}" ] && [ "${TRAVIS_PULL_REQUEST:-}" != "false" ]; then
   154          echo Checking pull request summary
   155          ./check-pr-title.py "$TRAVIS_PULL_REQUEST"
   156      fi
   157  
   158      echo Checking formatting
   159      fmt=""
   160      for dir in $(go list -f '{{.Dir}}' ./... | grep -v '/vendor/' ); do
   161          s="$(gofmt -s -l "$dir" | grep -v /vendor/ || true)"
   162          if [ -n "$s" ]; then
   163              fmt="$s\\n$fmt"
   164          fi
   165      done
   166  
   167      if [ -n "$fmt" ]; then
   168          echo "Formatting wrong in following files:"
   169          echo "$fmt" | sed -e 's/\\n/\n/g'
   170          if [ -z "${SKIP_GOFMT:-}" ]; then
   171              exit 1
   172          else
   173              echo "Ignoring gofmt errors as requested"
   174          fi
   175      fi
   176  
   177      # go vet
   178      echo Running vet
   179      go list ./... | grep -v '/vendor/' | xargs go vet
   180  
   181      echo 'Check for usages of http.Status*'
   182      got=""
   183      for dir in $(go list -f '{{.Dir}}' ./... | grep -v '/vendor/' ); do
   184          s="$(grep -nP 'http\.Status(?!Text)' "$dir"/*.go || true)"
   185          if [ -n "$s" ]; then
   186              got="$s\\n$got"
   187          fi
   188      done
   189  
   190      if [ -n "$got" ]; then
   191          echo 'Usages of http.Status*, we prefer the numeric values directly:'
   192          echo "$got"
   193          exit 1
   194      fi
   195  
   196      if command -v shellcheck >/dev/null; then
   197          echo Checking shell scripts...
   198          ( git ls-files -z 2>/dev/null ||
   199                  find . \( -name .git -o -name vendor \) -prune -o -print0 ) |
   200              xargs -0 file -N |
   201              awk -F": " '$2~/shell.script/{print $1}' |
   202              xargs shellcheck
   203          regexp='GOPATH(?!%%:\*)(?!:)[^= ]*/'
   204          if  grep -qPr                   --exclude HACKING.md --exclude 'Makefile.*' --exclude-dir .git --exclude-dir vendor "$regexp"; then
   205              echo "Using GOPATH as if it were a single entry and not a list:"
   206              grep -PHrn -C1 --color=auto --exclude HACKING.md --exclude 'Makefile.*' --exclude-dir .git --exclude-dir vendor "$regexp"
   207              echo "Use GOHOME, or {GOPATH%%:*}, instead."
   208              exit 1
   209          fi
   210          unset regexp
   211      fi
   212  
   213      echo Checking spelling errors
   214      if ! command -v misspell >/dev/null; then
   215          go get -u github.com/client9/misspell/cmd/misspell
   216      fi
   217      for file in *; do
   218          if [ "$file" = "vendor" ] || [ "$file" = "po" ]; then
   219              continue
   220          fi
   221          misspell -error -i auther,PROCES,PROCESSS,proces,processs,exportfs "$file"
   222      done
   223  
   224      echo Checking for ineffective assignments
   225      if ! command -v ineffassign >/dev/null; then
   226          go get -u github.com/gordonklaus/ineffassign
   227      fi
   228      # ineffassign knows about ignoring vendor/ \o/
   229      ineffassign .
   230  
   231      echo Checking for naked returns
   232      if ! command -v nakedret >/dev/null; then
   233          go get -u github.com/alexkohler/nakedret
   234      fi
   235      got=$(go list ./... | grep -v '/osutil/udev/' | grep -v '/vendor/' | xargs nakedret 2>&1)
   236      if [ -n "$got" ]; then
   237          echo "$got"
   238          exit 1
   239      fi
   240  
   241      echo Checking all interfaces have minimal spread test
   242      missing_interface_spread_test
   243  
   244      # FIXME: re-add staticcheck with a matching version for the used go-version
   245  fi
   246  
   247  if [ "$UNIT" = 1 ]; then
   248      ./get-deps.sh
   249  
   250      echo Building
   251      go build -v github.com/snapcore/snapd/...
   252  
   253      # tests
   254      echo Running tests from "$PWD"
   255      if [ "$short" = 1 ]; then
   256              # shellcheck disable=SC2046
   257              GOTRACEBACK=1 $goctest -short -v -timeout 5m $(go list ./... | grep -v '/vendor/' )
   258      else
   259          # Prepare the coverage output profile.
   260          rm -rf .coverage
   261          mkdir .coverage
   262          echo "mode: $COVERMODE" > .coverage/coverage.out
   263  
   264          if dpkg --compare-versions "$(go version | awk '$3 ~ /^go[0-9]/ {print substr($3, 3)}')" ge 1.10; then
   265              # shellcheck disable=SC2046
   266              GOTRACEBACK=1 $goctest -v -timeout 5m -coverprofile=.coverage/coverage.out -covermode="$COVERMODE" $(go list ./... | grep -v '/vendor/' )
   267          else
   268              for pkg in $(go list ./... | grep -v '/vendor/' ); do
   269                  GOTRACEBACK=1 go test -timeout 5m -i "$pkg"
   270                  GOTRACEBACK=1 $goctest -v -timeout 5m -coverprofile=.coverage/profile.out -covermode="$COVERMODE" "$pkg"
   271                  append_coverage .coverage/profile.out
   272              done
   273          fi
   274          # upload to codecov.io if on travis
   275          if [ "${TRAVIS_BUILD_NUMBER:-}" ]; then
   276              curl -s https://codecov.io/bash | bash /dev/stdin -f .coverage/coverage.out
   277          fi
   278      fi
   279  
   280      # python unit test for mountinfo-tool and version-tool
   281      command -v python2 && python2 ./tests/lib/bin/mountinfo-tool --run-unit-tests
   282      command -v python3 && python3 ./tests/lib/bin/mountinfo-tool --run-unit-tests
   283      command -v python2 && python2 ./tests/lib/bin/version-tool --run-unit-tests
   284      command -v python3 && python3 ./tests/lib/bin/version-tool --run-unit-tests
   285  fi
   286  
   287  if [ -n "$SPREAD" ]; then
   288      TMP_SPREAD="$(mktemp -d)"
   289      addtrap "rm -rf \"$TMP_SPREAD\""
   290  
   291      export PATH=$TMP_SPREAD:$PATH
   292      ( cd "$TMP_SPREAD" && curl -s -O https://niemeyer.s3.amazonaws.com/spread-amd64.tar.gz && tar xzvf spread-amd64.tar.gz )
   293  
   294      case "$SPREAD" in
   295          full)
   296              spread "google:"
   297              ;;
   298          ubuntu-only)
   299              spread "google:[u]...:tests/..."
   300              ;;
   301          no-ubuntu)
   302              spread "google:[^u]...:tests/..."
   303              ;;
   304          unstable)
   305              spread "google-unstable:"
   306              ;;
   307          *)
   308              echo "Spread parameter $SPREAD not supported"
   309              exit 1
   310      esac
   311  
   312      # cleanup the debian-ubuntu-14.04
   313      rm -rf debian-ubuntu-14.04
   314  fi
   315  
   316  UNCLEAN="$(git status -s|grep '^??')" || true
   317  SKIP_UNCLEAN=${SKIP_UNCLEAN=}
   318  if [ -n "$UNCLEAN" ] && [ -z "$SKIP_UNCLEAN" ]; then
   319      cat <<EOF
   320  
   321  There are files left in the git tree after the tests:
   322  
   323  $UNCLEAN
   324  EOF
   325      exit 1
   326  fi