github.com/opencontainers/umoci@v0.4.8-0.20240508124516-656e4836fb0d/hack/test-unit.sh (about)

     1  #!/bin/bash
     2  # umoci: Umoci Modifies Open Containers' Images
     3  # Copyright (C) 2016-2020 SUSE LLC
     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  set -Eeuxo pipefail
    18  source "$(dirname "$BASH_SOURCE")/readlinkf.sh"
    19  
    20  export ROOT="$(readlinkf_posix "$(dirname "$BASH_SOURCE")/..")"
    21  
    22  GO="${GO:-go}"
    23  COVERAGE="${COVERAGE:-}"
    24  PROJECT="${PROJECT:-github.com/opencontainers/umoci}"
    25  
    26  extra_args=("$@")
    27  
    28  # -coverprofile= truncates the target file, so we need to create a
    29  # temporary file for this test run and collate it with the current
    30  # $COVERAGE file.
    31  COVERAGE_FILE="$(mktemp -t umoci-coverage.XXXXXX)"
    32  
    33  # If we have to generate a coverage file, make sure the coverage covers the
    34  # entire project and not just the package being tested. This mirrors
    35  # ${TEST_BUILD_FLAGS} from the Makefile.
    36  extra_args+=("-covermode=count" "-coverprofile=$COVERAGE_FILE" "-coverpkg=$PROJECT/...")
    37  
    38  # Run the tests.
    39  "$GO" test -v -cover "${extra_args[@]}" "$PROJECT/..." 2>/dev/null
    40  
    41  if [ -n "${TRAVIS:-}" ]
    42  then
    43  	coverage_tags=unit
    44  	[[ "$(id -u)" == 0 ]] || coverage_tags+=",rootless"
    45  
    46  	# If we're running in Travis, upload the coverage files and don't bother
    47  	# with the local coverage generation.
    48  	"$ROOT/hack/ci-codecov.sh" codecov -cZ -f "$COVERAGE_FILE" -F "$coverage_tags"
    49  elif [ -n "$COVERAGE" ]
    50  then
    51  	# If running locally, collate the coverage information.
    52  	touch "$COVERAGE"
    53  	"$ROOT/hack/collate.awk" "$COVERAGE_FILE" "$COVERAGE" | sponge "$COVERAGE"
    54  fi
    55  rm -f "$COVERAGE_FILE"