[Test] Fix path tests on windows (#5803)

## Description

Fixes #5802.

Today, tests fail on most Windows machines because we hard-code `D:` as
the root drive, but most machines use `C:`. This change uses the same
normalization function in the test assertion to ensure the drives match.

## To Do

- [ ] ~~Documentation.~~
- [x] Changelog.
- [x] Tests. (this is a tests change)

## What changed?

* Updated tests to generate the drive name via normalization, instead of
hard-coding `D:`.
* Updated the `Item::destination()` method to document the
`relative_to_libdir` param.

## How tested?

* [x] Tests pass locally.
This commit is contained in:
Ben Stolovitz 2025-05-26 13:24:57 -04:00 committed by GitHub
parent 60f24cdc74
commit da5ec00aaf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 13 additions and 3 deletions

View file

@ -1084,7 +1084,9 @@ class Item(LibModel):
(i.e., where the file ought to be). (i.e., where the file ought to be).
The path is returned as a bytestring. ``basedir`` can override the The path is returned as a bytestring. ``basedir`` can override the
library's base directory for the destination. library's base directory for the destination. If ``relative_to_libdir``
is true, returns just the fragment of the path underneath the library
base directory.
""" """
db = self._check_db() db = self._check_db()
basedir = basedir or db.directory basedir = basedir or db.directory

View file

@ -28,6 +28,8 @@ Bug fixes:
* :doc:`plugins/musicbrainz`: fix regression where user configured * :doc:`plugins/musicbrainz`: fix regression where user configured
``extra_tags`` have been read incorrectly. ``extra_tags`` have been read incorrectly.
:bug:`5788` :bug:`5788`
* tests: Fix library tests failing on Windows when run from outside ``D:/``.
:bug:`5802`
For packagers: For packagers:

View file

@ -34,7 +34,7 @@ from beets.library import Album
from beets.test import _common from beets.test import _common
from beets.test._common import item from beets.test._common import item
from beets.test.helper import BeetsTestCase, ItemInDBTestCase from beets.test.helper import BeetsTestCase, ItemInDBTestCase
from beets.util import as_string, bytestring_path, syspath from beets.util import as_string, bytestring_path, normpath, syspath
# Shortcut to path normalization. # Shortcut to path normalization.
np = util.normpath np = util.normpath
@ -553,6 +553,9 @@ class ItemFormattedMappingTest(ItemInDBTestCase):
class PathFormattingMixin: class PathFormattingMixin:
"""Utilities for testing path formatting.""" """Utilities for testing path formatting."""
i: beets.library.Item
lib: beets.library.Library
def _setf(self, fmt): def _setf(self, fmt):
self.lib.path_formats.insert(0, ("default", fmt)) self.lib.path_formats.insert(0, ("default", fmt))
@ -560,9 +563,12 @@ class PathFormattingMixin:
if i is None: if i is None:
i = self.i i = self.i
# Handle paths on Windows.
if os.path.sep != "/": if os.path.sep != "/":
dest = dest.replace(b"/", os.path.sep.encode()) dest = dest.replace(b"/", os.path.sep.encode())
dest = b"D:" + dest
# Paths are normalized based on the CWD.
dest = normpath(dest)
actual = i.destination() actual = i.destination()