PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/hoe/test.rb

https://github.com/lbt/hoe
Ruby | 186 lines | 110 code | 40 blank | 36 comment | 8 complexity | 120b2fda066fd11acf4bb480ebd8f520 MD5 | raw file
  1. ##
  2. # Test plugin for hoe.
  3. #
  4. # === Tasks Provided:
  5. #
  6. # audit:: Run ZenTest against the package.
  7. # default:: Run the default task(s).
  8. # multi:: Run the test suite using multiruby.
  9. # test:: Run the test suite.
  10. # test_deps:: Show which test files fail when run alone.
  11. module Hoe::Test
  12. ##
  13. # Configuration for the supported test frameworks for test task.
  14. SUPPORTED_TEST_FRAMEWORKS = {
  15. :testunit => "test/unit",
  16. :minitest => "minitest/autorun",
  17. :none => nil,
  18. }
  19. ##
  20. # Used to add flags to test_unit (e.g., -n test_borked).
  21. #
  22. # eg FILTER="-n test_blah"
  23. FILTER = ENV['FILTER'] || ENV['TESTOPTS']
  24. ##
  25. # Optional: Array of incompatible versions for multiruby filtering.
  26. # Used as a regex.
  27. attr_accessor :multiruby_skip
  28. ##
  29. # Optional: What test library to require [default: :testunit]
  30. attr_accessor :testlib
  31. ##
  32. # Optional: Additional ruby to run before the test framework is loaded.
  33. attr_accessor :test_prelude
  34. ##
  35. # Optional: RSpec dirs. [default: %w(spec lib)]
  36. attr_accessor :rspec_dirs
  37. ##
  38. # Optional: RSpec options. [default: []]
  39. attr_accessor :rspec_options
  40. ##
  41. # Initialize variables for plugin.
  42. def initialize_test
  43. self.multiruby_skip ||= []
  44. self.testlib ||= :testunit
  45. self.test_prelude ||= nil
  46. self.rspec_dirs ||= %w(spec lib)
  47. self.rspec_options ||= []
  48. end
  49. ##
  50. # Define tasks for plugin.
  51. def define_test_tasks
  52. default_tasks = []
  53. if File.directory? "test" then
  54. desc 'Run the test suite. Use FILTER or TESTOPTS to add flags/args.'
  55. task :test do
  56. ruby make_test_cmd
  57. end
  58. desc 'Run the test suite using multiruby.'
  59. task :multi do
  60. ruby make_test_cmd(:multi)
  61. end
  62. desc 'Show which test files fail when run alone.'
  63. task :test_deps do
  64. tests = Dir["test/**/test_*.rb"] + Dir["test/**/*_test.rb"]
  65. paths = ['bin', 'lib', 'test'].join(File::PATH_SEPARATOR)
  66. null_dev = Hoe::WINDOZE ? '> NUL 2>&1' : '&> /dev/null'
  67. tests.each do |test|
  68. if not system "ruby -I#{paths} #{test} #{null_dev}" then
  69. puts "Dependency Issues: #{test}"
  70. end
  71. end
  72. end
  73. default_tasks << :test
  74. end
  75. if File.directory? "spec" then
  76. found = true
  77. begin
  78. require 'spec/rake/spectask' # rspec 1
  79. desc "Run all specifications"
  80. Spec::Rake::SpecTask.new(:spec) do |t|
  81. t.libs = self.rspec_dirs
  82. t.spec_opts = self.rspec_options
  83. end
  84. rescue LoadError
  85. begin
  86. require 'rspec/core/rake_task' # rspec 2
  87. desc "Run all specifications"
  88. RSpec::Core::RakeTask.new(:spec) do |t|
  89. t.rspec_opts = self.rspec_options
  90. t.rspec_opts << "-I#{self.rspec_dirs.join(":")}" unless
  91. rspec_dirs.empty?
  92. end
  93. rescue LoadError
  94. found = false
  95. end
  96. end
  97. if found then
  98. default_tasks << :spec
  99. else
  100. warn "Found spec dir, but couldn't load rspec (1 or 2) task. skipping."
  101. end
  102. end
  103. desc 'Run the default task(s).'
  104. task :default => default_tasks
  105. unless default_tasks.empty? then
  106. ##
  107. # This is for Erik Hollensbe's rubygems-test project. Hoe is
  108. # test-happy, so by using this plugin you're already testable. For
  109. # more information, see: <https://github.com/erikh/rubygems-test>
  110. # and/or <http://www.gem-testers.org/>
  111. self.spec.files += [".gemtest"]
  112. pkg = pkg_path
  113. turd = "#{pkg}/.gemtest"
  114. task pkg do
  115. touch turd
  116. end
  117. end
  118. desc 'Run ZenTest against the package.'
  119. task :audit do
  120. libs = %w(lib test ext).join(File::PATH_SEPARATOR)
  121. sh "zentest -I=#{libs} #{spec.files.grep(/^(lib|test)/).join(' ')}"
  122. end
  123. end
  124. ##
  125. # Generate the test command-line.
  126. def make_test_cmd multi = false # :nodoc:
  127. unless SUPPORTED_TEST_FRAMEWORKS.has_key?(testlib)
  128. raise "unsupported test framework #{testlib}"
  129. end
  130. framework = SUPPORTED_TEST_FRAMEWORKS[testlib]
  131. tests = ["rubygems"]
  132. tests << framework if framework
  133. tests << test_globs.sort.map { |g| Dir.glob(g) }
  134. tests.flatten!
  135. tests.map! {|f| %(require "#{f}")}
  136. tests.insert 1, test_prelude if test_prelude
  137. cmd = "#{Hoe::RUBY_FLAGS} -e '#{tests.join("; ")}' -- #{FILTER}"
  138. if multi then
  139. ENV['EXCLUDED_VERSIONS'] = multiruby_skip.join ":"
  140. cmd = "-S multiruby #{cmd}"
  141. end
  142. cmd
  143. end
  144. end