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