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

Extract option logic from Grover class #46

Merged
merged 19 commits into from
Feb 26, 2020

Conversation

willkoehler
Copy link
Contributor

@abrom I'm pushing up a WIP to get your feedback on the general shape of the solution. If it looks ok, I'll refine and add some specs.

More specifically

  • What do you think about deriving OptionsBuilder from Hash? Too "tricky"? I could also just return the options hash from an accessor method or something.
  • Also not sure on the best structure for OptionsFixer.new i.e. does OptionsFixer.new(options).run look like a reasonable API?
  • I'm also seeing several spec errors like expected: ["165", "42", "42"] got: [165, 42, 42] on my system (/spec/grover_spec.rb:424) which is making it hard to know if I broke anything. These specs are failing even on the Release v0.10.1 commit so I don't think they're caused by my changes. But I don't remember seeing these failures before on my system so that's kind of 🤔

Copy link
Contributor

@abrom abrom left a comment

Choose a reason for hiding this comment

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

  • I could go either way on extending Hash. Fine how it is though.
  • OptionsFixer.new...run looks fine. It's only an internal class so not too bothered
  • That's odd.. must be something different about the MiniMagick install on your machine?! I've tested it on Linux and Mac and not come across that before. In any case, easy fix will be to modify the mean_colour_statistics method ~L578 in the spec/grover_spec.rb file to something like:
  def mean_colour_statistics(image)
    %w[red green blue].map { |colour| image.data.dig('channelStatistics', colour, 'mean').to_s }
  end

(i.e. convert the mean colour coming from the image data hash into a string).

Weird!

lib/grover/options_builder.rb Show resolved Hide resolved
lib/grover/options_builder.rb Outdated Show resolved Hide resolved
lib/grover/options_builder.rb Outdated Show resolved Hide resolved
lib/grover/options_builder.rb Outdated Show resolved Hide resolved
lib/grover/options_fixer.rb Outdated Show resolved Hide resolved
lib/grover/options_fixer.rb Outdated Show resolved Hide resolved
@willkoehler
Copy link
Contributor Author

willkoehler commented Feb 23, 2020

In any case, easy fix will be to modify the mean_colour_statistics method ~L578 in the spec/grover_spec.rb file to something like:

  def mean_colour_statistics(image)
    %w[red green blue].map { |colour| image.data.dig('channelStatistics', colour, 'mean').to_s }
  end

👍 I was thinking the same thing. Thanks for confirming. I'll add it to the PR.

...and Code Climate is at it again 😬 https://codeclimate.com/github/Studiosity/grover/pull/46 But I was actually thinking of making a helper for all the fix_xx_options! methods that took an array of options and a transformation method/block. Maybe I'll look into that and see if that makes Code Climate happy.

@abrom
Copy link
Contributor

abrom commented Feb 23, 2020

...and Code Climate is at it again 😬 https://codeclimate.com/github/Studiosity/grover/pull/46 But I was actually thinking of making a helper for all the fix_xx_options! methods that took an array of options and a transformation method/block. Maybe I'll look into that and see if that makes Code Climate happy.

I commented about that: #46 (comment)

@willkoehler
Copy link
Contributor Author

  • That's odd.. must be something different about the MiniMagick install on your machine?!

It actually turned out to be a change in ImageMagick (I geeked out and dug through the source) ImageMagick/ImageMagick@bc978e0#diff-b911bfc2e4a717df4fc996ef7decb2ffL713-R716

@willkoehler
Copy link
Contributor Author

willkoehler commented Feb 23, 2020

Edit: Nevermind. I get it now. It converts strings that contains arrays into arrays.
Ex "['--something']" --> ['--something']

Can you clarify what this does?

grover/lib/grover.rb

Lines 322 to 326 in d455f75

def fix_array_options!(options)
return unless options['launch_args'].is_a? String
options['launch_args'] = YAML.safe_load options['launch_args']
end

From what I can see options['launch_args'] is usually (always?) an array.

Example: ["--disable-speech-api"] in

let(:launch_args) { ['--disable-speech-api'] }

So return unless options['launch_args'].is_a? String will return early and this method doesn't do anything?

@willkoehler
Copy link
Contributor Author

willkoehler commented Feb 23, 2020

FYI this spec is failing on my machine

grover/spec/grover_spec.rb

Lines 115 to 119 in d455f75

