#!/bin/bash # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. # This script builds and runs Chromium OS unit tests. Note that this script # utilizes the src_test stanza in chromeos-base packages. These stanzas # should both build and run the unit tests. # This script requires that you run build_packages first. # --- BEGIN COMMON.SH BOILERPLATE --- # Load common CrOS utilities. Inside the chroot this file is installed in # /usr/lib/crosutils. Outside the chroot we find it relative to the script's # location. find_common_sh() { local common_paths=(/usr/lib/crosutils $(dirname "$(readlink -f "$0")")) local path SCRIPT_ROOT= for path in "${common_paths[@]}"; do if [ -r "${path}/common.sh" ]; then SCRIPT_ROOT=${path} break fi done } find_common_sh . "${SCRIPT_ROOT}/common.sh" || (echo "Unable to load common.sh" && exit 1) # --- END COMMON.SH BOILERPLATE --- get_default_board # Flags DEFINE_string board "${DEFAULT_BOARD}" \ "Target board of which tests were built" DEFINE_string build_root "${DEFAULT_BUILD_ROOT}" \ "Root of build output" DEFINE_string package_file "" \ "File with space-separated list of packages to run unit tests" f DEFINE_string packages "" \ "Optional space-separated list of packages to run unit tests" p DEFINE_boolean withdebug "${FLAGS_TRUE}" \ "Build debug versions of Chromium-OS-specific packages." # Total count of packages with unit tests. TEST_COUNT=0 # Number of unit test failures. TEST_FAILURES=0 # List of packages with no unit tests. NO_UNITTESTS="" # List of packages that have unit test failures. FAILED_PACKAGES="" function check_src_test() { egrep '^src_test()' "${1}" > /dev/null } function record_test_failure() { TEST_FAILURES=$(( TEST_FAILURES + 1 )) FAILED_PACKAGES="${FAILED_PACKAGES} ${1}" } function run_unit_test() { FEATURES="-buildpkg -digest noauto" \ ebuild-${FLAGS_board} "${1}" clean unpack prepare configure test } # Parse command line and die if unexpected parameters given. FLAGS_HELP="usage: ${0} [flags]" FLAGS "${@}" || exit 1 eval set -- "${FLAGS_ARGV}" check_flags_only_and_allow_null_arg "${@}" && set -- set -e [ -z "${FLAGS_board}" ] && die "--board required" # Create package list from package file and list of packages. if [ -n "${FLAGS_package_file}" ]; then if [ -f "${FLAGS_package_file}" ]; then PACKAGE_LIST="$(cat ${FLAGS_package_file})" else warn "Missing package file." fi fi [ -n "${FLAGS_packages}" ] && PACKAGE_LIST="${PACKAGE_LIST} ${FLAGS_packages}" # If we didn't specify packages, find all packages. if [ -z "${FLAGS_package_file}" -a -z "${FLAGS_packages}" ]; then PACKAGE_LIST=$( ./get_package_list chromeos --board="${FLAGS_board}" | egrep '^chromeos-base' ) fi BLACK_LIST_FILE="${SCRIPTS_DIR}/unit_test_black_list.txt" if [ "${FLAGS_withdebug}" -eq "${FLAGS_FALSE}" ]; then export USE="${USE} -cros-debug" fi for package in ${PACKAGE_LIST}; do if grep -xq "${package}" "${BLACK_LIST_FILE}"; then warn "Skipping package ${package} since it is blacklisted." continue fi EBUILD_PATH=$( equery-${FLAGS_board} which ${package} 2> /dev/null ) || \ warn "${package} not found" if [ -n "${EBUILD_PATH}" ]; then if check_src_test "${EBUILD_PATH}"; then run_unit_test "${EBUILD_PATH}" || record_test_failure "${package}" else NO_UNITTESTS="${NO_UNITTESTS} ${package}" fi TEST_COUNT=$(( TEST_COUNT + 1 )) fi done if [ -n "${NO_UNITTESTS}" ]; then warn "The following packages have no unit tests:" warn "${NO_UNITTESTS}" fi if [ ${TEST_FAILURES} -ne 0 ]; then error "Run completed with ${TEST_FAILURES}/${TEST_COUNT} test failures." error "Following packages had failing tests:" die "${FAILED_PACKAGES}" else info "All unit tests passed." fi