github.com/yorinasub17/go-cloud@v0.27.40/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\.19* ]]; 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    if [[ $latest_go_version -eq 1 ]]; then
    73      # Only do coverage for the latest Linux build because it is slow, and
    74      # codecov will only save the last one anyway.
    75      if [[ "${RUNNER_OS:-}" == "Linux" ]]; then
    76        gotestflags+=("-coverpkg=./..." "-coverprofile=$rootdir/modcoverage.out")
    77      fi
    78      # Previous versions of the "go" command may have
    79      # different opinions about what go.mod should look
    80      # like.
    81      gotestflags+=("-mod=readonly")
    82    fi
    83  
    84    # Run the tests.
    85    (cd "$path" && go test "${gotestflags[@]}" ./...) | ./internal/testing/test-summary/test-summary "${testsummaryflags[@]}" || result=1
    86    if [ -f modcoverage.out ] && [ $result -eq 0 ]; then
    87      cat modcoverage.out >> coverage.out
    88      rm modcoverage.out
    89    fi
    90  done < <( sed -e '/^#/d' -e '/^$/d' allmodules | awk '{print $1}' )
    91  # The above filters out comments and empty lines from allmodules and only takes
    92  # the first (whitespace-separated) field from each line.
    93  
    94  # Upload cumulative coverage data if we generated it.
    95  if [ -f coverage.out ] && [ $result -eq 0 ]; then
    96    # Filter out test packages.
    97    grep -v test coverage.out > coverage2.out
    98    mv coverage2.out coverage.out
    99    bash <(curl -s https://codecov.io/bash)
   100    rm coverage.out
   101  fi
   102  
   103  # The rest of these checks are not OS-specific, so we only run them for the
   104  # Linux build, or when running locally.
   105  if [[ "${RUNNER_OS:-linux}" != "Linux" ]]; then
   106    exit $result
   107  fi
   108  
   109  if [[ ${latest_go_version} -eq 1 ]]; then
   110    echo
   111    echo "************************"
   112    echo "* Checking go mod tidy"
   113    echo "************************"
   114    echo
   115    while read -r path || [[ -n "$path" ]]; do
   116      echo "Module: $path"
   117      ( cd "$path" && "$rootdir"/internal/testing/check_mod_tidy.sh && echo "  OK" ) || { echo "FAIL: please run ./internal/testing/gomodcleanup.sh" && result=1; }
   118    done < <( sed -e '/^#/d' -e '/^$/d' allmodules | awk '{print $1}' )
   119    # The above filters out comments and empty lines from allmodules and only takes
   120    # the first (whitespace-separated) field from each line.
   121  fi
   122  
   123  
   124  if [[ ${latest_go_version} -eq 1 ]]; then
   125    echo
   126    echo "**********************"
   127    echo "* Checking wire diff"
   128    echo "**********************"
   129    echo
   130    echo "Installing Wire..."
   131    go install github.com/google/wire/cmd/wire@latest
   132    echo
   133    while read -r path || [[ -n "$path" ]]; do
   134      echo "Module: $path"
   135      ( cd "$path" && wire diff ./... && echo "  OK" ) || { echo "FAIL: wire diff found diffs!" && result=1; }
   136    done < <( sed -e '/^#/d' -e '/^$/d' allmodules | awk '{print $1}' )
   137    # The above filters out comments and empty lines from allmodules and only takes
   138    # the first (whitespace-separated) field from each line.
   139  fi;
   140  
   141  if [[ ${latest_go_version} -eq 1 ]]; then
   142    echo
   143    echo "******************************"
   144    echo "* Doing non-module checks"
   145    echo "******************************"
   146    echo
   147    echo "Ensuring .go files are formatted with gofmt -s..."
   148    DIFF="$(gofmt -s -d .)"
   149    if [ -n "$DIFF" ]; then
   150      echo "FAIL: please run 'gofmt -s -w .' and commit the result"
   151      echo "$DIFF";
   152      exit 1;
   153    else
   154      echo "  OK"
   155    fi;
   156  fi;
   157  
   158  if [[ ${latest_go_version} -eq 1 ]]; then
   159    echo
   160    echo "Ensuring that there are no dependencies not listed in ./internal/testing/alldeps..."
   161    ( ./internal/testing/listdeps.sh | diff -u ./internal/testing/alldeps - && echo "  OK" ) || {
   162      echo "FAIL: dependencies changed; run: internal/testing/listdeps.sh > internal/testing/alldeps" && result=1
   163      # Module behavior may differ across versions.
   164      echo "using the most recent go version."
   165    }
   166  fi
   167  
   168  
   169  if [[ ${latest_go_version} -eq 1 ]]; then
   170    echo
   171    echo "Ensuring that any new packages have the corresponding entries in Hugo..."
   172    missing_packages="$(internal/website/listnewpkgs.sh)"
   173    if ! [[ -z "$missing_packages" ]]; then
   174      echo "FAIL: missing package meta tags for:" 1>&2
   175      echo "$missing_packages" 1>&2
   176      result=1
   177    else
   178      echo "  OK"
   179    fi
   180  
   181    echo
   182    echo "Ensuring that all examples used in Hugo match what's in source..."
   183    (internal/website/gatherexamples/run.sh | diff -u internal/website/data/examples.json - > /dev/null && echo "  OK") || {
   184      echo "FAIL: examples changed; run: internal/website/gatherexamples/run.sh > internal/website/data/examples.json"
   185      result=1
   186    }
   187  fi;
   188  
   189  
   190  # For pull requests, check if there are undeclared incompatible API changes.
   191  # Skip this if we're already going to fail since it is expensive.
   192  # CURRENTLY BROKEN
   193  # if [[ ${latest_go_version} -eq 1 ]] && [[ ${result} -eq 0 ]] && [[ ! -z "${GITHUB_HEAD_REF:-}" ]]; then
   194    # echo
   195    # ./internal/testing/check_api_change.sh || result=1;
   196  # fi
   197  
   198  
   199  echo
   200  if [[ ${result} -eq 0 ]]; then
   201    echo "SUCCESS!"
   202  else
   203    echo "FAILED; see above for more info."
   204  fi
   205  
   206  exit $result