Skip to content
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

Merged
merged 4 commits into from
Mar 10, 2020

Conversation

scotje
Copy link
Contributor

@scotje scotje commented Dec 18, 2019

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

@scotje scotje added the WIP label Dec 18, 2019
@scotje scotje added this to the January 2020 milestone Dec 18, 2019
@scotje scotje requested a review from a team as a code owner December 18, 2019 19:52
@scotje
Copy link
Contributor Author

scotje commented Dec 18, 2019

This isn't working for --parallel mode, going to iterate a bit more.

@coveralls
Copy link

coveralls commented Dec 18, 2019

Coverage Status

Coverage increased (+0.0002%) to 91.27% when pulling 984c140 on scotje:gh821_unbalanced_rspec_json into 807b1b7 on puppetlabs:master.

@scotje scotje modified the milestones: January 2020, February 2020 Jan 28, 2020
@rodjek rodjek force-pushed the gh821_unbalanced_rspec_json branch from 411b575 to a87b9c6 Compare February 24, 2020 02:00
@scotje scotje modified the milestones: February 2020, March 2020 Feb 25, 2020
@rodjek rodjek force-pushed the gh821_unbalanced_rspec_json branch from 052e66f to 4817abc Compare March 3, 2020 04:41
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
Copy link
Contributor

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 ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

end

after(:all) do
FileUtils.rm_rf(File.join('spec', 'unit'))
Copy link
Contributor

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?

Copy link
Contributor

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

Copy link
Contributor

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 🤷‍♂️

scotje and others added 4 commits March 4, 2020 15:10
…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.
@rodjek rodjek force-pushed the gh821_unbalanced_rspec_json branch from 4817abc to 984c140 Compare March 4, 2020 04:11
@rodjek rodjek changed the title WIP: (#821) Allow for unbalanced JSON fragments in RSpec output (#821) Allow for unbalanced JSON fragments in RSpec output Mar 4, 2020
around(:all) do |example|
path = File.join('spec', 'unit', filename)
FileUtils.mkdir_p(File.dirname(path))
File.open(path, 'w') { |f| f.puts content }
Copy link
Contributor

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)

Copy link
Contributor

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

@rodjek rodjek merged commit f3c3036 into puppetlabs:master Mar 10, 2020
@glennsarti glennsarti modified the milestones: March 2020, April 2020 Mar 27, 2020
@scotje scotje added the bug label May 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Generated descriptions for certain test comparisons break JUnit output
4 participants