Chef 13 Upgrade: Rubocop Changes for Testing `render_file` with ChefSpec and a `with_content` Block

As part of an upgrade from Chef 12 to Chef 13, this is one of the posts in which I've been detailing the issues I've encountered, and how I've resolved them .

When testing that Chef’s templates are being rendered correctly, the easiest way to do this is via render_file(...).with_content(&block).

However, when running the below code against Chef 13’s Rubocop, this gives the error Parenthesize the param render_file:

expect(chef_run).to render_file('/chef/.ssh/authorized_keys')
  .with_content do |content|
    expect(content).to match(%r{^ssh-rsa this is long key$})
    expect(content).to match(%r{^ssh-another key$})
    expect(content).to match(%r{^wibble deploy$})
  end

The fix is to have the whole render_file method call wrapped in parentheses, such as:

-expect(chef_run).to render_file('/chef/.ssh/authorized_keys')
-  .with_content { |content|
+expect(chef_run).to(render_file('/chef/.ssh/authorized_keys')
+  .with_content do |content|
     expect(content).to match(%r{^ssh-rsa this is long key$})
     expect(content).to match(%r{^ssh-another key$})
     expect(content).to match(%r{^wibble deploy$})
-  }
+  end)

Additionally, I’ve converted the multi-line block to a do / end block, as that was another complaint of Rubocop.

*****

Written by Jamie Tanna on 06 March 2018, and last updated on 03 April 2018.

Tags

Categories