nealrichardson / httptest

A Test Environment for HTTP Requests in R
https://enpiar.com/r/httptest/
Other
79 stars 10 forks source link

Should .mockPaths() return different paths from tests/testthat and from the root of the folder #50

Closed maelle closed 3 years ago

maelle commented 3 years ago

Context (hehe) for my issue: when debugging a test you might find yourself, I think, running use_mockapi() in your R session. But this won't use your existing mock files as they are in tests/testthat.

Therefore, should .mockPaths() work like testthat::test_path()?

nealrichardson commented 3 years ago

To put it in concrete terms, I think you're suggesting this, right?

diff --git a/R/mock-paths.R b/R/mock-paths.R
index a3f4f35..472082d 100644
--- a/R/mock-paths.R
+++ b/R/mock-paths.R
@@ -26,7 +26,7 @@
 #' @rdname mockPaths
 #' @export
 .mockPaths <- function (new) {
-    current <- getOption("httptest.mock.paths", default=".")
+    current <- getOption("httptest.mock.paths", default=testthat::test_path())
     if (missing(new)) {
         ## We're calling the function to get the list of paths
         return(current)
@@ -36,7 +36,7 @@
         invisible(current)
     } else {
         ## We're adding one or more paths
-        current <- unique(c(new, current))
+        current <- unique(c(testthat::test_path(new), current))
         options(httptest.mock.paths=current)
         invisible(current)
     }
maelle commented 3 years ago

Yes I think so, it'd work in both modes (tests, interactive test debugging)

nealrichardson commented 3 years ago

My only concern is that the vignette tooling (start_vignette(), change_state()) use .mockPaths() too, so they may need to be adapted--they take the vignettes dir as the current working directory, so test_path wouldn't work. (The test suite passes with this change, but as it turns out, the vignette tests also call .mockPaths() in the assertions, so they aren't actually testing that the vignette would work IRL.)

maelle commented 3 years ago

Ouch indeed more complex than I thought. Could .mockPaths detect it is run from knitr? Which wouldn't help if you're debugging vignette code outside of the vignette...

nealrichardson commented 3 years ago

Could .mockPaths detect it is run from knitr?

Sounds hacky; it would be less hacky for start_vignette and change_state just to use different code to set the paths. I'll think about this some more, see if there's a less bad way. Maybe .mockPaths() can check if the (relative) path exists and try test_path() if it does not?

maelle commented 3 years ago

But "." would be a special case?

jonkeane commented 3 years ago

The way that I “solved” this in dittodb was hacky, but has so far worked well enough: I set as default both “.” and “tests/testthat/“.

nealrichardson commented 3 years ago

Ah interesting, that could work well for loading mocks since if the file is not found in the first path it tries the next, but what about recording?

jonkeane commented 3 years ago

For recording, it defaults to the first path (so ”.”), which can be confusing (though in practice when I’m recording mocks during package development, I like that the recordings plop into the root directory so that I can look at them/move them over to tests/testthat).