summaryrefslogtreecommitdiffstats
path: root/spec
diff options
context:
space:
mode:
authorDaniel Pittman <daniel@puppetlabs.com>2011-04-28 10:48:24 -0700
committerDaniel Pittman <daniel@puppetlabs.com>2011-04-28 14:50:12 -0700
commit351b6fc5327baf8f2339fed04c3a8e54280fc394 (patch)
tree27d58386885d13baedacde8efd3e17413a39b234 /spec
parentf1b7fafd12f1071b940ed507e8394a66b69d8284 (diff)
downloadpuppet-351b6fc5327baf8f2339fed04c3a8e54280fc394.tar.gz
puppet-351b6fc5327baf8f2339fed04c3a8e54280fc394.tar.xz
puppet-351b6fc5327baf8f2339fed04c3a8e54280fc394.zip
maint: add a 'print' matcher to rspec, to inspect std{out,err}
You can now write expectations along the lines of: expect { ... }.to print /whatever/ This will do the expected thing, which is to require that we print something matching that regular expression during the block. The output itself is suppressed during operation of the matcher. Paired-With: Max Martin <max@puppetlabs.com>
Diffstat (limited to 'spec')
-rw-r--r--spec/lib/puppet_spec/matchers.rb87
-rwxr-xr-xspec/spec_helper.rb47
-rwxr-xr-xspec/unit/application/face_base_spec.rb10
-rwxr-xr-xspec/unit/application/facts_spec.rb27
4 files changed, 124 insertions, 47 deletions
diff --git a/spec/lib/puppet_spec/matchers.rb b/spec/lib/puppet_spec/matchers.rb
new file mode 100644
index 000000000..77f580330
--- /dev/null
+++ b/spec/lib/puppet_spec/matchers.rb
@@ -0,0 +1,87 @@
+require 'stringio'
+
+########################################################################
+# Backward compatibility for Jenkins outdated environment.
+module RSpec
+ module Matchers
+ module BlockAliases
+ alias_method :to, :should unless method_defined? :to
+ alias_method :to_not, :should_not unless method_defined? :to_not
+ alias_method :not_to, :should_not unless method_defined? :not_to
+ end
+ end
+end
+
+
+########################################################################
+# Custom matchers...
+RSpec::Matchers.define :have_matching_element do |expected|
+ match do |actual|
+ actual.any? { |item| item =~ expected }
+ end
+end
+
+
+RSpec::Matchers.define :exit_with do |expected|
+ actual = nil
+ match do |block|
+ begin
+ block.call
+ rescue SystemExit => e
+ actual = e.status
+ end
+ actual and actual == expected
+ end
+ failure_message_for_should do |block|
+ "expected exit with code #{expected} but " +
+ (actual.nil? ? " exit was not called" : "we exited with #{actual} instead")
+ end
+ failure_message_for_should_not do |block|
+ "expected that exit would not be called with #{expected}"
+ end
+ description do
+ "expect exit with #{expected}"
+ end
+end
+
+
+RSpec::Matchers.define :have_printed do |expected|
+ match do |block|
+ $stderr = $stdout = StringIO.new
+
+ begin
+ block.call
+ ensure
+ $stdout.rewind
+ @actual = $stdout.read
+
+ $stdout = STDOUT
+ $stderr = STDERR
+ end
+
+ if @actual then
+ case expected
+ when String
+ @actual.include? expected
+ when Regexp
+ expected.match @actual
+ else
+ raise ArgumentError, "No idea how to match a #{@actual.class.name}"
+ end
+ end
+ end
+
+ failure_message_for_should do |actual|
+ if actual.nil? then
+ "expected #{expected.inspect}, but nothing was printed"
+ else
+ "expected #{expected.inspect} to be printed; got:\n#{actual}"
+ end
+ end
+
+ description do
+ "expect #{expected.inspect} to be printed"
+ end
+
+ diffable
+end
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
index a01ae56cd..6b6b1c2fb 100755
--- a/spec/spec_helper.rb
+++ b/spec/spec_helper.rb
@@ -17,9 +17,10 @@ end
require 'pathname'
require 'tmpdir'
-require 'lib/puppet_spec/verbose'
-require 'lib/puppet_spec/files'
-require 'lib/puppet_spec/fixtures'
+require 'puppet_spec/verbose'
+require 'puppet_spec/files'
+require 'puppet_spec/fixtures'
+require 'puppet_spec/matchers'
require 'monkey_patches/alias_should_to_must'
require 'monkey_patches/publicize_methods'
require 'monkey_patches/disable_signal_trap'
@@ -74,43 +75,3 @@ RSpec.configure do |config|
GC.enable
end
end
-
-RSpec::Matchers.define :have_matching_element do |expected|
- match do |actual|
- actual.any? { |item| item =~ expected }
- end
-end
-
-RSpec::Matchers.define :exit_with do |expected|
- actual = nil
- match do |block|
- begin
- block.call
- rescue SystemExit => e
- actual = e.status
- end
- actual and actual == expected
- end
- failure_message_for_should do |block|
- "expected exit with code #{expected} but " +
- (actual.nil? ? " exit was not called" : "we exited with #{actual} instead")
- end
- failure_message_for_should_not do |block|
- "expected that exit would not be called with #{expected}"
- end
- description do
- "expect exit with #{expected}"
- end
-end
-
-# Backward compatibility for Jenkins outdated environment.
-module RSpec
- module Matchers
- module BlockAliases
- alias_method :to, :should unless method_defined? :to
- alias_method :to_not, :should_not unless method_defined? :to_not
- alias_method :not_to, :should_not unless method_defined? :not_to
- end
- end
-end
-
diff --git a/spec/unit/application/face_base_spec.rb b/spec/unit/application/face_base_spec.rb
index 275702085..d6ca28d64 100755
--- a/spec/unit/application/face_base_spec.rb
+++ b/spec/unit/application/face_base_spec.rb
@@ -309,13 +309,15 @@ EOT
Puppet::Face[:help, :current].expects(:help).never
expect {
- expect { app.setup; app.run }.to exit_with 1
- }.to print(/I don't know how to render 'interpretive-dance'/)
+ expect { app.run }.to exit_with 1
+ }.to have_printed(/I don't know how to render 'interpretive-dance'/)
end
it "should work if asked to render a NetworkHandler format" do
- app.command_line.stubs(:args).returns %w{facts find dummy --render-as yaml}
- expect { app.parse_options; app.setup; app.run }.to exit_with 0
+ app.command_line.stubs(:args).returns %w{dummy find dummy --render-as yaml}
+ expect {
+ expect { app.run }.to exit_with 0
+ }.to have_printed(/--- 3/)
end
end
end
diff --git a/spec/unit/application/facts_spec.rb b/spec/unit/application/facts_spec.rb
new file mode 100755
index 000000000..aed042675
--- /dev/null
+++ b/spec/unit/application/facts_spec.rb
@@ -0,0 +1,27 @@
+#!/usr/bin/env rspec
+require 'spec_helper'
+require 'puppet/application/facts'
+
+describe Puppet::Application::Facts do
+ before :each do
+ subject.command_line.stubs(:subcommand_name).returns 'facts'
+ end
+
+ it "should fail if no key is given to find" do
+ subject.command_line.stubs(:args).returns %w{find}
+ expect {
+ expect { subject.run }.to exit_with 1
+ }.to have_printed /1 argument expected but 0 given/
+ @logs.first.to_s.should =~ /1 argument expected but 0 given/
+ end
+
+ it "should return facts if a key is given to find" do
+ subject.command_line.stubs(:args).returns %w{find whatever --render-as yaml}
+
+ expect {
+ expect { subject.run }.to exit_with 0
+ }.should have_printed(/object:Puppet::Node::Facts/)
+
+ @logs.should be_empty
+ end
+end