xref: /aosp_15_r20/external/cronet/android/tools/gn2bp/test/gen_android_bp_test.py (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1# Copyright (C) 2024 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#      http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import unittest
16import tempfile
17from pathlib import Path
18import os
19import sys
20import glob
21
22PARENT_ROOT = os.path.abspath(
23    os.path.join(os.path.dirname(__file__), os.pardir))
24
25sys.path.insert(0, PARENT_ROOT)
26import gen_android_bp
27
28
29class GenerateAndroidBpTest(unittest.TestCase):
30
31  def test_rust_flags_normalize_success_case(self):
32    self.assertDictEqual(gen_android_bp.normalize_rust_flags(
33        ["--cfg=feature=\"float_roundtrip\""]),
34        {'--cfg': {'feature="float_roundtrip"'}})
35
36  def test_rust_flags_normalize_value_without_key(self):
37    self.assertDictEqual(gen_android_bp.normalize_rust_flags(
38        ['-Aunused-imports']),
39        {'-Aunused-imports': None})
40
41  def test_rust_flags_complicated_case_success(self):
42    self.assertDictEqual(gen_android_bp.normalize_rust_flags(
43        ["-Aunused-imports", "-Cforce-unwind-tables=no",
44         "--target=aarch64-linux-android", "--cfg", "feature=X",
45         "--cfg=feature2=Y"]),
46        {'-Aunused-imports': None,
47         '-Cforce-unwind-tables': {'no'},
48         '--target': {'aarch64-linux-android'},
49         '--cfg': {'feature=X', 'feature2=Y'}})
50
51  def test_rust_flags_throw_invalid(self):
52    self.assertRaisesRegex(ValueError,
53                           "Field feature=float_roundtrip does not relate to any key",
54                           lambda: gen_android_bp.normalize_rust_flags(
55                               ['feature=float_roundtrip']))
56
57  def test_rust_flags_throw_invalid(self):
58    self.assertRaisesRegex(ValueError,
59                           "Could not normalize flag --cfg=feature=A=B as it has multiple equal signs.",
60                           lambda: gen_android_bp.normalize_rust_flags(
61                               ['--cfg=feature=A=B']))
62
63  def test_rust_flag_at_symbol(self):
64    # @filepath is allowed in rustflags to tell the compiler to write the output
65    # to that specific file. We don't support it in AOSP but we need to make sure
66    # that it doesn't cause any issue and is safely ignored.
67    self.assertDictEqual(gen_android_bp.normalize_rust_flags(
68        ['@somefilepath']),
69        {'@somefilepath': None})
70
71
72if __name__ == '__main__':
73  unittest.main()
74