Make example tests work again
authorEli Bendersky <eliben@google.com>
Tue, 7 Jul 2015 23:07:51 +0000 (16:07 -0700)
committerEli Bendersky <eliben@google.com>
Tue, 7 Jul 2015 23:07:51 +0000 (16:07 -0700)
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.

examples/dwarf_decode_address.py
examples/dwarf_die_tree.py
examples/dwarf_location_lists.py
examples/dwarf_range_lists.py
examples/elf_low_high_api.py
examples/elf_relocations.py
examples/elf_show_debug_sections.py
examples/elfclass_address_size.py
examples/examine_dwarf_info.py
test/run_examples_test.py
test/utils.py

index 8b473f1ed5851131fe1ef94bce420ae11af5e94a..3f5108ec3245f611368ca0ef420d66318f95204c 100644 (file)
@@ -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} <address> <executable>'.format(sys.argv[0]))
         sys.exit(1)
index ef108c35adf7410f3fe667c642ee72e62f2859e3..0e46d9302771b7bfb38169a26de49fcfc9aa7104 100644 (file)
@@ -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)
index d8ee1e97ea5be0272487362561ff3328da9e297d..06401d348d632176f6ab6e48c7612048e7763878 100644 (file)
@@ -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)
index ef742f3d570d98f783f55f2ed331ae80f9576aad..9157f3b920fcedca14fe9b51b637d7be3602f377 100644 (file)
@@ -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)
index f6fbed19c18765f194bfb2c8811392b75300904c..8b261c6c92b6b01fedff20cc40c39c14ffe60dde 100644 (file)
@@ -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)
index eecd72fd1179cb20927a53c81c183c3bf5e0f82b..cc69a4fade208d57c42ac9456b42596d27882cd8 100644 (file)
@@ -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)
index 02a13fbc9d0d7181f28adf65895bbcbdee7b94d1..346a0d41a2589148a430155b4bc697bc95a9d4cb 100644 (file)
@@ -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)
index ac2bfaadcc3e728249faca14d85c8f0cf9e4523b..b31f819a6da01c8161f4fa6933eb896e75cda9cc 100644 (file)
@@ -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)
index 0e1619e9f7a65bba9f2a950489f6a3712342d65d..bb43bcc2f760d02aacfbd63ac45673a7f59ab42e 100644 (file)
@@ -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)
index 24b42c96154cad2ce29854359c8e3562e36446b5..2fd3ad1ab429f6e6990af00bd596df75709c4565 100755 (executable)
@@ -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
index ff586780f5d528370cf1cdbfff5ea24c316edb81..0425f4594c7b5b3ec0bd76a9d5bc97b4ce73df14 100644 (file)
@@ -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