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

Upgrade to testthat 3e #949

Merged
merged 21 commits into from
May 31, 2022
Merged

Upgrade to testthat 3e #949

merged 21 commits into from
May 31, 2022

Conversation

IndrajeetPatil
Copy link
Collaborator

@IndrajeetPatil IndrajeetPatil commented May 26, 2022

Closes #783

TODO:

  • Remove context() statements using-
xfun::gsub_dir(
  'context\\(.+\\)',
  '',
  ext = "R",
  dir = "tests/"
)
  • expect_equivalent() -> expect_equal()
  • Replace expect_known_value() with expect_snapshot_value()
  • Delete unnecessary binary files
  • Use expect_snapshot_error(...) instead of expect_error(..., regexp)
  • Use expect_snapshot_warning(...) instead of expect_warning(..., regexp)
  • Use expect_snapshot_message(...) instead of expect_message(..., regexp)

@github-actions
Copy link
Contributor

This is how benchmark results would change (along with a 95% confidence interval in relative change) if 2af428b is merged into main:

  •   :ballot_box_with_check:cache_applying: 32.2ms -> 32.5ms [-1.81%, +3.55%]
  •   :ballot_box_with_check:cache_recording: 1.55s -> 1.55s [-1.14%, +1.13%]
  •   :ballot_box_with_check:without_cache: 4.15s -> 4.14s [-1.22%, +0.83%]

Further explanation regarding interpretation and methodology can be found in the documentation.

@lorenzwalthert
Copy link
Collaborator

You are on a roll today 🥳. All checks green so far. Also, some checks in test-public_api.R (or similar) are checking for printed output too, maybe they should also use snapshots instead. Did you see https://www.tidyverse.org/blog/2022/02/upkeep-testthat-3/ ?

@lorenzwalthert
Copy link
Collaborator

Also note that some utf8 related tests give a different Output for me locally, but it passes CI and CRAN, so I suggest to not touch them.

@IndrajeetPatil
Copy link
Collaborator Author

maybe they should also use snapshots instead

Yes, I am now using snapshots for the relevant tests. Let's see if the tests pass now.

If I understand the previous testing infrastructure correctly, with a switch to snapshots, the binary files here should be deleted, right?

@codecov-commenter
Copy link

codecov-commenter commented May 28, 2022

Codecov Report

Merging #949 (ce61f69) into main (08301c0) will not change coverage.
The diff coverage is n/a.

@@           Coverage Diff           @@
##             main     #949   +/-   ##
=======================================
  Coverage   89.81%   89.81%           
=======================================
  Files          47       47           
  Lines        2611     2611           
=======================================
  Hits         2345     2345           
  Misses        266      266           

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 08301c0...ce61f69. Read the comment docs.

@IndrajeetPatil
Copy link
Collaborator Author

IndrajeetPatil commented May 28, 2022

@lorenzwalthert The following code:

 cache_info[, c("n", "size", "last_modified", "activated")]

produces the following output on R versions > 3.6

      # A tibble: 1 x 4
            n  size last_modified activated
        <int> <dbl> <dttm>        <lgl>    
      1     0     0 -Inf -Inf     FALSE 

and otherwise

      # A tibble: 1 x 4
            n  size last_modified activated
        <int> <dbl> <dttm>        <lgl>    
      1     0     0 NA NA         FALSE 

Should this test be skipped on older versions?

@lorenzwalthert
Copy link
Collaborator

Yes. That one can be skipped on old R versions. And the binary (but not the R files). can be deleted in that dir.

@github-actions
Copy link
Contributor

This is how benchmark results would change (along with a 95% confidence interval in relative change) if c963258 is merged into main:

  •   :ballot_box_with_check:cache_applying: 26.1ms -> 26.2ms [-0.67%, +1.85%]
  •   :ballot_box_with_check:cache_recording: 1.13s -> 1.13s [-0.82%, +0.45%]
  •   :ballot_box_with_check:without_cache: 3.06s -> 3.06s [-0.59%, +0.18%]

Further explanation regarding interpretation and methodology can be found in the documentation.

@lorenzwalthert Can you please check if this is what you want?

More specifically, do you want to include snaphots of entire error messages or are you happy with the current approach of using regexps to check error messages?
@lorenzwalthert
Copy link
Collaborator

lorenzwalthert commented May 30, 2022

@IndrajeetPatil about the errors: I am not sure we want to catch exact errors that are defined outside {styler}. In the past, R core changed the parser and I had to release a new styler version just to fix these. I am happy snapshotting our own errors but probably it's better to rely on regex for f1d87bd and the like. Is there a way to do that with the third edition?

@github-actions
Copy link
Contributor

This is how benchmark results would change (along with a 95% confidence interval in relative change) if a241065 is merged into main:

  •   :ballot_box_with_check:cache_applying: 34.1ms -> 33.3ms [-4.94%, +0.38%]
  •   :ballot_box_with_check:cache_recording: 1.62s -> 1.62s [-0.73%, +1.02%]
  •   :ballot_box_with_check:without_cache: 4.41s -> 4.4s [-1.18%, +0.76%]

Further explanation regarding interpretation and methodology can be found in the documentation.

@MichaelChirico
Copy link
Contributor

@IndrajeetPatil about the errors: I am not sure we want to catch exact errors that are defined outside {styler}. In the past, R core changed the parser and I had to release a new styler version just to fix these. I am happy snapshotting our own errors but probably it's better to rely on regex for f1d87bd and the like. Is there a way to do that with the third edition?

