Skip to content

expect_download fails with binary (xlsx, zip, ...) files #159

Closed

Description

@bersbersbers

Again, no reprex, but a stacktrace and code analysis. This happens when downloading an XLSX (technically a ZIP) file:

Error (test-xyz.R:24:5): xyz
Error in `brio::read_lines(old)`: embedded nul in string: 'PK\003\004\024\0\b\b\b\0\0\0!R\0\0\0...
Backtrace:
  1. get_tab_shapshots(...)
       at test-combo_sample-sep_xra_step.R:24:4
  2. app$expect_download(cran = TRUE, button, name = filename)
  3. shinytest2:::app_expect_download(...)
       at shinytest2/R/app-driver.R:621:6
  4. shinytest2:::app__expect_snapshot_file(...)
       at shinytest2/R/app-driver-expect-download.R:63:2
  6. testthat::expect_snapshot_file(...)
       at shinytest2/R/expect-snapshot.R:35:2
  7. snapshotter$take_file_snapshot(...)
       at testthat/R/snapshot-file.R:129:2
  8. testthat:::snapshot_file_equal(...)
       at testthat/R/snapshot-reporter.R:101:6
  9. testthat file_equal(cur_path, path)
       at testthat/R/snapshot-file.R:178:4
 10. brio::read_lines(old)
       at testthat/R/snapshot-file.R:259:2

I think the problem is here, in line 67:

app__expect_snapshot_file(
self, private,
snapshot_info$download_path,
cran = cran,
compare = testthat::compare_file_text
)

Which leads to the wrong reader being selected here:

https://github.com/r-lib/testthat/blob/22a68dd85b65c95173424760a78955b6c55f3848/R/snapshot-file.R#L248-L262

This probably went unnoticed because https://github.com/rstudio/shinytest2/blob/main/tests/testthat/apps/download/app.R only has csv files for download tests.

Activity

  1. bersbersbers commented on Apr 1, 2022

    @bersbersbers
    ContributorAuthor

    I think it would be good to expose compare as an argument to app_expect_download, like this:

    app_expect_download <- function(
      self,
      private,
      output,
      ...,
      compare = testthat::compare_file_text, # this is new
      name = NULL,
      cran = FALSE
    ) {
    

    and then call

      app__expect_snapshot_file(
        self, private,
        snapshot_info$download_path,
        cran = cran,
        compare = compare
      )
    

    below.

    This would also allow users to apply some postprocessing between downloading and comparing the file, which is easily possible in shinytest but not (currently) in shinytest2.

  2. added this to the v0.0.1 milestone on Apr 4, 2022
  3. added
    bug
    on Apr 4, 2022
  4. schloerke commented on Apr 4, 2022

    @schloerke
    Collaborator

    I like the compare arg idea 😄

  5. bersbersbers commented on Apr 4, 2022

    @bersbersbers
    ContributorAuthor

    One could even set the default argument of compare to NULL due to the following logic (although that does not cover the csv test cases in shinytest2):

    https://github.com/r-lib/testthat/blob/bb25375016ece879cecabc81fdf1d5b3072ab835/R/snapshot-file.R#L116-L120

  6. added a commit that references this issue on Apr 4, 2022
    72e48f7
  7. added a commit that references this issue on Apr 5, 2022
    b6929ab
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions