Autotest against llvm-dwarfdump (#428)
[pyelftools.git] / test / run_examples_test.py
1 #!/usr/bin/env python
2 #-------------------------------------------------------------------------------
3 # test/run_examples_test.py
4 #
5 # Run the examples and compare their output to a reference
6 #
7 # Eli Bendersky (eliben@gmail.com)
8 # This code is in the public domain
9 #-------------------------------------------------------------------------------
10 import os, sys
11 import logging
12 from utils import run_exe, is_in_rootdir, dump_output_to_temp_files
13
14 # Make it possible to run this file from the root dir of pyelftools without
15 # installing pyelftools; useful for CI testing, etc.
16 sys.path[0:0] = ['.']
17
18 # Create a global logger object
19 testlog = logging.getLogger('run_examples_test')
20 testlog.setLevel(logging.DEBUG)
21 testlog.addHandler(logging.StreamHandler(sys.stdout))
22
23
24 def discover_examples():
25 """ Return paths to all example scripts. Assume we're in the root source
26 dir of pyelftools.
27 """
28 root = './examples'
29 for filename in os.listdir(root):
30 if os.path.splitext(filename)[1] == '.py':
31 yield os.path.join(root, filename)
32
33
34 def reference_output_path(example_path):
35 """ Compute the reference output path from a given example path.
36 """
37 examples_root, example_name = os.path.split(example_path)
38 example_noext, _ = os.path.splitext(example_name)
39 return os.path.join(examples_root, 'reference_output',
40 example_noext + '.out')
41
42
43 def run_example_and_compare(example_path):
44 testlog.info("Example '%s'" % example_path)
45
46 reference_path = reference_output_path(example_path)
47 ref_str = ''
48 try:
49 with open(reference_path) as ref_f:
50 ref_str = ref_f.read()
51 except (IOError, OSError) as e:
52 testlog.info('.......ERROR - reference output cannot be read! - %s' % e)
53 return False
54
55 rc, example_out = run_exe(example_path, ['--test',
56 './examples/sample_exe64.elf'])
57 if rc != 0:
58 testlog.info('.......ERROR - example returned error code %s' % rc)
59 return False
60
61 # Comparison is done as lists of lines, to avoid EOL problems
62 if example_out.split() == ref_str.split():
63 return True
64 else:
65 testlog.info('.......FAIL comparison')
66 dump_output_to_temp_files(testlog, example_path, '', example_out, ref_str)
67 return False
68
69
70 def main():
71 if not is_in_rootdir():
72 testlog.error('Error: Please run me from the root dir of pyelftools!')
73 return 1
74
75 success = True
76 for example_path in discover_examples():
77 if success:
78 success = success and run_example_and_compare(example_path)
79
80 if success:
81 testlog.info('\nConclusion: SUCCESS')
82 return 0
83 else:
84 testlog.info('\nConclusion: FAIL')
85 return 1
86
87
88 if __name__ == '__main__':
89 sys.exit(main())