summaryrefslogtreecommitdiffstats
path: root/extras/javatest.py
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2019-03-17 17:31:05 +0100
committerDavid Robillard <d@drobilla.net>2019-03-17 17:31:05 +0100
commit406f89271452fdb573c7e28113b1ed08ff2b4eda (patch)
treed2dcbaf61f3749f73dc7a5e10d3fc6cd5e6e129a /extras/javatest.py
parent7983a5aae615290d04fd43cbc2752f8cf4a46d10 (diff)
downloadsuil-406f89271452fdb573c7e28113b1ed08ff2b4eda.tar.gz
suil-406f89271452fdb573c7e28113b1ed08ff2b4eda.tar.bz2
suil-406f89271452fdb573c7e28113b1ed08ff2b4eda.zip
Squashed 'waflib/' changes from 915dcb1..e7a29b6
e7a29b6 Upgrade to waf 2.0.15 8280f9d Add command for running executables from the build directory 8073c1a Make make_simple_dox() safe in case of exception 70d03b8 Avoid use of global counter hacks for configuration display b7d689a Rewrite test framework 94deadf Automatically add options and move add_flags() to options context f4259ee Reduce system include path noise 927b608 Automatically display configuration header c44b8f3 Set line justification from a constant in the wscript a48e26f Automatically detect if wscript has a test hook ef66724 Save runtime variables in the environment 63bcbcd Clean up TestContext b1d9505 Add ExecutionContext for setting runtime environment 387c1df Add show_diff() and test_file_equals() utilities 29d4d29 Fix in-tree library paths 9fde01f Add custom configuration context 6d3612f Add lib_path_name constant git-subtree-dir: waflib git-subtree-split: e7a29b6b9b2f842314244c23c14d8f8f560904e1
Diffstat (limited to 'extras/javatest.py')
-rwxr-xr-xextras/javatest.py118
1 files changed, 118 insertions, 0 deletions
diff --git a/extras/javatest.py b/extras/javatest.py
new file mode 100755
index 0000000..979b8d8
--- /dev/null
+++ b/extras/javatest.py
@@ -0,0 +1,118 @@
+#! /usr/bin/env python
+# encoding: utf-8
+# Federico Pellegrin, 2017 (fedepell)
+
+"""
+Provides Java Unit test support using :py:class:`waflib.Tools.waf_unit_test.utest`
+task via the **javatest** feature.
+
+This gives the possibility to run unit test and have them integrated into the
+standard waf unit test environment. It has been tested with TestNG and JUnit
+but should be easily expandable to other frameworks given the flexibility of
+ut_str provided by the standard waf unit test environment.
+
+Example usage:
+
+def options(opt):
+ opt.load('java waf_unit_test javatest')
+
+def configure(conf):
+ conf.load('java javatest')
+
+def build(bld):
+
+ [ ... mainprog is built here ... ]
+
+ bld(features = 'javac javatest',
+ srcdir = 'test/',
+ outdir = 'test',
+ sourcepath = ['test'],
+ classpath = [ 'src' ],
+ basedir = 'test',
+ use = ['JAVATEST', 'mainprog'], # mainprog is the program being tested in src/
+ ut_str = 'java -cp ${CLASSPATH} ${JTRUNNER} ${SRC}',
+ jtest_source = bld.path.ant_glob('test/*.xml'),
+ )
+
+
+At command line the CLASSPATH where to find the testing environment and the
+test runner (default TestNG) that will then be seen in the environment as
+CLASSPATH_JAVATEST (then used for use) and JTRUNNER and can be used for
+dependencies and ut_str generation.
+
+Example configure for TestNG:
+ waf configure --jtpath=/tmp/testng-6.12.jar:/tmp/jcommander-1.71.jar --jtrunner=org.testng.TestNG
+ or as default runner is TestNG:
+ waf configure --jtpath=/tmp/testng-6.12.jar:/tmp/jcommander-1.71.jar
+
+Example configure for JUnit:
+ waf configure --jtpath=/tmp/junit.jar --jtrunner=org.junit.runner.JUnitCore
+
+The runner class presence on the system is checked for at configuration stage.
+
+"""
+
+import os
+from waflib import Task, TaskGen, Options
+
+@TaskGen.feature('javatest')
+@TaskGen.after_method('apply_java', 'use_javac_files', 'set_classpath')
+def make_javatest(self):
+ """
+ Creates a ``utest`` task with a populated environment for Java Unit test execution
+
+ """
+ tsk = self.create_task('utest')
+ tsk.set_run_after(self.javac_task)
+
+ # Put test input files as waf_unit_test relies on that for some prints and log generation
+ # If jtest_source is there, this is specially useful for passing XML for TestNG
+ # that contain test specification, use that as inputs, otherwise test sources
+ if getattr(self, 'jtest_source', None):
+ tsk.inputs = self.to_nodes(self.jtest_source)
+ else:
+ if self.javac_task.srcdir[0].exists():
+ tsk.inputs = self.javac_task.srcdir[0].ant_glob('**/*.java', remove=False)
+
+ if getattr(self, 'ut_str', None):
+ self.ut_run, lst = Task.compile_fun(self.ut_str, shell=getattr(self, 'ut_shell', False))
+ tsk.vars = lst + tsk.vars
+
+ if getattr(self, 'ut_cwd', None):
+ if isinstance(self.ut_cwd, str):
+ # we want a Node instance
+ if os.path.isabs(self.ut_cwd):
+ self.ut_cwd = self.bld.root.make_node(self.ut_cwd)
+ else:
+ self.ut_cwd = self.path.make_node(self.ut_cwd)
+ else:
+ self.ut_cwd = self.bld.bldnode
+
+ # Get parent CLASSPATH and add output dir of test, we run from wscript dir
+ # We have to change it from list to the standard java -cp format (: separated)
+ tsk.env.CLASSPATH = ':'.join(self.env.CLASSPATH) + ':' + self.outdir.abspath()
+
+ if not self.ut_cwd.exists():
+ self.ut_cwd.mkdir()
+
+ if not hasattr(self, 'ut_env'):
+ self.ut_env = dict(os.environ)
+
+def configure(ctx):
+ cp = ctx.env.CLASSPATH or '.'
+ if getattr(Options.options, 'jtpath', None):
+ ctx.env.CLASSPATH_JAVATEST = getattr(Options.options, 'jtpath').split(':')
+ cp += ':' + getattr(Options.options, 'jtpath')
+
+ if getattr(Options.options, 'jtrunner', None):
+ ctx.env.JTRUNNER = getattr(Options.options, 'jtrunner')
+
+ if ctx.check_java_class(ctx.env.JTRUNNER, with_classpath=cp):
+ ctx.fatal('Could not run test class %r' % ctx.env.JTRUNNER)
+
+def options(opt):
+ opt.add_option('--jtpath', action='store', default='', dest='jtpath',
+ help='Path to jar(s) needed for javatest execution, colon separated, if not in the system CLASSPATH')
+ opt.add_option('--jtrunner', action='store', default='org.testng.TestNG', dest='jtrunner',
+ help='Class to run javatest test [default: org.testng.TestNG]')
+