junit.py 2.4 KB

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