From 4181ed65e9e20ad80750898c8f01b76d01e1bea8 Mon Sep 17 00:00:00 2001 From: Thomas Kluyver Date: Tue, 1 Aug 2017 11:28:40 +0100 Subject: [PATCH] Add failing test (on Python 2) for unicode file names --- test_plat_other.py | 39 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/test_plat_other.py b/test_plat_other.py index 031eab5..8d36dff 100644 --- a/test_plat_other.py +++ b/test_plat_other.py @@ -1,3 +1,5 @@ +# encoding: utf-8 +import codecs import unittest import os from os import path as op @@ -7,8 +9,11 @@ from configparser import ConfigParser from tempfile import mkdtemp, NamedTemporaryFile, mktemp import shutil import stat +import sys # Could still use cleaning up. But no longer relies on ramfs. +HOMETRASH = send2trash.plat_other.HOMETRASH + def touch(path): with open(path, 'a'): os.utime(path, None) @@ -23,10 +28,38 @@ class TestHomeTrash(unittest.TestCase): self.assertFalse(op.exists(self.file.name)) def tearDown(self): - hometrash = send2trash.plat_other.HOMETRASH name = op.basename(self.file.name) - os.remove(op.join(hometrash, 'files', name)) - os.remove(op.join(hometrash, 'info', name+'.trashinfo')) + os.remove(op.join(HOMETRASH, 'files', name)) + os.remove(op.join(HOMETRASH, 'info', name+'.trashinfo')) + +def _filesys_enc(): + enc = sys.getfilesystemencoding() + # Get canonical name of codec + return codecs.lookup(enc).name + +@unittest.skipIf(_filesys_enc() == 'ascii', 'ASCII filesystem') +class TestUnicodeTrash(unittest.TestCase): + def setUp(self): + self.name = u'send2trash_tést1' + self.file = op.join(op.expanduser(b'~'), self.name.encode('utf-8')) + touch(self.file) + + def test_trash_bytes(self): + s2t(self.file) + assert not op.exists(self.file) + + def test_trash_unicode(self): + s2t(self.file.decode(sys.getfilesystemencoding())) + assert not op.exists(self.file) + + def tearDown(self): + if op.exists(self.file): + os.remove(self.file) + + trash_file = op.join(HOMETRASH, 'files', self.name) + if op.exists(trash_file): + os.remove(trash_file) + os.remove(op.join(HOMETRASH, 'info', self.name+'.trashinfo')) # # Tests for files on some other volume than the user's home directory.