summaryrefslogtreecommitdiffstats
path: root/autotest
diff options
context:
space:
mode:
authorLuke Kanies <luke@madstop.com>2008-02-12 17:14:21 -0600
committerLuke Kanies <luke@madstop.com>2008-02-12 17:14:21 -0600
commitbcb9b564281003e22d72752d84fa9dc9c8c7107b (patch)
tree57ba50f64b5c5092c1901cb4c0664deffa61ab0a /autotest
parent3af6827875f4e02b47fe2293280ff9afe811485f (diff)
downloadpuppet-bcb9b564281003e22d72752d84fa9dc9c8c7107b.tar.gz
puppet-bcb9b564281003e22d72752d84fa9dc9c8c7107b.tar.xz
puppet-bcb9b564281003e22d72752d84fa9dc9c8c7107b.zip
Copying over Rick's work from the master branch supporting autotest and
cleaning up the rspec support.
Diffstat (limited to 'autotest')
-rw-r--r--autotest/discover.rb9
-rw-r--r--autotest/puppet_rspec.rb61
-rw-r--r--autotest/rspec.rb95
3 files changed, 165 insertions, 0 deletions
diff --git a/autotest/discover.rb b/autotest/discover.rb
new file mode 100644
index 000000000..0ac563724
--- /dev/null
+++ b/autotest/discover.rb
@@ -0,0 +1,9 @@
+require 'autotest'
+
+Autotest.add_discovery do
+ "rspec"
+end
+
+Autotest.add_discovery do
+ "puppet"
+end
diff --git a/autotest/puppet_rspec.rb b/autotest/puppet_rspec.rb
new file mode 100644
index 000000000..fc537e016
--- /dev/null
+++ b/autotest/puppet_rspec.rb
@@ -0,0 +1,61 @@
+require 'autotest'
+require 'autotest/rspec'
+
+class Autotest::PuppetRspec < Autotest::Rspec
+ def initialize # :nodoc:
+ super
+ @test_mappings = {
+ # the libraries under lib/puppet
+ %r%^lib/puppet/(.*)\.rb$% => proc { |filename, m|
+ files_matching %r!spec/(unit|integration)/#{m[1]}.rb!
+ },
+
+ # the actual spec files themselves
+ %r%^spec/(unit|integration)/.*\.rb$% => proc { |filename, _|
+ filename
+ },
+
+ # force a complete re-run for all of these:
+
+ # main puppet lib
+ %r!^lib/puppet\.rb$! => proc { |filename, _|
+ files_matching %r!spec/(unit|integration)/.*\.rb!
+ },
+
+ # the spec_helper
+ %r!^spec/spec_helper\.rb$! => proc { |filename, _|
+ files_matching %r!spec/(unit|integration)/.*\.rb!
+ },
+
+ # the puppet test libraries
+ %r!^test/lib/puppettest/.*! => proc { |filename, _|
+ files_matching %r!spec/(unit|integration)/.*\.rb!
+ },
+
+ # the puppet spec libraries
+ %r!^spec/lib/spec.*! => proc { |filename, _|
+ files_matching %r!spec/(unit|integration)/.*\.rb!
+ },
+
+ # the monkey patches for rspec
+ %r!^spec/lib/monkey_patches/.*! => proc { |filename, _|
+ files_matching %r!spec/(unit|integration)/.*\.rb!
+ },
+ }
+ end
+
+ # Autotest will look for spec commands in the following
+ # locations, in this order:
+ #
+ # * bin/spec
+ # * default spec bin/loader installed in Rubygems
+ # * our local vendor/gems/rspec/bin/spec
+ def spec_commands
+ [
+ File.join('bin', 'spec'),
+ File.join(Config::CONFIG['bindir'], 'spec'),
+ File.join('vendor', 'gems', 'rspec', 'bin', 'spec')
+ ]
+ end
+
+end
diff --git a/autotest/rspec.rb b/autotest/rspec.rb
new file mode 100644
index 000000000..d4b77ea6b
--- /dev/null
+++ b/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