»Core Development>Code coverage>Lib/test/test_ntpath.py

Python code coverage for Lib/test/test_ntpath.py

#countcontent
1n/aimport ntpath
2n/aimport os
3n/aimport sys
4n/aimport unittest
5n/aimport warnings
6n/afrom test.support import TestFailed
7n/afrom test import support, test_genericpath
8n/afrom tempfile import TemporaryFile
9n/a
10n/a
11n/adef tester(fn, wantResult):
12n/a fn = fn.replace("\\", "\\\\")
13n/a gotResult = eval(fn)
14n/a if wantResult != gotResult:
15n/a raise TestFailed("%s should return: %s but returned: %s" \
16n/a %(str(fn), str(wantResult), str(gotResult)))
17n/a
18n/a # then with bytes
19n/a fn = fn.replace("('", "(b'")
20n/a fn = fn.replace('("', '(b"')
21n/a fn = fn.replace("['", "[b'")
22n/a fn = fn.replace('["', '[b"')
23n/a fn = fn.replace(", '", ", b'")
24n/a fn = fn.replace(', "', ', b"')
25n/a fn = os.fsencode(fn).decode('latin1')
26n/a fn = fn.encode('ascii', 'backslashreplace').decode('ascii')
27n/a with warnings.catch_warnings():
28n/a warnings.simplefilter("ignore", DeprecationWarning)
29n/a gotResult = eval(fn)
30n/a if isinstance(wantResult, str):
31n/a wantResult = os.fsencode(wantResult)
32n/a elif isinstance(wantResult, tuple):
33n/a wantResult = tuple(os.fsencode(r) for r in wantResult)
34n/a
35n/a gotResult = eval(fn)
36n/a if wantResult != gotResult:
37n/a raise TestFailed("%s should return: %s but returned: %s" \
38n/a %(str(fn), str(wantResult), repr(gotResult)))
39n/a
40n/a
41n/aclass TestNtpath(unittest.TestCase):
42n/a def test_splitext(self):
43n/a tester('ntpath.splitext("foo.ext")', ('foo', '.ext'))
44n/a tester('ntpath.splitext("/foo/foo.ext")', ('/foo/foo', '.ext'))
45n/a tester('ntpath.splitext(".ext")', ('.ext', ''))
46n/a tester('ntpath.splitext("\\foo.ext\\foo")', ('\\foo.ext\\foo', ''))
47n/a tester('ntpath.splitext("foo.ext\\")', ('foo.ext\\', ''))
48n/a tester('ntpath.splitext("")', ('', ''))
49n/a tester('ntpath.splitext("foo.bar.ext")', ('foo.bar', '.ext'))
50n/a tester('ntpath.splitext("xx/foo.bar.ext")', ('xx/foo.bar', '.ext'))
51n/a tester('ntpath.splitext("xx\\foo.bar.ext")', ('xx\\foo.bar', '.ext'))
52n/a tester('ntpath.splitext("c:a/b\\c.d")', ('c:a/b\\c', '.d'))
53n/a
54n/a def test_splitdrive(self):
55n/a tester('ntpath.splitdrive("c:\\foo\\bar")',
56n/a ('c:', '\\foo\\bar'))
57n/a tester('ntpath.splitdrive("c:/foo/bar")',
58n/a ('c:', '/foo/bar'))
59n/a tester('ntpath.splitdrive("\\\\conky\\mountpoint\\foo\\bar")',
60n/a ('\\\\conky\\mountpoint', '\\foo\\bar'))
61n/a tester('ntpath.splitdrive("//conky/mountpoint/foo/bar")',
62n/a ('//conky/mountpoint', '/foo/bar'))
63n/a tester('ntpath.splitdrive("\\\\\\conky\\mountpoint\\foo\\bar")',
64n/a ('', '\\\\\\conky\\mountpoint\\foo\\bar'))
65n/a tester('ntpath.splitdrive("///conky/mountpoint/foo/bar")',
66n/a ('', '///conky/mountpoint/foo/bar'))
67n/a tester('ntpath.splitdrive("\\\\conky\\\\mountpoint\\foo\\bar")',
68n/a ('', '\\\\conky\\\\mountpoint\\foo\\bar'))
69n/a tester('ntpath.splitdrive("//conky//mountpoint/foo/bar")',
70n/a ('', '//conky//mountpoint/foo/bar'))
71n/a # Issue #19911: UNC part containing U+0130
72n/a self.assertEqual(ntpath.splitdrive('//conky/MOUNTPOÄ°NT/foo/bar'),
73n/a ('//conky/MOUNTPOÄ°NT', '/foo/bar'))
74n/a
75n/a def test_split(self):
76n/a tester('ntpath.split("c:\\foo\\bar")', ('c:\\foo', 'bar'))
77n/a tester('ntpath.split("\\\\conky\\mountpoint\\foo\\bar")',
78n/a ('\\\\conky\\mountpoint\\foo', 'bar'))
79n/a
80n/a tester('ntpath.split("c:\\")', ('c:\\', ''))
81n/a tester('ntpath.split("\\\\conky\\mountpoint\\")',
82n/a ('\\\\conky\\mountpoint\\', ''))
83n/a
84n/a tester('ntpath.split("c:/")', ('c:/', ''))
85n/a tester('ntpath.split("//conky/mountpoint/")', ('//conky/mountpoint/', ''))
86n/a
87n/a def test_isabs(self):
88n/a tester('ntpath.isabs("c:\\")', 1)
89n/a tester('ntpath.isabs("\\\\conky\\mountpoint\\")', 1)
90n/a tester('ntpath.isabs("\\foo")', 1)
91n/a tester('ntpath.isabs("\\foo\\bar")', 1)
92n/a
93n/a def test_commonprefix(self):
94n/a tester('ntpath.commonprefix(["/home/swenson/spam", "/home/swen/spam"])',
95n/a "/home/swen")
96n/a tester('ntpath.commonprefix(["\\home\\swen\\spam", "\\home\\swen\\eggs"])',
97n/a "\\home\\swen\\")
98n/a tester('ntpath.commonprefix(["/home/swen/spam", "/home/swen/spam"])',
99n/a "/home/swen/spam")
100n/a
101n/a def test_join(self):
102n/a tester('ntpath.join("")', '')
103n/a tester('ntpath.join("", "", "")', '')
104n/a tester('ntpath.join("a")', 'a')
105n/a tester('ntpath.join("/a")', '/a')
106n/a tester('ntpath.join("\\a")', '\\a')
107n/a tester('ntpath.join("a:")', 'a:')
108n/a tester('ntpath.join("a:", "\\b")', 'a:\\b')
109n/a tester('ntpath.join("a", "\\b")', '\\b')
110n/a tester('ntpath.join("a", "b", "c")', 'a\\b\\c')
111n/a tester('ntpath.join("a\\", "b", "c")', 'a\\b\\c')
112n/a tester('ntpath.join("a", "b\\", "c")', 'a\\b\\c')
113n/a tester('ntpath.join("a", "b", "\\c")', '\\c')
114n/a tester('ntpath.join("d:\\", "\\pleep")', 'd:\\pleep')
115n/a tester('ntpath.join("d:\\", "a", "b")', 'd:\\a\\b')
116n/a
117n/a tester("ntpath.join('', 'a')", 'a')
118n/a tester("ntpath.join('', '', '', '', 'a')", 'a')
119n/a tester("ntpath.join('a', '')", 'a\\')
120n/a tester("ntpath.join('a', '', '', '', '')", 'a\\')
121n/a tester("ntpath.join('a\\', '')", 'a\\')
122n/a tester("ntpath.join('a\\', '', '', '', '')", 'a\\')
123n/a tester("ntpath.join('a/', '')", 'a/')
124n/a
125n/a tester("ntpath.join('a/b', 'x/y')", 'a/b\\x/y')
126n/a tester("ntpath.join('/a/b', 'x/y')", '/a/b\\x/y')
127n/a tester("ntpath.join('/a/b/', 'x/y')", '/a/b/x/y')
128n/a tester("ntpath.join('c:', 'x/y')", 'c:x/y')
129n/a tester("ntpath.join('c:a/b', 'x/y')", 'c:a/b\\x/y')
130n/a tester("ntpath.join('c:a/b/', 'x/y')", 'c:a/b/x/y')
131n/a tester("ntpath.join('c:/', 'x/y')", 'c:/x/y')
132n/a tester("ntpath.join('c:/a/b', 'x/y')", 'c:/a/b\\x/y')
133n/a tester("ntpath.join('c:/a/b/', 'x/y')", 'c:/a/b/x/y')
134n/a tester("ntpath.join('//computer/share', 'x/y')", '//computer/share\\x/y')
135n/a tester("ntpath.join('//computer/share/', 'x/y')", '//computer/share/x/y')
136n/a tester("ntpath.join('//computer/share/a/b', 'x/y')", '//computer/share/a/b\\x/y')
137n/a
138n/a tester("ntpath.join('a/b', '/x/y')", '/x/y')
139n/a tester("ntpath.join('/a/b', '/x/y')", '/x/y')
140n/a tester("ntpath.join('c:', '/x/y')", 'c:/x/y')
141n/a tester("ntpath.join('c:a/b', '/x/y')", 'c:/x/y')
142n/a tester("ntpath.join('c:/', '/x/y')", 'c:/x/y')
143n/a tester("ntpath.join('c:/a/b', '/x/y')", 'c:/x/y')
144n/a tester("ntpath.join('//computer/share', '/x/y')", '//computer/share/x/y')
145n/a tester("ntpath.join('//computer/share/', '/x/y')", '//computer/share/x/y')
146n/a tester("ntpath.join('//computer/share/a', '/x/y')", '//computer/share/x/y')
147n/a
148n/a tester("ntpath.join('c:', 'C:x/y')", 'C:x/y')
149n/a tester("ntpath.join('c:a/b', 'C:x/y')", 'C:a/b\\x/y')
150n/a tester("ntpath.join('c:/', 'C:x/y')", 'C:/x/y')
151n/a tester("ntpath.join('c:/a/b', 'C:x/y')", 'C:/a/b\\x/y')
152n/a
153n/a for x in ('', 'a/b', '/a/b', 'c:', 'c:a/b', 'c:/', 'c:/a/b',
154n/a '//computer/share', '//computer/share/', '//computer/share/a/b'):
155n/a for y in ('d:', 'd:x/y', 'd:/', 'd:/x/y',
156n/a '//machine/common', '//machine/common/', '//machine/common/x/y'):
157n/a tester("ntpath.join(%r, %r)" % (x, y), y)
158n/a
159n/a tester("ntpath.join('\\\\computer\\share\\', 'a', 'b')", '\\\\computer\\share\\a\\b')
160n/a tester("ntpath.join('\\\\computer\\share', 'a', 'b')", '\\\\computer\\share\\a\\b')
161n/a tester("ntpath.join('\\\\computer\\share', 'a\\b')", '\\\\computer\\share\\a\\b')
162n/a tester("ntpath.join('//computer/share/', 'a', 'b')", '//computer/share/a\\b')
163n/a tester("ntpath.join('//computer/share', 'a', 'b')", '//computer/share\\a\\b')
164n/a tester("ntpath.join('//computer/share', 'a/b')", '//computer/share\\a/b')
165n/a
166n/a def test_normpath(self):
167n/a tester("ntpath.normpath('A//////././//.//B')", r'A\B')
168n/a tester("ntpath.normpath('A/./B')", r'A\B')
169n/a tester("ntpath.normpath('A/foo/../B')", r'A\B')
170n/a tester("ntpath.normpath('C:A//B')", r'C:A\B')
171n/a tester("ntpath.normpath('D:A/./B')", r'D:A\B')
172n/a tester("ntpath.normpath('e:A/foo/../B')", r'e:A\B')
173n/a
174n/a tester("ntpath.normpath('C:///A//B')", r'C:\A\B')
175n/a tester("ntpath.normpath('D:///A/./B')", r'D:\A\B')
176n/a tester("ntpath.normpath('e:///A/foo/../B')", r'e:\A\B')
177n/a
178n/a tester("ntpath.normpath('..')", r'..')
179n/a tester("ntpath.normpath('.')", r'.')
180n/a tester("ntpath.normpath('')", r'.')
181n/a tester("ntpath.normpath('/')", '\\')
182n/a tester("ntpath.normpath('c:/')", 'c:\\')
183n/a tester("ntpath.normpath('/../.././..')", '\\')
184n/a tester("ntpath.normpath('c:/../../..')", 'c:\\')
185n/a tester("ntpath.normpath('../.././..')", r'..\..\..')
186n/a tester("ntpath.normpath('K:../.././..')", r'K:..\..\..')
187n/a tester("ntpath.normpath('C:////a/b')", r'C:\a\b')
188n/a tester("ntpath.normpath('//machine/share//a/b')", r'\\machine\share\a\b')
189n/a
190n/a tester("ntpath.normpath('\\\\.\\NUL')", r'\\.\NUL')
191n/a tester("ntpath.normpath('\\\\?\\D:/XY\\Z')", r'\\?\D:/XY\Z')
192n/a
193n/a def test_expandvars(self):
194n/a with support.EnvironmentVarGuard() as env:
195n/a env.clear()
196n/a env["foo"] = "bar"
197n/a env["{foo"] = "baz1"
198n/a env["{foo}"] = "baz2"
199n/a tester('ntpath.expandvars("foo")', "foo")
200n/a tester('ntpath.expandvars("$foo bar")', "bar bar")
201n/a tester('ntpath.expandvars("${foo}bar")', "barbar")
202n/a tester('ntpath.expandvars("$[foo]bar")', "$[foo]bar")
203n/a tester('ntpath.expandvars("$bar bar")', "$bar bar")
204n/a tester('ntpath.expandvars("$?bar")', "$?bar")
205n/a tester('ntpath.expandvars("$foo}bar")', "bar}bar")
206n/a tester('ntpath.expandvars("${foo")', "${foo")
207n/a tester('ntpath.expandvars("${{foo}}")', "baz1}")
208n/a tester('ntpath.expandvars("$foo$foo")', "barbar")
209n/a tester('ntpath.expandvars("$bar$bar")', "$bar$bar")
210n/a tester('ntpath.expandvars("%foo% bar")', "bar bar")
211n/a tester('ntpath.expandvars("%foo%bar")', "barbar")
212n/a tester('ntpath.expandvars("%foo%%foo%")', "barbar")
213n/a tester('ntpath.expandvars("%%foo%%foo%foo%")', "%foo%foobar")
214n/a tester('ntpath.expandvars("%?bar%")', "%?bar%")
215n/a tester('ntpath.expandvars("%foo%%bar")', "bar%bar")
216n/a tester('ntpath.expandvars("\'%foo%\'%bar")', "\'%foo%\'%bar")
217n/a tester('ntpath.expandvars("bar\'%foo%")', "bar\'%foo%")
218n/a
219n/a @unittest.skipUnless(support.FS_NONASCII, 'need support.FS_NONASCII')
220n/a def test_expandvars_nonascii(self):
221n/a def check(value, expected):
222n/a tester('ntpath.expandvars(%r)' % value, expected)
223n/a with support.EnvironmentVarGuard() as env:
224n/a env.clear()
225n/a nonascii = support.FS_NONASCII
226n/a env['spam'] = nonascii
227n/a env[nonascii] = 'ham' + nonascii
228n/a check('$spam bar', '%s bar' % nonascii)
229n/a check('$%s bar' % nonascii, '$%s bar' % nonascii)
230n/a check('${spam}bar', '%sbar' % nonascii)
231n/a check('${%s}bar' % nonascii, 'ham%sbar' % nonascii)
232n/a check('$spam}bar', '%s}bar' % nonascii)
233n/a check('$%s}bar' % nonascii, '$%s}bar' % nonascii)
234n/a check('%spam% bar', '%s bar' % nonascii)
235n/a check('%{}% bar'.format(nonascii), 'ham%s bar' % nonascii)
236n/a check('%spam%bar', '%sbar' % nonascii)
237n/a check('%{}%bar'.format(nonascii), 'ham%sbar' % nonascii)
238n/a
239n/a def test_expanduser(self):
240n/a tester('ntpath.expanduser("test")', 'test')
241n/a
242n/a with support.EnvironmentVarGuard() as env:
243n/a env.clear()
244n/a tester('ntpath.expanduser("~test")', '~test')
245n/a
246n/a env['HOMEPATH'] = 'eric\\idle'
247n/a env['HOMEDRIVE'] = 'C:\\'
248n/a tester('ntpath.expanduser("~test")', 'C:\\eric\\test')
249n/a tester('ntpath.expanduser("~")', 'C:\\eric\\idle')
250n/a
251n/a del env['HOMEDRIVE']
252n/a tester('ntpath.expanduser("~test")', 'eric\\test')
253n/a tester('ntpath.expanduser("~")', 'eric\\idle')
254n/a
255n/a env.clear()
256n/a env['USERPROFILE'] = 'C:\\eric\\idle'
257n/a tester('ntpath.expanduser("~test")', 'C:\\eric\\test')
258n/a tester('ntpath.expanduser("~")', 'C:\\eric\\idle')
259n/a
260n/a env.clear()
261n/a env['HOME'] = 'C:\\idle\\eric'
262n/a tester('ntpath.expanduser("~test")', 'C:\\idle\\test')
263n/a tester('ntpath.expanduser("~")', 'C:\\idle\\eric')
264n/a
265n/a tester('ntpath.expanduser("~test\\foo\\bar")',
266n/a 'C:\\idle\\test\\foo\\bar')
267n/a tester('ntpath.expanduser("~test/foo/bar")',
268n/a 'C:\\idle\\test/foo/bar')
269n/a tester('ntpath.expanduser("~\\foo\\bar")',
270n/a 'C:\\idle\\eric\\foo\\bar')
271n/a tester('ntpath.expanduser("~/foo/bar")',
272n/a 'C:\\idle\\eric/foo/bar')
273n/a
274n/a def test_abspath(self):
275n/a # ntpath.abspath() can only be used on a system with the "nt" module
276n/a # (reasonably), so we protect this test with "import nt". This allows
277n/a # the rest of the tests for the ntpath module to be run to completion
278n/a # on any platform, since most of the module is intended to be usable
279n/a # from any platform.
280n/a try:
281n/a import nt
282n/a tester('ntpath.abspath("C:\\")', "C:\\")
283n/a except ImportError:
284n/a self.skipTest('nt module not available')
285n/a
286n/a def test_relpath(self):
287n/a tester('ntpath.relpath("a")', 'a')
288n/a tester('ntpath.relpath(os.path.abspath("a"))', 'a')
289n/a tester('ntpath.relpath("a/b")', 'a\\b')
290n/a tester('ntpath.relpath("../a/b")', '..\\a\\b')
291n/a with support.temp_cwd(support.TESTFN) as cwd_dir:
292n/a currentdir = os.path.basename(cwd_dir)
293n/a tester('ntpath.relpath("a", "../b")', '..\\'+currentdir+'\\a')
294n/a tester('ntpath.relpath("a/b", "../c")', '..\\'+currentdir+'\\a\\b')
295n/a tester('ntpath.relpath("a", "b/c")', '..\\..\\a')
296n/a tester('ntpath.relpath("c:/foo/bar/bat", "c:/x/y")', '..\\..\\foo\\bar\\bat')
297n/a tester('ntpath.relpath("//conky/mountpoint/a", "//conky/mountpoint/b/c")', '..\\..\\a')
298n/a tester('ntpath.relpath("a", "a")', '.')
299n/a tester('ntpath.relpath("/foo/bar/bat", "/x/y/z")', '..\\..\\..\\foo\\bar\\bat')
300n/a tester('ntpath.relpath("/foo/bar/bat", "/foo/bar")', 'bat')
301n/a tester('ntpath.relpath("/foo/bar/bat", "/")', 'foo\\bar\\bat')
302n/a tester('ntpath.relpath("/", "/foo/bar/bat")', '..\\..\\..')
303n/a tester('ntpath.relpath("/foo/bar/bat", "/x")', '..\\foo\\bar\\bat')
304n/a tester('ntpath.relpath("/x", "/foo/bar/bat")', '..\\..\\..\\x')
305n/a tester('ntpath.relpath("/", "/")', '.')
306n/a tester('ntpath.relpath("/a", "/a")', '.')
307n/a tester('ntpath.relpath("/a/b", "/a/b")', '.')
308n/a tester('ntpath.relpath("c:/foo", "C:/FOO")', '.')
309n/a
310n/a def test_commonpath(self):
311n/a def check(paths, expected):
312n/a tester(('ntpath.commonpath(%r)' % paths).replace('\\\\', '\\'),
313n/a expected)
314n/a def check_error(exc, paths):
315n/a self.assertRaises(exc, ntpath.commonpath, paths)
316n/a self.assertRaises(exc, ntpath.commonpath,
317n/a [os.fsencode(p) for p in paths])
318n/a
319n/a self.assertRaises(ValueError, ntpath.commonpath, [])
320n/a check_error(ValueError, ['C:\\Program Files', 'Program Files'])
321n/a check_error(ValueError, ['C:\\Program Files', 'C:Program Files'])
322n/a check_error(ValueError, ['\\Program Files', 'Program Files'])
323n/a check_error(ValueError, ['Program Files', 'C:\\Program Files'])
324n/a check(['C:\\Program Files'], 'C:\\Program Files')
325n/a check(['C:\\Program Files', 'C:\\Program Files'], 'C:\\Program Files')
326n/a check(['C:\\Program Files\\', 'C:\\Program Files'],
327n/a 'C:\\Program Files')
328n/a check(['C:\\Program Files\\', 'C:\\Program Files\\'],
329n/a 'C:\\Program Files')
330n/a check(['C:\\\\Program Files', 'C:\\Program Files\\\\'],
331n/a 'C:\\Program Files')
332n/a check(['C:\\.\\Program Files', 'C:\\Program Files\\.'],
333n/a 'C:\\Program Files')
334n/a check(['C:\\', 'C:\\bin'], 'C:\\')
335n/a check(['C:\\Program Files', 'C:\\bin'], 'C:\\')
336n/a check(['C:\\Program Files', 'C:\\Program Files\\Bar'],
337n/a 'C:\\Program Files')
338n/a check(['C:\\Program Files\\Foo', 'C:\\Program Files\\Bar'],
339n/a 'C:\\Program Files')
340n/a check(['C:\\Program Files', 'C:\\Projects'], 'C:\\')
341n/a check(['C:\\Program Files\\', 'C:\\Projects'], 'C:\\')
342n/a
343n/a check(['C:\\Program Files\\Foo', 'C:/Program Files/Bar'],
344n/a 'C:\\Program Files')
345n/a check(['C:\\Program Files\\Foo', 'c:/program files/bar'],
346n/a 'C:\\Program Files')
347n/a check(['c:/program files/bar', 'C:\\Program Files\\Foo'],
348n/a 'c:\\program files')
349n/a
350n/a check_error(ValueError, ['C:\\Program Files', 'D:\\Program Files'])
351n/a
352n/a check(['spam'], 'spam')
353n/a check(['spam', 'spam'], 'spam')
354n/a check(['spam', 'alot'], '')
355n/a check(['and\\jam', 'and\\spam'], 'and')
356n/a check(['and\\\\jam', 'and\\spam\\\\'], 'and')
357n/a check(['and\\.\\jam', '.\\and\\spam'], 'and')
358n/a check(['and\\jam', 'and\\spam', 'alot'], '')
359n/a check(['and\\jam', 'and\\spam', 'and'], 'and')
360n/a check(['C:and\\jam', 'C:and\\spam'], 'C:and')
361n/a
362n/a check([''], '')
363n/a check(['', 'spam\\alot'], '')
364n/a check_error(ValueError, ['', '\\spam\\alot'])
365n/a
366n/a self.assertRaises(TypeError, ntpath.commonpath,
367n/a [b'C:\\Program Files', 'C:\\Program Files\\Foo'])
368n/a self.assertRaises(TypeError, ntpath.commonpath,
369n/a [b'C:\\Program Files', 'Program Files\\Foo'])
370n/a self.assertRaises(TypeError, ntpath.commonpath,
371n/a [b'Program Files', 'C:\\Program Files\\Foo'])
372n/a self.assertRaises(TypeError, ntpath.commonpath,
373n/a ['C:\\Program Files', b'C:\\Program Files\\Foo'])
374n/a self.assertRaises(TypeError, ntpath.commonpath,
375n/a ['C:\\Program Files', b'Program Files\\Foo'])
376n/a self.assertRaises(TypeError, ntpath.commonpath,
377n/a ['Program Files', b'C:\\Program Files\\Foo'])
378n/a
379n/a def test_sameopenfile(self):
380n/a with TemporaryFile() as tf1, TemporaryFile() as tf2:
381n/a # Make sure the same file is really the same
382n/a self.assertTrue(ntpath.sameopenfile(tf1.fileno(), tf1.fileno()))
383n/a # Make sure different files are really different
384n/a self.assertFalse(ntpath.sameopenfile(tf1.fileno(), tf2.fileno()))
385n/a # Make sure invalid values don't cause issues on win32
386n/a if sys.platform == "win32":
387n/a with self.assertRaises(OSError):
388n/a # Invalid file descriptors shouldn't display assert
389n/a # dialogs (#4804)
390n/a ntpath.sameopenfile(-1, -1)
391n/a
392n/a def test_ismount(self):
393n/a self.assertTrue(ntpath.ismount("c:\\"))
394n/a self.assertTrue(ntpath.ismount("C:\\"))
395n/a self.assertTrue(ntpath.ismount("c:/"))
396n/a self.assertTrue(ntpath.ismount("C:/"))
397n/a self.assertTrue(ntpath.ismount("\\\\.\\c:\\"))
398n/a self.assertTrue(ntpath.ismount("\\\\.\\C:\\"))
399n/a
400n/a self.assertTrue(ntpath.ismount(b"c:\\"))
401n/a self.assertTrue(ntpath.ismount(b"C:\\"))
402n/a self.assertTrue(ntpath.ismount(b"c:/"))
403n/a self.assertTrue(ntpath.ismount(b"C:/"))
404n/a self.assertTrue(ntpath.ismount(b"\\\\.\\c:\\"))
405n/a self.assertTrue(ntpath.ismount(b"\\\\.\\C:\\"))
406n/a
407n/a with support.temp_dir() as d:
408n/a self.assertFalse(ntpath.ismount(d))
409n/a
410n/a if sys.platform == "win32":
411n/a #
412n/a # Make sure the current folder isn't the root folder
413n/a # (or any other volume root). The drive-relative
414n/a # locations below cannot then refer to mount points
415n/a #
416n/a drive, path = ntpath.splitdrive(sys.executable)
417n/a with support.change_cwd(os.path.dirname(sys.executable)):
418n/a self.assertFalse(ntpath.ismount(drive.lower()))
419n/a self.assertFalse(ntpath.ismount(drive.upper()))
420n/a
421n/a self.assertTrue(ntpath.ismount("\\\\localhost\\c$"))
422n/a self.assertTrue(ntpath.ismount("\\\\localhost\\c$\\"))
423n/a
424n/a self.assertTrue(ntpath.ismount(b"\\\\localhost\\c$"))
425n/a self.assertTrue(ntpath.ismount(b"\\\\localhost\\c$\\"))
426n/a
427n/aclass NtCommonTest(test_genericpath.CommonTest, unittest.TestCase):
428n/a pathmodule = ntpath
429n/a attributes = ['relpath']
430n/a
431n/a
432n/aclass PathLikeTests(unittest.TestCase):
433n/a
434n/a path = ntpath
435n/a
436n/a class PathLike:
437n/a def __init__(self, path=''):
438n/a self.path = path
439n/a def __fspath__(self):
440n/a if isinstance(self.path, BaseException):
441n/a raise self.path
442n/a else:
443n/a return self.path
444n/a
445n/a def setUp(self):
446n/a self.file_name = support.TESTFN.lower()
447n/a self.file_path = self.PathLike(support.TESTFN)
448n/a self.addCleanup(support.unlink, self.file_name)
449n/a with open(self.file_name, 'xb', 0) as file:
450n/a file.write(b"test_ntpath.PathLikeTests")
451n/a
452n/a def assertPathEqual(self, func):
453n/a self.assertEqual(func(self.file_path), func(self.file_name))
454n/a
455n/a def test_path_normcase(self):
456n/a self.assertPathEqual(self.path.normcase)
457n/a
458n/a def test_path_isabs(self):
459n/a self.assertPathEqual(self.path.isabs)
460n/a
461n/a def test_path_join(self):
462n/a self.assertEqual(self.path.join('a', self.PathLike('b'), 'c'),
463n/a self.path.join('a', 'b', 'c'))
464n/a
465n/a def test_path_split(self):
466n/a self.assertPathEqual(self.path.split)
467n/a
468n/a def test_path_splitext(self):
469n/a self.assertPathEqual(self.path.splitext)
470n/a
471n/a def test_path_splitdrive(self):
472n/a self.assertPathEqual(self.path.splitdrive)
473n/a
474n/a def test_path_basename(self):
475n/a self.assertPathEqual(self.path.basename)
476n/a
477n/a def test_path_dirname(self):
478n/a self.assertPathEqual(self.path.dirname)
479n/a
480n/a def test_path_islink(self):
481n/a self.assertPathEqual(self.path.islink)
482n/a
483n/a def test_path_lexists(self):
484n/a self.assertPathEqual(self.path.lexists)
485n/a
486n/a def test_path_ismount(self):
487n/a self.assertPathEqual(self.path.ismount)
488n/a
489n/a def test_path_expanduser(self):
490n/a self.assertPathEqual(self.path.expanduser)
491n/a
492n/a def test_path_expandvars(self):
493n/a self.assertPathEqual(self.path.expandvars)
494n/a
495n/a def test_path_normpath(self):
496n/a self.assertPathEqual(self.path.normpath)
497n/a
498n/a def test_path_abspath(self):
499n/a self.assertPathEqual(self.path.abspath)
500n/a
501n/a def test_path_realpath(self):
502n/a self.assertPathEqual(self.path.realpath)
503n/a
504n/a def test_path_relpath(self):
505n/a self.assertPathEqual(self.path.relpath)
506n/a
507n/a def test_path_commonpath(self):
508n/a common_path = self.path.commonpath([self.file_path, self.file_name])
509n/a self.assertEqual(common_path, self.file_name)
510n/a
511n/a def test_path_isdir(self):
512n/a self.assertPathEqual(self.path.isdir)
513n/a
514n/a
515n/aif __name__ == "__main__":
516n/a unittest.main()