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

Should the default mock path be absolute? #29

Open
maelle opened this issue Jul 13, 2023 · 6 comments
Open

Should the default mock path be absolute? #29

maelle opened this issue Jul 13, 2023 · 6 comments

Comments

@maelle
Copy link
Contributor

maelle commented Jul 13, 2023

Sorry in advance for not writing a proper reprex! I might come to this later. 😇

I think my current problem is similar to r-lib/testthat#1476 in testthat.

When changing the current directory in a test, then using say httptest2::with_mock_dir(), nothing gets recorded in the actual package. I think it's because httptest2:::default_mock_path() is relative to the current directory.

@nealrichardson
Copy link
Owner

I'd be interested to see what you do to trigger that--I can imagine how it might be possible, but I can't picture the exact workflow that would get you there.

@maelle
Copy link
Contributor Author

maelle commented Jul 13, 2023

I created a reprex! 😇

If I run the test in https://github.com/maelle/mockmock/blob/main/tests/testthat/test-blop.R no mock file is created.

@maelle
Copy link
Contributor Author

maelle commented Jul 13, 2023

(but if I comment out withr::local_dir(temp_dir), a mock file is created)

@nealrichardson
Copy link
Owner

I think a mock file is created, it's just in the temp_dir you created.

with_mock_dir() just checks if dir exists; whether dir is relative or absolute is up to you. In your example, if you want dir not to be relative to temp_dir, you could normalizePath before you change local_dir(temp_dir). I'm not sure what else would make sense for the function to do. Is there a realistic scenario where this is a problem?

@maelle
Copy link
Contributor Author

maelle commented Jul 17, 2023

I discovered this behavior, that I agree is not that surprising, when writing tests for fledge, a package helping with package development, in which tests we often change the current directory to the directory of a toy package.

In fledge tests, using snapshot tests without further customization works since the PR r-lib/testthat#1476 so I wonder whether that could be tweaked in httptest2 too.

I don't know exactly how that could happen. The initial current directory could be "recorded" by code stored in the setup/helper file that loads httptest2, and then used throughout the httptest2 calls in the tests.

But I agree I can also tweak my tests.

@maelle
Copy link
Contributor Author

maelle commented Jul 17, 2023

For the record, in the fledge branch I was working on, tests/testthat/helper-httptest2.r now is

library("httptest2")

test_path <- normalizePath(testthat::test_path())
Sys.setenv("fledge.test.path" = test_path)
with_mock_dir <- function(name, ...) {
  dir <- file.path(dirname(Sys.getenv("fledge.test.path")), "fixtures", name)
  httptest2::with_mock_dir(dir, ...)
}

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

No branches or pull requests

2 participants