xref: /aosp_15_r20/external/pigweed/pw_build/py/file_prefix_map_test.py (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1# Copyright 2022 The Pigweed Authors
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4# use this file except in compliance with the License. You may obtain a copy of
5# the License at
6#
7#     https://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, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations under
13# the License.
14"""Tests for the file_prefix_map utility"""
15
16from io import StringIO
17import json
18import unittest
19
20from pw_build import file_prefix_map
21
22# pylint: disable=line-too-long
23JSON_SOURCE_FILES = json.dumps(
24    [
25        "../pigweed/pw_polyfill/standard_library_public/pw_polyfill/standard_library/assert.h",
26        "protocol_buffer/gen/pigweed/pw_protobuf/common_protos.proto_library/nanopb/pw_protobuf_protos/status.pb.h",
27        "../pigweed/pw_rpc/client_server.cc",
28        "../pigweed/pw_rpc/public/pw_rpc/client_server.h",
29        "/home/user/pigweed/out/../gen/generated_build_info.cc",
30        "/home/user/pigweed/pw_protobuf/encoder.cc",
31    ]
32)
33
34JSON_PATH_TRANSFORMATIONS = json.dumps(
35    [
36        "/home/user/pigweed/out=out",
37        "/home/user/pigweed/=",
38        "../=",
39        "/home/user/pigweed/out=out",
40    ]
41)
42
43EXPECTED_TRANSFORMED_PATHS = json.dumps(
44    [
45        "pigweed/pw_polyfill/standard_library_public/pw_polyfill/standard_library/assert.h",
46        "protocol_buffer/gen/pigweed/pw_protobuf/common_protos.proto_library/nanopb/pw_protobuf_protos/status.pb.h",
47        "pigweed/pw_rpc/client_server.cc",
48        "pigweed/pw_rpc/public/pw_rpc/client_server.h",
49        "out/../gen/generated_build_info.cc",
50        "pw_protobuf/encoder.cc",
51    ]
52)
53
54
55class FilePrefixMapTest(unittest.TestCase):
56    def test_prefix_remap(self):
57        path_list = [
58            '/foo_root/root_subdir/source.cc',
59            '/foo_root/root_subdir/out/../gen.cc',
60            'out/../gen.cc',
61        ]
62        prefix_maps = [('/foo_root/root_subdir/', ''), ('out/../', 'out/')]
63        expected_paths = ['source.cc', 'out/../gen.cc', 'out/gen.cc']
64        self.assertEqual(
65            list(file_prefix_map.remap_paths(path_list, prefix_maps)),
66            expected_paths,
67        )
68
69    def test_json_prefix_map(self):
70        in_fd = StringIO(JSON_SOURCE_FILES)
71        prefix_map_fd = StringIO(JSON_PATH_TRANSFORMATIONS)
72        out_fd = StringIO()
73        file_prefix_map.remap_json_paths(in_fd, out_fd, prefix_map_fd)
74        self.assertEqual(
75            json.loads(out_fd.getvalue()),
76            json.loads(EXPECTED_TRANSFORMED_PATHS),
77        )
78
79
80if __name__ == '__main__':
81    unittest.main()
82