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-output
38mkdir -p $GOLDEN_DIR
39
40DIFF_CMD=${DIFF:-diff -u --ignore-blank-lines --ignore-space-change}
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 m  dump-jar tiny-framework-dump-test
66
67
68# Get the path to the generate text files. (not the golden files.)
69# We get them from $OUT/module-info.json
70
71files=(
72$(python3 -c '
73import sys
74import os
75import json
76
77with open(sys.argv[1], "r") as f:
78    data = json.load(f)
79
80    # Equivalent to: jq -r '.["tiny-framework-dump-test"]["installed"][]'
81    for path in data["tiny-framework-dump-test"]["installed"]:
82
83      if "golden-output" in path:
84        continue
85      if path.endswith(".txt"):
86        print(os.getenv("ANDROID_BUILD_TOP") + "/" + path)
87' $OUT/module-info.json)
88)
89
90# Next, compare each file and update them in $GOLDEN_DIR
91
92any_file_changed=0
93
94for file in ${files[*]} ; do
95  name=$(basename $file)
96  echo "# Checking $name ..."
97
98  file_changed=0
99  if run $DIFF_CMD $GOLDEN_DIR/$name $file; then
100    : # No diff
101  else
102    file_changed=1
103    any_file_changed=1
104  fi
105
106  if (( $update && $file_changed )) ; then
107    echo "# Updating $name ..."
108    run cp $file $GOLDEN_DIR/$name
109  fi
110done
111
112if (( $three_way )) ; then
113  echo "# Running 3-way diff with meld..."
114  run meld ${files[0]} ${files[1]} ${files[2]} &
115fi
116
117if (( $two_way )) ; then
118  echo "# Running meld..."
119  run meld --diff ${files[0]} ${files[1]} --diff ${files[1]} ${files[2]} --diff ${files[0]} ${files[2]}
120fi
121
122if (( $any_file_changed == 0 )) ; then
123  echo "$SCRIPT_NAME: Success: no changes detected."
124  exit 0
125else
126  if (( $update )) ; then
127    echo "$SCRIPT_NAME: Warning: golden files have been updated."
128    exit 2
129  else
130    echo "$SCRIPT_NAME: Failure: changes detected. See above diff for the details."
131    exit 3
132  fi
133fi
134