nealrichardson / httptest2

Utilities for testing R 📦s that use httr2
https://enpiar.com/httptest2/
Other
26 stars 6 forks source link

`with_mock_dir()` creates non-portable paths #36

Closed ErdaradunGaztea closed 2 months ago

ErdaradunGaztea commented 2 months ago

I'm developing a package that makes a heavy use of httr2 and, subsequently, of httptest2. I need to mock a lot of calls to different sites, and I have very limited control over the length of URL addresses that the httr2 requests use. This turns out to be a problem in conjunction with httptest2 strategy of placing mock files.

Running R CMD check yields:

Tarballs are only required to store paths of up to 100 bytes and cannot store those of more than 256 bytes, with restrictions including to 100 bytes for the final component.

Now, I have seen that the workaround is to set dir parameter to a single-letter path or something like that. I'm using /f/<$>/..., where <$> stands for any single lowercase letter or digit, each for a different mock; I've almost run out of those, by the way.

Unfortunately, there are paths that are still too long even with this strategy employed. For example, one of the addresses I'm calling as a part of my tests is raw.githubusercontent.com/turtletopia/versionsort/master/DESCRIPTION. It is 68 characters long by itself, but we have to add .txt at the end, so that 4 extra characters. We've got 28 characters left to spare, but there's more to come. The mocks are placed in test directory, i.e. <package>/tests/testthat/, which is 16 characters + the length of package name, which is 10 in my case. We're suddenly left with 2 characters, and we still need a directory for the mock. /f/x is already 4 characters, which brings the total up to 102, which in turn triggers the NOTE mentioned above.

I think I'd use hashing here, transforming the URL to a, say, 32 character hash. There would be chances of clashes, but probably negligible. And it'd solve one more issue I had: R CMD check detecting .github (name of a special Github repository) as a hidden folder, triggering another NOTE.

nealrichardson commented 2 months ago

The recommended way to handle this is to use a redactor function to shorten the URLs: https://enpiar.com/httptest2/articles/faq.html#how-do-i-fix-non-portable-file-paths

In your case, it looks like you could sub out raw.githubusercontent.com and that would get you enough headroom.

ErdaradunGaztea commented 2 months ago

Ooh, I did not see it! Guess I'll blame search engines not picking up this FAQ. Thanks a lot, that will work :)