1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3# Copyright 2020 The Chromium OS Authors. All rights reserved. 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 9from __future__ import print_function 10 11import io 12import unittest 13 14import fetch_gn_descs 15 16# pylint: disable=protected-access 17 18 19class Test(unittest.TestCase): 20 """Tests for fetch_gn_descs.""" 21 22 def test_fix_result_removes_uninteresting_items(self): 23 items = { 24 '//uninteresting:a': {}, 25 '//uninteresting:b': { 26 'sources': ['whee'], 27 }, 28 '//uninteresting:c': { 29 'configs': ['whee'], 30 }, 31 '//uninteresting:d': { 32 'sources': [], 33 'configs': [], 34 }, 35 '//interesting:a': { 36 'sources': ['a'], 37 'configs': ['b'], 38 }, 39 '//interesting:b': { 40 'sources': ['d'], 41 'configs': ['c'], 42 }, 43 } 44 45 expected_items = { 46 '//interesting:a': items['//interesting:a'], 47 '//interesting:b': items['//interesting:b'], 48 } 49 50 self.assertDictEqual( 51 fetch_gn_descs._fix_result('/', '/', '/', items), expected_items) 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('\n'.join(( 80 'foo', 81 'warning: "{" is bad', 82 '{"bar": "baz",', 83 ' "qux": true}', 84 ))) 85 86 warnings, desc_json = fetch_gn_descs._parse_gn_desc_output(gn_desc) 87 self.assertEqual(warnings, '\n'.join(( 88 'foo', 89 'warning: "{" is bad', 90 ))) 91 self.assertEqual(desc_json, { 92 'bar': 'baz', 93 'qux': True, 94 }) 95 96 def test_gn_desc_output_parsing_issues_no_warnings_if_none_are_present(self): 97 gn_desc = io.StringIO('{"bar": "baz"}') 98 warnings, desc_json = fetch_gn_descs._parse_gn_desc_output(gn_desc) 99 self.assertEqual(warnings, '') 100 self.assertEqual(desc_json, {'bar': 'baz'}) 101 102 gn_desc = io.StringIO('\n \n\t\n{"bar": "baz"}') 103 warnings, desc_json = fetch_gn_descs._parse_gn_desc_output(gn_desc) 104 self.assertEqual(warnings, '') 105 self.assertEqual(desc_json, {'bar': 'baz'}) 106 107 108if __name__ == '__main__': 109 unittest.main() 110