You might also consider generating the base error message yourself and comparing to that -- should be even more stable than regexes. data.table does this -- see the get_msg() helper in our main test suite: https://raw.githubusercontent.com/Rdatatable/data.table/master/inst/tests/tests.Rraw

@IndrajeetPatil IndrajeetPatil marked this pull request as ready for review May 31, 2022 06:21
@lorenzwalthert
Copy link
Collaborator

Thanks @MichaelChirico. But what's the advantage over expect_error() in that context. Since expect_error() is not (soft-)deprecated or superseded, I think we should just continue to use it for those type of non-styler messages.

@IndrajeetPatil
Copy link
Collaborator Author

but probably it's better to rely on regex for f1d87bd and the like. Is there a way to do that with the third edition?

Yeah, the third edition has no issues with using regex for testing error messages. So basically nothing needs to be changed with these tests.

In this case, there is nothing more that needs to be done for this PR.

@IndrajeetPatil
Copy link
Collaborator Author

@lorenzwalthert I am not sure why pre-commit GHA is failing now. Seems to do with end of files, but not sure how to fix this:

Screenshot 2022-05-31 at 08 55 35

@github-actions
Copy link
Contributor

This is how benchmark results would change (along with a 95% confidence interval in relative change) if 06241bd is merged into main:

  •   :ballot_box_with_check:cache_applying: 25.7ms -> 25.6ms [-1.15%, +0.82%]
  •   :ballot_box_with_check:cache_recording: 1.09s -> 1.1s [-0.18%, +1.29%]
  •   :ballot_box_with_check:without_cache: 2.9s -> 2.92s [-1.17%, +2.95%]

Further explanation regarding interpretation and methodology can be found in the documentation.

@lorenzwalthert
Copy link
Collaborator

lorenzwalthert commented May 31, 2022

Well there is some important definition / convention that a file should end with one and exactly one line break. That's enforced with that hook, so here are the options to fix it:

  • remove the redundant lines manually.
  • install pre-commit locally and run pre-commit run (makes sense if you want to use that framework in the future)
  • file an issue with testthat that every snapshot should contain one and exactly one empty line at EOF. That of course would be a breaking chnage to testthat snaps, but I think it's the right thing to do. Do you want to file an issue or should I?

@IndrajeetPatil
Copy link
Collaborator Author

I am not sure if this is a testthat issue.

This happens because the code output itself contains a line break, and then testthat adds another line break.

Screenshot 2022-05-31 at 11 22 16

As a result, utils.md contains two line breaks:

Screenshot 2022-05-31 at 11 24 25

@IndrajeetPatil
Copy link
Collaborator Author

The easiest and quickest solution here is to remove the extra line break manually.

@lorenzwalthert
Copy link
Collaborator

But then whenever you run testthat, it will add it, no? In that case, maybe we should exclude the snaps from pre-commit...

@IndrajeetPatil
Copy link
Collaborator Author

maybe we should exclude the snaps from pre-commit...

Indeed, that seems like the best way to go.

Sorry, not too familiar with the precommit framework. How can I exclude the .md files in tests/testthat/_snaps folder?

@lorenzwalthert
Copy link
Collaborator

lorenzwalthert commented May 31, 2022

Adapt .pre-commit-config.yaml like this

    -   id: end-of-file-fixer
        exclude: >
          (?x)^(
          \.Rd|
          tests/testthat/exception_handling/empty_file\.R|
          tests/testthat/parse_comments/eol_eof_spaces-.*|
          tests/testthat/reference-objects/.*|
          tests/testthat/_snaps/.*|
          )$

@github-actions
Copy link
Contributor

This is how benchmark results would change (along with a 95% confidence interval in relative change) if b38acf5 is merged into main:

  •   :ballot_box_with_check:cache_applying: 22.9ms -> 23.3ms [-1.29%, +4.43%]
  •   :ballot_box_with_check:cache_recording: 970ms -> 971ms [-0.35%, +0.59%]
  •   :ballot_box_with_check:without_cache: 2.6s -> 2.6s [-0.42%, +1.03%]

Further explanation regarding interpretation and methodology can be found in the documentation.

@lorenzwalthert
Copy link
Collaborator

Perfect @IndrajeetPatil. Thanks a lot. I also added a NEWS entry.

@lorenzwalthert lorenzwalthert merged commit 76a7421 into r-lib:main May 31, 2022
@github-actions
Copy link
Contributor

This is how benchmark results would change (along with a 95% confidence interval in relative change) if 19c5099 is merged into main:

  •   :ballot_box_with_check:cache_applying: 27.7ms -> 27.6ms [-1.35%, +0.96%]
  •   :ballot_box_with_check:cache_recording: 1.17s -> 1.17s [-1.08%, +0.48%]
  •   :ballot_box_with_check:without_cache: 3.16s -> 3.17s [-0.15%, +0.51%]

Further explanation regarding interpretation and methodology can be found in the documentation.

@IndrajeetPatil IndrajeetPatil deleted the 783_testthat3e branch May 31, 2022 11:49
@IndrajeetPatil
Copy link
Collaborator Author

Thanks, Lorenz!

Btw, precommit is really awesome, so thanks for that as well. Included it here:
https://github.com/IndrajeetPatil/awesome-r-pkgtools/blob/master/README.md

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.

Adopt to testthat 3e
4 participants