xref: /aosp_15_r20/external/toolchain-utils/cwp/cr-os/fetch_gn_descs_test.py (revision 760c253c1ed00ce9abd48f8546f08516e57485fe)
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3# Copyright 2020 The ChromiumOS Authors
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7"""Tests for fetch_gn_descs.py."""
8
9
10import io
11import unittest
12
13import fetch_gn_descs
14
15# pylint: disable=protected-access
16
17
18class Test(unittest.TestCase):
19    """Tests for fetch_gn_descs."""
20
21    def test_fix_result_removes_uninteresting_items(self):
22        items = {
23            "//uninteresting:a": {},
24            "//uninteresting:b": {
25                "sources": ["whee"],
26            },
27            "//uninteresting:c": {
28                "configs": ["whee"],
29            },
30            "//uninteresting:d": {
31                "sources": [],
32                "configs": [],
33            },
34            "//interesting:a": {
35                "sources": ["a"],
36                "configs": ["b"],
37            },
38            "//interesting:b": {
39                "sources": ["d"],
40                "configs": ["c"],
41            },
42        }
43
44        expected_items = {
45            "//interesting:a": items["//interesting:a"],
46            "//interesting:b": items["//interesting:b"],
47        }
48
49        self.assertDictEqual(
50            fetch_gn_descs._fix_result("/", "/", "/", items), expected_items
51        )
52
53    def test_fix_result_translates_paths_in_out_dir(self):
54        items = {
55            "//interesting:a": {
56                "sources": ["//out_dir/foo", "//out_dir"],
57                "configs": ["b"],
58            },
59        }
60
61        expected_items = {
62            "//interesting:a": {
63                "sources": ["//out_translated/foo", "//out_translated/"],
64                "configs": ["b"],
65            },
66        }
67
68        self.assertDictEqual(
69            fetch_gn_descs._fix_result(
70                rename_out="//out_translated",
71                out_dir="/chromium/src/out_dir",
72                chromium_root="/chromium",
73                gn_desc=items,
74            ),
75            expected_items,
76        )
77
78    def test_gn_desc_output_parsing_skips_pre_json_warnings(self):
79        gn_desc = io.StringIO(
80            "\n".join(
81                (
82                    "foo",
83                    'warning: "{" is bad',
84                    '{"bar": "baz",',
85                    ' "qux": true}',
86                )
87            )
88        )
89
90        warnings, desc_json = fetch_gn_descs._parse_gn_desc_output(gn_desc)
91        self.assertEqual(
92            warnings,
93            "\n".join(
94                (
95                    "foo",
96                    'warning: "{" is bad',
97                )
98            ),
99        )
100        self.assertEqual(
101            desc_json,
102            {
103                "bar": "baz",
104                "qux": True,
105            },
106        )
107
108    def test_gn_desc_output_parsing_issues_no_warnings_if_none_are_present(
109        self,
110    ):
111        gn_desc = io.StringIO('{"bar": "baz"}')
112        warnings, desc_json = fetch_gn_descs._parse_gn_desc_output(gn_desc)
113        self.assertEqual(warnings, "")
114        self.assertEqual(desc_json, {"bar": "baz"})
115
116        gn_desc = io.StringIO('\n  \n\t\n{"bar": "baz"}')
117        warnings, desc_json = fetch_gn_descs._parse_gn_desc_output(gn_desc)
118        self.assertEqual(warnings, "")
119        self.assertEqual(desc_json, {"bar": "baz"})
120
121
122if __name__ == "__main__":
123    unittest.main()
124