Monday, February 12, 2024

Compare a whole file content with rspec-puppet

 I was attempting to use the entire body of a file for a quick and dirty 'golden master' style test while I was refactoring some things.

I ran into a problem with having a multi-line string representing the full body of my output - I would run into this problem after putting in something like the following( https://github.com/puppetlabs/rspec-puppet/issues/100 ):

Failure/Error: it { is expected.to contain_file('/mock/file.sh').with_content(multiline_content) }
  expected that the catalogue would contain File[/mock/file.sh] with content set to supplied string
  Diff: 
    <The diff is empty, are your objects producing identical '#inspect' output?>

And my test looked something like this ...

my_content=<<-CONTENT.chomp
file line one
file line two
file last line
CONTENT

on_supported_os.each do |os, os_facts|
  context "on #{os}" do
  it { is_expected.to contain_file('/mock/file.sh').with_content(my_content) }
  end
end

Since I was unable to figure out a 'proper' solution (with and without the chomp) for this problem, I did the next best thing - I simply looped over each line of the file input, and made sure all the lines were in the content - this could give a false 'passing' as it doesn't prevent extra lines from being in there, or the line order isn't tracked, but it was 'good enough' for my short purpose

my_content=<<-CONTENT.chomp
file line one
file line two
file last line
CONTENT

on_supported_os.each do |os, os_facts|
  context "on #{os}" do
  it {
    my_content.each_line do |line|
      is_expected.to contain_file('/mock/file.sh').with_content(/#{Regexp.quote(line)}/) 
  }
  end
end


Hope this helps someone, or perhaps someone see's this and can tell me what I'm doing wrong!

No comments:

Post a Comment