summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRick Bradley <rick@rickbradley.com>2007-10-26 15:23:51 -0500
committerRick Bradley <rick@rickbradley.com>2007-10-26 15:23:51 -0500
commitc7b36b76f1319ee18efee8ec1bdf08825cb66f81 (patch)
tree67f525922644402eabab9f933d14c2daf82d70f6
parent6dd0d71b13f6b98b3fd4152421d0124e9786f726 (diff)
downloadpuppet-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)
-rw-r--r--lib/puppet/network/http/handler.rb3
-rw-r--r--spec/lib/autotest/discover.rb9
-rw-r--r--spec/lib/autotest/puppet_rspec.rb13
-rw-r--r--spec/lib/autotest/rspec.rb95
-rw-r--r--spec/lib/monkey_patches/add_confine_and_runnable_to_rspec_dsl.rb30
-rw-r--r--spec/lib/spec/dsl/behaviour.rb3
-rw-r--r--spec/lib/spec/runner/behaviour_runner.rb2
-rw-r--r--spec/spec_helper.rb13
-rwxr-xr-xspec/unit/indirector/node/memory.rb5
-rw-r--r--spec/unit/network/http.rb2
10 files changed, 158 insertions, 17 deletions
diff --git a/lib/puppet/network/http/handler.rb b/lib/puppet/network/http/handler.rb
index 172939538..773381c8d 100644
--- a/lib/puppet/network/http/handler.rb
+++ b/lib/puppet/network/http/handler.rb
@@ -29,8 +29,7 @@ class Puppet::Network::HTTP::Handler
def do_search(request, response)
args = params(request)
result = @model.search(args).collect {|obj| obj.to_yaml }
- encode_result(request, response, result)
-
+ encode_result(request, response, result)
end
def do_destroy(request, response)
diff --git a/spec/lib/autotest/discover.rb b/spec/lib/autotest/discover.rb
new file mode 100644
index 000000000..0ac563724
--- /dev/null
+++ b/spec/lib/autotest/discover.rb
@@ -0,0 +1,9 @@
+require 'autotest'
+
+Autotest.add_discovery do
+ "rspec"
+end
+
+Autotest.add_discovery do
+ "puppet"
+end
diff --git a/spec/lib/autotest/puppet_rspec.rb b/spec/lib/autotest/puppet_rspec.rb
new file mode 100644
index 000000000..e7de8b615
--- /dev/null
+++ b/spec/lib/autotest/puppet_rspec.rb
@@ -0,0 +1,13 @@
+require 'autotest'
+require 'autotest/rspec'
+
+class Autotest::PuppetRspec < Autotest::Rspec
+ def initialize # :nodoc:
+ super
+ @test_mappings = {
+ %r%^spec/(unit|integration)/.*\.rb$% => proc { |filename, _|
+ filename
+ },
+ }
+ end
+end
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
diff --git a/spec/lib/monkey_patches/add_confine_and_runnable_to_rspec_dsl.rb b/spec/lib/monkey_patches/add_confine_and_runnable_to_rspec_dsl.rb
new file mode 100644
index 000000000..a2467eac8
--- /dev/null
+++ b/spec/lib/monkey_patches/add_confine_and_runnable_to_rspec_dsl.rb
@@ -0,0 +1,30 @@
+dir = File.expand_path(File.dirname(__FILE__))
+$LOAD_PATH.unshift("#{dir}/../../lib")
+$LOAD_PATH.unshift("#{dir}/../../../lib")
+$LOAD_PATH.unshift("#{dir}/../../../test/lib") # Add the old test dir, so that we can still find our local mocha and spec
+
+require 'spec'
+require 'puppettest'
+require 'puppettest/runnable_test'
+
+module Spec
+ module Runner
+ class BehaviourRunner
+ def run_behaviours
+ @behaviours.each do |behaviour|
+ # LAK:NOTE: this 'runnable' test is Puppet-specific.
+ next unless behaviour.runnable?
+ behaviour.run(@options.reporter, @options.dry_run, @options.reverse, @options.timeout)
+ end
+ end
+ end
+ end
+end
+
+module Spec
+ module DSL
+ class EvalModule < Module
+ include PuppetTest::RunnableTest
+ end
+ end
+end
diff --git a/spec/lib/spec/dsl/behaviour.rb b/spec/lib/spec/dsl/behaviour.rb
index 159a0ba7e..cc71ccffe 100644
--- a/spec/lib/spec/dsl/behaviour.rb
+++ b/spec/lib/spec/dsl/behaviour.rb
@@ -2,9 +2,6 @@ require(File.expand_path(File.dirname(__FILE__) + '../../../../../test/lib/puppe
module Spec
module DSL
- class EvalModule < Module;
- include PuppetTest::RunnableTest
- end
class Behaviour
extend BehaviourCallbacks
diff --git a/spec/lib/spec/runner/behaviour_runner.rb b/spec/lib/spec/runner/behaviour_runner.rb
index 078490e92..1ac891f3c 100644
--- a/spec/lib/spec/runner/behaviour_runner.rb
+++ b/spec/lib/spec/runner/behaviour_runner.rb
@@ -55,8 +55,6 @@ module Spec
def run_behaviours
@behaviours.each do |behaviour|
- # LAK:NOTE: this 'runnable' test is Puppet-specific.
- next unless behaviour.runnable?
behaviour.run(@options.reporter, @options.dry_run, @options.reverse, @options.timeout)
end
end
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
index 3017f272a..bfac9095f 100644
--- a/spec/spec_helper.rb
+++ b/spec/spec_helper.rb
@@ -1,12 +1,11 @@
dir = File.expand_path(File.dirname(__FILE__))
-$:.unshift("#{dir}/lib")
-$:.unshift("#{dir}/../lib")
+$LOAD_PATH.unshift("#{dir}/lib")
+$LOAD_PATH.unshift("#{dir}/../lib")
+$LOAD_PATH.unshift("#{dir}/../test/lib") # Add the old test dir, so that we can still find our local mocha and spec
-# Add the old test dir, so that we can still find mocha and spec
-$:.unshift("#{dir}/../test/lib")
-
-require 'mocha'
require 'puppettest'
+require 'puppettest/runnable_test'
+require 'mocha'
require 'spec'
Spec::Runner.configure do |config|
@@ -19,3 +18,5 @@ Spec::Runner.configure do |config|
teardown() if respond_to? :teardown
end
end
+
+require "#{dir}/lib/monkey_patches/add_confine_and_runnable_to_rspec_dsl"
diff --git a/spec/unit/indirector/node/memory.rb b/spec/unit/indirector/node/memory.rb
index f57cae818..a924c6209 100755
--- a/spec/unit/indirector/node/memory.rb
+++ b/spec/unit/indirector/node/memory.rb
@@ -4,9 +4,8 @@ require File.dirname(__FILE__) + '/../../../spec_helper'
require 'puppet/indirector/node/memory'
-# All of our behaviour is described here, so we always have to
-# include it.
-require 'unit/indirector/memory'
+# All of our behaviour is described here, so we always have to include it.
+require File.dirname(__FILE__) + '/../memory'
describe Puppet::Node::Memory do
before do
diff --git a/spec/unit/network/http.rb b/spec/unit/network/http.rb
index a282b650a..79a0a88d4 100644
--- a/spec/unit/network/http.rb
+++ b/spec/unit/network/http.rb
@@ -26,4 +26,4 @@ describe Puppet::Network::HTTP do
it "should return an error when asked for an unknown server" do
Proc.new { Puppet::Network::HTTP.server_class_by_type :foo }.should raise_error(ArgumentError)
end
-end \ No newline at end of file
+end