2 #-------------------------------------------------------------------------------
3 # test/run_examples_test.py
5 # Run the examples and compare their output to a reference
7 # Eli Bendersky (eliben@gmail.com)
8 # This code is in the public domain
9 #-------------------------------------------------------------------------------
12 from utils
import setup_syspath
; setup_syspath()
13 from utils
import run_exe
, is_in_rootdir
, dump_output_to_temp_files
16 # Create a global logger object
18 testlog
= logging
.getLogger('run_examples_test')
19 testlog
.setLevel(logging
.DEBUG
)
20 testlog
.addHandler(logging
.StreamHandler(sys
.stdout
))
23 def discover_examples():
24 """ Return paths to all example scripts. Assume we're in the root source
28 for filename
in os
.listdir(root
):
29 if os
.path
.splitext(filename
)[1] == '.py':
30 yield os
.path
.join(root
, filename
)
33 def reference_output_path(example_path
):
34 """ Compute the reference output path from a given example path.
36 examples_root
, example_name
= os
.path
.split(example_path
)
37 example_noext
, _
= os
.path
.splitext(example_name
)
38 return os
.path
.join(examples_root
, 'reference_output',
39 example_noext
+ '.out')
42 def run_example_and_compare(example_path
):
43 testlog
.info("Example '%s'" % example_path
)
45 reference_path
= reference_output_path(example_path
)
48 with
open(reference_path
) as ref_f
:
49 ref_str
= ref_f
.read()
50 except (IOError, OSError) as e
:
51 testlog
.info('.......ERROR - reference output cannot be read! - %s' % e
)
54 rc
, example_out
= run_exe(example_path
, ['--test',
55 './examples/sample_exe64.elf'])
57 testlog
.info('.......ERROR - example returned error code %s' % rc
)
60 # Comparison is done as lists of lines, to avoid EOL problems
61 if example_out
.split() == ref_str
.split():
64 testlog
.info('.......FAIL comparison')
65 dump_output_to_temp_files(testlog
, example_out
)
70 if not is_in_rootdir():
71 testlog
.error('Error: Please run me from the root dir of pyelftools!')
75 for example_path
in discover_examples():
77 success
= success
and run_example_and_compare(example_path
)
80 testlog
.info('\nConclusion: SUCCESS')
83 testlog
.info('\nConclusion: FAIL')
87 if __name__
== '__main__':