kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/cxx/extractor/proto/testdata/kzip_diff_test.sh (about)

     1  #!/bin/bash
     2  # Copyright 2018 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  
    17  # Output every line except ones that contains match_string. Also, do not output
    18  # extra_lines_to_skip lines that follow the line that contained match_string.
    19  function skip() {
    20    if [ $# -ne 3 ] && [ $# -ne 2 ]; then
    21      echo "Usage: $0 match_string extra_lines_to_skip [input_file]"
    22      return 1
    23    fi
    24    local -r match_string="$1"
    25    local -r lines_to_skip="$2"
    26  
    27     if [ $# -eq 2 ]; then
    28      awk "/$match_string/{skip=$lines_to_skip;next} skip>0{--skip;next} {print}"
    29    else
    30      awk "/$match_string/{skip=$lines_to_skip;next} skip>0{--skip;next} {print}" "$3"
    31    fi
    32    return 0
    33  }
    34  
    35  # Modify input_file to remove lines that contains match_string. Also remove
    36  # extra_lines_to_skip lines that follow the line that contained match_string.
    37  function skip_inplace() {
    38    if [ $# -ne 3 ]; then
    39      echo "Usage: $0 match_string lines_to_skip input_file"
    40      return 1
    41    fi
    42    local -r match_string="$1"
    43    local -r lines_to_skip="$2"
    44    local -r input_file="$3"
    45  
    46     local -r temp_file=$(mktemp)
    47    skip "$match_string" "$lines_to_skip" "$input_file" > "$temp_file"
    48    mv "$temp_file" "$input_file"
    49  }
    50  
    51  
    52  set -e
    53  
    54  KZIP_TOOL=$1; shift
    55  FORMATJSON=$1; shift
    56  KZIP=$1; shift
    57  GOLDEN=$1; shift
    58  
    59  # Work in tmpdir because the runfiles dir is not writable
    60  cp $KZIP "${TEST_TMPDIR}/file.kzip"
    61  # Extract json version of compilation unit from kzip.
    62  UNIT_FILE="${TEST_TMPDIR}/$(basename $GOLDEN)"
    63  "${KZIP_TOOL}" view "${TEST_TMPDIR}/file.kzip" | "$FORMATJSON" > "$UNIT_FILE"
    64  # Remove working_directory, which will change depending on the machine the test
    65  # is run on.
    66  skip_inplace "working_directory" 0 $UNIT_FILE
    67  
    68  echo
    69  echo "Diffing generated unit against golden"
    70  echo "-------------------------------------"
    71  diff -u $GOLDEN $UNIT_FILE
    72  echo "No diffs found!"