/vendor/bundle/jruby/2.1/gems/rspec-core-2.14.8/lib/autotest/rspec2.rb

https://github.com/delowong/logstash · Ruby · 73 lines · 51 code · 12 blank · 10 comment · 1 complexity · b0cdc2834771141d5b792d5e5339a10c MD5 · raw file

  1. require 'autotest'
  2. require 'rspec/core/deprecation'
  3. # Derived from the `Autotest` class, extends the `autotest` command to work
  4. # with RSpec.
  5. #
  6. # @note this will be extracted to a separate gem when we release rspec-3.
  7. class Autotest::Rspec2 < Autotest
  8. RSPEC_EXECUTABLE = File.expand_path('../../../exe/rspec', __FILE__)
  9. def initialize
  10. super()
  11. clear_mappings
  12. setup_rspec_project_mappings
  13. # Example for Ruby 1.8: http://rubular.com/r/AOXNVDrZpx
  14. # Example for Ruby 1.9: http://rubular.com/r/85ag5AZ2jP
  15. self.failed_results_re = /^\s*\d+\).*\n\s+(?:\e\[\d*m)?Failure.*(\n(?:\e\[\d*m)?\s+#\s(.*)?:\d+(?::.*)?(?:\e\[\d*m)?)+$/m
  16. self.completed_re = /\n(?:\e\[\d*m)?\d* examples?/m
  17. end
  18. # Adds conventional spec-to-file mappings to Autotest configuation.
  19. def setup_rspec_project_mappings
  20. add_mapping(%r%^spec/.*_spec\.rb$%) { |filename, _|
  21. filename
  22. }
  23. add_mapping(%r%^lib/(.*)\.rb$%) { |_, m|
  24. ["spec/#{m[1]}_spec.rb"]
  25. }
  26. add_mapping(%r%^spec/(spec_helper|shared/.*)\.rb$%) {
  27. files_matching %r%^spec/.*_spec\.rb$%
  28. }
  29. end
  30. # Overrides Autotest's implementation to read rspec output
  31. def consolidate_failures(failed)
  32. filters = new_hash_of_arrays
  33. failed.each do |spec, trace|
  34. if trace =~ /(.*spec\.rb)/
  35. filters[$1] << spec
  36. end
  37. end
  38. return filters
  39. end
  40. # Overrides Autotest's implementation to generate the rspec command to run
  41. def make_test_cmd(files_to_test)
  42. files_to_test.empty? ? '' :
  43. %|#{prefix}"#{ruby}"#{suffix} -S "#{RSPEC_EXECUTABLE}" --tty #{normalize(files_to_test).keys.flatten.map { |f| %|"#{f}"|}.join(' ')}|
  44. end
  45. # Generates a map of filenames to Arrays for Autotest
  46. def normalize(files_to_test)
  47. files_to_test.keys.inject({}) do |result, filename|
  48. result.merge!(File.expand_path(filename) => [])
  49. end
  50. end
  51. private
  52. def suffix
  53. using_bundler? ? "" : defined?(:Gem) ? " -rrubygems" : ""
  54. end
  55. def using_bundler?
  56. prefix =~ /bundle exec/
  57. end
  58. def gemfile?
  59. File.exist?('./Gemfile')
  60. end
  61. end