/rspec/ruby/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/rake_task.rb

https://github.com/cloudspokes/cs-website · Ruby · 179 lines · 91 code · 27 blank · 61 comment · 13 complexity · 8957906479e036a10c10daed1286eb0c MD5 · raw file

  1. require 'rspec/core'
  2. require 'rspec/core/deprecation'
  3. require 'rake'
  4. require 'rake/tasklib'
  5. module RSpec
  6. module Core
  7. class RakeTask < ::Rake::TaskLib
  8. include ::Rake::DSL if defined?(::Rake::DSL)
  9. # Name of task.
  10. #
  11. # default:
  12. # :spec
  13. attr_accessor :name
  14. # Glob pattern to match files.
  15. #
  16. # default:
  17. # 'spec/**/*_spec.rb'
  18. attr_accessor :pattern
  19. # @deprecated
  20. # Has no effect. The rake task now checks ENV['BUNDLE_GEMFILE'] instead.
  21. def skip_bundler=(*)
  22. RSpec.deprecate("RSpec::Core::RakeTask#skip_bundler=")
  23. end
  24. # @deprecated
  25. # Has no effect. The rake task now checks ENV['BUNDLE_GEMFILE'] instead.
  26. def gemfile=(*)
  27. RSpec.deprecate("RSpec::Core::RakeTask#gemfile=", 'ENV["BUNDLE_GEMFILE"]')
  28. end
  29. # @deprecated
  30. # Use ruby_opts="-w" instead.
  31. #
  32. # When true, requests that the specs be run with the warning flag set.
  33. # e.g. "ruby -w"
  34. #
  35. # default:
  36. # false
  37. def warning=(true_or_false)
  38. RSpec.deprecate("RSpec::Core::RakeTask#warning=", 'ruby_opts="-w"')
  39. @warning = true_or_false
  40. end
  41. # Whether or not to fail Rake when an error occurs (typically when examples fail).
  42. #
  43. # default:
  44. # true
  45. attr_accessor :fail_on_error
  46. # A message to print to stderr when there are failures.
  47. attr_accessor :failure_message
  48. # Use verbose output. If this is set to true, the task will print the
  49. # executed spec command to stdout.
  50. #
  51. # default:
  52. # true
  53. attr_accessor :verbose
  54. # Use rcov for code coverage?
  55. #
  56. # default:
  57. # false
  58. attr_accessor :rcov
  59. # Path to rcov.
  60. #
  61. # default:
  62. # 'rcov'
  63. attr_accessor :rcov_path
  64. # Command line options to pass to rcov.
  65. #
  66. # default:
  67. # nil
  68. attr_accessor :rcov_opts
  69. # Command line options to pass to ruby.
  70. #
  71. # default:
  72. # nil
  73. attr_accessor :ruby_opts
  74. # Path to rspec
  75. #
  76. # default:
  77. # 'rspec'
  78. attr_accessor :rspec_path
  79. # Command line options to pass to rspec.
  80. #
  81. # default:
  82. # nil
  83. attr_accessor :rspec_opts
  84. # @deprecated
  85. # Use rspec_opts instead.
  86. #
  87. # Command line options to pass to rspec.
  88. #
  89. # default:
  90. # nil
  91. def spec_opts=(opts)
  92. RSpec.deprecate('RSpec::Core::RakeTask#spec_opts=', 'rspec_opts=')
  93. @rspec_opts = opts
  94. end
  95. def initialize(*args)
  96. @name = args.shift || :spec
  97. @pattern, @rcov_path, @rcov_opts, @ruby_opts, @rspec_opts = nil, nil, nil, nil, nil
  98. @warning, @rcov = false, false
  99. @verbose, @fail_on_error = true, true
  100. yield self if block_given?
  101. @rcov_path ||= 'rcov'
  102. @rspec_path ||= 'rspec'
  103. @pattern ||= './spec{,/*/**}/*_spec.rb'
  104. desc("Run RSpec code examples") unless ::Rake.application.last_comment
  105. task name do
  106. RakeFileUtils.send(:verbose, verbose) do
  107. if files_to_run.empty?
  108. puts "No examples matching #{pattern} could be found"
  109. else
  110. begin
  111. puts spec_command if verbose
  112. success = system(spec_command)
  113. rescue
  114. puts failure_message if failure_message
  115. end
  116. raise("#{spec_command} failed") if fail_on_error unless success
  117. end
  118. end
  119. end
  120. end
  121. private
  122. def files_to_run
  123. if ENV['SPEC']
  124. FileList[ ENV['SPEC'] ]
  125. else
  126. FileList[ pattern ].map { |f| f.gsub(/"/, '\"').gsub(/'/, "\\\\'") }
  127. end
  128. end
  129. def spec_command
  130. @spec_command ||= begin
  131. cmd_parts = []
  132. cmd_parts << RUBY
  133. cmd_parts << ruby_opts
  134. cmd_parts << "-w" if @warning
  135. cmd_parts << "-S" << runner
  136. cmd_parts << "-Ispec:lib" << rcov_opts if rcov
  137. cmd_parts << files_to_run
  138. cmd_parts << "--" if rcov && rspec_opts
  139. cmd_parts << rspec_opts
  140. cmd_parts.flatten.reject(&blank).join(" ")
  141. end
  142. end
  143. private
  144. def runner
  145. rcov ? rcov_path : rspec_path
  146. end
  147. def blank
  148. lambda {|s| s.nil? || s == ""}
  149. end
  150. end
  151. end
  152. end