httptest icon indicating copy to clipboard operation
httptest copied to clipboard

Using httptest in vignettes: How to make sure that recorded requests are actually used when building the package?

Open max-alletsee opened this issue 3 years ago • 5 comments

Thanks a lot for your great work on this package.

I'm using httptest in the vignette for a package that interacts with an API. The API credentials are stored in my .renviron file. I am using the process as described in the "Vignettes" vignette. After building the vignette locally, I also have the expected folders and file names on my computer that should then be used by httptest in the vignette.

However, it seems that httptest ignores the recorded requests. This is most obvious when building a source package and checking it on another machine, which leads to the following warning.

* checking re-building of vignette outputs ... WARNING
Error(s) in re-building vignettes:
  ...
--- re-building 'introduction_to_pocketapi.Rmd' using rmarkdown
Quitting from lines 85-90 (introduction_to_pocketapi.Rmd) 
Error: processing vignette 'introduction_to_pocketapi.Rmd' failed with diagnostics:
POCKET_CONSUMER_KEY does not exist as environment variable. Add it to your R environment or manually specify the consumer_key argument.
--- failed re-building 'introduction_to_pocketapi.Rmd'

SUMMARY: processing the following file failed:
  'introduction_to_pocketapi.Rmd'
Error: Vignette re-building failed.
Execution halted

I suspect that this is related to a similar problem that I experienced with unit testing when httptest was expecting a specific hashed file name for the stored request, but the file name when capturing the requests was different. Here, it was possible to identify the expected file name based on the error message and manually change it; however, I could not figure out which file name was expected for the requests in the vignette. Could you please help me understand how I could nudge httptest to use the actually stored requests for the vignette?

The full code incl. the recorded requests can be found here. It's built based on your vignette, but I've also checked the "rcrunch" and "pivotaltrackr" vignettes to check if there is an issue with the vignette code (and couldn't find any).

Thanks in advance for your help!

max-alletsee avatar Jul 23 '20 05:07 max-alletsee

Looking at the source of start_vignette() and the path you've provided, it's setting a mock path relative to the current working directory, which when building vignettes is vignettes/. In the pivotaltrackR example, it calls start_vignette("stories"), and I see that https://github.com/nealrichardson/pivotaltrackR/tree/master/vignettes/stories exists and contains mock responses. But I don't see that kind of directory structure in https://github.com/CorrelAid/pocketapi/tree/vignette/vignettes.

My guess is that however you built the vignette before was not running in the vignettes directory and so it didn't write the mocks to the right location. Try

setwd("vignettes")
rmarkdown::render("introduction_to_pocketapi.Rmd") # Aside, it's probably better to rename that to just "pocketapi.Rmd"

and see if you get the right directory structure built.

nealrichardson avatar Jul 23 '20 22:07 nealrichardson

Thanks, this is really helpful. Setting the working directory to "vignettes" before running the vignette for the first time indeed creates the folder structure "vignettes\ introduction_to_pocketapi\0 \getpocket.com\v3".

However, unfortunately I now run into issues when I try to re-knit the vignette after I've knitted it for the first time. I'm guessing that this is now related to the with_mock_api element of httptest? Below is the screenshot of the traceback; the German error message says "Missing value where TRUE/FALSE" is needed. My suspicion is that "simplifyvector = simplifyvector" could cause the error when the simplifyvector object is not logical, but I couldn't check this directly... I've tried to find the code in httptest which calls the fromJSON function, but couldn't find it easily...

Would you be so kind to help me with tracking down this error or point me in the right direction? (I've run the JSON file through a validator and there seem to be no issues with it.)

Thanks a lot!

httptest_pocketapi

max-alletsee avatar Jul 24 '20 08:07 max-alletsee

The error is from this line: https://github.com/jeroen/jsonlite/blob/57d7891cc2888e06585d5cca8e42ede6f77248d2/R/fromJSON.R#L86

Perhaps run with options(error=recover) and when the debugger opens, step into fromJSON and see what txt is.

nealrichardson avatar Jul 24 '20 15:07 nealrichardson

Hi Neal,

Thanks for your help, this helped me to find the issue: The JSON file I got from the API was encoded in ANSI, while fromJSON / httptest were assuming utf-8 and throwing errors for any other encoding. Converting the JSON file manually to utf-8 did the trick...

Would you mind if I would open a PR for the "Vignettes" vignette and add the points mentioned above? This might give others more guidance when working with httptest and help them solve similar problems directly without opening a new issue.

Thanks for your help, it's really appreciated! Max

max-alletsee avatar Jul 28 '20 18:07 max-alletsee

Hi @nealrichardson , Thanks for working on this and including this in the plan for your new release (#53). I have re-tried my example with the latest Github version of the package (Dec 17, f9ffe6408bf2514760c55b7d8244116ce6da16b7) and unfortunately the problem still persists. I have used the same reproducible example code as in the R script that I had shared via e-mail on Aug 2 and I end up with the same error message as before.

I fully agree with you that it makes sense to not merge my PR as it's better to address this via error messages instead of package documentation.

If I can help you to identify and resolve the issue, please don't hesitate to let me know. Thanks again for the work that you are doing!

max-alletsee avatar Dec 19 '20 11:12 max-alletsee