Handle missing reflink dependency in tests

This commit is contained in:
Šarūnas Nejus 2024-09-04 16:42:20 +01:00
parent 06ca500ff2
commit 4f3b0faba5
No known key found for this signature in database
GPG key ID: DD28F6704DBE3435
4 changed files with 30 additions and 24 deletions

View file

@ -16,7 +16,6 @@
import os import os
import sys import sys
import tempfile
import unittest import unittest
from contextlib import contextmanager from contextlib import contextmanager
@ -66,13 +65,6 @@ _item_ident = 0
HAVE_SYMLINK = sys.platform != "win32" HAVE_SYMLINK = sys.platform != "win32"
HAVE_HARDLINK = sys.platform != "win32" HAVE_HARDLINK = sys.platform != "win32"
try:
import reflink
HAVE_REFLINK = reflink.supported_at(tempfile.gettempdir())
except ImportError:
HAVE_REFLINK = False
def item(lib=None): def item(lib=None):
global _item_ident global _item_ident

View file

@ -42,7 +42,7 @@ from enum import Enum
from functools import cached_property from functools import cached_property
from io import StringIO from io import StringIO
from pathlib import Path from pathlib import Path
from tempfile import mkdtemp, mkstemp from tempfile import gettempdir, mkdtemp, mkstemp
from typing import Any, ClassVar from typing import Any, ClassVar
from unittest.mock import patch from unittest.mock import patch
@ -147,6 +147,20 @@ def has_program(cmd, args=["--version"]):
return True return True
def check_reflink_support(path: str) -> bool:
try:
import reflink
except ImportError:
return False
return reflink.supported_at(path)
NEEDS_REFLINK = unittest.skipUnless(
check_reflink_support(gettempdir()), "no reflink support for libdir"
)
class TestHelper(_common.Assertions): class TestHelper(_common.Assertions):
"""Helper mixin for high-level cli and plugin tests. """Helper mixin for high-level cli and plugin tests.

View file

@ -12,8 +12,7 @@
# The above copyright notice and this permission notice shall be # The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software. # included in all copies or substantial portions of the Software.
"""Test file manipulation functionality of Item. """Test file manipulation functionality of Item."""
"""
import os import os
import shutil import shutil
@ -27,7 +26,7 @@ import beets.library
from beets import util from beets import util
from beets.test import _common from beets.test import _common
from beets.test._common import item, touch from beets.test._common import item, touch
from beets.test.helper import BeetsTestCase from beets.test.helper import NEEDS_REFLINK, BeetsTestCase
from beets.util import MoveOperation, bytestring_path, syspath from beets.util import MoveOperation, bytestring_path, syspath
@ -87,22 +86,22 @@ class MoveTest(BeetsTestCase):
self.i.move(operation=MoveOperation.COPY) self.i.move(operation=MoveOperation.COPY)
self.assertExists(self.path) self.assertExists(self.path)
@unittest.skipUnless(_common.HAVE_REFLINK, "need reflink") @NEEDS_REFLINK
def test_reflink_arrives(self): def test_reflink_arrives(self):
self.i.move(operation=MoveOperation.REFLINK_AUTO) self.i.move(operation=MoveOperation.REFLINK_AUTO)
self.assertExists(self.dest) self.assertExists(self.dest)
@unittest.skipUnless(_common.HAVE_REFLINK, "need reflink") @NEEDS_REFLINK
def test_reflink_does_not_depart(self): def test_reflink_does_not_depart(self):
self.i.move(operation=MoveOperation.REFLINK_AUTO) self.i.move(operation=MoveOperation.REFLINK_AUTO)
self.assertExists(self.path) self.assertExists(self.path)
@unittest.skipUnless(_common.HAVE_REFLINK, "need reflink") @NEEDS_REFLINK
def test_force_reflink_arrives(self): def test_force_reflink_arrives(self):
self.i.move(operation=MoveOperation.REFLINK) self.i.move(operation=MoveOperation.REFLINK)
self.assertExists(self.dest) self.assertExists(self.dest)
@unittest.skipUnless(_common.HAVE_REFLINK, "need reflink") @NEEDS_REFLINK
def test_force_reflink_does_not_depart(self): def test_force_reflink_does_not_depart(self):
self.i.move(operation=MoveOperation.REFLINK) self.i.move(operation=MoveOperation.REFLINK)
self.assertExists(self.path) self.assertExists(self.path)
@ -286,7 +285,7 @@ class AlbumFileTest(BeetsTestCase):
self.assertExists(oldpath) self.assertExists(oldpath)
self.assertExists(self.i.path) self.assertExists(self.i.path)
@unittest.skipUnless(_common.HAVE_REFLINK, "need reflink") @NEEDS_REFLINK
def test_albuminfo_move_reflinks_file(self): def test_albuminfo_move_reflinks_file(self):
oldpath = self.i.path oldpath = self.i.path
self.ai.album = "newAlbumName" self.ai.album = "newAlbumName"
@ -571,7 +570,7 @@ class SafeMoveCopyTest(BeetsTestCase):
self.assertExists(self.dest) self.assertExists(self.dest)
self.assertExists(self.path) self.assertExists(self.path)
@unittest.skipUnless(_common.HAVE_REFLINK, "need reflink") @NEEDS_REFLINK
def test_successful_reflink(self): def test_successful_reflink(self):
util.reflink(self.path, self.dest) util.reflink(self.path, self.dest)
self.assertExists(self.dest) self.assertExists(self.dest)
@ -585,7 +584,7 @@ class SafeMoveCopyTest(BeetsTestCase):
with pytest.raises(util.FilesystemError): with pytest.raises(util.FilesystemError):
util.copy(self.path, self.otherpath) util.copy(self.path, self.otherpath)
@unittest.skipUnless(_common.HAVE_REFLINK, "need reflink") @NEEDS_REFLINK
def test_unsuccessful_reflink(self): def test_unsuccessful_reflink(self):
with pytest.raises(util.FilesystemError): with pytest.raises(util.FilesystemError):
util.reflink(self.path, self.otherpath) util.reflink(self.path, self.otherpath)

