1#!/bin/bash -eu
2
3set -o pipefail
4
5# How to run: bash path-to-script/androidmk_test.sh
6# Tests of converting license functionality of the androidmk tool
7REAL_TOP="$(readlink -f "$(dirname "$0")"/../../..)"
8"$REAL_TOP/build/soong/soong_ui.bash" --make-mode TARGET_RELEASE=trunk_staging androidmk
9
10source "$(dirname "$0")/lib.sh"
11
12# Expect to create a new license module
13function test_rewrite_license_property_inside_current_directory {
14  setup
15
16  # Create an Android.mk file
17  mkdir -p a/b
18  cat > a/b/Android.mk <<'EOF'
19include $(CLEAR_VARS)
20LOCAL_MODULE := foo
21LOCAL_LICENSE_KINDS := license_kind1 license_kind2
22LOCAL_LICENSE_CONDITIONS := license_condition
23LOCAL_NOTICE_FILE := $(LOCAL_PATH)/license_notice1 $(LOCAL_PATH)/license_notice2
24include $(BUILD_PACKAGE)
25EOF
26
27  # Create an expected Android.bp file for the module "foo"
28  cat > a/b/Android.bp <<'EOF'
29package {
30    // See: http://go/android-license-faq
31    default_applicable_licenses: [
32        "a_b_license",
33    ],
34}
35
36license {
37    name: "a_b_license",
38    visibility: [":__subpackages__"],
39    license_kinds: [
40        "license_kind1",
41        "license_kind2",
42    ],
43    license_text: [
44        "license_notice1",
45        "license_notice2",
46    ],
47}
48
49android_app {
50    name: "foo",
51}
52EOF
53
54  run_androidmk_test "a/b/Android.mk" "a/b/Android.bp"
55}
56
57# Expect to reference to an existing license module
58function test_rewrite_license_property_outside_current_directory {
59  setup
60
61  # Create an Android.mk file
62  mkdir -p a/b/c/d
63  cat > a/b/c/d/Android.mk <<'EOF'
64include $(CLEAR_VARS)
65LOCAL_MODULE := foo
66LOCAL_LICENSE_KINDS := license_kind1 license_kind2
67LOCAL_LICENSE_CONDITIONS := license_condition
68LOCAL_NOTICE_FILE := $(LOCAL_PATH)/../../license_notice1 $(LOCAL_PATH)/../../license_notice2
69include $(BUILD_PACKAGE)
70EOF
71
72  # Create an expected (input) Android.bp file at a/b/
73  cat > a/b/Android.bp <<'EOF'
74package {
75    // See: http://go/android-license-faq
76    default_applicable_licenses: [
77        "a_b_license",
78    ],
79}
80
81license {
82    name: "a_b_license",
83    visibility: [":__subpackages__"],
84    license_kinds: [
85        "license_kind1",
86        "license_kind2",
87    ],
88    license_text: [
89        "license_notice1",
90        "license_notice2",
91    ],
92}
93
94android_app {
95    name: "bar",
96}
97EOF
98
99  # Create an expected (output) Android.bp file for the module "foo"
100  cat > a/b/c/d/Android.bp <<'EOF'
101package {
102    // See: http://go/android-license-faq
103    default_applicable_licenses: [
104        "a_b_license",
105    ],
106}
107
108android_app {
109    name: "foo",
110}
111EOF
112
113  run_androidmk_test "a/b/c/d/Android.mk" "a/b/c/d/Android.bp"
114}
115
116function run_androidmk_test {
117  export ANDROID_BUILD_TOP="$MOCK_TOP"
118  local -r androidmk=("$REAL_TOP"/*/host/*/bin/androidmk)
119  if [[ ${#androidmk[@]} -ne 1 ]]; then
120    fail "Multiple androidmk binaries found: ${androidmk[*]}"
121  fi
122  local -r out=$("${androidmk[0]}" "$1")
123  local -r expected=$(<"$2")
124
125  if [[ "$out" != "$expected" ]]; then
126    ANDROID_BUILD_TOP="$REAL_TOP"
127    cleanup_mock_top
128    fail "The output is not the same as the expected"
129  fi
130
131  ANDROID_BUILD_TOP="$REAL_TOP"
132  cleanup_mock_top
133  echo "Succeeded"
134}
135
136scan_and_run_tests
137