annotate Lib/test/test_unicode_file.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents 457aa8da490a
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
17817
f7c385226f65 Add support for Windows using "mbcs" as the default Unicode encoding when dealing with the file system. As discussed on python-dev and in patch 410465.
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
1 # Test some Unicode file name semantics
f7c385226f65 Add support for Windows using "mbcs" as the default Unicode encoding when dealing with the file system. As discussed on python-dev and in patch 410465.
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
2 # We dont test many operations on files other than
f7c385226f65 Add support for Windows using "mbcs" as the default Unicode encoding when dealing with the file system. As discussed on python-dev and in patch 410465.
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
3 # that their names can be used with Unicode characters.
31079
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
4 import os, glob, time, shutil
31755
b39fdc9c807e Normalized files in test_unicode_file to eliminate failure on OSX
Nicholas Bastin <nick.bastin@gmail.com>
parents: 31389
diff changeset
5 import unicodedata
17817
f7c385226f65 Add support for Windows using "mbcs" as the default Unicode encoding when dealing with the file system. As discussed on python-dev and in patch 410465.
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
6
31079
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
7 import unittest
52355
228cfb6e2900 fix incorrect auto-translation of TestSkipped -> unittest.SkipTest
Benjamin Peterson <benjamin@python.org>
parents: 52353
diff changeset
8 from test.test_support import run_unittest, TESTFN_UNICODE
64602
457aa8da490a Recorded merge of revisions 83987 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 58460
diff changeset
9 from test.test_support import TESTFN_ENCODING, TESTFN_UNENCODABLE
17817
f7c385226f65 Add support for Windows using "mbcs" as the default Unicode encoding when dealing with the file system. As discussed on python-dev and in patch 410465.
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
10 try:
28774
c9bee11eaa8b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 28316
diff changeset
11 TESTFN_ENCODED = TESTFN_UNICODE.encode(TESTFN_ENCODING)
28316
3e19e3b72d9b Test for UnicodeError instead of ImportError to determine whether
Martin v. Löwis <martin@v.loewis.de>
parents: 28237
diff changeset
12 except (UnicodeError, TypeError):
28237
48a61a4b73a1 Skip the test if TESTFN_ENCODING is None. Fixes #699386.
Martin v. Löwis <martin@v.loewis.de>
parents: 25535
diff changeset
13 # Either the file system encoding is None, or the file name
48a61a4b73a1 Skip the test if TESTFN_ENCODING is None. Fixes #699386.
Martin v. Löwis <martin@v.loewis.de>
parents: 25535
diff changeset
14 # cannot be encoded in the file system encoding.
52355
228cfb6e2900 fix incorrect auto-translation of TestSkipped -> unittest.SkipTest
Benjamin Peterson <benjamin@python.org>
parents: 52353
diff changeset
15 raise unittest.SkipTest("No Unicode filesystem semantics on this platform.")
17817
f7c385226f65 Add support for Windows using "mbcs" as the default Unicode encoding when dealing with the file system. As discussed on python-dev and in patch 410465.
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
16
33938
81cbf031549a Try a different filename if the Latin-1 file name cannot
Martin v. Löwis <martin@v.loewis.de>
parents: 32432
diff changeset
17 if TESTFN_ENCODED.decode(TESTFN_ENCODING) != TESTFN_UNICODE:
81cbf031549a Try a different filename if the Latin-1 file name cannot
Martin v. Löwis <martin@v.loewis.de>
parents: 32432
diff changeset
18 # The file system encoding does not support Latin-1
81cbf031549a Try a different filename if the Latin-1 file name cannot
Martin v. Löwis <martin@v.loewis.de>
parents: 32432
diff changeset
19 # (which test_support assumes), so try the file system
81cbf031549a Try a different filename if the Latin-1 file name cannot
Martin v. Löwis <martin@v.loewis.de>
parents: 32432
diff changeset
20 # encoding instead.
81cbf031549a Try a different filename if the Latin-1 file name cannot
Martin v. Löwis <martin@v.loewis.de>
parents: 32432
diff changeset
21 import sys
81cbf031549a Try a different filename if the Latin-1 file name cannot
Martin v. Löwis <martin@v.loewis.de>
parents: 32432
diff changeset
22 try:
81cbf031549a Try a different filename if the Latin-1 file name cannot
Martin v. Löwis <martin@v.loewis.de>
parents: 32432
diff changeset
23 TESTFN_UNICODE = unicode("@test-\xe0\xf2", sys.getfilesystemencoding())
81cbf031549a Try a different filename if the Latin-1 file name cannot
Martin v. Löwis <martin@v.loewis.de>
parents: 32432
diff changeset
24 TESTFN_ENCODED = TESTFN_UNICODE.encode(TESTFN_ENCODING)
81cbf031549a Try a different filename if the Latin-1 file name cannot
Martin v. Löwis <martin@v.loewis.de>
parents: 32432
diff changeset
25 if '?' in TESTFN_ENCODED:
81cbf031549a Try a different filename if the Latin-1 file name cannot
Martin v. Löwis <martin@v.loewis.de>
parents: 32432
diff changeset
26 # MBCS will not report the error properly
81cbf031549a Try a different filename if the Latin-1 file name cannot
Martin v. Löwis <martin@v.loewis.de>
parents: 32432
diff changeset
27 raise UnicodeError, "mbcs encoding problem"
81cbf031549a Try a different filename if the Latin-1 file name cannot
Martin v. Löwis <martin@v.loewis.de>
parents: 32432
diff changeset
28 except (UnicodeError, TypeError):
52355
228cfb6e2900 fix incorrect auto-translation of TestSkipped -> unittest.SkipTest
Benjamin Peterson <benjamin@python.org>
parents: 52353
diff changeset
29 raise unittest.SkipTest("Cannot find a suiteable filename.")
33938
81cbf031549a Try a different filename if the Latin-1 file name cannot
Martin v. Löwis <martin@v.loewis.de>
parents: 32432
diff changeset
30
81cbf031549a Try a different filename if the Latin-1 file name cannot
Martin v. Löwis <martin@v.loewis.de>
parents: 32432
diff changeset
31 if TESTFN_ENCODED.decode(TESTFN_ENCODING) != TESTFN_UNICODE:
52355
228cfb6e2900 fix incorrect auto-translation of TestSkipped -> unittest.SkipTest
Benjamin Peterson <benjamin@python.org>
parents: 52353
diff changeset
32 raise unittest.SkipTest("Cannot find a suitable filename.")
33938
81cbf031549a Try a different filename if the Latin-1 file name cannot
Martin v. Löwis <martin@v.loewis.de>
parents: 32432
diff changeset
33
31079
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
34 def remove_if_exists(filename):
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
35 if os.path.exists(filename):
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
36 os.unlink(filename)
17817
f7c385226f65 Add support for Windows using "mbcs" as the default Unicode encoding when dealing with the file system. As discussed on python-dev and in patch 410465.
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
37
31079
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
38 class TestUnicodeFiles(unittest.TestCase):
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
39 # The 'do_' functions are the actual tests. They generally assume the
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
40 # file already exists etc.
31389
aca1ce1b4b61 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 31087
diff changeset
41
31079
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
42 # Do all the tests we can given only a single filename. The file should
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
43 # exist.
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
44 def _do_single(self, filename):
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52355
diff changeset
45 self.assertTrue(os.path.exists(filename))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52355
diff changeset
46 self.assertTrue(os.path.isfile(filename))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52355
diff changeset
47 self.assertTrue(os.access(filename, os.R_OK))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52355
diff changeset
48 self.assertTrue(os.path.exists(os.path.abspath(filename)))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52355
diff changeset
49 self.assertTrue(os.path.isfile(os.path.abspath(filename)))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52355
diff changeset
50 self.assertTrue(os.access(os.path.abspath(filename), os.R_OK))
31079
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
51 os.chmod(filename, 0777)
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
52 os.utime(filename, None)
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
53 os.utime(filename, (time.time(), time.time()))
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
54 # Copy/rename etc tests using the same filename
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
55 self._do_copyish(filename, filename)
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
56 # Filename should appear in glob output
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52355
diff changeset
57 self.assertTrue(
31079
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
58 os.path.abspath(filename)==os.path.abspath(glob.glob(filename)[0]))
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
59 # basename should appear in listdir.
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
60 path, base = os.path.split(os.path.abspath(filename))
32432
20adb7ce1b08 Patch from Mark Hammond to fix a test error.
Raymond Hettinger <python@rcn.com>
parents: 31755
diff changeset
61 if isinstance(base, str):
20adb7ce1b08 Patch from Mark Hammond to fix a test error.
Raymond Hettinger <python@rcn.com>
parents: 31755
diff changeset
62 base = base.decode(TESTFN_ENCODING)
20adb7ce1b08 Patch from Mark Hammond to fix a test error.
Raymond Hettinger <python@rcn.com>
parents: 31755
diff changeset
63 file_list = os.listdir(path)
20adb7ce1b08 Patch from Mark Hammond to fix a test error.
Raymond Hettinger <python@rcn.com>
parents: 31755
diff changeset
64 # listdir() with a unicode arg may or may not return Unicode
20adb7ce1b08 Patch from Mark Hammond to fix a test error.
Raymond Hettinger <python@rcn.com>
parents: 31755
diff changeset
65 # objects, depending on the platform.
20adb7ce1b08 Patch from Mark Hammond to fix a test error.
Raymond Hettinger <python@rcn.com>
parents: 31755
diff changeset
66 if file_list and isinstance(file_list[0], str):
20adb7ce1b08 Patch from Mark Hammond to fix a test error.
Raymond Hettinger <python@rcn.com>
parents: 31755
diff changeset
67 file_list = [f.decode(TESTFN_ENCODING) for f in file_list]
31755
b39fdc9c807e Normalized files in test_unicode_file to eliminate failure on OSX
Nicholas Bastin <nick.bastin@gmail.com>
parents: 31389
diff changeset
68
32432
20adb7ce1b08 Patch from Mark Hammond to fix a test error.
Raymond Hettinger <python@rcn.com>
parents: 31755
diff changeset
69 # Normalize the unicode strings, as round-tripping the name via the OS
20adb7ce1b08 Patch from Mark Hammond to fix a test error.
Raymond Hettinger <python@rcn.com>
parents: 31755
diff changeset
70 # may return a different (but equivalent) value.
20adb7ce1b08 Patch from Mark Hammond to fix a test error.
Raymond Hettinger <python@rcn.com>
parents: 31755
diff changeset
71 base = unicodedata.normalize("NFD", base)
31755
b39fdc9c807e Normalized files in test_unicode_file to eliminate failure on OSX
Nicholas Bastin <nick.bastin@gmail.com>
parents: 31389
diff changeset
72 file_list = [unicodedata.normalize("NFD", f) for f in file_list]
b39fdc9c807e Normalized files in test_unicode_file to eliminate failure on OSX
Nicholas Bastin <nick.bastin@gmail.com>
parents: 31389
diff changeset
73
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55002
diff changeset
74 self.assertIn(base, file_list)
31389
aca1ce1b4b61 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 31087
diff changeset
75
31079
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
76 # Do as many "equivalancy' tests as we can - ie, check that although we
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
77 # have different types for the filename, they refer to the same file.
45938
6509ef8bfe9c Make sure that the warnings filter is not reset or changed beyond the current
Brett Cannon <bcannon@gmail.com>
parents: 41534
diff changeset
78 def _do_equivalent(self, filename1, filename2):
31079
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
79 # Note we only check "filename1 against filename2" - we don't bother
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
80 # checking "filename2 against 1", as we assume we are called again with
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
81 # the args reversed.
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52355
diff changeset
82 self.assertTrue(type(filename1)!=type(filename2),
31079
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
83 "No point checking equivalent filenames of the same type")
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
84 # stat and lstat should return the same results.
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52355
diff changeset
85 self.assertEqual(os.stat(filename1),
31079
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
86 os.stat(filename2))
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52355
diff changeset
87 self.assertEqual(os.lstat(filename1),
31079
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
88 os.lstat(filename2))
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
89 # Copy/rename etc tests using equivalent filename
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
90 self._do_copyish(filename1, filename2)
17817
f7c385226f65 Add support for Windows using "mbcs" as the default Unicode encoding when dealing with the file system. As discussed on python-dev and in patch 410465.
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
91
31079
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
92 # Tests that copy, move, etc one file to another.
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
93 def _do_copyish(self, filename1, filename2):
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
94 # Should be able to rename the file using either name.
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52355
diff changeset
95 self.assertTrue(os.path.isfile(filename1)) # must exist.
31079
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
96 os.rename(filename1, filename2 + ".new")
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52355
diff changeset
97 self.assertTrue(os.path.isfile(filename1+".new"))
31079
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
98 os.rename(filename1 + ".new", filename2)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52355
diff changeset
99 self.assertTrue(os.path.isfile(filename2))
17817
f7c385226f65 Add support for Windows using "mbcs" as the default Unicode encoding when dealing with the file system. As discussed on python-dev and in patch 410465.
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
100
45938
6509ef8bfe9c Make sure that the warnings filter is not reset or changed beyond the current
Brett Cannon <bcannon@gmail.com>
parents: 41534
diff changeset
101 shutil.copy(filename1, filename2 + ".new")
6509ef8bfe9c Make sure that the warnings filter is not reset or changed beyond the current
Brett Cannon <bcannon@gmail.com>
parents: 41534
diff changeset
102 os.unlink(filename1 + ".new") # remove using equiv name.
6509ef8bfe9c Make sure that the warnings filter is not reset or changed beyond the current
Brett Cannon <bcannon@gmail.com>
parents: 41534
diff changeset
103 # And a couple of moves, one using each name.
6509ef8bfe9c Make sure that the warnings filter is not reset or changed beyond the current
Brett Cannon <bcannon@gmail.com>
parents: 41534
diff changeset
104 shutil.move(filename1, filename2 + ".new")
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52355
diff changeset
105 self.assertTrue(not os.path.exists(filename2))
45938
6509ef8bfe9c Make sure that the warnings filter is not reset or changed beyond the current
Brett Cannon <bcannon@gmail.com>
parents: 41534
diff changeset
106 shutil.move(filename1 + ".new", filename2)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52355
diff changeset
107 self.assertTrue(os.path.exists(filename1))
45938
6509ef8bfe9c Make sure that the warnings filter is not reset or changed beyond the current
Brett Cannon <bcannon@gmail.com>
parents: 41534
diff changeset
108 # Note - due to the implementation of shutil.move,
6509ef8bfe9c Make sure that the warnings filter is not reset or changed beyond the current
Brett Cannon <bcannon@gmail.com>
parents: 41534
diff changeset
109 # it tries a rename first. This only fails on Windows when on
6509ef8bfe9c Make sure that the warnings filter is not reset or changed beyond the current
Brett Cannon <bcannon@gmail.com>
parents: 41534
diff changeset
110 # different file systems - and this test can't ensure that.
6509ef8bfe9c Make sure that the warnings filter is not reset or changed beyond the current
Brett Cannon <bcannon@gmail.com>
parents: 41534
diff changeset
111 # So we test the shutil.copy2 function, which is the thing most
6509ef8bfe9c Make sure that the warnings filter is not reset or changed beyond the current
Brett Cannon <bcannon@gmail.com>
parents: 41534
diff changeset
112 # likely to fail.
6509ef8bfe9c Make sure that the warnings filter is not reset or changed beyond the current
Brett Cannon <bcannon@gmail.com>
parents: 41534
diff changeset
113 shutil.copy2(filename1, filename2 + ".new")
6509ef8bfe9c Make sure that the warnings filter is not reset or changed beyond the current
Brett Cannon <bcannon@gmail.com>
parents: 41534
diff changeset
114 os.unlink(filename1 + ".new")
21658
7c582d1b9cf8 Ensure Unicode filenames work with glob - they already do, but the test seems worth keeping.
Mark Hammond <mhammond@skippinet.com.au>
parents: 19735
diff changeset
115
31755
b39fdc9c807e Normalized files in test_unicode_file to eliminate failure on OSX
Nicholas Bastin <nick.bastin@gmail.com>
parents: 31389
diff changeset
116 def _do_directory(self, make_name, chdir_name, encoded):
31079
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
117 cwd = os.getcwd()
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
118 if os.path.isdir(make_name):
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
119 os.rmdir(make_name)
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
120 os.mkdir(make_name)
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
121 try:
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
122 os.chdir(chdir_name)
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
123 try:
31755
b39fdc9c807e Normalized files in test_unicode_file to eliminate failure on OSX
Nicholas Bastin <nick.bastin@gmail.com>
parents: 31389
diff changeset
124 if not encoded:
b39fdc9c807e Normalized files in test_unicode_file to eliminate failure on OSX
Nicholas Bastin <nick.bastin@gmail.com>
parents: 31389
diff changeset
125 cwd_result = os.getcwdu()
b39fdc9c807e Normalized files in test_unicode_file to eliminate failure on OSX
Nicholas Bastin <nick.bastin@gmail.com>
parents: 31389
diff changeset
126 name_result = make_name
b39fdc9c807e Normalized files in test_unicode_file to eliminate failure on OSX
Nicholas Bastin <nick.bastin@gmail.com>
parents: 31389
diff changeset
127 else:
b39fdc9c807e Normalized files in test_unicode_file to eliminate failure on OSX
Nicholas Bastin <nick.bastin@gmail.com>
parents: 31389
diff changeset
128 cwd_result = os.getcwd().decode(TESTFN_ENCODING)
b39fdc9c807e Normalized files in test_unicode_file to eliminate failure on OSX
Nicholas Bastin <nick.bastin@gmail.com>
parents: 31389
diff changeset
129 name_result = make_name.decode(TESTFN_ENCODING)
b39fdc9c807e Normalized files in test_unicode_file to eliminate failure on OSX
Nicholas Bastin <nick.bastin@gmail.com>
parents: 31389
diff changeset
130
b39fdc9c807e Normalized files in test_unicode_file to eliminate failure on OSX
Nicholas Bastin <nick.bastin@gmail.com>
parents: 31389
diff changeset
131 cwd_result = unicodedata.normalize("NFD", cwd_result)
b39fdc9c807e Normalized files in test_unicode_file to eliminate failure on OSX
Nicholas Bastin <nick.bastin@gmail.com>
parents: 31389
diff changeset
132 name_result = unicodedata.normalize("NFD", name_result)
b39fdc9c807e Normalized files in test_unicode_file to eliminate failure on OSX
Nicholas Bastin <nick.bastin@gmail.com>
parents: 31389
diff changeset
133
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52355
diff changeset
134 self.assertEqual(os.path.basename(cwd_result),name_result)
31079
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
135 finally:
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
136 os.chdir(cwd)
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
137 finally:
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
138 os.rmdir(make_name)
17817
f7c385226f65 Add support for Windows using "mbcs" as the default Unicode encoding when dealing with the file system. As discussed on python-dev and in patch 410465.
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
139
31079
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
140 # The '_test' functions 'entry points with params' - ie, what the
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
141 # top-level 'test' functions would be if they could take params
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
142 def _test_single(self, filename):
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
143 remove_if_exists(filename)
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
144 f = file(filename, "w")
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
145 f.close()
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
146 try:
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
147 self._do_single(filename)
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
148 finally:
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
149 os.unlink(filename)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52355
diff changeset
150 self.assertTrue(not os.path.exists(filename))
31079
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
151 # and again with os.open.
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
152 f = os.open(filename, os.O_CREAT)
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
153 os.close(f)
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
154 try:
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
155 self._do_single(filename)
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
156 finally:
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
157 os.unlink(filename)
31389
aca1ce1b4b61 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 31087
diff changeset
158
31079
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
159 def _test_equivalent(self, filename1, filename2):
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
160 remove_if_exists(filename1)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 52355
diff changeset
161 self.assertTrue(not os.path.exists(filename2))
31079
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
162 f = file(filename1, "w")
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
163 f.close()
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
164 try:
45938
6509ef8bfe9c Make sure that the warnings filter is not reset or changed beyond the current
Brett Cannon <bcannon@gmail.com>
parents: 41534
diff changeset
165 self._do_equivalent(filename1, filename2)
31079
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
166 finally:
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
167 os.unlink(filename1)
17817
f7c385226f65 Add support for Windows using "mbcs" as the default Unicode encoding when dealing with the file system. As discussed on python-dev and in patch 410465.
Mark Hammond <mhammond@skippinet.com.au>
parents:
diff changeset
168
31079
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
169 # The 'test' functions are unittest entry points, and simply call our
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
170 # _test functions with each of the filename combinations we wish to test
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
171 def test_single_files(self):
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
172 self._test_single(TESTFN_ENCODED)
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
173 self._test_single(TESTFN_UNICODE)
64602
457aa8da490a Recorded merge of revisions 83987 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 58460
diff changeset
174 if TESTFN_UNENCODABLE is not None:
457aa8da490a Recorded merge of revisions 83987 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 58460
diff changeset
175 self._test_single(TESTFN_UNENCODABLE)
31079
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
176
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
177 def test_equivalent_files(self):
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
178 self._test_equivalent(TESTFN_ENCODED, TESTFN_UNICODE)
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
179 self._test_equivalent(TESTFN_UNICODE, TESTFN_ENCODED)
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
180
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
181 def test_directories(self):
45938
6509ef8bfe9c Make sure that the warnings filter is not reset or changed beyond the current
Brett Cannon <bcannon@gmail.com>
parents: 41534
diff changeset
182 # For all 'equivalent' combinations:
31079
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
183 # Make dir with encoded, chdir with unicode, checkdir with encoded
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
184 # (or unicode/encoded/unicode, etc
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
185 ext = ".dir"
31755
b39fdc9c807e Normalized files in test_unicode_file to eliminate failure on OSX
Nicholas Bastin <nick.bastin@gmail.com>
parents: 31389
diff changeset
186 self._do_directory(TESTFN_ENCODED+ext, TESTFN_ENCODED+ext, True)
b39fdc9c807e Normalized files in test_unicode_file to eliminate failure on OSX
Nicholas Bastin <nick.bastin@gmail.com>
parents: 31389
diff changeset
187 self._do_directory(TESTFN_ENCODED+ext, TESTFN_UNICODE+ext, True)
b39fdc9c807e Normalized files in test_unicode_file to eliminate failure on OSX
Nicholas Bastin <nick.bastin@gmail.com>
parents: 31389
diff changeset
188 self._do_directory(TESTFN_UNICODE+ext, TESTFN_ENCODED+ext, False)
b39fdc9c807e Normalized files in test_unicode_file to eliminate failure on OSX
Nicholas Bastin <nick.bastin@gmail.com>
parents: 31389
diff changeset
189 self._do_directory(TESTFN_UNICODE+ext, TESTFN_UNICODE+ext, False)
31079
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
190 # Our directory name that can't use a non-unicode name.
64602
457aa8da490a Recorded merge of revisions 83987 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 58460
diff changeset
191 if TESTFN_UNENCODABLE is not None:
457aa8da490a Recorded merge of revisions 83987 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 58460
diff changeset
192 self._do_directory(TESTFN_UNENCODABLE+ext,
457aa8da490a Recorded merge of revisions 83987 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents: 58460
diff changeset
193 TESTFN_UNENCODABLE+ext,
31755
b39fdc9c807e Normalized files in test_unicode_file to eliminate failure on OSX
Nicholas Bastin <nick.bastin@gmail.com>
parents: 31389
diff changeset
194 False)
31079
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
195
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
196 def test_main():
41534
6c4d7678af5e Standardize on test.test_support.run_unittest() (as opposed to a mix of run_unittest() and run_suite()). Also, add functionality to run_unittest() that admits usage of unittest.TestLoader.loadTestsFromModule().
Collin Winter <collinw@gmail.com>
parents: 34747
diff changeset
197 run_unittest(__name__)
31079
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
198
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
199 if __name__ == "__main__":
622991cc1e3c Add test for bug "[ 846133 ] os.chmod/os.utime/shutil do not work with
Mark Hammond <mhammond@skippinet.com.au>
parents: 30074
diff changeset
200 test_main()