1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3# Copyright 2019 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 post_process_orderfile.py.""" 8 9 10import os 11import shutil 12import tempfile 13import unittest 14 15import post_process_orderfile 16 17 18def _write_nm_file(name): 19 with open(name, "w") as out: 20 out.write("000001 s NotAValidSymbol1\n") 21 out.write("000002 S NotAValidSymbol2\n") 22 out.write("000010 t FirstValidSymbol\n") 23 out.write("000012 t \n") 24 out.write("000020 T Builtins_SecondValidSymbol\n") 25 out.write("000030 T $SymbolToIgnore\n") 26 out.write("000036 T Builtins_LastValidSymbol\n") 27 28 29def _write_orderfile(name): 30 with open(name, "w") as out: 31 out.write("SymbolOrdered1\n") 32 out.write("SymbolOrdered2\n") 33 34 35def _cleanup(files): 36 for f in files: 37 shutil.rmtree(f, ignore_errors=True) 38 39 40class Tests(unittest.TestCase): 41 """All of our tests for post_process_orderfile.""" 42 43 # pylint: disable=protected-access 44 def test__parse_nm_output(self): 45 temp_dir = tempfile.mkdtemp() 46 self.addCleanup(_cleanup, [temp_dir]) 47 chrome_nm_file = os.path.join(temp_dir, "chrome_nm.txt") 48 _write_nm_file(chrome_nm_file) 49 with open(chrome_nm_file) as f: 50 results = list(post_process_orderfile._parse_nm_output(f)) 51 self.assertEqual(len(results), 3) 52 self.assertIn("FirstValidSymbol", results) 53 self.assertIn("Builtins_SecondValidSymbol", results) 54 self.assertIn("Builtins_LastValidSymbol", results) 55 56 def test__remove_duplicates(self): 57 duplicates = ["marker1", "marker2", "marker3", "marker2", "marker1"] 58 results = list(post_process_orderfile._remove_duplicates(duplicates)) 59 self.assertEqual(results, ["marker1", "marker2", "marker3"]) 60 61 def test_run(self): 62 temp_dir = tempfile.mkdtemp() 63 self.addCleanup(_cleanup, [temp_dir]) 64 orderfile_input = os.path.join(temp_dir, "orderfile.in.txt") 65 orderfile_output = os.path.join(temp_dir, "orderfile.out.txt") 66 chrome_nm_file = os.path.join(temp_dir, "chrome_nm.txt") 67 _write_nm_file(chrome_nm_file) 68 _write_orderfile(orderfile_input) 69 with open(orderfile_input) as in_stream, open( 70 orderfile_output, "w" 71 ) as out_stream, open(chrome_nm_file) as chrome_nm_stream: 72 post_process_orderfile.run(in_stream, chrome_nm_stream, out_stream) 73 74 with open(orderfile_output) as check: 75 results = [x.strip() for x in check.readlines()] 76 self.assertEqual( 77 results, 78 [ 79 # Start marker should be put first. 80 "chrome_begin_ordered_code", 81 # Symbols in orderfile come next. 82 "SymbolOrdered1", 83 "SymbolOrdered2", 84 # Builtin functions in chrome_nm come next, and sorted. 85 "Builtins_LastValidSymbol", 86 "Builtins_SecondValidSymbol", 87 # Last symbol should be end marker. 88 "chrome_end_ordered_code", 89 ], 90 ) 91 92 93if __name__ == "__main__": 94 unittest.main() 95