Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1 | # Test some Unicode file name semantics |
| 2 | # We dont test many operations on files other than |
| 3 | # that their names can be used with Unicode characters. |
| 4 | import os |
| 5 | |
| 6 | from test_support import verify, TestSkipped, TESTFN_UNICODE |
| 7 | try: |
| 8 | from test_support import TESTFN_ENCODING |
| 9 | except ImportError: |
| 10 | raise TestSkipped("No Unicode filesystem semantics on this platform.") |
| 11 | |
| 12 | TESTFN_ENCODED = TESTFN_UNICODE.encode(TESTFN_ENCODING) |
| 13 | |
| 14 | # Check with creation as Unicode string. |
| 15 | f = open(TESTFN_UNICODE, 'wb') |
| 16 | if not os.path.isfile(TESTFN_UNICODE): |
| 17 | print "File doesn't exist after creating it" |
| 18 | |
| 19 | if not os.path.isfile(TESTFN_ENCODED): |
| 20 | print "File doesn't exist (encoded string) after creating it" |
| 21 | |
| 22 | f.close() |
| 23 | |
| 24 | # Test stat and chmod |
| 25 | if os.stat(TESTFN_ENCODED) != os.stat(TESTFN_UNICODE): |
| 26 | print "os.stat() did not agree on the 2 filenames" |
| 27 | os.chmod(TESTFN_ENCODED, 0777) |
| 28 | os.chmod(TESTFN_UNICODE, 0777) |
| 29 | |
| 30 | # Test rename |
| 31 | os.rename(TESTFN_ENCODED, TESTFN_ENCODED + ".new") |
| 32 | os.rename(TESTFN_UNICODE+".new", TESTFN_ENCODED) |
| 33 | |
| 34 | os.unlink(TESTFN_ENCODED) |
| 35 | if os.path.isfile(TESTFN_ENCODED) or \ |
| 36 | os.path.isfile(TESTFN_UNICODE): |
| 37 | print "File exists after deleting it" |
| 38 | |
| 39 | # Check with creation as encoded string. |
| 40 | f = open(TESTFN_ENCODED, 'wb') |
| 41 | if not os.path.isfile(TESTFN_UNICODE) or \ |
| 42 | not os.path.isfile(TESTFN_ENCODED): |
| 43 | print "File doesn't exist after creating it" |
| 44 | |
| 45 | path, base = os.path.split(os.path.abspath(TESTFN_ENCODED)) |
| 46 | if base not in os.listdir(path): |
| 47 | print "Filename did not appear in os.listdir()" |
| 48 | |
| 49 | f.close() |
| 50 | os.unlink(TESTFN_UNICODE) |
| 51 | if os.path.isfile(TESTFN_ENCODED) or \ |
| 52 | os.path.isfile(TESTFN_UNICODE): |
| 53 | print "File exists after deleting it" |
| 54 | |
| 55 | # test os.open |
| 56 | f = os.open(TESTFN_ENCODED, os.O_CREAT) |
| 57 | if not os.path.isfile(TESTFN_UNICODE) or \ |
| 58 | not os.path.isfile(TESTFN_ENCODED): |
| 59 | print "File doesn't exist after creating it" |
| 60 | os.close(f) |
| 61 | os.unlink(TESTFN_UNICODE) |
| 62 | |
| 63 | # Test directories etc |
| 64 | cwd = os.getcwd() |
| 65 | abs_encoded = os.path.abspath(TESTFN_ENCODED) + ".dir" |
| 66 | abs_unicode = os.path.abspath(TESTFN_UNICODE) + ".dir" |
| 67 | os.mkdir(abs_encoded) |
| 68 | try: |
| 69 | os.chdir(abs_encoded) |
| 70 | os.chdir(abs_unicode) |
| 71 | finally: |
| 72 | os.chdir(cwd) |
| 73 | os.rmdir(abs_unicode) |
| 74 | os.mkdir(abs_unicode) |
| 75 | try: |
| 76 | os.chdir(abs_encoded) |
| 77 | os.chdir(abs_unicode) |
| 78 | finally: |
| 79 | os.chdir(cwd) |
| 80 | os.rmdir(abs_encoded) |
| 81 | print "All the Unicode tests appeared to work" |