if /darwin/ =~ RUBY_PLATFORM
it { expect(pdf_reader.pages.first.attributes).to include(MediaBox: [0, 0, 841.91998, 1188]) }
else
it { expect(pdf_reader.pages.first.attributes).to include(MediaBox: [0, 0, 841.91998, 1189.91992]) }
end

It's using the expectation in the first branch. But on my system, I have MediaBox: [0, 0, 841.91998, 1189.91992], which matches the second expectation.

I'm running MacOS Catalina 10.15.3 with ImageMagick:

Version: ImageMagick 6.9.10-84 Q16 x86_64 2020-01-06 https://imagemagick.org
Copyright: © 1999-2020 ImageMagick Studio LLC
License: https://imagemagick.org/script/license.php
Features: Cipher DPC Modules
Delegates (built-in): bzlib freetype jng jp2 jpeg lcms ltdl lzma png tiff webp xml zlib

I can ignore for now. May be due to the newer version of ImageMagick on my system?

@willkoehler
Copy link
Contributor Author

Also FYI this spec is failing.

grover/spec/grover_spec.rb

Lines 313 to 317 in d455f75

it do
expect do
to_pdf
end.to raise_error Schmooze::JavaScript::Error, %r{Failed to launch chrome! spawn /totes/invalid/path}
end

With

expected Schmooze::JavaScript::Error with message matching /Failed to launch chrome! spawn \/totes\/invalid\/path/, got #<Schmooze::JavaScript::Error: Failed to launch the browser process! spawn /totes/invalid/path ENOENT

And this spec is failing

it { expect(image.data.dig('imageStatistics', 'all', 'mean').to_f).to be_within(1).of 97.7473 }

With

Failure/Error: it { expect(image.data.dig('imageStatistics', 'all', 'mean').to_f).to be_within(1).of 97.7473 }
       expected 161.497 to be within 1 of 97.7473

These failures are on master and not caused by my changes. I can ignore for now.

@willkoehler
Copy link
Contributor Author

willkoehler commented Feb 24, 2020

Please hold off on merge until I add a few specs...

@abrom
Copy link
Contributor

abrom commented Feb 24, 2020

Edit: Nevermind. I get it now. It converts strings that contains arrays into arrays.
Ex "['--something']" --> ['--something']

Correct. It allows the launch_args to be passed in through a meta tag (string value) and parse it as an array

@abrom
Copy link
Contributor

abrom commented Feb 24, 2020

Also FYI this spec is failing.

grover/spec/grover_spec.rb

Lines 313 to 317 in d455f75

it do
expect do
to_pdf
end.to raise_error Schmooze::JavaScript::Error, %r{Failed to launch chrome! spawn /totes/invalid/path}
end

With

expected Schmooze::JavaScript::Error with message matching /Failed to launch chrome! spawn \/totes\/invalid\/path/, got #<Schmooze::JavaScript::Error: Failed to launch the browser process! spawn /totes/invalid/path ENOENT

Right. Puppeteer have been moving towards making the library driver agnostic and it looks like they've released a new version that does that (along with some changes to the error message copy). Best solution is likely going to be to make the copy match a regex to allow for chrome or browser process. You're welcome to make that change, or I'll get to it when I can.

And this spec is failing

it { expect(image.data.dig('imageStatistics', 'all', 'mean').to_f).to be_within(1).of 97.7473 }

With

Failure/Error: it { expect(image.data.dig('imageStatistics', 'all', 'mean').to_f).to be_within(1).of 97.7473 }
       expected 161.497 to be within 1 of 97.7473

These failures are on master and not caused by my changes. I can ignore for now.

I've never been a fan of the method used here, but it was a lot more reliable than pixel testing. It appears to be passing in Travis.. no doubt slightly different versions of the ImageMagick toolset etc

@willkoehler
Copy link
Contributor Author

willkoehler commented Feb 24, 2020

Right. Puppeteer have been moving towards making the library driver agnostic and it looks like they've released a new version that does that (along with some changes to the error message copy). Best solution is likely going to be to make the copy match a regex to allow for chrome or browser process. You're welcome to make that change, or I'll get to it when I can.

I added fix for this. See 0d1b1c9

I've never been a fan of the method used here, but it was a lot more reliable than pixel testing. It appears to be passing in Travis.. no doubt slightly different versions of the ImageMagick toolset etc

I added a variant to fix the spec on my system See 3d10ae5. Feel free to pull if this isn't a direction you want to go.

@willkoehler
Copy link
Contributor Author

