rodjek/rspec-puppet — pull request #17 by jeffmccune (2011-12-20 23:31:27+00:00)
Title: 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.