Guido van Rossum | ead9d8d | 1999-02-03 17:21:21 +0000 | [diff] [blame] | 1 | import ntpath |
Barry Warsaw | 04f357c | 2002-07-23 19:04:11 +0000 | [diff] [blame] | 2 | from test.test_support import verbose, TestFailed |
Mark Hammond | 673c6cf | 2000-08-14 06:21:26 +0000 | [diff] [blame] | 3 | import os |
Guido van Rossum | ead9d8d | 1999-02-03 17:21:21 +0000 | [diff] [blame] | 4 | |
| 5 | errors = 0 |
| 6 | |
| 7 | def tester(fn, wantResult): |
Tim Peters | d4f7f60 | 2001-07-19 19:11:41 +0000 | [diff] [blame] | 8 | global errors |
Eric S. Raymond | fc170b1 | 2001-02-09 11:51:27 +0000 | [diff] [blame] | 9 | fn = fn.replace("\\", "\\\\") |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 10 | gotResult = eval(fn) |
| 11 | if wantResult != gotResult: |
| 12 | print "error!" |
| 13 | print "evaluated: " + str(fn) |
| 14 | print "should be: " + str(wantResult) |
| 15 | print " returned: " + str(gotResult) |
| 16 | print "" |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 17 | errors = errors + 1 |
Tim Peters | 6578dc9 | 2002-12-24 18:31:27 +0000 | [diff] [blame] | 18 | |
Martin v. Löwis | de33379 | 2002-12-12 20:30:20 +0000 | [diff] [blame] | 19 | tester('ntpath.splitext("foo.ext")', ('foo', '.ext')) |
| 20 | tester('ntpath.splitext("/foo/foo.ext")', ('/foo/foo', '.ext')) |
| 21 | tester('ntpath.splitext(".ext")', ('', '.ext')) |
| 22 | tester('ntpath.splitext("\\foo.ext\\foo")', ('\\foo.ext\\foo', '')) |
| 23 | tester('ntpath.splitext("foo.ext\\")', ('foo.ext\\', '')) |
| 24 | tester('ntpath.splitext("")', ('', '')) |
| 25 | tester('ntpath.splitext("foo.bar.ext")', ('foo.bar', '.ext')) |
| 26 | tester('ntpath.splitext("xx/foo.bar.ext")', ('xx/foo.bar', '.ext')) |
| 27 | tester('ntpath.splitext("xx\\foo.bar.ext")', ('xx\\foo.bar', '.ext')) |
Guido van Rossum | ead9d8d | 1999-02-03 17:21:21 +0000 | [diff] [blame] | 28 | |
Tim Peters | 3b5e4d1 | 2001-07-19 19:02:12 +0000 | [diff] [blame] | 29 | tester('ntpath.splitdrive("c:\\foo\\bar")', |
| 30 | ('c:', '\\foo\\bar')) |
| 31 | tester('ntpath.splitunc("\\\\conky\\mountpoint\\foo\\bar")', |
| 32 | ('\\\\conky\\mountpoint', '\\foo\\bar')) |
| 33 | tester('ntpath.splitdrive("c:/foo/bar")', |
| 34 | ('c:', '/foo/bar')) |
| 35 | tester('ntpath.splitunc("//conky/mountpoint/foo/bar")', |
| 36 | ('//conky/mountpoint', '/foo/bar')) |
Guido van Rossum | ead9d8d | 1999-02-03 17:21:21 +0000 | [diff] [blame] | 37 | |
| 38 | tester('ntpath.split("c:\\foo\\bar")', ('c:\\foo', 'bar')) |
Tim Peters | 3b5e4d1 | 2001-07-19 19:02:12 +0000 | [diff] [blame] | 39 | tester('ntpath.split("\\\\conky\\mountpoint\\foo\\bar")', |
| 40 | ('\\\\conky\\mountpoint\\foo', 'bar')) |
Guido van Rossum | ead9d8d | 1999-02-03 17:21:21 +0000 | [diff] [blame] | 41 | |
| 42 | tester('ntpath.split("c:\\")', ('c:\\', '')) |
Tim Peters | 3b5e4d1 | 2001-07-19 19:02:12 +0000 | [diff] [blame] | 43 | tester('ntpath.split("\\\\conky\\mountpoint\\")', |
| 44 | ('\\\\conky\\mountpoint', '')) |
Guido van Rossum | ead9d8d | 1999-02-03 17:21:21 +0000 | [diff] [blame] | 45 | |
| 46 | tester('ntpath.split("c:/")', ('c:/', '')) |
Guido van Rossum | 630a9a6 | 1999-04-06 19:38:18 +0000 | [diff] [blame] | 47 | tester('ntpath.split("//conky/mountpoint/")', ('//conky/mountpoint', '')) |
Guido van Rossum | ead9d8d | 1999-02-03 17:21:21 +0000 | [diff] [blame] | 48 | |
| 49 | tester('ntpath.isabs("c:\\")', 1) |
| 50 | tester('ntpath.isabs("\\\\conky\\mountpoint\\")', 1) |
| 51 | tester('ntpath.isabs("\\foo")', 1) |
| 52 | tester('ntpath.isabs("\\foo\\bar")', 1) |
| 53 | |
Skip Montanaro | 877d62e | 2000-08-23 16:54:27 +0000 | [diff] [blame] | 54 | tester('ntpath.commonprefix(["/home/swenson/spam", "/home/swen/spam"])', |
| 55 | "/home/swen") |
| 56 | tester('ntpath.commonprefix(["\\home\\swen\\spam", "\\home\\swen\\eggs"])', |
| 57 | "\\home\\swen\\") |
| 58 | tester('ntpath.commonprefix(["/home/swen/spam", "/home/swen/spam"])', |
| 59 | "/home/swen/spam") |
Mark Hammond | 673c6cf | 2000-08-14 06:21:26 +0000 | [diff] [blame] | 60 | |
Tim Peters | d4f7f60 | 2001-07-19 19:11:41 +0000 | [diff] [blame] | 61 | tester('ntpath.join("")', '') |
| 62 | tester('ntpath.join("", "", "")', '') |
| 63 | tester('ntpath.join("a")', 'a') |
| 64 | tester('ntpath.join("/a")', '/a') |
| 65 | tester('ntpath.join("\\a")', '\\a') |
| 66 | tester('ntpath.join("a:")', 'a:') |
| 67 | tester('ntpath.join("a:", "b")', 'a:b') |
| 68 | tester('ntpath.join("a:", "/b")', 'a:/b') |
| 69 | tester('ntpath.join("a:", "\\b")', 'a:\\b') |
| 70 | tester('ntpath.join("a", "/b")', '/b') |
| 71 | tester('ntpath.join("a", "\\b")', '\\b') |
| 72 | tester('ntpath.join("a", "b", "c")', 'a\\b\\c') |
| 73 | tester('ntpath.join("a\\", "b", "c")', 'a\\b\\c') |
| 74 | tester('ntpath.join("a", "b\\", "c")', 'a\\b\\c') |
| 75 | tester('ntpath.join("a", "b", "\\c")', '\\c') |
Tim Peters | 4223f89 | 2001-07-26 21:54:37 +0000 | [diff] [blame] | 76 | tester('ntpath.join("d:\\", "\\pleep")', 'd:\\pleep') |
Tim Peters | 33dc0a1 | 2001-07-27 08:09:54 +0000 | [diff] [blame] | 77 | tester('ntpath.join("d:\\", "a", "b")', 'd:\\a\\b') |
| 78 | tester("ntpath.join('c:', '/a')", 'c:/a') |
| 79 | tester("ntpath.join('c:/', '/a')", 'c:/a') |
| 80 | tester("ntpath.join('c:/a', '/b')", '/b') |
| 81 | tester("ntpath.join('c:', 'd:/')", 'd:/') |
| 82 | tester("ntpath.join('c:/', 'd:/')", 'd:/') |
| 83 | tester("ntpath.join('c:/', 'd:/a/b')", 'd:/a/b') |
Tim Peters | d4f7f60 | 2001-07-19 19:11:41 +0000 | [diff] [blame] | 84 | |
Tim Peters | 6a3e5f1 | 2001-11-05 21:25:02 +0000 | [diff] [blame] | 85 | tester("ntpath.join('')", '') |
| 86 | tester("ntpath.join('', '', '', '', '')", '') |
| 87 | tester("ntpath.join('a')", 'a') |
| 88 | tester("ntpath.join('', 'a')", 'a') |
| 89 | tester("ntpath.join('', '', '', '', 'a')", 'a') |
| 90 | tester("ntpath.join('a', '')", 'a\\') |
| 91 | tester("ntpath.join('a', '', '', '', '')", 'a\\') |
Tim Peters | e5a611c | 2001-11-05 21:33:04 +0000 | [diff] [blame] | 92 | tester("ntpath.join('a\\', '')", 'a\\') |
| 93 | tester("ntpath.join('a\\', '', '', '', '')", 'a\\') |
Tim Peters | 6a3e5f1 | 2001-11-05 21:25:02 +0000 | [diff] [blame] | 94 | |
Tim Peters | 54a14a3 | 2001-08-30 22:05:26 +0000 | [diff] [blame] | 95 | tester("ntpath.normpath('A//////././//.//B')", r'A\B') |
| 96 | tester("ntpath.normpath('A/./B')", r'A\B') |
| 97 | tester("ntpath.normpath('A/foo/../B')", r'A\B') |
| 98 | tester("ntpath.normpath('C:A//B')", r'C:A\B') |
| 99 | tester("ntpath.normpath('D:A/./B')", r'D:A\B') |
| 100 | tester("ntpath.normpath('e:A/foo/../B')", r'e:A\B') |
| 101 | |
| 102 | # Next 3 seem dubious, and especially the 3rd, but normpath is possibly |
| 103 | # trying to leave UNC paths alone without actually knowing anything about |
| 104 | # them. |
| 105 | tester("ntpath.normpath('C:///A//B')", r'C:\\\A\B') |
| 106 | tester("ntpath.normpath('D:///A/./B')", r'D:\\\A\B') |
| 107 | tester("ntpath.normpath('e:///A/foo/../B')", r'e:\\\A\B') |
| 108 | |
| 109 | tester("ntpath.normpath('..')", r'..') |
| 110 | tester("ntpath.normpath('.')", r'.') |
| 111 | tester("ntpath.normpath('')", r'.') |
| 112 | tester("ntpath.normpath('/')", '\\') |
| 113 | tester("ntpath.normpath('c:/')", 'c:\\') |
| 114 | tester("ntpath.normpath('/../.././..')", '\\') |
| 115 | tester("ntpath.normpath('c:/../../..')", 'c:\\') |
| 116 | tester("ntpath.normpath('../.././..')", r'..\..\..') |
| 117 | tester("ntpath.normpath('K:../.././..')", r'K:..\..\..') |
| 118 | |
Fred Drake | 5e99731 | 2002-01-15 03:46:43 +0000 | [diff] [blame] | 119 | # ntpath.abspath() can only be used on a system with the "nt" module |
| 120 | # (reasonably), so we protect this test with "import nt". This allows |
| 121 | # the rest of the tests for the ntpath module to be run to completion |
| 122 | # on any platform, since most of the module is intended to be usable |
| 123 | # from any platform. |
| 124 | try: |
| 125 | import nt |
| 126 | except ImportError: |
| 127 | pass |
| 128 | else: |
| 129 | tester('ntpath.abspath("C:\\")', "C:\\") |
| 130 | |
Guido van Rossum | ead9d8d | 1999-02-03 17:21:21 +0000 | [diff] [blame] | 131 | if errors: |
Tim Peters | d4f7f60 | 2001-07-19 19:11:41 +0000 | [diff] [blame] | 132 | raise TestFailed(str(errors) + " errors.") |
Tim Peters | 3b5e4d1 | 2001-07-19 19:02:12 +0000 | [diff] [blame] | 133 | elif verbose: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 134 | print "No errors. Thank your lucky stars." |