sigs.k8s.io/prow@v0.0.0-20240503223140-c5e374dc7eb1/hack/make-rules/go-test/unit.sh (about)

     1  #!/usr/bin/env bash
     2  # Copyright 2021 The Kubernetes 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  #     http://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  set -o nounset
    17  set -o errexit
    18  set -o pipefail
    19  
    20  usage()
    21  {
    22    >&2 cat <<EOF
    23  Usage: $0 [FOLDER_TO_TEST]
    24  
    25  FOLDER_TO_TEST is a single argument, but can denote multiple folders with the
    26  "..." notation. See examples below.
    27  
    28  Examples:
    29    Test all folders recursively; this is the default if FOLDER_TO_TEST is not provided:
    30      $0 ...
    31  
    32    Test all folders for the "Crier" component:
    33      $0 prow/crier/...
    34  EOF
    35  }
    36  
    37  if (( $# > 1 )); then
    38    usage
    39    exit 1
    40  fi
    41  
    42  # By default, look for *_test.go files in all folders recursively.
    43  folder_to_test="${1:-...}"
    44  
    45  # cd to the repo root and setup go
    46  REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../../.." && pwd -P)"
    47  cd "${REPO_ROOT}"
    48  source hack/build/setup-go.sh
    49  
    50  # build gotestsum
    51  cd 'hack/tools'
    52  go build -o "${REPO_ROOT}/_bin/gotestsum" gotest.tools/gotestsum
    53  cd "${REPO_ROOT}"
    54  
    55  JUNIT_RESULT_DIR="${REPO_ROOT}/_output"
    56  # if we are in CI, copy to the artifact upload location
    57  if [[ -n "${ARTIFACTS:-}" ]]; then
    58    JUNIT_RESULT_DIR="${ARTIFACTS}"
    59  fi
    60  
    61  # run unit tests with junit output
    62  (
    63    set -x;
    64    umask 0022
    65    mkdir -p "${JUNIT_RESULT_DIR}"
    66    "${REPO_ROOT}/_bin/gotestsum" --junitfile="${JUNIT_RESULT_DIR}/junit-unit.xml" \
    67      -- \
    68      ${PROW_UNIT_TEST_EXTRA_FLAGS[@]+${PROW_UNIT_TEST_EXTRA_FLAGS[@]}} \
    69      "./${folder_to_test}"
    70  )