aboutsummaryrefslogtreecommitdiff
path: root/cwp/cr-os/fetch_gn_descs_test.py
blob: b6fc0eebffd32fb66cadb828cd9a035088747c05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright 2020 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Tests for fetch_gn_descs.py."""

from __future__ import print_function

import io
import unittest

import fetch_gn_descs

# pylint: disable=protected-access


class Test(unittest.TestCase):
  """Tests for fetch_gn_descs."""

  def test_fix_result_removes_uninteresting_items(self):
    items = {
        '//uninteresting:a': {},
        '//uninteresting:b': {
            'sources': ['whee'],
        },
        '//uninteresting:c': {
            'configs': ['whee'],
        },
        '//uninteresting:d': {
            'sources': [],
            'configs': [],
        },
        '//interesting:a': {
            'sources': ['a'],
            'configs': ['b'],
        },
        '//interesting:b': {
            'sources': ['d'],
            'configs': ['c'],
        },
    }

    expected_items = {
        '//interesting:a': items['//interesting:a'],
        '//interesting:b': items['//interesting:b'],
    }

    self.assertDictEqual(
        fetch_gn_descs._fix_result('/', '/', '/', items), expected_items)

  def test_fix_result_translates_paths_in_out_dir(self):
    items = {
        '//interesting:a': {
            'sources': ['//out_dir/foo', '//out_dir'],
            'configs': ['b'],
        },
    }

    expected_items = {
        '//interesting:a': {
            'sources': ['//out_translated/foo', '//out_translated/'],
            'configs': ['b'],
        },
    }

    self.assertDictEqual(
        fetch_gn_descs._fix_result(
            rename_out='//out_translated',
            out_dir='/chromium/src/out_dir',
            chromium_root='/chromium',
            gn_desc=items,
        ),
        expected_items,
    )

  def test_gn_desc_output_parsing_skips_pre_json_warnings(self):
    gn_desc = io.StringIO('\n'.join((
        'foo',
        'warning: "{" is bad',
        '{"bar": "baz",',
        ' "qux": true}',
    )))

    warnings, desc_json = fetch_gn_descs._parse_gn_desc_output(gn_desc)
    self.assertEqual(warnings, '\n'.join((
        'foo',
        'warning: "{" is bad',
    )))
    self.assertEqual(desc_json, {
        'bar': 'baz',
        'qux': True,
    })

  def test_gn_desc_output_parsing_issues_no_warnings_if_none_are_present(self):
    gn_desc = io.StringIO('{"bar": "baz"}')
    warnings, desc_json = fetch_gn_descs._parse_gn_desc_output(gn_desc)
    self.assertEqual(warnings, '')
    self.assertEqual(desc_json, {'bar': 'baz'})

    gn_desc = io.StringIO('\n  \n\t\n{"bar": "baz"}')
    warnings, desc_json = fetch_gn_descs._parse_gn_desc_output(gn_desc)
    self.assertEqual(warnings, '')
    self.assertEqual(desc_json, {'bar': 'baz'})


if __name__ == '__main__':
  unittest.main()