Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

render_file match failures should show diff #869

Open
atheiman opened this issue Aug 8, 2017 · 2 comments
Open

render_file match failures should show diff #869

atheiman opened this issue Aug 8, 2017 · 2 comments

Comments

@atheiman
Copy link

atheiman commented Aug 8, 2017

is there a way to get diffs running on the render_file matcher in chefspec? Im having a hard time mapping what is described here https://relishapp.com/rspec/rspec-expectations/docs/custom-matchers/define-diffable-matcher to how render_file is defined here https://github.com/chefspec/chefspec/blob/master/lib/chefspec/matchers/render_file_matcher.rb

@coderanger
Copy link
Contributor

Would be a nice feature, but not super high on my list. If someone wants to figure out how to hook in to the diff generators in RSpec itself, that would be cool though :)

@davidalpert
Copy link

davidalpert commented Oct 19, 2017

@atheiman looking into this a bit (though not enough to solve it properly) I discovered that the implementation of the render_file_matcher is a straight-up reimplementation of a matcher, satisfying the contract but defined explicitly rather than using the DSL referenced at https://relishapp.com/rspec/rspec-expectations/docs/custom-matchers/define-diffable-matcher

If you look closely at the code sample in that link you'll see that the first line starts with RSpec::Matchers.define which is the entry point into the DSL where the diffable helper can take effect as described.

With a little help from @coderanger yesterday I was able to find the implementation of renders_file and with_content where I learned that with_content accepts a proc and calls it

It seems that the diff output is generated by ::RSpec::Expectations.fail_with message, expected, actual, though I haven't found source for that yet.

I can put those two facts together however and do this in my spec:

    it 'renders the test.txt with the expected content' do
      approved_file = 'test/fixtures/approved.txt'
      expected_content = File.read(approved_file)

      verifier = Proc.new do |rendered_content|
        ::RSpec::Expectations.fail_with "does not match approved #{approved_file}", expected_content, rendered_content unless false # content == expected_content
      end

      expect(chef_run).to render_file('/etc/test.txt').with_content(verifier)
    end

In my code I have extracted that into a helper modelled after @isidore's http://approvaltests.com/ framework so that my spec can look like this:

    it 'renders the test.txt with the expected content' do
      verifier = Approvals.verifier_for('/etc/test.txt', 'test/fixtures/approved.txt')
      expect(chef_run).to render_file('/etc/test.txt').with_content(verifier)
    end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants