github.com/thiagoyeds/go-cloud@v0.26.0/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, 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  # start_local_deps.sh requires that Docker is installed,
    29  # which is only supported on Linux.
    30  # Tests that depend on them should check the RUNNER_OS environment before running.
    31  # Don't do this when running locally, as it's slow; user should do it.
    32  if [[ "${RUNNER_OS:-}" == "Linux" ]]; then
    33    echo
    34    echo "Starting local dependencies..."
    35    ./internal/testing/start_local_deps.sh
    36  else
    37    echo "Not starting local dependencies for ${RUNNER_OS:-}."
    38  fi
    39  
    40  result=0
    41  rootdir="$(pwd)"
    42  
    43  # Update the regexp below when upgrading to a
    44  # new Go version. Some checks below we only run
    45  # for the latest Go version.
    46  latest_go_version=0
    47  if [[ $(go version) == *go1\.17* ]]; then
    48    latest_go_version=1
    49  fi
    50  
    51  # Build the test-summary app, which is used inside the loop to summarize results
    52  # from Go tests.
    53  (cd internal/testing/test-summary && go build)
    54  while read -r path || [[ -n "$path" ]]; do
    55    echo
    56    echo "******************************"
    57    echo "* Running Go tests for module: $path"
    58    echo "******************************"
    59    echo
    60  
    61    # TODO(rvangent): Special case modules to skip for Windows. Perhaps
    62    # this should be data-driven by allmodules?
    63    # (https://github.com/google/go-cloud/issues/2111).
    64    if [[ "${RUNNER_OS:-}" == "Windows" ]] && [[ "$path" == "internal/website" ]]; then
    65      echo "  Skipping on Windows"
    66      continue
    67    fi
    68  
    69    gotestflags=("-json" "-race")
    70    testsummaryflags=("-progress")
    71  
    72    # Only do coverage for the Linux build because it is slow, and
    73    # codecov will only save the last one anyway.
    74    if [[ "${RUNNER_OS:-}" == "Linux" ]]; then
    75      gotestflags+=("-coverpkg=./..." "-coverprofile=$rootdir/modcoverage.out")
    76    fi
    77  
    78    # Previous versions of the "go" command may have
    79    # different opinions about what go.mod should look
    80    # like.
    81    if [[ $latest_go_version -eq 1 ]]; then
    82      gotestflags+=("-mod=readonly")
    83    fi
    84  
    85    # Run the tests.
    86    (cd "$path" && go test "${gotestflags[@]}" ./...) | ./internal/testing/test-summary/test-summary "${testsummaryflags[@]}" || result=1
    87    if [ -f modcoverage.out ] && [ $result -eq 0 ]; then
    88      cat modcoverage.out >> coverage.out
    89      rm modcoverage.out
    90    fi
    91  done < <( sed -e '/^#/d' -e '/^$/d' allmodules | awk '{print $1}' )
    92  # The above filters out comments and empty lines from allmodules and only takes
    93  # the first (whitespace-separated) field from each line.
    94  
    95  # Upload cumulative coverage data if we generated it.
    96  if [ -f coverage.out ] && [ $result -eq 0 ]; then
    97    # Filter out test packages.
    98    grep -v test coverage.out > coverage2.out
    99    mv coverage2.out coverage.out
   100    bash <(curl -s https://codecov.io/bash)
   101    rm coverage.out
   102  fi
   103  
   104  # The rest of these checks are not OS-specific, so we only run them for the
   105  # Linux build, or when running locally.
   106  if [[ "${RUNNER_OS:-linux}" != "Linux" ]]; then
   107    exit $result
   108  fi
   109  
   110  if [[ ${latest_go_version} -eq 1 ]]; then
   111    echo
   112    echo "************************"
   113    echo "* Checking go mod tidy"
   114    echo "************************"
   115    echo
   116    while read -r path || [[ -n "$path" ]]; do
   117      echo "Module: $path"
   118      ( cd "$path" && "$rootdir"/internal/testing/check_mod_tidy.sh && echo "  OK" ) || { echo "FAIL: please run ./internal/testing/gomodcleanup.sh" && result=1; }
   119    done < <( sed -e '/^#/d' -e '/^$/d' allmodules | awk '{print $1}' )
   120    # The above filters out comments and empty lines from allmodules and only takes
   121    # the first (whitespace-separated) field from each line.
   122  fi
   123  
   124  
   125  if [[ ${latest_go_version} -eq 1 ]]; then
   126    echo
   127    echo "**********************"
   128    echo "* Checking wire diff"
   129    echo "**********************"
   130    echo
   131    echo "Installing Wire..."
   132    go install github.com/google/wire/cmd/wire@latest
   133    echo
   134    while read -r path || [[ -n "$path" ]]; do
   135      echo "Module: $path"
   136      ( cd "$path" && wire diff ./... && echo "  OK" ) || { echo "FAIL: wire diff found diffs!" && result=1; }
   137    done < <( sed -e '/^#/d' -e '/^$/d' allmodules | awk '{print $1}' )
   138    # The above filters out comments and empty lines from allmodules and only takes
   139    # the first (whitespace-separated) field from each line.
   140  fi;
   141  
   142  if [[ ${latest_go_version} -eq 1 ]]; then
   143    echo
   144    echo "******************************"
   145    echo "* Doing non-module checks"
   146    echo "******************************"
   147    echo
   148    echo "Ensuring .go files are formatted with gofmt -s..."
   149    DIFF="$(gofmt -s -d .)"
   150    if [ -n "$DIFF" ]; then
   151      echo "FAIL: please run 'gofmt -s -w .' and commit the result"
   152      echo "$DIFF";
   153      exit 1;
   154    else
   155      echo "  OK"
   156    fi;
   157  fi;
   158  
   159  if [[ ${latest_go_version} -eq 1 ]]; then
   160    echo
   161    echo "Ensuring that there are no dependencies not listed in ./internal/testing/alldeps..."
   162    ( ./internal/testing/listdeps.sh | diff -u ./internal/testing/alldeps - && echo "  OK" ) || {
   163      echo "FAIL: dependencies changed; run: internal/testing/listdeps.sh > internal/testing/alldeps" && result=1
   164      # Module behavior may differ across versions.
   165      echo "using the most recent go version."
   166    }
   167  fi
   168  
   169  
   170  if [[ ${latest_go_version} -eq 1 ]]; then
   171    echo
   172    echo "Ensuring that any new packages have the corresponding entries in Hugo..."
   173    missing_packages="$(internal/website/listnewpkgs.sh)"
   174    if ! [[ -z "$missing_packages" ]]; then
   175      echo "FAIL: missing package meta tags for:" 1>&2
   176      echo "$missing_packages" 1>&2
   177      result=1
   178    else
   179      echo "  OK"
   180    fi
   181  
   182    echo
   183    echo "Ensuring that all examples used in Hugo match what's in source..."
   184    (internal/website/gatherexamples/run.sh | diff -u internal/website/data/examples.json - > /dev/null && echo "  OK") || {
   185      echo "FAIL: examples changed; run: internal/website/gatherexamples/run.sh > internal/website/data/examples.json"
   186      result=1
   187    }
   188  fi;
   189  
   190  
   191  # For pull requests, check if there are undeclared incompatible API changes.
   192  # Skip this if we're already going to fail since it is expensive.
   193  # CURRENTLY BROKEN
   194  # if [[ ${latest_go_version} -eq 1 ]] && [[ ${result} -eq 0 ]] && [[ ! -z "${GITHUB_HEAD_REF:-}" ]]; then
   195    # echo
   196    # ./internal/testing/check_api_change.sh || result=1;
   197  # fi
   198  
   199  
   200  echo
   201  if [[ ${result} -eq 0 ]]; then
   202    echo "SUCCESS!"
   203  else
   204    echo "FAILED; see above for more info."
   205  fi
   206  
   207  exit $result