• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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    "../pigweed/pw_polyfill/standard_library_public/pw_polyfill/standard_library/assert.h",
25    "protocol_buffer/gen/pigweed/pw_protobuf/common_protos.proto_library/nanopb/pw_protobuf_protos/status.pb.h",
26    "../pigweed/pw_rpc/client_server.cc",
27    "../pigweed/pw_rpc/public/pw_rpc/client_server.h",
28    "/home/user/pigweed/out/../gen/generated_build_info.cc",
29    "/home/user/pigweed/pw_protobuf/encoder.cc",
30])
31
32JSON_PATH_TRANSFORMATIONS = json.dumps([
33    "/home/user/pigweed/out=out",
34    "/home/user/pigweed/=",
35    "../=",
36    "/home/user/pigweed/out=out",
37])
38
39EXPECTED_TRANSFORMED_PATHS = json.dumps([
40    "pigweed/pw_polyfill/standard_library_public/pw_polyfill/standard_library/assert.h",
41    "protocol_buffer/gen/pigweed/pw_protobuf/common_protos.proto_library/nanopb/pw_protobuf_protos/status.pb.h",
42    "pigweed/pw_rpc/client_server.cc",
43    "pigweed/pw_rpc/public/pw_rpc/client_server.h",
44    "out/../gen/generated_build_info.cc",
45    "pw_protobuf/encoder.cc",
46])
47
48
49class FilePrefixMapTest(unittest.TestCase):
50    def test_prefix_remap(self):
51        path_list = [
52            '/foo_root/root_subdir/source.cc',
53            '/foo_root/root_subdir/out/../gen.cc'
54        ]
55        prefix_maps = [('/foo_root/root_subdir/', ''), ('out/../', 'out/')]
56        expected_paths = ['source.cc', 'out/gen.cc']
57        self.assertEqual(
58            list(file_prefix_map.remap_paths(path_list, prefix_maps)),
59            expected_paths)
60
61    def test_json_prefix_map(self):
62        in_fd = StringIO(JSON_SOURCE_FILES)
63        prefix_map_fd = StringIO(JSON_PATH_TRANSFORMATIONS)
64        out_fd = StringIO()
65        file_prefix_map.remap_json_paths(in_fd, out_fd, prefix_map_fd)
66        self.assertEqual(json.loads(out_fd.getvalue()),
67                         json.loads(EXPECTED_TRANSFORMED_PATHS))
68
69
70if __name__ == '__main__':
71    unittest.main()
72