This is ready for another review. We might want to keep 561127f un-squashed to document that change. I'm happy to rebase to keep the commits we want and squash everything else.

@willkoehler
Copy link
Contributor Author

Had some insight overnight. I'm adding some specs for OptionsBuilder please don't merge until those are in.

@willkoehler
Copy link
Contributor Author

OptionsBuilder specs are in. Everything is ready for a review 👍

With the addition of OptionsBuilder specs, it looks like you can move some of the Grover specs to OptionsBuilder and eliminate a few of the dependencies on image content and PDF generation? I'm not sure of the full intention of those specs, so I'll leave that to you if it makes sense.

Copy link
Contributor

@abrom abrom left a comment

Choose a reason for hiding this comment

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

Change is looking really good @willkoehler 👍

I've noticed a few things that I think should be addressed before merging though. Please see comments below.

lib/grover/options_builder.rb Outdated Show resolved Hide resolved
lib/grover/options_builder.rb Show resolved Hide resolved
lib/grover/options_fixer.rb Outdated Show resolved Hide resolved
spec/grover_spec.rb Outdated Show resolved Hide resolved
spec/grover_spec.rb Outdated Show resolved Hide resolved
spec/grover/options_fixer_spec.rb Outdated Show resolved Hide resolved
spec/grover/options_fixer_spec.rb Outdated Show resolved Hide resolved
spec/grover/options_fixer_spec.rb Outdated Show resolved Hide resolved
spec/grover/options_builder_spec.rb Show resolved Hide resolved
@abrom
Copy link
Contributor

abrom commented Feb 25, 2020

This is ready for another review. We might want to keep 561127f un-squashed to document that change. I'm happy to rebase to keep the commits we want and squash everything else.

I'm not too bothered either way. It's a pretty trivial fix and only relates to the specs so not sure it really needs much documenting

@abrom
Copy link
Contributor

abrom commented Feb 25, 2020

OptionsBuilder specs are in. Everything is ready for a review 👍

With the addition of OptionsBuilder specs, it looks like you can move some of the Grover specs to OptionsBuilder and eliminate a few of the dependencies on image content and PDF generation? I'm not sure of the full intention of those specs, so I'll leave that to you if it makes sense.

Not sure I following your meaning here? The image/PDF reader dependencies are necessary to validate the conversion process is doing what it's supposed to. How would this change eliminate that need?

@willkoehler
Copy link
Contributor Author

Not sure I following your meaning here? The image/PDF reader dependencies are necessary to validate the conversion process is doing what it's supposed to. How would this change eliminate that need?

I though some of those specs may have just been checking that parameters were parsed and combined correctly. (Just a guess on my part since I don't have much context.) But if they are validating the conversion process, I agree, it wouldn't make sense to change them.

@willkoehler
Copy link
Contributor Author

I'm not too bothered either way. It's a pretty trivial fix and only relates to the specs so not sure it really needs much documenting

Good point. We should just squash it in.

@willkoehler
Copy link
Contributor Author

I fixed the last remaining broken spec on my system: c4abfb4

@willkoehler
Copy link
Contributor Author

willkoehler commented Feb 25, 2020

The failure is a flickering spec: https://travis-ci.org/Studiosity/grover/jobs/655011626. Is there a way I can re-run these? 👇

@willkoehler willkoehler requested a review from abrom February 25, 2020 18:40
@abrom
Copy link
Contributor

abrom commented Feb 26, 2020

No, but I can :)

I'm thinking I might add https://github.com/NoRedInk/rspec-retry at some point to help with the flake. It's often an issue with puppeteer just not booting. Bit odd..

Copy link
Contributor

@abrom abrom left a comment

Choose a reason for hiding this comment

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

Nice one @willkoehler, looks great! 👍

@abrom abrom merged commit 10e1adc into Studiosity:master Feb 26, 2020
@willkoehler
Copy link
Contributor Author

Nice one @willkoehler, looks great! 👍

Awesome 🙌 Let me know if anything else comes up that I can help with. It's a pleasure working with you.

@abrom
Copy link
Contributor

abrom commented Feb 26, 2020

Great, thanks @willkoehler. I can't think of any pressing changes needed at the moment. I'm currently updating the OptionsFixer to include some more of the missing type casting (some viewport options and some screenshot options).

There are plenty of things that Puppeteer can do that Grover doesn't support, but to be honest I'm not sure the merits of going all in on trying to get full API support.

@willkoehler willkoehler deleted the extract_option_logic branch February 26, 2020 18:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants