-
Notifications
You must be signed in to change notification settings - Fork 202
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
How to get the content of a rendered .erb template as a string? #778
Comments
Why can't you use |
@ekohl Because I'd like to query the content of my XML file with XPath, in order to improve the maintainability of my tests but, for all I know, only a regex is supported with |
This happens when the resource is not found in the catalog. We have similar helpers and they start with def get_content(subject, title)
is_expected.to contain_file(title)
content = subject.resource('file', title).send(:parameters)[:content]
content.split(/\n/).reject { |line| line =~ /(^#|^$|^\s+#)/ }
end While looking at this issue, I checked the source and the alternative is to use a proc: it { is_expected.to contain_concat__fragment(title).with_content(Proc.new { |content| content.split("\n") & expected_lines == expected_lines}) } The downside of this is that the error reporting is not great: the
A more advanced example would be to use a class: class ValidateConcatFragmentContent
def initialize(expected_lines)
@expected_lines = expected_lines
end
def call(content)
content.split("\n") & @expected_lines == @expected_lines
end
def to_s
@expected_lines.join("\n")
end
end
it { is_expected.to contain_concat__fragment(title).with_content(ValidateConcatFragmentContent.new(expected_lines)) } Now the error reporting is better:
Perhaps the code should be extended to allow raising an error to allow customizing the message. See rspec-puppet/lib/rspec-puppet/matchers/parameter_matcher.rb Lines 73 to 78 in 6db5fb0
|
Looking closer it appears the call was not actually called since it's now a class and not a proc. It just matched the to_s representation. It may be better to write a custom matcher or use the workaround of first checking if the catalog really contains the desired resource. |
I have a resource which creates an XML file via an erb template and I'd like to test its content via XPath. I found Nokogiri library and it works but I need to access the content of the rendered file as a string.
I tried:
but I get:
My puppet version is 5.5.17.
Is there an alternative way to access the content of a rendered .erb template as a string?
The text was updated successfully, but these errors were encountered: