third_party/waf: upgrade to waf 2.0.8
[bbaumbach/samba-autobuild/.git] / third_party / waf / waflib / extras / javatest.py
1 #! /usr/bin/env python
2 # encoding: utf-8
3 # Federico Pellegrin, 2017 (fedepell)
4
5 """
6 Provides Java Unit test support using :py:class:`waflib.Tools.waf_unit_test.utest`
7 task via the **javatest** feature.
8
9 This gives the possibility to run unit test and have them integrated into the
10 standard waf unit test environment. It has been tested with TestNG and JUnit
11 but should be easily expandable to other frameworks given the flexibility of
12 ut_str provided by the standard waf unit test environment.
13
14 Example usage:
15
16 def options(opt):
17         opt.load('java waf_unit_test javatest')
18
19 def configure(conf):
20         conf.load('java javatest')
21
22 def build(bld):
23         
24         [ ... mainprog is built here ... ]
25
26         bld(features = 'javac javatest',
27                 srcdir     = 'test/', 
28                 outdir     = 'test', 
29                 sourcepath = ['test'],
30                 classpath  = [ 'src' ], 
31                 basedir    = 'test', 
32                 use = ['JAVATEST', 'mainprog'], # mainprog is the program being tested in src/
33                 ut_str = 'java -cp ${CLASSPATH} ${JTRUNNER} ${SRC}',
34                 jtest_source = bld.path.ant_glob('test/*.xml'),
35         )
36
37
38 At command line the CLASSPATH where to find the testing environment and the
39 test runner (default TestNG) that will then be seen in the environment as
40 CLASSPATH_JAVATEST (then used for use) and JTRUNNER and can be used for
41 dependencies and ut_str generation.
42
43 Example configure for TestNG:
44         waf configure --jtpath=/tmp/testng-6.12.jar:/tmp/jcommander-1.71.jar --jtrunner=org.testng.TestNG
45                  or as default runner is TestNG:
46         waf configure --jtpath=/tmp/testng-6.12.jar:/tmp/jcommander-1.71.jar
47
48 Example configure for JUnit:
49         waf configure --jtpath=/tmp/junit.jar --jtrunner=org.junit.runner.JUnitCore
50
51 The runner class presence on the system is checked for at configuration stage.
52
53 """
54
55 import os
56 from waflib import Task, TaskGen, Options
57
58 @TaskGen.feature('javatest')
59 @TaskGen.after_method('apply_java', 'use_javac_files', 'set_classpath')
60 def make_javatest(self):
61         """
62         Creates a ``utest`` task with a populated environment for Java Unit test execution
63
64         """
65         tsk = self.create_task('utest')
66         tsk.set_run_after(self.javac_task)
67
68         # Put test input files as waf_unit_test relies on that for some prints and log generation
69         # If jtest_source is there, this is specially useful for passing XML for TestNG
70         # that contain test specification, use that as inputs, otherwise test sources
71         if getattr(self, 'jtest_source', None):
72                 tsk.inputs = self.to_nodes(self.jtest_source)
73         else:
74                 if self.javac_task.srcdir[0].exists():
75                         tsk.inputs = self.javac_task.srcdir[0].ant_glob('**/*.java', remove=False)
76
77         if getattr(self, 'ut_str', None):
78                 self.ut_run, lst = Task.compile_fun(self.ut_str, shell=getattr(self, 'ut_shell', False))
79                 tsk.vars = lst + tsk.vars
80
81         if getattr(self, 'ut_cwd', None):
82                 if isinstance(self.ut_cwd, str):
83                         # we want a Node instance
84                         if os.path.isabs(self.ut_cwd):
85                                 self.ut_cwd = self.bld.root.make_node(self.ut_cwd)
86                         else:
87                                 self.ut_cwd = self.path.make_node(self.ut_cwd)
88         else:
89                 self.ut_cwd = self.bld.bldnode
90
91         # Get parent CLASSPATH and add output dir of test, we run from wscript dir
92         # We have to change it from list to the standard java -cp format (: separated)
93         tsk.env.CLASSPATH = ':'.join(self.env.CLASSPATH) + ':' + self.outdir.abspath()
94
95         if not self.ut_cwd.exists():
96                 self.ut_cwd.mkdir()
97
98         if not hasattr(self, 'ut_env'):
99                 self.ut_env = dict(os.environ)
100
101 def configure(ctx):
102         cp = ctx.env.CLASSPATH or '.'
103         if getattr(Options.options, 'jtpath', None):
104                 ctx.env.CLASSPATH_JAVATEST = getattr(Options.options, 'jtpath').split(':')
105                 cp += ':' + getattr(Options.options, 'jtpath')
106
107         if getattr(Options.options, 'jtrunner', None):
108                 ctx.env.JTRUNNER = getattr(Options.options, 'jtrunner')
109
110         if ctx.check_java_class(ctx.env.JTRUNNER, with_classpath=cp):
111                 ctx.fatal('Could not run test class %r' % ctx.env.JTRUNNER)
112
113 def options(opt):
114         opt.add_option('--jtpath', action='store', default='', dest='jtpath',
115                 help='Path to jar(s) needed for javatest execution, colon separated, if not in the system CLASSPATH')
116         opt.add_option('--jtrunner', action='store', default='org.testng.TestNG', dest='jtrunner',
117                 help='Class to run javatest test [default: org.testng.TestNG]')
118