3 # WARNING! Do not edit! http://waf.googlecode.com/git/docs/wafbook/single.html#_obtaining_the_waf_file
6 from waflib.TaskGen import feature,after_method,taskgen_method
7 from waflib import Utils,Task,Logs,Options
8 testlock=Utils.threading.Lock()
10 @after_method('apply_link')
12 if getattr(self,'link_task',None):
13 self.create_task('utest',self.link_task.outputs)
15 def add_test_results(self,tup):
16 Logs.debug("ut: %r",tup)
19 self.bld.utest_results.append(tup)
20 except AttributeError:
21 self.bld.utest_results=[tup]
22 class utest(Task.Task):
26 def runnable_status(self):
27 if getattr(Options.options,'no_tests',False):
29 ret=super(utest,self).runnable_status()
31 if getattr(Options.options,'all_tests',False):
34 def add_path(self,dct,path,var):
35 dct[var]=os.pathsep.join(Utils.to_list(path)+[os.environ.get(var,'')])
36 def get_test_env(self):
38 fu=getattr(self.generator.bld,'all_test_paths')
39 except AttributeError:
42 for g in self.generator.bld.groups:
44 if getattr(tg,'link_task',None):
45 s=tg.link_task.outputs[0].parent.abspath()
49 self.add_path(fu,lst,'PATH')
50 elif Utils.unversioned_sys_platform()=='darwin':
51 self.add_path(fu,lst,'DYLD_LIBRARY_PATH')
52 self.add_path(fu,lst,'LD_LIBRARY_PATH')
54 self.add_path(fu,lst,'LD_LIBRARY_PATH')
55 self.generator.bld.all_test_paths=fu
58 filename=self.inputs[0].abspath()
59 self.ut_exec=getattr(self.generator,'ut_exec',[filename])
60 if getattr(self.generator,'ut_fun',None):
61 self.generator.ut_fun(self)
62 cwd=getattr(self.generator,'ut_cwd','')or self.inputs[0].parent.abspath()
63 testcmd=getattr(self.generator,'ut_cmd',False)or getattr(Options.options,'testcmd',False)
65 self.ut_exec=(testcmd%self.ut_exec[0]).split(' ')
66 proc=Utils.subprocess.Popen(self.ut_exec,cwd=cwd,env=self.get_test_env(),stderr=Utils.subprocess.PIPE,stdout=Utils.subprocess.PIPE)
67 (stdout,stderr)=proc.communicate()
68 tup=(filename,proc.returncode,stdout,stderr)
71 return self.generator.add_test_results(tup)
75 lst=getattr(bld,'utest_results',[])
77 Logs.pprint('CYAN','execution summary')
79 tfail=len([x for x in lst if x[1]])
80 Logs.pprint('CYAN',' tests that pass %d/%d'%(total-tfail,total))
81 for(f,code,out,err)in lst:
83 Logs.pprint('CYAN',' %s'%f)
84 Logs.pprint('CYAN',' tests that fail %d/%d'%(tfail,total))
85 for(f,code,out,err)in lst:
87 Logs.pprint('CYAN',' %s'%f)
88 def set_exit_code(bld):
89 lst=getattr(bld,'utest_results',[])
90 for(f,code,out,err)in lst:
94 msg.append('stdout:%s%s'%(os.linesep,out.decode('utf-8')))
96 msg.append('stderr:%s%s'%(os.linesep,err.decode('utf-8')))
97 bld.fatal(os.linesep.join(msg))
99 opt.add_option('--notests',action='store_true',default=False,help='Exec no unit tests',dest='no_tests')
100 opt.add_option('--alltests',action='store_true',default=False,help='Exec all unit tests',dest='all_tests')
101 opt.add_option('--testcmd',action='store',default=False,help='Run the unit tests using the test-cmd string'' example "--test-cmd="valgrind --error-exitcode=1'' %s" to run under valgrind',dest='testcmd')