2001-05-13 05:04:26 -03:00
|
|
|
# Test some Unicode file name semantics
|
|
|
|
# We dont test many operations on files other than
|
|
|
|
# that their names can be used with Unicode characters.
|
2003-12-02 21:29:56 -04:00
|
|
|
import os, glob, time, shutil
|
2004-03-21 16:55:47 -04:00
|
|
|
import unicodedata
|
2001-05-13 05:04:26 -03:00
|
|
|
|
2003-12-02 21:29:56 -04:00
|
|
|
import unittest
|
2009-03-26 18:10:30 -03:00
|
|
|
from test.test_support import run_unittest, TESTFN_UNICODE
|
2003-12-02 21:29:56 -04:00
|
|
|
from test.test_support import TESTFN_ENCODING, TESTFN_UNICODE_UNENCODEABLE
|
2001-05-13 05:04:26 -03:00
|
|
|
try:
|
2003-04-24 13:02:54 -03:00
|
|
|
TESTFN_ENCODED = TESTFN_UNICODE.encode(TESTFN_ENCODING)
|
2003-03-17 14:30:15 -04:00
|
|
|
except (UnicodeError, TypeError):
|
2003-03-08 06:25:31 -04:00
|
|
|
# Either the file system encoding is None, or the file name
|
|
|
|
# cannot be encoded in the file system encoding.
|
2009-03-26 18:10:30 -03:00
|
|
|
raise unittest.SkipTest("No Unicode filesystem semantics on this platform.")
|
2001-05-13 05:04:26 -03:00
|
|
|
|
2004-11-07 15:57:35 -04:00
|
|
|
if TESTFN_ENCODED.decode(TESTFN_ENCODING) != TESTFN_UNICODE:
|
|
|
|
# The file system encoding does not support Latin-1
|
|
|
|
# (which test_support assumes), so try the file system
|
|
|
|
# encoding instead.
|
|
|
|
import sys
|
|
|
|
try:
|
|
|
|
TESTFN_UNICODE = unicode("@test-\xe0\xf2", sys.getfilesystemencoding())
|
|
|
|
TESTFN_ENCODED = TESTFN_UNICODE.encode(TESTFN_ENCODING)
|
|
|
|
if '?' in TESTFN_ENCODED:
|
|
|
|
# MBCS will not report the error properly
|
|
|
|
raise UnicodeError, "mbcs encoding problem"
|
|
|
|
except (UnicodeError, TypeError):
|
2009-03-26 18:10:30 -03:00
|
|
|
raise unittest.SkipTest("Cannot find a suiteable filename.")
|
2004-11-07 15:57:35 -04:00
|
|
|
|
|
|
|
if TESTFN_ENCODED.decode(TESTFN_ENCODING) != TESTFN_UNICODE:
|
2009-03-26 18:10:30 -03:00
|
|
|
raise unittest.SkipTest("Cannot find a suitable filename.")
|
2004-11-07 15:57:35 -04:00
|
|
|
|
2003-12-02 21:29:56 -04:00
|
|
|
def remove_if_exists(filename):
|
|
|
|
if os.path.exists(filename):
|
|
|
|
os.unlink(filename)
|
2001-05-13 05:04:26 -03:00
|
|
|
|
2003-12-02 21:29:56 -04:00
|
|
|
class TestUnicodeFiles(unittest.TestCase):
|
|
|
|
# The 'do_' functions are the actual tests. They generally assume the
|
|
|
|
# file already exists etc.
|
2004-01-18 16:29:55 -04:00
|
|
|
|
2003-12-02 21:29:56 -04:00
|
|
|
# Do all the tests we can given only a single filename. The file should
|
|
|
|
# exist.
|
|
|
|
def _do_single(self, filename):
|
|
|
|
self.failUnless(os.path.exists(filename))
|
|
|
|
self.failUnless(os.path.isfile(filename))
|
2005-03-08 05:10:29 -04:00
|
|
|
self.failUnless(os.access(filename, os.R_OK))
|
2003-12-02 21:29:56 -04:00
|
|
|
self.failUnless(os.path.exists(os.path.abspath(filename)))
|
|
|
|
self.failUnless(os.path.isfile(os.path.abspath(filename)))
|
2005-03-08 05:10:29 -04:00
|
|
|
self.failUnless(os.access(os.path.abspath(filename), os.R_OK))
|
2003-12-02 21:29:56 -04:00
|
|
|
os.chmod(filename, 0777)
|
|
|
|
os.utime(filename, None)
|
|
|
|
os.utime(filename, (time.time(), time.time()))
|
|
|
|
# Copy/rename etc tests using the same filename
|
|
|
|
self._do_copyish(filename, filename)
|
|
|
|
# Filename should appear in glob output
|
|
|
|
self.failUnless(
|
|
|
|
os.path.abspath(filename)==os.path.abspath(glob.glob(filename)[0]))
|
|
|
|
# basename should appear in listdir.
|
|
|
|
path, base = os.path.split(os.path.abspath(filename))
|
2004-06-28 03:57:19 -03:00
|
|
|
if isinstance(base, str):
|
|
|
|
base = base.decode(TESTFN_ENCODING)
|
|
|
|
file_list = os.listdir(path)
|
|
|
|
# listdir() with a unicode arg may or may not return Unicode
|
|
|
|
# objects, depending on the platform.
|
|
|
|
if file_list and isinstance(file_list[0], str):
|
|
|
|
file_list = [f.decode(TESTFN_ENCODING) for f in file_list]
|
2004-03-21 16:55:47 -04:00
|
|
|
|
2004-06-28 03:57:19 -03:00
|
|
|
# Normalize the unicode strings, as round-tripping the name via the OS
|
|
|
|
# may return a different (but equivalent) value.
|
|
|
|
base = unicodedata.normalize("NFD", base)
|
2004-03-21 16:55:47 -04:00
|
|
|
file_list = [unicodedata.normalize("NFD", f) for f in file_list]
|
|
|
|
|
2004-06-28 03:57:19 -03:00
|
|
|
self.failUnless(base in file_list)
|
2004-01-18 16:29:55 -04:00
|
|
|
|
2003-12-02 21:29:56 -04:00
|
|
|
# Do as many "equivalancy' tests as we can - ie, check that although we
|
|
|
|
# have different types for the filename, they refer to the same file.
|
2008-03-19 20:01:17 -03:00
|
|
|
def _do_equivalent(self, filename1, filename2):
|
2003-12-02 21:29:56 -04:00
|
|
|
# Note we only check "filename1 against filename2" - we don't bother
|
|
|
|
# checking "filename2 against 1", as we assume we are called again with
|
|
|
|
# the args reversed.
|
|
|
|
self.failUnless(type(filename1)!=type(filename2),
|
|
|
|
"No point checking equivalent filenames of the same type")
|
|
|
|
# stat and lstat should return the same results.
|
|
|
|
self.failUnlessEqual(os.stat(filename1),
|
|
|
|
os.stat(filename2))
|
|
|
|
self.failUnlessEqual(os.lstat(filename1),
|
|
|
|
os.lstat(filename2))
|
|
|
|
# Copy/rename etc tests using equivalent filename
|
|
|
|
self._do_copyish(filename1, filename2)
|
2001-05-13 05:04:26 -03:00
|
|
|
|
2003-12-02 21:29:56 -04:00
|
|
|
# Tests that copy, move, etc one file to another.
|
|
|
|
def _do_copyish(self, filename1, filename2):
|
|
|
|
# Should be able to rename the file using either name.
|
|
|
|
self.failUnless(os.path.isfile(filename1)) # must exist.
|
|
|
|
os.rename(filename1, filename2 + ".new")
|
|
|
|
self.failUnless(os.path.isfile(filename1+".new"))
|
|
|
|
os.rename(filename1 + ".new", filename2)
|
|
|
|
self.failUnless(os.path.isfile(filename2))
|
2001-05-13 05:04:26 -03:00
|
|
|
|
2008-03-19 20:01:17 -03:00
|
|
|
shutil.copy(filename1, filename2 + ".new")
|
|
|
|
os.unlink(filename1 + ".new") # remove using equiv name.
|
|
|
|
# And a couple of moves, one using each name.
|
|
|
|
shutil.move(filename1, filename2 + ".new")
|
|
|
|
self.failUnless(not os.path.exists(filename2))
|
|
|
|
shutil.move(filename1 + ".new", filename2)
|
|
|
|
self.failUnless(os.path.exists(filename1))
|
|
|
|
# Note - due to the implementation of shutil.move,
|
|
|
|
# it tries a rename first. This only fails on Windows when on
|
|
|
|
# different file systems - and this test can't ensure that.
|
|
|
|
# So we test the shutil.copy2 function, which is the thing most
|
|
|
|
# likely to fail.
|
|
|
|
shutil.copy2(filename1, filename2 + ".new")
|
|
|
|
os.unlink(filename1 + ".new")
|
2001-05-13 05:04:26 -03:00
|
|
|
|
2004-03-21 16:55:47 -04:00
|
|
|
def _do_directory(self, make_name, chdir_name, encoded):
|
2003-12-02 21:29:56 -04:00
|
|
|
cwd = os.getcwd()
|
|
|
|
if os.path.isdir(make_name):
|
|
|
|
os.rmdir(make_name)
|
|
|
|
os.mkdir(make_name)
|
|
|
|
try:
|
|
|
|
os.chdir(chdir_name)
|
|
|
|
try:
|
2004-03-21 16:55:47 -04:00
|
|
|
if not encoded:
|
|
|
|
cwd_result = os.getcwdu()
|
|
|
|
name_result = make_name
|
|
|
|
else:
|
|
|
|
cwd_result = os.getcwd().decode(TESTFN_ENCODING)
|
|
|
|
name_result = make_name.decode(TESTFN_ENCODING)
|
|
|
|
|
|
|
|
cwd_result = unicodedata.normalize("NFD", cwd_result)
|
|
|
|
name_result = unicodedata.normalize("NFD", name_result)
|
|
|
|
|
|
|
|
self.failUnlessEqual(os.path.basename(cwd_result),name_result)
|
2003-12-02 21:29:56 -04:00
|
|
|
finally:
|
|
|
|
os.chdir(cwd)
|
|
|
|
finally:
|
|
|
|
os.rmdir(make_name)
|
2001-05-13 05:04:26 -03:00
|
|
|
|
2003-12-02 21:29:56 -04:00
|
|
|
# The '_test' functions 'entry points with params' - ie, what the
|
|
|
|
# top-level 'test' functions would be if they could take params
|
|
|
|
def _test_single(self, filename):
|
|
|
|
remove_if_exists(filename)
|
|
|
|
f = file(filename, "w")
|
|
|
|
f.close()
|
|
|
|
try:
|
|
|
|
self._do_single(filename)
|
|
|
|
finally:
|
|
|
|
os.unlink(filename)
|
|
|
|
self.failUnless(not os.path.exists(filename))
|
|
|
|
# and again with os.open.
|
|
|
|
f = os.open(filename, os.O_CREAT)
|
|
|
|
os.close(f)
|
|
|
|
try:
|
|
|
|
self._do_single(filename)
|
|
|
|
finally:
|
|
|
|
os.unlink(filename)
|
2004-01-18 16:29:55 -04:00
|
|
|
|
2003-12-02 21:29:56 -04:00
|
|
|
def _test_equivalent(self, filename1, filename2):
|
|
|
|
remove_if_exists(filename1)
|
|
|
|
self.failUnless(not os.path.exists(filename2))
|
|
|
|
f = file(filename1, "w")
|
|
|
|
f.close()
|
|
|
|
try:
|
2008-03-19 20:01:17 -03:00
|
|
|
self._do_equivalent(filename1, filename2)
|
2003-12-02 21:29:56 -04:00
|
|
|
finally:
|
|
|
|
os.unlink(filename1)
|
2001-05-13 05:04:26 -03:00
|
|
|
|
2003-12-02 21:29:56 -04:00
|
|
|
# The 'test' functions are unittest entry points, and simply call our
|
|
|
|
# _test functions with each of the filename combinations we wish to test
|
|
|
|
def test_single_files(self):
|
|
|
|
self._test_single(TESTFN_ENCODED)
|
|
|
|
self._test_single(TESTFN_UNICODE)
|
2003-12-03 18:16:47 -04:00
|
|
|
if TESTFN_UNICODE_UNENCODEABLE is not None:
|
|
|
|
self._test_single(TESTFN_UNICODE_UNENCODEABLE)
|
2002-01-06 22:11:43 -04:00
|
|
|
|
2003-12-02 21:29:56 -04:00
|
|
|
def test_equivalent_files(self):
|
|
|
|
self._test_equivalent(TESTFN_ENCODED, TESTFN_UNICODE)
|
|
|
|
self._test_equivalent(TESTFN_UNICODE, TESTFN_ENCODED)
|
2001-05-13 05:04:26 -03:00
|
|
|
|
2003-12-02 21:29:56 -04:00
|
|
|
def test_directories(self):
|
2008-03-19 20:01:17 -03:00
|
|
|
# For all 'equivalent' combinations:
|
2003-12-02 21:29:56 -04:00
|
|
|
# Make dir with encoded, chdir with unicode, checkdir with encoded
|
|
|
|
# (or unicode/encoded/unicode, etc
|
|
|
|
ext = ".dir"
|
2004-03-21 16:55:47 -04:00
|
|
|
self._do_directory(TESTFN_ENCODED+ext, TESTFN_ENCODED+ext, True)
|
|
|
|
self._do_directory(TESTFN_ENCODED+ext, TESTFN_UNICODE+ext, True)
|
|
|
|
self._do_directory(TESTFN_UNICODE+ext, TESTFN_ENCODED+ext, False)
|
|
|
|
self._do_directory(TESTFN_UNICODE+ext, TESTFN_UNICODE+ext, False)
|
2003-12-02 21:29:56 -04:00
|
|
|
# Our directory name that can't use a non-unicode name.
|
2003-12-03 18:16:47 -04:00
|
|
|
if TESTFN_UNICODE_UNENCODEABLE is not None:
|
|
|
|
self._do_directory(TESTFN_UNICODE_UNENCODEABLE+ext,
|
|
|
|
TESTFN_UNICODE_UNENCODEABLE+ext,
|
2004-03-21 16:55:47 -04:00
|
|
|
False)
|
2001-05-13 05:04:26 -03:00
|
|
|
|
2003-12-02 21:29:56 -04:00
|
|
|
def test_main():
|
2007-04-25 14:29:52 -03:00
|
|
|
run_unittest(__name__)
|
2001-05-13 05:04:26 -03:00
|
|
|
|
2003-12-02 21:29:56 -04:00
|
|
|
if __name__ == "__main__":
|
|
|
|
test_main()
|