waf, waflib: update to 1.7.13
[aubio.git] / waflib / Tools / waf_unit_test.py
1 #! /usr/bin/env python
2 # encoding: utf-8
3 # WARNING! Do not edit! http://waf.googlecode.com/git/docs/wafbook/single.html#_obtaining_the_waf_file
4
5 import os,sys
6 from waflib.TaskGen import feature,after_method
7 from waflib import Utils,Task,Logs,Options
8 testlock=Utils.threading.Lock()
9 @feature('test')
10 @after_method('apply_link')
11 def make_test(self):
12         if getattr(self,'link_task',None):
13                 self.create_task('utest',self.link_task.outputs)
14 class utest(Task.Task):
15         color='PINK'
16         after=['vnum','inst']
17         vars=[]
18         def runnable_status(self):
19                 if getattr(Options.options,'no_tests',False):
20                         return Task.SKIP_ME
21                 ret=super(utest,self).runnable_status()
22                 if ret==Task.SKIP_ME:
23                         if getattr(Options.options,'all_tests',False):
24                                 return Task.RUN_ME
25                 return ret
26         def run(self):
27                 filename=self.inputs[0].abspath()
28                 self.ut_exec=getattr(self.generator,'ut_exec',[filename])
29                 if getattr(self.generator,'ut_fun',None):
30                         self.generator.ut_fun(self)
31                 try:
32                         fu=getattr(self.generator.bld,'all_test_paths')
33                 except AttributeError:
34                         fu=os.environ.copy()
35                         lst=[]
36                         for g in self.generator.bld.groups:
37                                 for tg in g:
38                                         if getattr(tg,'link_task',None):
39                                                 s=tg.link_task.outputs[0].parent.abspath()
40                                                 if s not in lst:
41                                                         lst.append(s)
42                         def add_path(dct,path,var):
43                                 dct[var]=os.pathsep.join(Utils.to_list(path)+[os.environ.get(var,'')])
44                         if Utils.is_win32:
45                                 add_path(fu,lst,'PATH')
46                         elif Utils.unversioned_sys_platform()=='darwin':
47                                 add_path(fu,lst,'DYLD_LIBRARY_PATH')
48                                 add_path(fu,lst,'LD_LIBRARY_PATH')
49                         else:
50                                 add_path(fu,lst,'LD_LIBRARY_PATH')
51                         self.generator.bld.all_test_paths=fu
52                 cwd=getattr(self.generator,'ut_cwd','')or self.inputs[0].parent.abspath()
53                 testcmd=getattr(Options.options,'testcmd',False)
54                 if testcmd:
55                         self.ut_exec=(testcmd%self.ut_exec[0]).split(' ')
56                 proc=Utils.subprocess.Popen(self.ut_exec,cwd=cwd,env=fu,stderr=Utils.subprocess.PIPE,stdout=Utils.subprocess.PIPE)
57                 (stdout,stderr)=proc.communicate()
58                 tup=(filename,proc.returncode,stdout,stderr)
59                 self.generator.utest_result=tup
60                 testlock.acquire()
61                 try:
62                         bld=self.generator.bld
63                         Logs.debug("ut: %r",tup)
64                         try:
65                                 bld.utest_results.append(tup)
66                         except AttributeError:
67                                 bld.utest_results=[tup]
68                 finally:
69                         testlock.release()
70 def summary(bld):
71         lst=getattr(bld,'utest_results',[])
72         if lst:
73                 Logs.pprint('CYAN','execution summary')
74                 total=len(lst)
75                 tfail=len([x for x in lst if x[1]])
76                 Logs.pprint('CYAN','  tests that pass %d/%d'%(total-tfail,total))
77                 for(f,code,out,err)in lst:
78                         if not code:
79                                 Logs.pprint('CYAN','    %s'%f)
80                 Logs.pprint('CYAN','  tests that fail %d/%d'%(tfail,total))
81                 for(f,code,out,err)in lst:
82                         if code:
83                                 Logs.pprint('CYAN','    %s'%f)
84 def set_exit_code(bld):
85         lst=getattr(bld,'utest_results',[])
86         for(f,code,out,err)in lst:
87                 if code:
88                         msg=[]
89                         if out:
90                                 msg.append('stdout:%s%s'%(os.linesep,out.decode('utf-8')))
91                         if err:
92                                 msg.append('stderr:%s%s'%(os.linesep,err.decode('utf-8')))
93                         bld.fatal(os.linesep.join(msg))
94 def options(opt):
95         opt.add_option('--notests',action='store_true',default=False,help='Exec no unit tests',dest='no_tests')
96         opt.add_option('--alltests',action='store_true',default=False,help='Exec all unit tests',dest='all_tests')
97         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')