test: separate case_sensitive unit tests from PathQueryTest

- move tests for case_sensitive to test_util.py, since this is not
  really the concern of PathQueryTest
- removes part of the tests, since the tests that patch os.path.samefile
  and os.path.exists are super brittle since they test the
  implementation rather than the functionality of case_sensitive().
  This is a prepartory step for actually changing the implementation,
  which would otherwise break the tests in a confusing way...
This commit is contained in:
wisp3rwind 2022-12-24 13:36:53 +01:00
parent d24cf69269
commit a6d74686d8
2 changed files with 25 additions and 35 deletions

View file

@ -603,40 +603,7 @@ class PathQueryTest(_common.LibTestCase, TestHelper, AssertsMixin):
results = self.lib.items(makeq(case_sensitive=False)) results = self.lib.items(makeq(case_sensitive=False))
self.assert_items_matched(results, ['path item', 'caps path']) self.assert_items_matched(results, ['path item', 'caps path'])
# Check for correct case sensitivity selection (this check
# only works on non-Windows OSes).
with _common.system_mock('Darwin'):
# exists = True and samefile = True => Case insensitive
q = makeq()
self.assertEqual(q.case_sensitive, False)
# exists = True and samefile = False => Case sensitive
self.patcher_samefile.stop()
self.patcher_samefile.start().return_value = False
try:
q = makeq()
self.assertEqual(q.case_sensitive, True)
finally:
self.patcher_samefile.stop()
self.patcher_samefile.start().return_value = True
# Test platform-aware default sensitivity when the library path
# does not exist. For the duration of this check, we change the
# `os.path.exists` mock to return False.
self.patcher_exists.stop()
self.patcher_exists.start().return_value = False
try:
with _common.system_mock('Darwin'):
q = makeq()
self.assertEqual(q.case_sensitive, True)
with _common.system_mock('Windows'):
q = makeq()
self.assertEqual(q.case_sensitive, False)
finally:
# Restore the `os.path.exists` mock to its original state.
self.patcher_exists.stop()
self.patcher_exists.start().return_value = True
@patch('beets.library.os') @patch('beets.library.os')
def test_path_sep_detection(self, mock_os): def test_path_sep_detection(self, mock_os):

View file

@ -14,10 +14,11 @@
"""Tests for base utils from the beets.util package. """Tests for base utils from the beets.util package.
""" """
import sys
import re
import os import os
import platform
import re
import subprocess import subprocess
import sys
import unittest import unittest
from unittest.mock import patch, Mock from unittest.mock import patch, Mock
@ -122,6 +123,28 @@ class UtilTest(unittest.TestCase):
self.assertEqual(exc_context.exception.returncode, 1) self.assertEqual(exc_context.exception.returncode, 1)
self.assertEqual(exc_context.exception.cmd, 'taga \xc3\xa9') self.assertEqual(exc_context.exception.cmd, 'taga \xc3\xa9')
def test_case_sensitive_default(self):
path = util.bytestring_path(util.normpath(
"/this/path/does/not/exist",
))
self.assertEqual(
util.case_sensitive(path),
platform.system() != 'Windows',
)
@unittest.skipIf(sys.platform == 'win32', 'fs is not case sensitive')
def test_case_sensitive_detects_sensitive(self):
# FIXME: Add tests for more code paths of case_sensitive()
# when the filesystem on the test runner is not case sensitive
pass
@unittest.skipIf(sys.platform != 'win32', 'fs is case sensitive')
def test_case_sensitive_detects_insensitive(self):
# FIXME: Add tests for more code paths of case_sensitive()
# when the filesystem on the test runner is case sensitive
pass
class PathConversionTest(_common.TestCase): class PathConversionTest(_common.TestCase):
def test_syspath_windows_format(self): def test_syspath_windows_format(self):