From 3b34a50f3b217b2d049eb24805a324ef821ca794 Mon Sep 17 00:00:00 2001 From: Jeff McCune Date: Tue, 20 Dec 2011 15:14:40 -0800 Subject: [PATCH] Add Regexp support to with_* matchers and improve error messages Without this patch it is difficult to match portions of long strings which are attribute values of resource parameters. For example, the content parameter of a file resource with a multi-line template. This patch makes it easier to match portions of the parameter's value by passing a Regexp instance to the with_ catch all matcher. The use case looks like: describe 'with lsbmajdistrelease available' do let(:facts) { @facter_facts.merge({'lsbmajdistrelease' => '6'}) } it { should_not contain_file('puppetenterprise.repo').with_content(missing_content) } it { should contain_package 'pe-ruby-devel' } it do should contain_file('puppetenterprise.repo').with_content(/baseurl=http/) should contain_file('puppetenterprise.repo').with_content(/pe_base/) should contain_file('puppetenterprise.repo').with_content(/pe_updates/) should contain_file('puppetenterprise.repo').with_content(/pe_extras/) end end This feature allows the same parameter to be matched multiple times using multiple different regular expressions. This patch also improves the error message by presenting the actual value in the catalog when it does not match the expectation. This change applies to both String and Regexp expectation matches. The error output now looks like: 1) pe_devel on redhat el6 os families with lsbmajdistrelease available Failure/Error: should contain_file('puppetenterprise.repo').with_content(/JEFF MCCUNE/) expected that the catalogue would contain File[puppetenterprise.repo] with content \ matching `/JEFF MCCUNE/` but its value of `"# KERMIT FROG\n"` does not Spec tests have been added to exercise the handling of Regexp instances. Reviewed-by: Dan Bode --- lib/rspec-puppet/matchers/create_generic.rb | 16 ++++++++++++---- spec/classes/boolean_regexp_spec.rb | 10 ++++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) create mode 100644 spec/classes/boolean_regexp_spec.rb diff --git a/lib/rspec-puppet/matchers/create_generic.rb b/lib/rspec-puppet/matchers/create_generic.rb index 286c279..19dc2a7 100644 --- a/lib/rspec-puppet/matchers/create_generic.rb +++ b/lib/rspec-puppet/matchers/create_generic.rb @@ -30,11 +30,19 @@ module RSpec::Puppet if resource.nil? ret = false else + rsrc_hsh = resource.to_hash if @expected_params @expected_params.each do |name, value| - unless resource.send(:parameters)[name.to_sym].to_s == value.to_s - ret = false - (@errors ||= []) << "#{name.to_s} set to `#{value.inspect}`" + if value.kind_of?(Regexp) then + unless rsrc_hsh[name.to_sym].to_s =~ value + ret = false + (@errors ||= []) << "#{name.to_s} matching `#{value.inspect}` but its value of `#{rsrc_hsh[name.to_sym].inspect}` does not" + end + else + unless rsrc_hsh[name.to_sym].to_s == value.to_s + ret = false + (@errors ||= []) << "#{name.to_s} set to `#{value.inspect}` but it is set to `#{rsrc_hsh[name.to_sym].inspect}` in the catalogue" + end end end end @@ -71,7 +79,7 @@ module RSpec::Puppet end def errors - @errors.nil? ? "" : " with #{@errors.join(', ')}" + @errors.nil? ? "" : " with #{@errors.join(', and parameter ')}" end end diff --git a/spec/classes/boolean_regexp_spec.rb b/spec/classes/boolean_regexp_spec.rb new file mode 100644 index 0000000..b4120a9 --- /dev/null +++ b/spec/classes/boolean_regexp_spec.rb @@ -0,0 +1,10 @@ +require 'spec_helper' + +describe 'boolean' do + let(:title) { 'bool.testing' } + let(:params) { { :bool => false } } + let(:message_re) { /bool is false/ } + + it { should create_notify("bool testing").with_message(message_re) } + it { should_not create_notify("bool testing").with_message(/true/) } +end