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

Cleanly handle HTTP error messages, add stop_on_error and exclude arguments to app$snapshot #324

Open
wants to merge 19 commits into
base: main
Choose a base branch
from

Conversation

warnes
Copy link

@warnes warnes commented Aug 5, 2020

I have a troublesome object that changes in unimportant ways from run to run, and don't want to explicitly name all of the other objects that should be included in a snapshot.

This PR provides an 'exclude' argument to app$snapshot that allows specification of object names to be excluded from the snapshot.

It also modifies app$snapshot() to return the processed JSON text, rather than the raw text received from the headless browser.

I have created a 'recorded' test that exercises the new code, and have updated the 'in-depth' vignette.

All of the tests (including the one for new code) succeed, except for one existing 'recorded' test which yields:

── 1. Failure: doc.Rmd (@test-recorded-tests.R#38)  ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Not all shinytest scripts passed for recorded_tests/rmd-prerendered: mytest

Diff output:
==== mytest ====
diff /tmp/RtmpzMKXCF/working_dir/RtmpYKEARY/shinytest-diff-3aea446621dc/mytest-expected/001.json /tmp/RtmpzMKXCF/working_dir/RtmpYKEARY/shinytest-diff-3aea446621dc/mytest-current/001.json
15,18c15,18
<               "left": 1.456,
<               "right": 5.344,
<               "bottom": -0.0300000000000001,
<               "top": 0.780000000000003
---
>               "left": 1.464,
>               "right": 5.136,
>               "bottom": -0.0340000000000001,
>               "top": 0.884000000000003
diff /tmp/RtmpzMKXCF/working_dir/RtmpYKEARY/shinytest-diff-3aea446621dc/mytest-expected/002.json /tmp/RtmpzMKXCF/working_dir/RtmpYKEARY/shinytest-diff-3aea446621dc/mytest-current/002.json
15,18c15,18
<               "left": 1.564,
<               "right": 5.236,
<               "bottom": -0.0360000000000001,
<               "top": 0.936000000000001
---
>               "left": 1.568,
>               "right": 5.132,
>               "bottom": -0.0440000000000001,
>               "top": 1.144


══ testthat results  ════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════
[ OK: 216 | SKIPPED: 0 | WARNINGS: 0 | FAILED: 1 ]
1. Failure: doc.Rmd (@test-recorded-tests.R#38) 

I suspect this is due to a subtle difference in the configurations of my system and the test system.

@CLAassistant
Copy link

CLAassistant commented Aug 5, 2020

CLA assistant check
All committers have signed the CLA.

…`stop_on_error` (defaulting to TRUE) to control whether an error is captured and recorded *in* the snapshot, or is raised to stop execution.
…l cause execution to be aborted and a clean error message displayed. When FALSE, the error message is captured as part of the snapshot, no error is displayed, and execution continues.
@warnes
Copy link
Author

warnes commented Aug 7, 2020

sd_snapshot now checks the HTTP code returned from the server. When the new argument stop_on_error is TRUE (the default) the code and error message are extracted from the HTTP response to generate an informative error message. When FALSE, the HTTP response is converted into a proper JSON object and included in the snapshot, and execution continues.

@warnes warnes changed the title Add 'exclude' option to app$shinytest Add exclude and stop_on_error arguments to app$snapshot Aug 7, 2020
@warnes warnes changed the title Add exclude and stop_on_error arguments to app$snapshot Cleanly handle HTTP error messages, add stop_on_error and exclude arguments to app$snapshot Aug 7, 2020
@hadley
Copy link
Member

hadley commented Aug 8, 2020

Do you think you could decompose this into two separate PRs? (One handling HTTP failures and the other for excluding values from the snapshot). That would make them easier to review.

}


# Remove any items specified in ignore
Copy link
Author

@warnes warnes Aug 11, 2020

Choose a reason for hiding this comment

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

Implementing the exclude arguments only involves this code block and adding the omit argument to the sd_snapshot and app$snapshot function definitions and handoff.

@warnes
Copy link
Author

warnes commented Aug 11, 2020

Hi Hadley,

I can decompose if you wish, but the changes for handling exclude are so small it's hardly worth it:

  1. add the parameter exclude to the snapshot thunk function in app_driver.R
  2. add the parameter to the function definition for sd_snapshot
  3. add the twelve lines at https://github.com/rstudio/shinytest/pull/324/files#diff-34ed19d4ada8a5949aea349f6013f6b1R64-R75:
if(length(exclude)>0)
  {
    rObj <- jsonlite::fromJSON(txt=content)

    dropItems <- function(l, i) l[! names(l) %in% i]

    rObj$input <- dropItems(rObj$input, exclude)
    rObj$output <- dropItems(rObj$output, exclude)
    rObj$export <- dropItems(rObj$export, exclude)

    content <- jsonlite::toJSON(rObj)
  }

@hadley
Copy link
Member

hadley commented Aug 12, 2020

I think it's still useful because that makes the http error handling code easier for me to review (and there's a few style issues that'll need to be resolved along the way).

@hadley
Copy link
Member

hadley commented Aug 13, 2020

Actually, there are three places where shinytest currently uses httr::GET() with out checking the returned status, so unless you're keen to do it, I'll take a stab at them either tomorrow or next week.

hadley and others added 10 commits August 14, 2020 07:30
Includes some improvements to `$snapshotDownload()` docs which I required to understand what was going wrong.

Fixes rstudio#191. Fixes rstudio#192.
Merge branch 'master' of https://github.com/rstudio/shinytest

# Conflicts:
#	DESCRIPTION
#	R/snapshot.R
#	tests/testthat/recorded_tests/rmd-prerendered/tests/mytest-expected/001.json
#	tests/testthat/recorded_tests/rmd-prerendered/tests/mytest-expected/002.json
@warnes
Copy link
Author

warnes commented Aug 18, 2020

Right, that's a deliberate choice because I wanted to start simple (and I wasn't sure that shiny allowed you to customise the 404 output in a way that would make sense to test)

The shiny app can return at least 404 and 500, and perhaps other codes, in fact I used PR #348 to track down and fix a 500 error that only occurred when running a shinytest script.

@warnes
Copy link
Author

warnes commented Aug 18, 2020

Aside: I think the check failures on Windows are due to pingr::is_up improperly returning TRUE on Windows even if the shiny app isn't listening on the port..

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.

4 participants