From: Eli Bendersky Date: Tue, 7 Jul 2015 23:07:51 +0000 (-0700) Subject: Make example tests work again X-Git-Tag: v0.24~22 X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=8df1ab95849e9445a520b736f25a80b447c0bfbf;p=pyelftools.git Make example tests work again The example test runner now passes a special --test flag as the first command-line argument. This affects the behavior of the examples - making them aim at testing. Only very simple command-line flag "parsing" is used here for the sake of minimality. --- diff --git a/examples/dwarf_decode_address.py b/examples/dwarf_decode_address.py index 8b473f1..3f5108e 100644 --- a/examples/dwarf_decode_address.py +++ b/examples/dwarf_decode_address.py @@ -79,6 +79,10 @@ def decode_file_line(dwarfinfo, address): if __name__ == '__main__': + if sys.argv[1] == '--test': + process_file(sys.argv[2], 0x400503) + sys.exit(0) + if len(sys.argv) < 3: print('Expected usage: {0}
'.format(sys.argv[0])) sys.exit(1) diff --git a/examples/dwarf_die_tree.py b/examples/dwarf_die_tree.py index ef108c3..0e46d93 100644 --- a/examples/dwarf_die_tree.py +++ b/examples/dwarf_die_tree.py @@ -61,11 +61,6 @@ def die_info_rec(die, indent_level=' '): if __name__ == '__main__': - for filename in sys.argv[1:]: - process_file(filename) - - - - - - + if sys.argv[1] == '--test': + for filename in sys.argv[2:]: + process_file(filename) diff --git a/examples/dwarf_location_lists.py b/examples/dwarf_location_lists.py index d8ee1e9..06401d3 100644 --- a/examples/dwarf_location_lists.py +++ b/examples/dwarf_location_lists.py @@ -101,11 +101,6 @@ def attribute_has_location_list(attr): if __name__ == '__main__': - for filename in sys.argv[1:]: - process_file(filename) - - - - - - + if sys.argv[1] == '--test': + for filename in sys.argv[2:]: + process_file(filename) diff --git a/examples/dwarf_range_lists.py b/examples/dwarf_range_lists.py index ef742f3..9157f3b 100644 --- a/examples/dwarf_range_lists.py +++ b/examples/dwarf_range_lists.py @@ -80,5 +80,6 @@ def attribute_has_range_list(attr): if __name__ == '__main__': - for filename in sys.argv[1:]: - process_file(filename) + if sys.argv[1] == '--test': + for filename in sys.argv[2:]: + process_file(filename) diff --git a/examples/elf_low_high_api.py b/examples/elf_low_high_api.py index f6fbed1..8b261c6 100644 --- a/examples/elf_low_high_api.py +++ b/examples/elf_low_high_api.py @@ -84,5 +84,6 @@ def section_info_highlevel(stream): if __name__ == '__main__': - for filename in sys.argv[1:]: - process_file(filename) + if sys.argv[1] == '--test': + for filename in sys.argv[2:]: + process_file(filename) diff --git a/examples/elf_relocations.py b/examples/elf_relocations.py index eecd72f..cc69a4f 100644 --- a/examples/elf_relocations.py +++ b/examples/elf_relocations.py @@ -42,5 +42,6 @@ def process_file(filename): if __name__ == '__main__': - for filename in sys.argv[1:]: - process_file(filename) + if sys.argv[1] == '--test': + for filename in sys.argv[2:]: + process_file(filename) diff --git a/examples/elf_show_debug_sections.py b/examples/elf_show_debug_sections.py index 02a13fb..346a0d4 100644 --- a/examples/elf_show_debug_sections.py +++ b/examples/elf_show_debug_sections.py @@ -27,5 +27,6 @@ def process_file(filename): if __name__ == '__main__': - for filename in sys.argv[1:]: - process_file(filename) + if sys.argv[1] == '--test': + for filename in sys.argv[2:]: + process_file(filename) diff --git a/examples/elfclass_address_size.py b/examples/elfclass_address_size.py index ac2bfaa..b31f819 100644 --- a/examples/elfclass_address_size.py +++ b/examples/elfclass_address_size.py @@ -33,6 +33,6 @@ def process_file(filename): if __name__ == '__main__': - for filename in sys.argv[1:]: - process_file(filename) - + if sys.argv[1] == '--test': + for filename in sys.argv[2:]: + process_file(filename) diff --git a/examples/examine_dwarf_info.py b/examples/examine_dwarf_info.py index 0e1619e..bb43bcc 100644 --- a/examples/examine_dwarf_info.py +++ b/examples/examine_dwarf_info.py @@ -46,10 +46,6 @@ def process_file(filename): print(' name=%s' % top_DIE.get_full_path()) if __name__ == '__main__': - for filename in sys.argv[1:]: - process_file(filename) - - - - - + if sys.argv[1] == '--test': + for filename in sys.argv[2:]: + process_file(filename) diff --git a/test/run_examples_test.py b/test/run_examples_test.py index 24b42c9..2fd3ad1 100755 --- a/test/run_examples_test.py +++ b/test/run_examples_test.py @@ -51,7 +51,8 @@ def run_example_and_compare(example_path): testlog.info('.......ERROR - reference output cannot be read! - %s' % e) return False - rc, example_out = run_exe(example_path, ['./examples/sample_exe64.elf']) + rc, example_out = run_exe(example_path, ['--test', + './examples/sample_exe64.elf']) if rc != 0: testlog.info('.......ERROR - example returned error code %s' % rc) return False diff --git a/test/utils.py b/test/utils.py index ff58678..0425f45 100644 --- a/test/utils.py +++ b/test/utils.py @@ -6,6 +6,7 @@ # Eli Bendersky (eliben@gmail.com) # This code is in the public domain #------------------------------------------------------------------------------- +from __future__ import print_function import os, sys, subprocess, tempfile # This module should not import elftools before setup_syspath() is called! @@ -19,7 +20,7 @@ def setup_syspath(): sys.path.insert(0, '.') -def run_exe(exe_path, args=[]): +def run_exe(exe_path, args=[], echo=False): """ Runs the given executable as a subprocess, given the list of arguments. Captures its return code (rc) and stdout and returns a pair: rc, stdout_str @@ -27,6 +28,8 @@ def run_exe(exe_path, args=[]): popen_cmd = [exe_path] + args if os.path.splitext(exe_path)[1] == '.py': popen_cmd.insert(0, sys.executable) + if echo: + print('[cmd]', ' '.join(popen_cmd)) proc = subprocess.Popen(popen_cmd, stdout=subprocess.PIPE) proc_stdout = proc.communicate()[0] from elftools.common.py3compat import bytes2str