6 # Make terminal colors work on windows
12 def add_nanopb_builders(env):
13 '''Add the necessary builder commands for nanopb tests.'''
15 # Build command that runs a test program and saves the output
16 def run_test(target, source, env):
18 infile = open(str(source[1]))
22 if env.has_key("COMMAND"):
23 args = [env["COMMAND"]]
25 args = [str(source[0])]
27 if env.has_key('ARGS'):
28 args.extend(env['ARGS'])
30 print 'Command line: ' + str(args)
31 pipe = subprocess.Popen(args,
33 stdout = open(str(target[0]), 'w'),
37 print '\033[32m[ OK ]\033[0m Ran ' + args[0]
39 print '\033[31m[FAIL]\033[0m Program ' + args[0] + ' returned ' + str(result)
42 run_test_builder = Builder(action = run_test,
44 env.Append(BUILDERS = {'RunTest': run_test_builder})
46 # Build command that decodes a message using protoc
47 def decode_actions(source, target, env, for_signature):
49 dirs = ' '.join(['-I' + esc(env.GetBuildPath(d)) for d in env['PROTOCPATH']])
50 return '$PROTOC $PROTOCFLAGS %s --decode=%s %s <%s >%s' % (
51 dirs, env['MESSAGE'], esc(str(source[1])), esc(str(source[0])), esc(str(target[0])))
53 decode_builder = Builder(generator = decode_actions,
55 env.Append(BUILDERS = {'Decode': decode_builder})
57 # Build command that encodes a message using protoc
58 def encode_actions(source, target, env, for_signature):
60 dirs = ' '.join(['-I' + esc(env.GetBuildPath(d)) for d in env['PROTOCPATH']])
61 return '$PROTOC $PROTOCFLAGS %s --encode=%s %s <%s >%s' % (
62 dirs, env['MESSAGE'], esc(str(source[1])), esc(str(source[0])), esc(str(target[0])))
64 encode_builder = Builder(generator = encode_actions,
66 env.Append(BUILDERS = {'Encode': encode_builder})
68 # Build command that asserts that two files be equal
69 def compare_files(target, source, env):
70 data1 = open(str(source[0]), 'rb').read()
71 data2 = open(str(source[1]), 'rb').read()
73 print '\033[32m[ OK ]\033[0m Files equal: ' + str(source[0]) + ' and ' + str(source[1])
76 print '\033[31m[FAIL]\033[0m Files differ: ' + str(source[0]) + ' and ' + str(source[1])
79 compare_builder = Builder(action = compare_files,
81 env.Append(BUILDERS = {'Compare': compare_builder})
83 # Build command that checks that each pattern in source2 is found in source1.
84 def match_files(target, source, env):
85 data = open(str(source[0]), 'rU').read()
86 patterns = open(str(source[1]))
87 for pattern in patterns:
90 if pattern.startswith('! '):
94 status = re.search(pattern.strip(), data, re.MULTILINE)
96 if not status and not invert:
97 print '\033[31m[FAIL]\033[0m Pattern not found in ' + str(source[0]) + ': ' + pattern
99 elif status and invert:
100 print '\033[31m[FAIL]\033[0m Pattern should not exist, but does in ' + str(source[0]) + ': ' + pattern
103 print '\033[32m[ OK ]\033[0m All patterns found in ' + str(source[0])
106 match_builder = Builder(action = match_files, suffix = '.matched')
107 env.Append(BUILDERS = {'Match': match_builder})