View file

@ -13,8 +13,8 @@
# included in all copies or substantial portions of the Software. # included in all copies or substantial portions of the Software.
"""Tests for the general importer functionality. """Tests for the general importer functionality."""
"""
import os import os
import re import re
import shutil import shutil
@ -37,6 +37,7 @@ from beets.autotag import AlbumInfo, AlbumMatch, TrackInfo
from beets.importer import albums_in_dir from beets.importer import albums_in_dir
from beets.test import _common from beets.test import _common
from beets.test.helper import ( from beets.test.helper import (
NEEDS_REFLINK,
AsIsImporterMixin, AsIsImporterMixin,
AutotagStub, AutotagStub,
BeetsTestCase, BeetsTestCase,
@ -209,7 +210,7 @@ class NonAutotaggedImportTest(AsIsImporterMixin, ImportTestCase):
s2[stat.ST_DEV], s2[stat.ST_DEV],
) )
@unittest.skipUnless(_common.HAVE_REFLINK, "need reflinks") @NEEDS_REFLINK
def test_import_reflink_arrives(self): def test_import_reflink_arrives(self):
# Detecting reflinks is currently tricky due to various fs # Detecting reflinks is currently tricky due to various fs
# implementations, we'll just check the file exists. # implementations, we'll just check the file exists.
@ -392,7 +393,7 @@ class ImportSingletonTest(ImportTestCase):
assert len(self.lib.albums()) == 2 assert len(self.lib.albums()) == 2
def test_set_fields(self): def test_set_fields(self):
genre = "\U0001F3B7 Jazz" genre = "\U0001f3b7 Jazz"
collection = "To Listen" collection = "To Listen"
config["import"]["set_fields"] = { config["import"]["set_fields"] = {
@ -579,7 +580,7 @@ class ImportTest(ImportTestCase):
self.lib.items().get().data_source self.lib.items().get().data_source
def test_set_fields(self): def test_set_fields(self):
genre = "\U0001F3B7 Jazz" genre = "\U0001f3b7 Jazz"
collection = "To Listen" collection = "To Listen"
comments = "managed by beets" comments = "managed by beets"