diff options
author | Rick Bradley <rick@rickbradley.com> | 2007-10-26 15:23:51 -0500 |
---|---|---|
committer | Rick Bradley <rick@rickbradley.com> | 2007-10-26 15:23:51 -0500 |
commit | c7b36b76f1319ee18efee8ec1bdf08825cb66f81 (patch) | |
tree | 67f525922644402eabab9f933d14c2daf82d70f6 /spec/lib/autotest/rspec.rb | |
parent | 6dd0d71b13f6b98b3fd4152421d0124e9786f726 (diff) | |
download | puppet-c7b36b76f1319ee18efee8ec1bdf08825cb66f81.tar.gz puppet-c7b36b76f1319ee18efee8ec1bdf08825cb66f81.tar.xz puppet-c7b36b76f1319ee18efee8ec1bdf08825cb66f81.zip |
One significant step closer to getting autotest running properly on the Puppet specs.
Created a spec/lib/monkey_patches/ directory for holding patches to RSpec functionality.
Extraced 'confine' and 'runnable?' support from the local copy of RSpec (spec/lib/spec/) and now load them from the monkey_patches/ directory.
Fixed a bad include in one of the specs.
Made it possible for the gem-installed spec binary (which autotest calls) to be used with Puppet.
Imported the Autotest::Rspec class, created a PuppetRspec autotest class, added a discovery.rb file for autotest to pick these up.
Autotest still has the following problems:
* it needs to be run with the proper include path:
% ruby -I spec/lib/ `which autotest`
* the patterns in our custom autotest handler (puppet_rspec) aren't yet fully specified (they
only recognize changes in our spec files, not changes in the puppet libs
which they are testing)
Diffstat (limited to 'spec/lib/autotest/rspec.rb')
-rw-r--r-- | spec/lib/autotest/rspec.rb | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/spec/lib/autotest/rspec.rb b/spec/lib/autotest/rspec.rb new file mode 100644 index 000000000..d4b77ea6b --- /dev/null +++ b/spec/lib/autotest/rspec.rb @@ -0,0 +1,95 @@ +require 'autotest' + +class RspecCommandError < StandardError; end + +class Autotest::Rspec < Autotest + + def initialize(kernel=Kernel, separator=File::SEPARATOR, alt_separator=File::ALT_SEPARATOR) # :nodoc: + super() + @kernel, @separator, @alt_separator = kernel, separator, alt_separator + @spec_command = spec_command + + # watch out: Ruby bug (1.8.6): + # %r(/) != /\// + # since Ruby compares the REGEXP source, not the resulting pattern + @test_mappings = { + %r%^spec/.*\.rb$% => kernel.proc { |filename, _| + filename + }, + %r%^lib/(.*)\.rb$% => kernel.proc { |_, m| + ["spec/#{m[1]}_spec.rb"] + }, + %r%^spec/(spec_helper|shared/.*)\.rb$% => kernel.proc { + files_matching %r%^spec/.*_spec\.rb$% + } + } + end + + def tests_for_file(filename) + super.select { |f| @files.has_key? f } + end + + alias :specs_for_file :tests_for_file + + def failed_results(results) + results.scan(/^\d+\)\n(?:\e\[\d*m)?(?:.*?Error in )?'([^\n]*)'(?: FAILED)?(?:\e\[\d*m)?\n(.*?)\n\n/m) + end + + def handle_results(results) + @files_to_test = consolidate_failures failed_results(results) + unless @files_to_test.empty? then + hook :red + else + hook :green + end unless $TESTING + @tainted = true unless @files_to_test.empty? + end + + def consolidate_failures(failed) + filters = Hash.new { |h,k| h[k] = [] } + failed.each do |spec, failed_trace| + @files.keys.select{|f| f =~ /spec\//}.each do |f| + if failed_trace =~ Regexp.new(f) + filters[f] << spec + break + end + end + end + return filters + end + + def make_test_cmd(files_to_test) + return "#{ruby} -S #{@spec_command} #{add_options_if_present} #{files_to_test.keys.flatten.join(' ')}" + end + + def add_options_if_present + File.exist?("spec/spec.opts") ? "-O spec/spec.opts " : "" + end + + # Finds the proper spec command to use. Precendence + # is set in the lazily-evaluated method spec_commands. Alias + Override + # that in ~/.autotest to provide a different spec command + # then the default paths provided. + def spec_command + spec_commands.each do |command| + if File.exists?(command) + return @alt_separator ? (command.gsub @separator, @alt_separator) : command + end + end + + raise RspecCommandError, "No spec command could be found!" + end + + # Autotest will look for spec commands in the following + # locations, in this order: + # + # * bin/spec + # * default spec bin/loader installed in Rubygems + def spec_commands + [ + File.join('bin', 'spec'), + File.join(Config::CONFIG['bindir'], 'spec') + ] + end + +end |