github.com/cornelk/go-cloud@v0.17.1/internal/testing/runchecks.sh (about)

     1  #!/usr/bin/env bash
     2  # Copyright 2018 The Go Cloud Development Kit Authors
     3  #
     4  # Licensed under the Apache License, Version 2.0 (the "License");
     5  # you may not use this file except in compliance with the License.
     6  # You may obtain a copy of the License at
     7  #
     8  #     https://www.apache.org/licenses/LICENSE-2.0
     9  #
    10  # Unless required by applicable law or agreed to in writing, software
    11  # distributed under the License is distributed on an "AS IS" BASIS,
    12  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  # See the License for the specific language governing permissions and
    14  # limitations under the License.
    15  
    16  # This script runs all checks for Go CDK on Travis, including go test suites,
    17  # compatibility checks, consistency checks, Wire, etc.
    18  
    19  # https://coderwall.com/p/fkfaqq/safer-bash-scripts-with-set-euxo-pipefail
    20  # Change to -euxo if debugging.
    21  set -euo pipefail
    22  
    23  if [[ $# -gt 0 ]]; then
    24    echo "usage: runchecks.sh" 1>&2
    25    exit 64
    26  fi
    27  
    28  # The following logic lets us skip the (lengthy) installation process and tests
    29  # in some cases where the PR carries trivial changes that don't affect the code
    30  # (such as documentation-only).
    31  if [[ ! -z "${TRAVIS_BRANCH:-}" ]] && [[ ! -z "${TRAVIS_PULL_REQUEST_SHA:-}" ]]; then
    32    tmpfile=$(mktemp)
    33    function cleanup() {
    34      rm -rf "$tmpfile"
    35    }
    36    trap cleanup EXIT
    37  
    38    mergebase="$(git merge-base -- "$TRAVIS_BRANCH" "$TRAVIS_PULL_REQUEST_SHA")"
    39    if [[ -z $mergebase ]]; then
    40      echo "merge-base empty. Please ensure that the PR is mergeable."
    41      exit 1
    42    fi
    43    git diff --name-only "$mergebase" "$TRAVIS_PULL_REQUEST_SHA" -- > "$tmpfile"
    44  
    45    # Find out if the diff has any files that are neither:
    46    #
    47    # * in internal/website, nor
    48    # * end with .md
    49    #
    50    # If there are no such files, grep returns 1 and we don't have to run the
    51    # tests.
    52    echo "The following files changed:"
    53    cat "$tmpfile"
    54    if grep -vP "(^internal/website|.md$)" "$tmpfile"; then
    55      echo "--> Found some non-trivial changes, running tests"
    56    else
    57      echo "--> Diff doesn't affect tests; not running them"
    58      exit 0
    59    fi
    60  fi
    61  
    62  
    63  # start_local_deps.sh requires that Docker is installed, via Travis services,
    64  # which are only supported on Linux.
    65  # Tests that depend on them should check the Travis environment before running.
    66  # Don't do this when running locally, as it's slow; user should do it.
    67  if [[ "${TRAVIS_OS_NAME:-}" == "linux" ]]; then
    68    echo
    69    echo "Starting local dependencies..."
    70    ./internal/testing/start_local_deps.sh
    71    echo
    72    echo "Installing Wire..."
    73    go install -mod=readonly github.com/google/wire/cmd/wire
    74  fi
    75  
    76  result=0
    77  rootdir="$(pwd)"
    78  
    79  # Build the test-summary app, which is used inside the loop to summarize results
    80  # from Go tests.
    81  (cd internal/testing/test-summary && go build)
    82  while read -r path || [[ -n "$path" ]]; do
    83    echo
    84    echo "******************************"
    85    echo "* Running Go tests for module: $path"
    86    echo "******************************"
    87    echo
    88  
    89    # TODO(rvangent): Special case modules to skip for Windows. Perhaps
    90    # this should be data-driven by allmodules?
    91    # (https://github.com/google/go-cloud/issues/2111).
    92    if [[ "${TRAVIS_OS_NAME:-}" == "windows" ]] && ([[ "$path" == "internal/contributebot" ]] || [[ "$path" == "internal/website" ]]); then
    93      echo "  Skipping on Windows"
    94      continue
    95    fi
    96  
    97    gotestflags=("-mod=readonly" "-json" "-race")
    98    testsummaryflags=("-progress")
    99  
   100    # Only do coverage for the Linux build on Travis because it is slow, and
   101    # codecov will only save the last one anyway.
   102    if [[ "${TRAVIS_OS_NAME:-}" == "linux" ]]; then
   103      gotestflags+=("-coverpkg=./..." "-coverprofile=$rootdir/modcoverage.out")
   104    fi
   105  
   106    # Run the tests.
   107    (cd "$path" && go test "${gotestflags[@]}" ./...) | ./internal/testing/test-summary/test-summary "${testsummaryflags[@]}" || result=1
   108    if [ -f modcoverage.out ] && [ $result -eq 0 ]; then
   109      cat modcoverage.out >> coverage.out
   110      rm modcoverage.out
   111    fi
   112  done < <( sed -e '/^#/d' -e '/^$/d' allmodules | awk '{print $1}' )
   113  # The above filters out comments and empty lines from allmodules and only takes
   114  # the first (whitespace-separated) field from each line.
   115  
   116  # Upload cumulative coverage data if we generated it.
   117  if [ -f coverage.out ] && [ $result -eq 0 ]; then
   118    # Filter out test packages.
   119    grep -v test coverage.out > coverage2.out
   120    mv coverage2.out coverage.out
   121    bash <(curl -s https://codecov.io/bash)
   122    rm coverage.out
   123  fi
   124  
   125  # The rest of these checks are not OS-specific, so we only run them for the
   126  # Linux build on Travis, or when running locally.
   127  if [[ "${TRAVIS_OS_NAME:-linux}" != "linux" ]]; then
   128    exit $result
   129  fi
   130  
   131  
   132  echo
   133  echo "************************"
   134  echo "* Checking go mod tidy"
   135  echo "************************"
   136  echo
   137  while read -r path || [[ -n "$path" ]]; do
   138    echo "Module: $path"
   139    ( cd "$path" && "$rootdir"/internal/testing/check_mod_tidy.sh && echo "  OK" ) || { echo "FAIL: please run ./internal/testing/gomodcleanup.sh" && result=1; }
   140  done < <( sed -e '/^#/d' -e '/^$/d' allmodules | awk '{print $1}' )
   141  # The above filters out comments and empty lines from allmodules and only takes
   142  # the first (whitespace-separated) field from each line.
   143  
   144  
   145  echo
   146  echo "**********************"
   147  echo "* Checking wire diff"
   148  echo "**********************"
   149  echo
   150  while read -r path || [[ -n "$path" ]]; do
   151    echo "Module: $path"
   152    ( cd "$path" && wire diff ./... && echo "  OK" ) || { echo "FAIL: wire diff found diffs!" && result=1; }
   153  done < <( sed -e '/^#/d' -e '/^$/d' allmodules | awk '{print $1}' )
   154  # The above filters out comments and empty lines from allmodules and only takes
   155  # the first (whitespace-separated) field from each line.
   156  
   157  
   158  echo
   159  echo "******************************"
   160  echo "* Doing non-module checks"
   161  echo "******************************"
   162  echo
   163  echo "Ensuring .go files are formatted with gofmt -s..."
   164  DIFF="$(gofmt -s -d .)"
   165  if [ -n "$DIFF" ]; then
   166    echo "FAIL: please run 'gofmt -s -w .' and commit the result"
   167    echo "$DIFF";
   168    exit 1;
   169  else
   170    echo "  OK"
   171  fi;
   172  
   173  
   174  if [[ $(go version) == *go1\.13* ]]; then
   175    echo
   176    echo "Ensuring that there are no dependencies not listed in ./internal/testing/alldeps..."
   177    ( ./internal/testing/listdeps.sh | diff -u ./internal/testing/alldeps - && echo "  OK" ) || {
   178      echo "FAIL: dependencies changed; run: internal/testing/listdeps.sh > internal/testing/alldeps" && result=1
   179      # Module behavior may differ across versions.
   180      echo "using go version 1.13."
   181    }
   182  fi
   183  
   184  
   185  echo
   186  echo "Ensuring that any new packages have the corresponding entries in Hugo..."
   187  missing_packages="$(internal/website/listnewpkgs.sh)"
   188  if ! [[ -z "$missing_packages" ]]; then
   189    echo "FAIL: missing package meta tags for:" 1>&2
   190    echo "$missing_packages" 1>&2
   191    result=1
   192  else
   193    echo "  OK"
   194  fi
   195  
   196  
   197  echo
   198  echo "Ensuring that all examples used in Hugo match what's in source..."
   199  (internal/website/gatherexamples/run.sh | diff -u internal/website/data/examples.json - > /dev/null && echo "  OK") || {
   200    echo "FAIL: examples changed; run: internal/website/gatherexamples/run.sh > internal/website/data/examples.json"
   201    result=1
   202  }
   203  
   204  
   205  # For pull requests, check if there are undeclared incompatible API changes.
   206  # Skip this if we're already going to fail since it is expensive.
   207  if [[ ${result} -eq 0 ]] && [[ ! -z "${TRAVIS_BRANCH:-}" ]] && [[ ! -z "${TRAVIS_PULL_REQUEST_SHA:-}" ]]; then
   208    echo
   209    ./internal/testing/check_api_change.sh || result=1;
   210  fi
   211  
   212  
   213  echo
   214  if [[ ${result} -eq 0 ]]; then
   215    echo "SUCCESS!"
   216  else
   217    echo "FAILED; see above for more info."
   218  fi
   219  
   220  exit $result