• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash
2# Copyright (C) 2023 The Android Open Source Project
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
16help() {
17  cat <<'EOF'
18
19  diff-and-update-golden.sh [OPTIONS]
20
21    Compare the generated jar files from tiny-framework to the "golden" files.
22
23  OPTIONS:
24    -u: Update the golden files.
25
26    -3: Run `meld` to compare original, stub and impl jar files in 3-way diff.
27        This is useful to visualize the exact differences between 3 jar files.
28
29    -2: Run `meld` to compare original <-> impl, and impl <-> stub as two different diffs.
30EOF
31}
32
33source "${0%/*}"/../common.sh
34
35SCRIPT_NAME="${0##*/}"
36
37GOLDEN_DIR=${GOLDEN_DIR:-golden-output}
38mkdir -p $GOLDEN_DIR
39
40DIFF_CMD=${DIFF_CMD:-./tiny-framework-dump-test.py run-diff}
41
42update=0
43three_way=0
44two_way=0
45while getopts "u32" opt; do
46case "$opt" in
47    u)
48        update=1
49        ;;
50    3)
51        three_way=1
52        ;;
53    2)
54        two_way=1
55        ;;
56    '?')
57        help
58        exit 1
59        ;;
60esac
61done
62shift $(($OPTIND - 1))
63
64# Build the dump files, which are the input of this test.
65run ${BUILD_CMD:-m} dump-jar tiny-framework-dump-test
66
67# Get the path to the generate text files. (not the golden files.)
68# We get them from $OUT/module-info.json
69files=(
70$(python3 -c '
71import sys
72import os
73import json
74
75with open(sys.argv[1], "r") as f:
76    data = json.load(f)
77
78    # Equivalent to:    jq -r '.["tiny-framework-dump-test"]["installed"][]'
79    for path in data["tiny-framework-dump-test"]["installed"]:
80
81      if "golden-output" in path:
82        continue
83      if path.endswith(".txt"):
84        print(os.getenv("ANDROID_BUILD_TOP") + "/" + path)
85' $OUT/module-info.json)
86)
87
88# Next, compare each file and update them in $GOLDEN_DIR
89
90any_file_changed=0
91
92for file in ${files[*]} ; do
93  name=$(basename $file)
94  echo "# Checking $name ..."
95
96  file_changed=0
97  if run $DIFF_CMD $GOLDEN_DIR/$name $file; then
98    : # No diff
99  else
100    file_changed=1
101    any_file_changed=1
102  fi
103
104  if (( $update && $file_changed )) ; then
105    echo "# Updating $name ..."
106    run cp $file $GOLDEN_DIR/$name
107  fi
108done
109
110if (( $three_way )) ; then
111  echo "# Running 3-way diff with meld..."
112  run meld ${files[0]} ${files[1]} ${files[2]} &
113fi
114
115if (( $two_way )) ; then
116  echo "# Running meld..."
117  run meld --diff ${files[0]} ${files[1]} --diff ${files[1]} ${files[2]} --diff ${files[0]} ${files[2]}
118fi
119
120if (( $any_file_changed == 0 )) ; then
121  echo "$SCRIPT_NAME: Success: no changes detected."
122  exit 0
123else
124  if (( $update )) ; then
125    echo "$SCRIPT_NAME: Warning: golden files have been updated."
126    exit 2
127  else
128    echo "$SCRIPT_NAME: Failure: changes detected. See above diff for the details."
129    exit 3
130  fi
131fi
132