istio.io/istio@v0.0.0-20240520182934-d79c90f27776/tools/go-ordered-test (about)

     1  #!/usr/bin/env bash
     2  
     3  # Copyright Istio Authors
     4  #
     5  # Licensed under the Apache License, Version 2.0 (the "License");
     6  # you may not use this file except in compliance with the License.
     7  # You may obtain a copy of the License at
     8  #
     9  #    http://www.apache.org/licenses/LICENSE-2.0
    10  #
    11  # Unless required by applicable law or agreed to in writing, software
    12  # distributed under the License is distributed on an "AS IS" BASIS,
    13  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  # See the License for the specific language governing permissions and
    15  # limitations under the License.
    16  
    17  # This script runs go tests in a package, but each test is run individually. This helps
    18  # isolate tests that are improperly depending on global state modification of other tests
    19  
    20  # Usage: `go test -p 1 -exec $PWD/tools/go-ordered-test ./...`
    21  # Supported `go test` flags: -v, -race, -run, -count. All others are ignored (-timeout, etc)
    22  
    23  set -u
    24  
    25  red='\e[0;31m'
    26  green='\e[0;32m'
    27  yellow='\e[0;33m'
    28  clr='\e[0m'
    29  
    30  binary="${1}"
    31  RUN="."
    32  COUNT=1
    33  VERBOSE=""
    34  while [[ "$#" -gt 0 ]]; do
    35      case $1 in
    36          -test.v=true) VERBOSE=true ;;
    37          -test.run=*) RUN="${1#-test.run=}" ;;
    38          -test.count=*) COUNT="${1#-test.count=}" ;;
    39      esac
    40      shift
    41  done
    42  
    43  RESULTS=/tmp/test-results"$(dirname ${binary})"
    44  mkdir -p "${RESULTS}"
    45  code=0
    46  
    47  for testname in $("${binary}" -test.list "${RUN}" | grep '^Test'); do
    48    "${binary}" -test.run '^'"${testname}"'$' -test.count "${COUNT}" -test.v &> "${RESULTS}/${testname}"
    49    # shellcheck disable=SC2181
    50    if [[ $? != 0 ]]; then
    51      echo -e "--- ${red}FAIL:${clr} ${testname}. See ${RESULTS}/${testname} for full logs"
    52      code=1
    53    elif [[ "${VERBOSE}" == true ]]; then
    54      echo -e "--- ${green}PASS:${clr} ${testname}"
    55    fi
    56  done
    57  
    58  exit ${code}