IBam
IBam

Reputation: 11814

Testing result of Chef FileEdit using ChefSpec

I'm trying to use FileEdit to insert a line into a file if it doesn't already exist:

ruby_block "insert_line" do
block do
  file = Chef::Util::FileEdit.new("/etc/environment")
  file.insert_line_if_no_match("/blah/", "blah")
  file.write_file
end

end

I then try to test this using chefspec:

it 'adds blah to the file' do
  expect(chef_run).to render_file('/etc/environment').with_content(/^blah$/)
end

The test fails, which I suspect is because render_file doesn't support FileEdit. My question is whether there's an equivalent? Or am I doing something stupid?

Upvotes: 2

Views: 309

Answers (1)

D.Shawley
D.Shawley

Reputation: 59563

I haven't tried this in ChefSpec but I suspect that you are correct in that render_file doesn't hook into Chef::Util::FileEdit. I would move this check into an integration test using Test Kitchen and BATS. After trying a few different techniques, I've settled on writing the majority of my tests as integration tests. When I need to test library code, I treat it as Ruby code and do straight RSpec testing. It is actually pretty rare for me to write ChefSpec tests now. I've found that it results in really fast test runs but a lot of duplication between tests and cookbook.

Upvotes: 1

Related Questions