Skip to content

Add ability to specify tolerance level for golden files - #76545

Closed
Piinks wants to merge 5 commits into
flutter:masterfrom
Piinks:goldenTolerance
Closed

Piinks wants to merge 5 commits into
flutter:masterfrom
Piinks:goldenTolerance

Conversation

@Piinks

@Piinks Piinks commented Feb 22, 2021 •

Copy link
Copy Markdown
Contributor

This adds the ability to specify a tolerance level for golden file comparison.
A good example of this use would be golden files that fail due to very small imperceptible changes, like from Skia rolls.
Here is one such case:
image

This image fails due to a realllly small diff.

Fixes #76337
Related to #71437

Pre-launch Checklist

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I read the Tree Hygiene wiki page, which explains my responsibilities.
  • I read and followed the Flutter Style Guide, including Features we expect every widget to implement.
  • I signed the CLA.
  • I listed at least one issue that this PR fixes in the description above.
  • I updated/added relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making or feature I am adding, or Hixie said the PR is test-exempt.
  • All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel on Discord.

@flutter-dashboard flutter-dashboard Bot added a: tests "flutter test", flutter_test, or one of our tests framework flutter/packages/flutter repository. See also f: labels. labels Feb 22, 2021
@google-cla google-cla Bot added the cla: yes label Feb 22, 2021

@CaseyHillers CaseyHillers left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM, thank you for the quick turn around!

Comment thread packages/flutter_test/lib/src/_matchers_io.dart
Comment thread packages/flutter_goldens/test/flutter_goldens_test.dart Outdated
Comment thread packages/flutter_test/lib/src/_matchers_io.dart
Comment thread packages/flutter_test/lib/src/goldens.dart
Comment thread packages/flutter_test/lib/src/goldens.dart Outdated
double _tolerance = 0.0;

@override
void setPrecisionTolerance(double precisionTolerance) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Could the base class "GoldenFileComparator" just have a getter/setter for this?

Comment thread packages/flutter_test/lib/src/_goldens_io.dart Outdated
throw 'must provide a Finder, Image, or Future<Image>';
}

goldenFileComparator.setPrecisionTolerance(precisionTolerance);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Instead of modifying the global state of the goldenFileComparator (which could be a surprising side effect), could the tolerance just be passed in below to the compare method?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I thought of going that route, but it might be more breaking that way. Still evaluating. The comparator class (and therefore compare function) is extended/implemented/overridden by many customers.

Comment thread packages/flutter_test/lib/src/goldens.dart Outdated
Comment on lines +80 to +83
double get precisionTolerance => _precisionTolerance;
double _precisionTolerance = 0.0;
/// Sets an allowable percentage of difference between golden file images.
set precisionTolerance(double value) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@goderbauer I made this getter/setter instead, it has ramifications for subclasses like the regular and web Trivial* comparators.. Not sure which is better.

@Piinks

Piinks commented Feb 26, 2021

Copy link
Copy Markdown
Contributor Author

I am not convinced adding this is a good idea yet, especially after conversation in #76831
Will wait to see decision from that discussion before proceeding.

@CaseyHillers what if this was just reduced to exposing the calculated diff in ComparisonResult? Then users could extend LocalFileComparator and just implement compare to allow pass through for whatever tolerance level the user would like?
In that case, you would not have to pass a tolerance level for each individual test. That feels a bit annoying and clunky.

@CaseyHillers

Copy link
Copy Markdown
Contributor

@CaseyHillers what if this was just reduced to exposing the calculated diff in ComparisonResult? Then users could extend LocalFileComparator and just implement compare to allow pass through for whatever tolerance level the user would like?
In that case, you would not have to pass a tolerance level for each individual test. That feels a bit annoying and clunky.

Either SGTM. In flutter/cocoon, the tests use a cocoon wrapper util for golden tests, so it's a one line change.

As another data point, not surfacing this as a parameter should have an example of constructing a comparator that can be used for enabling a diff tolerance.

@Piinks

Piinks commented Mar 1, 2021

Copy link
Copy Markdown
Contributor Author

I am going to close this and open a new PR to expose the ComparisonResult.diff, it'll be easier to start fresh than revert here. :)

@lukepighetti

Copy link
Copy Markdown
Contributor

@Piinks I didn't see this PR until I was nearly done with mine. I pushed it up here. #77703 If it's of value to you feel free to cannibalize.

@lukepighetti

lukepighetti commented Mar 15, 2021 •

Copy link
Copy Markdown
Contributor

@Piinks I tried #77014 and I have some feedback. I put it in here because the discussion in here is most pertinent to my feedback.

I am not convinced adding this is a good idea yet, especially after conversation in #76831
Will wait to see decision from that discussion before proceeding.

@CaseyHillers what if this was just reduced to exposing the calculated diff in ComparisonResult? Then users could extend LocalFileComparator and just implement compare to allow pass through for whatever tolerance level the user would like?

I'm trying this out now and I'm not sure how to proceed. There are private methods we don't have access to. Once you start unraveling that you're better off doing a copy-paste from what I can tell.

Screen Shot 2021-03-14 at 6 57 31 PM

From #76545 (comment)

In that case, you would not have to pass a tolerance level for each individual test. That feels a bit annoying and clunky.

I know the ship has sailed, but IMHO that's a non-issue because you can make a curried version of matchesGoldenFile with the tolerance baked in and use it everywhere. I also don't necessarily want to have a global tolerance. My strategy would be to employ this on problematic tests, picking a tolerance that matches what we typically see when they flake. An incremental tolerance that is easy to implement was my strong preference when I looked at addressing this.

@Piinks

Piinks commented Mar 15, 2021

Copy link
Copy Markdown
Contributor Author

Hey @lukepighetti! Thanks for the feedback. While implementing a custom comparator for flutter/cocoon (flutter/cocoon#1106), I too found this difficult. I just made some changes the other day to make this easier in #77825

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

a: tests "flutter test", flutter_test, or one of our tests framework flutter/packages/flutter repository. See also f: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add diffTolerance parameter to matchesGolden

4 participants