Characters counted in UTF-16 code points where appropriate.

NFD handled under OS X.
Windows bugfix when the relative directory path is short enough.
This commit is contained in:
Ionuț Ciocîrlan
2013-06-17 21:42:10 +03:00
parent e33a144ea5
commit fe8f6c2176
2 changed files with 172 additions and 63 deletions

View File

@@ -65,23 +65,61 @@ class SanitizeDateTest(unittest.TestCase):
class ShortFilenameTest(unittest.TestCase):
def __init__(self, *args, **kwargs):
self.maxDiff = None
self.root = os.path.join(sys.platform == "win32" and "X:\\" or "/", "x" * 10)
super(ShortFilenameTest, self).__init__(*args, **kwargs)
@unittest.skipUnless(sys.platform in ("win32", "darwin"), "windows / os x test")
def test_bmp_unicode_on_unicode_fs(self):
char = u"\N{LATIN SMALL LETTER SHARP S}"
fn = util.make_short_filename(self.root, os.path.join(*[char * 120] * 2))
self.assertEqual(fn, os.path.join(self.root, *[char * 120] * 2))
@unittest.skipUnless(sys.platform not in ("win32", "darwin"), "non-windows, non-osx test")
def test_bmp_unicode_on_nix(self):
char = u"\N{LATIN SMALL LETTER SHARP S}"
max_len = 255
divisor = len(char.encode(sys.getfilesystemencoding()))
fn = util.make_short_filename(self.root, os.path.join(*[char * 200] * 2))
self.assertEqual(fn, os.path.join(self.root, *[char * (max_len // divisor)] * 2))
@unittest.skipUnless(sys.platform == "darwin", "os x test")
def test_precomposed_unicode_on_osx(self):
char = u"\N{LATIN SMALL LETTER A WITH BREVE}"
max_len = 255
fn = util.make_short_filename(self.root, os.path.join(*[char * 200] * 2))
self.assertEqual(fn, os.path.join(self.root, *[char * (max_len // 2)] * 2))
@unittest.skipUnless(sys.platform == "win32", "windows test")
def test_unicode_on_windows(self):
fn = util.make_short_filename(self.root, os.path.join(*[u"\U00010916" * 100] * 2))
self.assertEqual(fn, os.path.join(self.root, *[u"\U00010916" * 100] * 2))
def test_nonbmp_unicode_on_windows(self):
char = u"\N{MUSICAL SYMBOL G CLEF}"
remaining = 259 - (3 + 10 + 1 + 200 + 1)
fn = util.make_short_filename(self.root, os.path.join(*[char * 100] * 2))
self.assertEqual(fn, os.path.join(self.root, char * 100, char * (remaining // 2)))
@unittest.skipUnless(sys.platform != "win32", "non-windows test")
def test_unicode_on_nix(self):
fn = util.make_short_filename(self.root, os.path.join(*[u"\U00010916" * 100] * 2))
self.assertEqual(fn, os.path.join(self.root, *[u"\U00010916" * (255/4)] * 2))
@unittest.skipUnless(sys.platform == "darwin", "os x test")
def test_nonbmp_unicode_on_osx(self):
char = u"\N{MUSICAL SYMBOL G CLEF}"
max_len = 255
fn = util.make_short_filename(self.root, os.path.join(*[char * 200] * 2))
self.assertEqual(fn, os.path.join(self.root, *[char * (max_len // 2)] * 2))
@unittest.skipUnless(sys.platform != "win32", "non-windows test")
def test_windows_compat_with_unicode_on_nix(self):
fn = util.make_short_filename(self.root, os.path.join(*[u"\U00010916" * 100] * 2), win_compat=True)
self.assertEqual(fn, os.path.join(self.root, *[u"\U00010916" * (255/4)] * 2))
@unittest.skipUnless(sys.platform not in ("win32", "darwin"), "non-windows, non-osx test")
def test_nonbmp_unicode_on_nix(self):
char = u"\N{MUSICAL SYMBOL G CLEF}"
max_len = 255
divisor = len(char.encode(sys.getfilesystemencoding()))
fn = util.make_short_filename(self.root, os.path.join(*[char * 100] * 2))
self.assertEqual(fn, os.path.join(self.root, *[char * (max_len // divisor)] * 2))
@unittest.skipUnless(sys.platform not in ("win32", "darwin"), "non-windows, non-osx test")
def test_nonbmp_unicode_on_nix_with_windows_compat(self):
char = u"\N{MUSICAL SYMBOL G CLEF}"
max_len = 255
remaining = 259 - (3 + 10 + 1 + 200 + 1)
divisor = len(char.encode(sys.getfilesystemencoding()))
fn = util.make_short_filename(self.root, os.path.join(*[char * 100] * 2), win_compat=True)
self.assertEqual(fn, os.path.join(self.root, char * (max_len // divisor), char * (remaining // 2)))
def test_windows_shortening(self):
fn = util.make_short_filename(self.root, os.path.join("a" * 200, "b" * 200, "c" * 200 + ".ext"), win_compat=True)
@@ -94,6 +132,12 @@ class ShortFilenameTest(unittest.TestCase):
win_compat=True, relative_to = self.root)
self.assertEqual(fn, os.path.join(self.root, "w" * 10, "x" * 10, "y" * 9, "z" * 9, "b" * 100, "c" * 100, "d" * 7 + ".ext"))
def test_windows_node_maxlength_shortening(self):
max_len = 226
remaining = 259 - (3 + 10 + 1 + max_len + 1)
fn = util.make_short_filename(self.root, os.path.join("a" * 300, "b" * 100 + ".ext"), win_compat=True)
self.assertEqual(fn, os.path.join(self.root, "a" * max_len, "b" * (remaining - 4) + ".ext"))
def test_windows_selective_shortening(self):
root = self.root + "x" * (44 - 10 - 3)
fn = util.make_short_filename(root, os.path.join(