kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/cxx/tools/fyi/testdata/test_case.sh (about)

     1  #!/bin/bash
     2  # Copyright 2015 The Kythe Authors. All rights reserved.
     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  # test_case.sh test_basename.cc
    17  # runs a single fyi test, expecting the following files to exist:
    18  #   test_basename.cc -- input source file
    19  #   test_basename.cc.expected -- expected output
    20  #   test_basename.cc.json -- input kythe database
    21  #   compile_commands.json.in -- shared compilation database
    22  #     will substitute "OUT_DIR" for the test output directory
    23  
    24  set -eo pipefail
    25  : ${FYI?:missing fyi}
    26  TEST_NAME="$1"
    27  BASE_DIR="$PWD/kythe/cxx/tools/fyi/testdata"
    28  TEST_JSON="${BASE_DIR}/${TEST_NAME}.json"
    29  TEST_EXPECTED="${BASE_DIR}/${TEST_NAME}.expected"
    30  OUT_DIR="$TEST_TMPDIR"
    31  TEST_FILE="${OUT_DIR}/${TEST_NAME}"
    32  HAD_ERRORS=0
    33  
    34  source "kythe/cxx/common/testdata/start_http_service.sh"
    35  
    36  mkdir -p "${OUT_DIR}"
    37  sed "s|OUT_DIR|${OUT_DIR}|g
    38  s|BASE_DIR|${BASE_DIR}|g" "${BASE_DIR}/compile_commands.json.in" > \
    39      "${OUT_DIR}/compile_commands.json"
    40  cp "${BASE_DIR}/${TEST_NAME}" "${TEST_FILE}"
    41  
    42  set +e
    43  "${FYI}" --xrefs="${XREFS_URI}" "${TEST_FILE}" > "${TEST_FILE}.actual" 
    44  RESULTS=$?
    45  set -e
    46  
    47  if [[ -e "${TEST_EXPECTED}" ]]; then
    48    if [[ ${RESULTS} -ne 0 ]]; then
    49      >&2 echo "Expected zero return from tool, saw ${RESULTS}"
    50      HAD_ERRORS=1
    51    fi
    52    diff "${TEST_FILE}.actual" "${TEST_EXPECTED}"
    53  else
    54    if [[ ${RESULTS} -eq 0 ]]; then
    55      >&2 echo "Expected nonzero return from tool"
    56      HAD_ERRORS=1
    57    fi
    58  fi
    59  
    60  if [[ ${HAD_ERRORS} -ne 0 ]]; then
    61    echo gdb --args "${FYI}" --xrefs="${XREFS_URI}" "${TEST_FILE}" >&2
    62  fi
    63  
    64  exit ${HAD_ERRORS}