Merge branch 'dev_tests_using_scons'
[apps/agl-service-can-low-level.git] / tests / site_scons / site_init.py
1 import subprocess
2 import sys
3 import re
4
5 try:
6     # Make terminal colors work on windows
7     import colorama
8     colorama.init()
9 except ImportError:
10     pass
11
12 def add_nanopb_builders(env):
13     '''Add the necessary builder commands for nanopb tests.'''
14     
15     # Build command for building .pb from .proto using protoc
16     def proto_actions(source, target, env, for_signature):
17         esc = env['ESCAPE']
18         dirs = ' '.join(['-I' + esc(env.GetBuildPath(d)) for d in env['PROTOCPATH']])
19         return '$PROTOC $PROTOCFLAGS %s -o%s %s' % (dirs, esc(str(target[0])), esc(str(source[0])))
20
21     proto_file_builder = Builder(generator = proto_actions,
22                                  suffix = '.pb',
23                                  src_suffix = '.proto')
24     env.Append(BUILDERS = {'Proto': proto_file_builder})
25     env.SetDefault(PROTOC = 'protoc')
26     env.SetDefault(PROTOCPATH = ['.'])
27
28     # Build command for running nanopb generator
29     import os.path
30     def nanopb_targets(target, source, env):
31         basename = os.path.splitext(str(source[0]))[0]
32         target.append(basename + '.pb.h')
33         return target, source
34
35     nanopb_file_builder = Builder(action = '$NANOPB_GENERATOR $NANOPB_FLAGS $SOURCE',
36                                   suffix = '.pb.c',
37                                   src_suffix = '.pb',
38                                   emitter = nanopb_targets)
39     env.Append(BUILDERS = {'Nanopb': nanopb_file_builder})
40     env.SetDefault(NANOPB_GENERATOR = 'python ' + env.GetBuildPath("#../generator/nanopb_generator.py"))
41     env.SetDefault(NANOPB_FLAGS = '-q')
42
43     # Combined method to run both protoc and nanopb generator
44     def run_protoc_and_nanopb(env, source):
45         b1 = env.Proto(source)
46         b2 = env.Nanopb(source)
47         return b1 + b2
48     env.AddMethod(run_protoc_and_nanopb, "NanopbProto")
49
50     # Build command that runs a test program and saves the output
51     def run_test(target, source, env):
52         if len(source) > 1:
53             infile = open(str(source[1]))
54         else:
55             infile = None
56         
57         pipe = subprocess.Popen(str(source[0]),
58                                 stdin = infile,
59                                 stdout = open(str(target[0]), 'w'),
60                                 stderr = sys.stderr)
61         result = pipe.wait()
62         if result == 0:
63             print '\033[32m[ OK ]\033[0m   Ran ' + str(source[0])
64         else:
65             print '\033[31m[FAIL]\033[0m   Program ' + str(source[0]) + ' returned ' + str(result)
66         return result
67         
68     run_test_builder = Builder(action = run_test,
69                                suffix = '.output')
70     env.Append(BUILDERS = {'RunTest': run_test_builder})
71
72     # Build command that decodes a message using protoc
73     def decode_actions(source, target, env, for_signature):
74         dirs = ' '.join(['-I' + env.GetBuildPath(d) for d in env['PROTOCPATH']])
75         return '$PROTOC $PROTOCFLAGS %s --decode=%s %s <%s >%s' % (dirs, env['MESSAGE'], source[1], source[0], target[0])
76
77     decode_builder = Builder(generator = decode_actions,
78                              suffix = '.decoded')
79     env.Append(BUILDERS = {'Decode': decode_builder})    
80
81     # Build command that asserts that two files be equal
82     def compare_files(target, source, env):
83         data1 = open(str(source[0]), 'rb').read()
84         data2 = open(str(source[1]), 'rb').read()
85         if data1 == data2:
86             print '\033[32m[ OK ]\033[0m   Files equal: ' + str(source[0]) + ' and ' + str(source[1])
87             return 0
88         else:
89             print '\033[31m[FAIL]\033[0m   Files differ: ' + str(source[0]) + ' and ' + str(source[1])
90             return 1
91
92     compare_builder = Builder(action = compare_files,
93                               suffix = '.equal')
94     env.Append(BUILDERS = {'Compare': compare_builder})
95
96     # Build command that checks that each pattern in source2 is found in source1.
97     def match_files(target, source, env):
98         data = open(str(source[0]), 'rU').read()
99         patterns = open(str(source[1]))
100         for pattern in patterns:
101             if pattern.strip() and not re.search(pattern.strip(), data, re.MULTILINE):
102                 print '\033[31m[FAIL]\033[0m   Pattern not found in ' + str(source[0]) + ': ' + pattern
103                 return 1
104         else:
105             print '\033[32m[ OK ]\033[0m   All patterns found in ' + str(source[0])
106             return 0
107
108     match_builder = Builder(action = match_files, suffix = '.matched')
109     env.Append(BUILDERS = {'Match': match_builder})
110     
111