You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I ended up needing to do quite a bit of this in gargle, because of temp paths that vary, token hashes that vary by OS or R version, random backoff waiting times, etc.
Observations to guide implementation:
Sometimes you know the random or sensitive thing in advance (e.g. a temp path you've made and stored and used throughout the test). Then you can get away with sub(random_thing, replacement, snapshot_content, fixed = TRUE), which is nice.
More often, you need a regular expression. In fact, I often needed look ahead and look behind, so perl = TRUE needs to be possible.
Sometimes you need multiple replacements for 1 snapshot and they could be a mix of fixed string and regex.
Sometimes you are eliminating a matched expression, i.e. replacing with "".
One big bummer about the workaround is that the code in your snapshot ends up being completely uninformative (writeLines(out)), so you really have to rely on the test name (or use a more informative name than out). It will be much nicer to have this implemented properly in expect_snapshot().
I think we'd default to perl = TRUE, and if you needed fixed for some reason, you'd have to manually escape the regexp (which I don't think should happen very often).
Could we just disallow replacing with ""? You could always just do xxxxx instead?
Could we just disallow replacing with ""? You could always just do xxxxx instead?
Well I have a piece of text that may or may not appear, so no. (Obviously one could also examine the problem from the other direction and rethink the message.)
So I have these two scrubbers and use them in many tests. I can imagine this being a common pattern. So, yes, it would be nice to have a way to apply custom scrubbers inside expect_snapshot().
Activity
jennybc commentedon Mar 19, 2021
I ended up needing to do quite a bit of this in gargle, because of temp paths that vary, token hashes that vary by OS or R version, random backoff waiting times, etc.
Observations to guide implementation:
sub(random_thing, replacement, snapshot_content, fixed = TRUE), which is nice.perl = TRUEneeds to be possible."".jennybc commentedon Mar 19, 2021
Here's a real example of the basic form I used that also happens to illustrate most of the above:
One big bummer about the workaround is that the code in your snapshot ends up being completely uninformative (
writeLines(out)), so you really have to rely on the test name (or use a more informative name thanout). It will be much nicer to have this implemented properly inexpect_snapshot().hadley commentedon Mar 19, 2021
I think we'd default to
perl = TRUE, and if you neededfixedfor some reason, you'd have to manually escape the regexp (which I don't think should happen very often).Could we just disallow replacing with ""? You could always just do
xxxxxinstead?jennybc commentedon Mar 19, 2021
Well I have a piece of text that may or may not appear, so no. (Obviously one could also examine the problem from the other direction and rethink the message.)
hadley commentedon Mar 19, 2021
Ooooh I didn't think of that case.
hadley commentedon Jul 7, 2021
May be better to leave completely generic:
jennybc commentedon Jul 7, 2021
Here's where things have evolved to in googledrive:
https://github.com/tidyverse/googledrive/blob/6c8f5dd8469689a458ae9d67ad8a359cd293fe25/tests/testthat/helper.R#L35
So I have these two scrubbers and use them in many tests. I can imagine this being a common pattern. So, yes, it would be nice to have a way to apply custom scrubbers inside
expect_snapshot().Optionally scrub text from snapshot_output
Optionally transform snapshot_output (#1414)