Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

test-cache: Fix XDG tests, make tests more consistent #880

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

Conversation

hseg
Copy link

@hseg hseg commented Feb 10, 2025

Solving the issues I raised in #814 (comment), this commit set (at least on my machine) fixes the tests on Linux systems with custom XDG variables.

While I was there, I noticed the tests were a little inconsistent, so this PR also contains a couple of commits addressing that. Three of the changes I'm unsure about, so I'd appreciate a second look at them before this is merged. (I've labeled this as a draft until the two RFCs below are resolved, since even if they are accepted their commit messages need to be reworded in light of that)

  • fix(test_cache): Correct XDG testing logic

    The tests on Linux were relying on XDG_CACHE_HOME being set to its fallback value of ∼/.cache, which made them fail on systems with custom values for XDG_CACHE_HOME.

    To account for this, we take the cue from the Windows tests and set XDG_CACHE_HOME to a known, custom, value. This incidentally also tests that our logic can account for custom values of XDG_CACHE_HOME, so this doesn't need its own test.

    The value of /tmp/home/.cache was chosen as a cross between the Windows value of /tmp/AppData/Local and the fallback ∼/.cache

    (This commit is a MVP for the PR, the others can be ignored if desired)

    Fixes: More respectful convention for caching #814

  • RFC: fix(test_cache): Make tests more consistent

    • test_get_cache_dir: Follow all other tests in not casting to posix paths in checking paths are as expected, instead checking an equal Path object is roundtripped. NOTE: This edit I'm least confident in, not least since I don't have a Windows machine available to test this on.
    • test_get_cache_dir_old_pip: Remove logic checking whether _get_cache_dir accepts explicitly-set paths. It is duplicated from test_get_cache_dir, doesn't appear to be exercising any new codepath, and it is unclear why this case needs extra exercise
    • test_get_pip_cache, test_get_cache_dir_old_pip: follow the lead of test_get_cache_dir_pip_disabled_in_environment and inline the call to _get_cache_dir
  • RFC: fix(test_cache): Cross-OS test_get_cache_dir

    In view of my doubts above in re whether I broke test_get_cache_dir on Windows, run the test simulating all platforms. However, I am unsure that my understanding of pathlib and the relevant monkeypatching is correct, or if this commit is even needed.

The tests on Linux were relying on XDG_CACHE_HOME being set to its
fallback value of ∼/.cache, which made them fail on systems with custom
values for XDG_CACHE_HOME.
To account for this, we take the cue from the Windows tests and set
XDG_CACHE_HOME to a known, custom, value. This incidentally also tests
that our logic can account for custom values of XDG_CACHE_HOME, so this
doesn't need its own test.
The value of "/tmp/home/.cache" was chosen as a cross between the
Windows value of "/tmp/AppData/Local" and the fallback "∼/.cache"

Fixes: pypa#814
Copy link
Member

@woodruffw woodruffw left a comment

Choose a reason for hiding this comment

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

Thanks @hseg, these changes look great! Please undraft this when you think it's ready and I'll merge it.

Several changes to the tests in test_cache.py to make them more
consistent with each other

- test_get_cache_dir: Follow all other tests in not casting to posix
  paths in checking paths are as expected, instead checking an equal
  Path object is roundtripped.
  Also, run the test simulating all platform's path logic, like the
  other tests.
- test_get_cache_dir_old_pip: Remove logic checking whether
  _get_cache_dir accepts explicitly-set paths.
  It is duplicated from test_get_cache_dir, doesn't appear to be
  exercising any new codepath, and it is unclear why this case needs
  extra exercise
- test_get_pip_cache, test_get_cache_dir_old_pip: follow the lead of
  test_get_cache_dir_pip_disabled_in_environment and inline the call to
  _get_cache_dir
@hseg hseg marked this pull request as ready for review February 13, 2025 16:06
@hseg
Copy link
Author

hseg commented Feb 13, 2025

OK, removed RFC markers from the commit messages. I'd appreciate you running the new test_get_cache_dir on a Windows machine, as I'm not confident my monkeypatching accurately tests that behaviour on my Linux machine. But other than that, I'm happy with this one.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants