-
Notifications
You must be signed in to change notification settings - Fork 104
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
(#821) Allow for unbalanced JSON fragments in RSpec output #822
Conversation
This isn't working for |
411b575
to
a87b9c6
Compare
052e66f
to
4817abc
Compare
lib/pdk/util.rb
Outdated
@@ -196,7 +197,7 @@ def in_module_root?(path = Dir.pwd) | |||
# @return [Hash, nil] subset of text as Hash of first valid JSON found, or nil if no valid | |||
# JSON found in the text | |||
def find_first_json_in(text) | |||
find_valid_json_in(text) | |||
PDK::Util::JSONFinder.new(text).objects.first |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Should this just be find_all_json_in(text).first
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
spec/acceptance/test_unit_spec.rb
Outdated
end | ||
|
||
after(:all) do | ||
FileUtils.rm_rf(File.join('spec', 'unit')) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems dangerous. What's cwd at this point?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cwd is the test module directory, but i can replace this with a couple of safer methods
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 I imagine my face if this ever went hay-wire would be 😕💥😢
Absolute path would be nice but that could be hard to get 🤷♂️
…tput This is an initial attempt at making the PDK::Util.find_valid_json_in method tolerate unbalanced JSON fragments nested as values inside of a larger, valid JSON document. Resolves puppetlabs#821
Regex + infinitely recursive data structures like JSON == sadness. It can be done, but its just not the right tool for the job. As the JSON formatter from rspec always outputs a condensed object, we can say with surety that each document will be on its own line. So rather than trying to scan for JSON objects using a regex, we can just split the output by lines and look for lines that start with `{` and end with `}` and attempt to parse them.
4817abc
to
984c140
Compare
around(:all) do |example| | ||
path = File.join('spec', 'unit', filename) | ||
FileUtils.mkdir_p(File.dirname(path)) | ||
File.open(path, 'w') { |f| f.puts content } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we care about line endings here? (not wb
so on Windows it'll use CRLF)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope, for the purposes of these short lived files, the line endings don't matter
This is an initial attempt at making the PDK::Util.find_valid_json_in
method tolerate unbalanced JSON fragments nested as values inside of
a larger, valid JSON document.
The simple change of making the closing brace optional allows the regex to more greedily match the largest valid document, but I would like to see this tested against more complex samples before declaring this a sufficient fix.
Resolves #821