junit.py (2508B)
1 #! /usr/bin/env python 2 # encoding: utf-8 3 4 """ 5 JUnit test system 6 7 - executes all junit tests in the specified subtree (junitsrc) 8 - only if --junit is given on the commandline 9 - method: 10 - add task to compile junitsrc after compiling srcdir 11 - additional junit_classpath specifiable 12 - defaults to classpath + destdir 13 - add task to run junit tests after they're compiled. 14 """ 15 16 import os 17 from waflib import Task, TaskGen, Utils, Options 18 from waflib.TaskGen import feature, before, after 19 from waflib.Configure import conf 20 21 JUNIT_RUNNER = 'org.junit.runner.JUnitCore' 22 23 def options(opt): 24 opt.add_option('--junit', action='store_true', default=False, 25 help='Run all junit tests', dest='junit') 26 opt.add_option('--junitpath', action='store', default='', 27 help='Give a path to the junit jar') 28 29 def configure(ctx): 30 cp = ctx.options.junitpath 31 val = ctx.env.JUNIT_RUNNER = ctx.env.JUNIT_RUNNER or JUNIT_RUNNER 32 if ctx.check_java_class(val, with_classpath=cp): 33 ctx.fatal('Could not run junit from %r' % val) 34 ctx.env.CLASSPATH_JUNIT = cp 35 36 #@feature('junit') 37 #@after('apply_java', 'use_javac_files') 38 def make_test(self): 39 """make the unit test task""" 40 if not getattr(self, 'junitsrc', None): 41 return 42 junit_task = self.create_task('junit_test') 43 try: 44 junit_task.set_run_after(self.javac_task) 45 except AttributeError: 46 pass 47 feature('junit')(make_test) 48 after('apply_java', 'use_javac_files')(make_test) 49 50 class junit_test(Task.Task): 51 color = 'YELLOW' 52 vars = ['JUNIT_EXEC_FLAGS', 'JUNIT_RUNNER'] 53 54 def runnable_status(self): 55 """ 56 Only run if --junit was set as an option 57 """ 58 for t in self.run_after: 59 if not t.hasrun: 60 return Task.ASK_LATER 61 62 n = self.generator.path.find_dir(self.generator.junitsrc) 63 if not n: 64 self.generator.bld.fatal('no such junit directory %r' % self.generator.junitsrc) 65 self.base = n 66 67 # make sure the tests are executed whenever the .class files change 68 self.inputs = n.ant_glob('**/*.java') 69 70 ret = super(junit_test, self).runnable_status() 71 if ret == Task.SKIP_ME: 72 if getattr(Options.options, 'junit', False): 73 ret = Task.RUN_ME 74 return ret 75 76 def run(self): 77 cmd = [] 78 cmd.extend(self.env.JAVA) 79 cmd.append('-classpath') 80 cmd.append(self.generator.javac_task.env.CLASSPATH + os.pathsep + self.generator.javac_task.env.OUTDIR) 81 cmd.extend(self.env.JUNIT_EXEC_FLAGS) 82 cmd.append(self.env.JUNIT_RUNNER) 83 cmd.extend([x.path_from(self.base).replace('.java', '').replace(os.sep, '.') for x in self.inputs]) 84 return self.exec_command(cmd) 85