blob: 10f8c1e38e0b1c3fa13028cdff52f600908d9616 [file] [log] [blame]
Eli Benderskyb9223602011-12-28 10:06:55 +02001#!/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#-------------------------------------------------------------------------------
10import os, sys
11import logging
Eli Benderskyd32d7112013-04-06 07:00:00 -070012
Eli Benderskyb9223602011-12-28 10:06:55 +020013from test.utils import run_exe, is_in_rootdir, dump_output_to_temp_files
14
15
16# Create a global logger object
17#
18testlog = logging.getLogger('run_examples_test')
19testlog.setLevel(logging.DEBUG)
20testlog.addHandler(logging.StreamHandler(sys.stdout))
21
22
23def discover_examples():
24 """ Return paths to all example scripts. Assume we're in the root source
25 dir of pyelftools.
26 """
27 root = './examples'
28 for filename in os.listdir(root):
29 if os.path.splitext(filename)[1] == '.py':
30 yield os.path.join(root, filename)
31
32
33def reference_output_path(example_path):
34 """ Compute the reference output path from a given example path.
35 """
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', example_noext + '.out')
39
40
41def run_example_and_compare(example_path):
42 testlog.info("Example '%s'" % example_path)
43
44 reference_path = reference_output_path(example_path)
45 ref_str = ''
46 try:
47 with open(reference_path) as ref_f:
48 ref_str = ref_f.read()
49 except (IOError, OSError) as e:
50 testlog.info('.......ERROR - reference output cannot be read! - %s' % e)
51 return False
52
53 rc, example_out = run_exe(example_path, ['./examples/sample_exe64.elf'])
54 if rc != 0:
55 testlog.info('.......ERROR - example returned error code %s' % rc)
56 return False
Eli Benderskyd32d7112013-04-06 07:00:00 -070057
eli.bendersky3bd3ecc2012-01-11 15:56:41 +020058 # Comparison is done as lists of lines, to avoid EOL problems
59 if example_out.split() == ref_str.split():
Eli Benderskyb9223602011-12-28 10:06:55 +020060 return True
61 else:
62 testlog.info('.......FAIL comparison')
63 dump_output_to_temp_files(testlog, example_out)
64 return False
65
66
67def main():
68 if not is_in_rootdir():
69 testlog.error('Error: Please run me from the root dir of pyelftools!')
70 return 1
71
72 success = True
73 for example_path in discover_examples():
74 if success:
75 success = success and run_example_and_compare(example_path)
76
77 if success:
78 testlog.info('\nConclusion: SUCCESS')
79 return 0
80 else:
81 testlog.info('\nConclusion: FAIL')
82 return 1
83
84
85if __name__ == '__main__':
86 sys.exit(main())
87