1#!/usr/bin/env bash
2
3# Copyright (C) 2020 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17set -e
18
19if [ $# != 1 ]; then
20  echo "Usage: golden_test.sh [check|update]"
21fi
22
23function _golden_test() {
24  local update=0
25  case $1 in
26    check)
27      update=0
28      ;;
29    update)
30      update=1
31      ;;
32    *)
33      echo "Argument must be 'check' or 'update'"
34      exit 1
35      ;;
36  esac
37
38  # warning: this list must be kept in sync with system/tools/aidl/Android.bp
39  modules=(
40    "aidl-test-interface-cpp-source"
41    "aidl-test-interface-java-source"
42    "aidl-test-interface-ndk_platform-source"
43    "aidl-test-interface-rust-source"
44    "aidl_test_loggable_interface-cpp-source"
45    "aidl_test_loggable_interface-java-source"
46    "aidl_test_loggable_interface-ndk_platform-source"
47    "aidl_test_loggable_interface-ndk-source"
48  )
49
50  local root="."
51  if [ "$ANDROID_BUILD_TOP" != "" ]; then
52    root="$ANDROID_BUILD_TOP"
53  fi
54
55  if [ "$update" = 1 ]; then
56    "$root"/build/soong/soong_ui.bash --make-mode \
57      $(for i in "${modules[@]}"; do
58          echo "out/soong/.intermediates/system/tools/aidl/$i/timestamp"
59        done)
60  fi
61
62  local e=0
63  for module in "${modules[@]}"; do
64    local built="$root/out/soong/.intermediates/system/tools/aidl/$module/"
65    local golden="$root/system/tools/aidl/tests/golden_output/$module/"
66
67    if [ "$update" = 1 ]; then
68      rm -rf "$golden"
69      mkdir -p "$golden"
70      cp -r "$built/gen" "$golden"
71    else
72      diff -r "$built" "$golden" || e=1
73    fi
74  done
75
76  if [ "$e" = 1 ]; then
77    echo "ERROR: The AIDL compiler is outputting files in an unknown way."
78    echo "ERROR: to accept these changes, please run:"
79    echo "ERROR:     \$ANDROID_BUILD_TOP/system/tools/aidl/tests/golden_test.sh update"
80    exit 1
81  else
82    if [ "$update" = 1 ]; then
83      echo "UPDATE GOLDEN TEST SUCCESS"
84    fi
85  fi
86}
87
88_golden_test "$@"
89