annotate Lib/test/test_posixpath.py @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents 85542715a249
children 6768b2350c57
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
29502
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
1 import unittest
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
2 from test import test_support
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
3
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
4 import posixpath, os
41218
f3d4274d063d Patch 1339796: add a relpath() function to os.path.
Collin Winter <collinw@gmail.com>
parents: 41133
diff changeset
5 from posixpath import realpath, abspath, join, dirname, basename, relpath
33129
ae9b47a8d648 bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents: 29707
diff changeset
6
ae9b47a8d648 bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents: 29707
diff changeset
7 # An absolute path to a temporary filename for testing. We can't rely on TESTFN
ae9b47a8d648 bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents: 29707
diff changeset
8 # being an absolute path, so we need this.
ae9b47a8d648 bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents: 29707
diff changeset
9
ae9b47a8d648 bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents: 29707
diff changeset
10 ABSTFN = abspath(test_support.TESTFN)
29502
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
11
41097
be0d49134e4f * Unlink test files before and after each test; hopefully this will cut down on recent buildbot failures in test_islink.
Collin Winter <collinw@gmail.com>
parents: 41057
diff changeset
12 def safe_rmdir(dirname):
be0d49134e4f * Unlink test files before and after each test; hopefully this will cut down on recent buildbot failures in test_islink.
Collin Winter <collinw@gmail.com>
parents: 41057
diff changeset
13 try:
be0d49134e4f * Unlink test files before and after each test; hopefully this will cut down on recent buildbot failures in test_islink.
Collin Winter <collinw@gmail.com>
parents: 41057
diff changeset
14 os.rmdir(dirname)
be0d49134e4f * Unlink test files before and after each test; hopefully this will cut down on recent buildbot failures in test_islink.
Collin Winter <collinw@gmail.com>
parents: 41057
diff changeset
15 except OSError:
be0d49134e4f * Unlink test files before and after each test; hopefully this will cut down on recent buildbot failures in test_islink.
Collin Winter <collinw@gmail.com>
parents: 41057
diff changeset
16 pass
be0d49134e4f * Unlink test files before and after each test; hopefully this will cut down on recent buildbot failures in test_islink.
Collin Winter <collinw@gmail.com>
parents: 41057
diff changeset
17
29502
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
18 class PosixPathTest(unittest.TestCase):
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
19
41097
be0d49134e4f * Unlink test files before and after each test; hopefully this will cut down on recent buildbot failures in test_islink.
Collin Winter <collinw@gmail.com>
parents: 41057
diff changeset
20 def setUp(self):
be0d49134e4f * Unlink test files before and after each test; hopefully this will cut down on recent buildbot failures in test_islink.
Collin Winter <collinw@gmail.com>
parents: 41057
diff changeset
21 self.tearDown()
be0d49134e4f * Unlink test files before and after each test; hopefully this will cut down on recent buildbot failures in test_islink.
Collin Winter <collinw@gmail.com>
parents: 41057
diff changeset
22
be0d49134e4f * Unlink test files before and after each test; hopefully this will cut down on recent buildbot failures in test_islink.
Collin Winter <collinw@gmail.com>
parents: 41057
diff changeset
23 def tearDown(self):
be0d49134e4f * Unlink test files before and after each test; hopefully this will cut down on recent buildbot failures in test_islink.
Collin Winter <collinw@gmail.com>
parents: 41057
diff changeset
24 for suffix in ["", "1", "2"]:
be0d49134e4f * Unlink test files before and after each test; hopefully this will cut down on recent buildbot failures in test_islink.
Collin Winter <collinw@gmail.com>
parents: 41057
diff changeset
25 test_support.unlink(test_support.TESTFN + suffix)
be0d49134e4f * Unlink test files before and after each test; hopefully this will cut down on recent buildbot failures in test_islink.
Collin Winter <collinw@gmail.com>
parents: 41057
diff changeset
26 safe_rmdir(test_support.TESTFN + suffix)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41097
diff changeset
27
29502
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
28 def assertIs(self, a, b):
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
29 self.assert_(a is b)
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
30
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
31 def test_normcase(self):
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
32 # Check that normcase() is idempotent
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
33 p = "FoO/./BaR"
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
34 p = posixpath.normcase(p)
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
35 self.assertEqual(p, posixpath.normcase(p))
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
36
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
37 self.assertRaises(TypeError, posixpath.normcase)
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
38
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
39 def test_join(self):
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
40 self.assertEqual(posixpath.join("/foo", "bar", "/bar", "baz"), "/bar/baz")
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
41 self.assertEqual(posixpath.join("/foo", "bar", "baz"), "/foo/bar/baz")
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
42 self.assertEqual(posixpath.join("/foo/", "bar/", "baz/"), "/foo/bar/baz/")
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
43
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
44 self.assertRaises(TypeError, posixpath.join)
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
45
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
46 def test_splitdrive(self):
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
47 self.assertEqual(posixpath.splitdrive("/foo/bar"), ("", "/foo/bar"))
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
48
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
49 self.assertRaises(TypeError, posixpath.splitdrive)
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
50
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
51 def test_split(self):
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
52 self.assertEqual(posixpath.split("/foo/bar"), ("/foo", "bar"))
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
53 self.assertEqual(posixpath.split("/"), ("/", ""))
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
54 self.assertEqual(posixpath.split("foo"), ("", "foo"))
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
55 self.assertEqual(posixpath.split("////foo"), ("////", "foo"))
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
56 self.assertEqual(posixpath.split("//foo//bar"), ("//foo", "bar"))
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
57
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
58 self.assertRaises(TypeError, posixpath.split)
13555
9c74b8239ff8 new test file for posixpath module
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
59
41057
98bbc93bd55b Bug #1115886: os.path.splitext('.cshrc') gives now ('.cshrc', '').
Martin v. Löwis <martin@v.loewis.de>
parents: 40649
diff changeset
60 def splitextTest(self, path, filename, ext):
98bbc93bd55b Bug #1115886: os.path.splitext('.cshrc') gives now ('.cshrc', '').
Martin v. Löwis <martin@v.loewis.de>
parents: 40649
diff changeset
61 self.assertEqual(posixpath.splitext(path), (filename, ext))
98bbc93bd55b Bug #1115886: os.path.splitext('.cshrc') gives now ('.cshrc', '').
Martin v. Löwis <martin@v.loewis.de>
parents: 40649
diff changeset
62 self.assertEqual(posixpath.splitext("/" + path), ("/" + filename, ext))
98bbc93bd55b Bug #1115886: os.path.splitext('.cshrc') gives now ('.cshrc', '').
Martin v. Löwis <martin@v.loewis.de>
parents: 40649
diff changeset
63 self.assertEqual(posixpath.splitext("abc/" + path), ("abc/" + filename, ext))
98bbc93bd55b Bug #1115886: os.path.splitext('.cshrc') gives now ('.cshrc', '').
Martin v. Löwis <martin@v.loewis.de>
parents: 40649
diff changeset
64 self.assertEqual(posixpath.splitext("abc.def/" + path), ("abc.def/" + filename, ext))
98bbc93bd55b Bug #1115886: os.path.splitext('.cshrc') gives now ('.cshrc', '').
Martin v. Löwis <martin@v.loewis.de>
parents: 40649
diff changeset
65 self.assertEqual(posixpath.splitext("/abc.def/" + path), ("/abc.def/" + filename, ext))
98bbc93bd55b Bug #1115886: os.path.splitext('.cshrc') gives now ('.cshrc', '').
Martin v. Löwis <martin@v.loewis.de>
parents: 40649
diff changeset
66 self.assertEqual(posixpath.splitext(path + "/"), (filename + ext + "/", ""))
98bbc93bd55b Bug #1115886: os.path.splitext('.cshrc') gives now ('.cshrc', '').
Martin v. Löwis <martin@v.loewis.de>
parents: 40649
diff changeset
67
29502
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
68 def test_splitext(self):
41057
98bbc93bd55b Bug #1115886: os.path.splitext('.cshrc') gives now ('.cshrc', '').
Martin v. Löwis <martin@v.loewis.de>
parents: 40649
diff changeset
69 self.splitextTest("foo.bar", "foo", ".bar")
98bbc93bd55b Bug #1115886: os.path.splitext('.cshrc') gives now ('.cshrc', '').
Martin v. Löwis <martin@v.loewis.de>
parents: 40649
diff changeset
70 self.splitextTest("foo.boo.bar", "foo.boo", ".bar")
98bbc93bd55b Bug #1115886: os.path.splitext('.cshrc') gives now ('.cshrc', '').
Martin v. Löwis <martin@v.loewis.de>
parents: 40649
diff changeset
71 self.splitextTest("foo.boo.biff.bar", "foo.boo.biff", ".bar")
98bbc93bd55b Bug #1115886: os.path.splitext('.cshrc') gives now ('.cshrc', '').
Martin v. Löwis <martin@v.loewis.de>
parents: 40649
diff changeset
72 self.splitextTest(".csh.rc", ".csh", ".rc")
98bbc93bd55b Bug #1115886: os.path.splitext('.cshrc') gives now ('.cshrc', '').
Martin v. Löwis <martin@v.loewis.de>
parents: 40649
diff changeset
73 self.splitextTest("nodots", "nodots", "")
98bbc93bd55b Bug #1115886: os.path.splitext('.cshrc') gives now ('.cshrc', '').
Martin v. Löwis <martin@v.loewis.de>
parents: 40649
diff changeset
74 self.splitextTest(".cshrc", ".cshrc", "")
98bbc93bd55b Bug #1115886: os.path.splitext('.cshrc') gives now ('.cshrc', '').
Martin v. Löwis <martin@v.loewis.de>
parents: 40649
diff changeset
75 self.splitextTest("...manydots", "...manydots", "")
98bbc93bd55b Bug #1115886: os.path.splitext('.cshrc') gives now ('.cshrc', '').
Martin v. Löwis <martin@v.loewis.de>
parents: 40649
diff changeset
76 self.splitextTest("...manydots.ext", "...manydots", ".ext")
98bbc93bd55b Bug #1115886: os.path.splitext('.cshrc') gives now ('.cshrc', '').
Martin v. Löwis <martin@v.loewis.de>
parents: 40649
diff changeset
77 self.splitextTest(".", ".", "")
98bbc93bd55b Bug #1115886: os.path.splitext('.cshrc') gives now ('.cshrc', '').
Martin v. Löwis <martin@v.loewis.de>
parents: 40649
diff changeset
78 self.splitextTest("..", "..", "")
98bbc93bd55b Bug #1115886: os.path.splitext('.cshrc') gives now ('.cshrc', '').
Martin v. Löwis <martin@v.loewis.de>
parents: 40649
diff changeset
79 self.splitextTest("........", "........", "")
98bbc93bd55b Bug #1115886: os.path.splitext('.cshrc') gives now ('.cshrc', '').
Martin v. Löwis <martin@v.loewis.de>
parents: 40649
diff changeset
80 self.splitextTest("", "", "")
29502
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
81 self.assertRaises(TypeError, posixpath.splitext)
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
82
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
83 def test_isabs(self):
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
84 self.assertIs(posixpath.isabs(""), False)
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
85 self.assertIs(posixpath.isabs("/"), True)
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
86 self.assertIs(posixpath.isabs("/foo"), True)
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
87 self.assertIs(posixpath.isabs("/foo/bar"), True)
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
88 self.assertIs(posixpath.isabs("foo/bar"), False)
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
89
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
90 self.assertRaises(TypeError, posixpath.isabs)
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
91
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
92 def test_splitdrive(self):
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
93 self.assertEqual(posixpath.splitdrive("/foo/bar"), ("", "/foo/bar"))
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
94
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
95 self.assertRaises(TypeError, posixpath.splitdrive)
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
96
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
97 def test_basename(self):
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
98 self.assertEqual(posixpath.basename("/foo/bar"), "bar")
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
99 self.assertEqual(posixpath.basename("/"), "")
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
100 self.assertEqual(posixpath.basename("foo"), "foo")
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
101 self.assertEqual(posixpath.basename("////foo"), "foo")
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
102 self.assertEqual(posixpath.basename("//foo//bar"), "bar")
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
103
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
104 self.assertRaises(TypeError, posixpath.basename)
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
105
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
106 def test_dirname(self):
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
107 self.assertEqual(posixpath.dirname("/foo/bar"), "/foo")
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
108 self.assertEqual(posixpath.dirname("/"), "/")
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
109 self.assertEqual(posixpath.dirname("foo"), "")
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
110 self.assertEqual(posixpath.dirname("////foo"), "////")
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
111 self.assertEqual(posixpath.dirname("//foo//bar"), "//foo")
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
112
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
113 self.assertRaises(TypeError, posixpath.dirname)
13555
9c74b8239ff8 new test file for posixpath module
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
114
29502
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
115 def test_commonprefix(self):
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
116 self.assertEqual(
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
117 posixpath.commonprefix([]),
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
118 ""
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
119 )
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
120 self.assertEqual(
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
121 posixpath.commonprefix(["/home/swenson/spam", "/home/swen/spam"]),
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
122 "/home/swen"
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
123 )
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
124 self.assertEqual(
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
125 posixpath.commonprefix(["/home/swen/spam", "/home/swen/eggs"]),
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
126 "/home/swen/"
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
127 )
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
128 self.assertEqual(
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
129 posixpath.commonprefix(["/home/swen/spam", "/home/swen/spam"]),
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
130 "/home/swen/spam"
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
131 )
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
132
41701
20b8b29593c0 Better tests for posixpath.commonprefix
Raymond Hettinger <python@rcn.com>
parents: 41293
diff changeset
133 testlist = ['', 'abc', 'Xbcd', 'Xb', 'XY', 'abcd', 'aXc', 'abd', 'ab', 'aX', 'abcX']
20b8b29593c0 Better tests for posixpath.commonprefix
Raymond Hettinger <python@rcn.com>
parents: 41293
diff changeset
134 for s1 in testlist:
20b8b29593c0 Better tests for posixpath.commonprefix
Raymond Hettinger <python@rcn.com>
parents: 41293
diff changeset
135 for s2 in testlist:
20b8b29593c0 Better tests for posixpath.commonprefix
Raymond Hettinger <python@rcn.com>
parents: 41293
diff changeset
136 p = posixpath.commonprefix([s1, s2])
20b8b29593c0 Better tests for posixpath.commonprefix
Raymond Hettinger <python@rcn.com>
parents: 41293
diff changeset
137 self.assert_(s1.startswith(p))
20b8b29593c0 Better tests for posixpath.commonprefix
Raymond Hettinger <python@rcn.com>
parents: 41293
diff changeset
138 self.assert_(s2.startswith(p))
20b8b29593c0 Better tests for posixpath.commonprefix
Raymond Hettinger <python@rcn.com>
parents: 41293
diff changeset
139 if s1 != s2:
20b8b29593c0 Better tests for posixpath.commonprefix
Raymond Hettinger <python@rcn.com>
parents: 41293
diff changeset
140 n = len(p)
20b8b29593c0 Better tests for posixpath.commonprefix
Raymond Hettinger <python@rcn.com>
parents: 41293
diff changeset
141 self.assertNotEqual(s1[n:n+1], s2[n:n+1])
20b8b29593c0 Better tests for posixpath.commonprefix
Raymond Hettinger <python@rcn.com>
parents: 41293
diff changeset
142
29502
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
143 def test_getsize(self):
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
144 f = open(test_support.TESTFN, "wb")
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
145 try:
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
146 f.write("foo")
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
147 f.close()
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
148 self.assertEqual(posixpath.getsize(test_support.TESTFN), 3)
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
149 finally:
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
150 if not f.closed:
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
151 f.close()
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
152
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
153 def test_time(self):
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
154 f = open(test_support.TESTFN, "wb")
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
155 try:
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
156 f.write("foo")
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
157 f.close()
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
158 f = open(test_support.TESTFN, "ab")
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
159 f.write("bar")
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
160 f.close()
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
161 f = open(test_support.TESTFN, "rb")
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
162 d = f.read()
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
163 f.close()
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
164 self.assertEqual(d, "foobar")
13555
9c74b8239ff8 new test file for posixpath module
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
165
29502
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
166 self.assert_(
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
167 posixpath.getctime(test_support.TESTFN) <=
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
168 posixpath.getmtime(test_support.TESTFN)
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
169 )
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
170 finally:
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
171 if not f.closed:
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
172 f.close()
13555
9c74b8239ff8 new test file for posixpath module
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
173
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41097
diff changeset
174 def test_islink(self):
29502
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
175 self.assertIs(posixpath.islink(test_support.TESTFN + "1"), False)
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
176 f = open(test_support.TESTFN + "1", "wb")
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
177 try:
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
178 f.write("foo")
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
179 f.close()
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
180 self.assertIs(posixpath.islink(test_support.TESTFN + "1"), False)
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
181 if hasattr(os, "symlink"):
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
182 os.symlink(test_support.TESTFN + "1", test_support.TESTFN + "2")
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
183 self.assertIs(posixpath.islink(test_support.TESTFN + "2"), True)
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
184 os.remove(test_support.TESTFN + "1")
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
185 self.assertIs(posixpath.islink(test_support.TESTFN + "2"), True)
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
186 self.assertIs(posixpath.exists(test_support.TESTFN + "2"), False)
33332
062177de8685 Patch #941486: add os.path.lexists(). Also fix bug #940578 by using lexists in glob.glob.
Johannes Gijsbers <jlg@dds.nl>
parents: 33204
diff changeset
187 self.assertIs(posixpath.lexists(test_support.TESTFN + "2"), True)
29502
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
188 finally:
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
189 if not f.close():
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
190 f.close()
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
191
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
192 self.assertRaises(TypeError, posixpath.islink)
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
193
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
194 def test_exists(self):
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
195 self.assertIs(posixpath.exists(test_support.TESTFN), False)
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
196 f = open(test_support.TESTFN, "wb")
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
197 try:
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
198 f.write("foo")
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
199 f.close()
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
200 self.assertIs(posixpath.exists(test_support.TESTFN), True)
33332
062177de8685 Patch #941486: add os.path.lexists(). Also fix bug #940578 by using lexists in glob.glob.
Johannes Gijsbers <jlg@dds.nl>
parents: 33204
diff changeset
201 self.assertIs(posixpath.lexists(test_support.TESTFN), True)
29502
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
202 finally:
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
203 if not f.close():
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
204 f.close()
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
205
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
206 self.assertRaises(TypeError, posixpath.exists)
13555
9c74b8239ff8 new test file for posixpath module
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
207
29502
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
208 def test_isdir(self):
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
209 self.assertIs(posixpath.isdir(test_support.TESTFN), False)
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
210 f = open(test_support.TESTFN, "wb")
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
211 try:
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
212 f.write("foo")
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
213 f.close()
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
214 self.assertIs(posixpath.isdir(test_support.TESTFN), False)
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
215 os.remove(test_support.TESTFN)
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
216 os.mkdir(test_support.TESTFN)
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
217 self.assertIs(posixpath.isdir(test_support.TESTFN), True)
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
218 os.rmdir(test_support.TESTFN)
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
219 finally:
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
220 if not f.close():
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
221 f.close()
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
222
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
223 self.assertRaises(TypeError, posixpath.isdir)
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
224
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
225 def test_isfile(self):
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
226 self.assertIs(posixpath.isfile(test_support.TESTFN), False)
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
227 f = open(test_support.TESTFN, "wb")
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
228 try:
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
229 f.write("foo")
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
230 f.close()
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
231 self.assertIs(posixpath.isfile(test_support.TESTFN), True)
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
232 os.remove(test_support.TESTFN)
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
233 os.mkdir(test_support.TESTFN)
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
234 self.assertIs(posixpath.isfile(test_support.TESTFN), False)
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
235 os.rmdir(test_support.TESTFN)
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
236 finally:
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
237 if not f.close():
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
238 f.close()
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
239
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
240 self.assertRaises(TypeError, posixpath.isdir)
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
241
41097
be0d49134e4f * Unlink test files before and after each test; hopefully this will cut down on recent buildbot failures in test_islink.
Collin Winter <collinw@gmail.com>
parents: 41057
diff changeset
242 def test_samefile(self):
be0d49134e4f * Unlink test files before and after each test; hopefully this will cut down on recent buildbot failures in test_islink.
Collin Winter <collinw@gmail.com>
parents: 41057
diff changeset
243 f = open(test_support.TESTFN + "1", "wb")
be0d49134e4f * Unlink test files before and after each test; hopefully this will cut down on recent buildbot failures in test_islink.
Collin Winter <collinw@gmail.com>
parents: 41057
diff changeset
244 try:
be0d49134e4f * Unlink test files before and after each test; hopefully this will cut down on recent buildbot failures in test_islink.
Collin Winter <collinw@gmail.com>
parents: 41057
diff changeset
245 f.write("foo")
be0d49134e4f * Unlink test files before and after each test; hopefully this will cut down on recent buildbot failures in test_islink.
Collin Winter <collinw@gmail.com>
parents: 41057
diff changeset
246 f.close()
be0d49134e4f * Unlink test files before and after each test; hopefully this will cut down on recent buildbot failures in test_islink.
Collin Winter <collinw@gmail.com>
parents: 41057
diff changeset
247 self.assertIs(
be0d49134e4f * Unlink test files before and after each test; hopefully this will cut down on recent buildbot failures in test_islink.
Collin Winter <collinw@gmail.com>
parents: 41057
diff changeset
248 posixpath.samefile(
be0d49134e4f * Unlink test files before and after each test; hopefully this will cut down on recent buildbot failures in test_islink.
Collin Winter <collinw@gmail.com>
parents: 41057
diff changeset
249 test_support.TESTFN + "1",
be0d49134e4f * Unlink test files before and after each test; hopefully this will cut down on recent buildbot failures in test_islink.
Collin Winter <collinw@gmail.com>
parents: 41057
diff changeset
250 test_support.TESTFN + "1"
be0d49134e4f * Unlink test files before and after each test; hopefully this will cut down on recent buildbot failures in test_islink.
Collin Winter <collinw@gmail.com>
parents: 41057
diff changeset
251 ),
be0d49134e4f * Unlink test files before and after each test; hopefully this will cut down on recent buildbot failures in test_islink.
Collin Winter <collinw@gmail.com>
parents: 41057
diff changeset
252 True
be0d49134e4f * Unlink test files before and after each test; hopefully this will cut down on recent buildbot failures in test_islink.
Collin Winter <collinw@gmail.com>
parents: 41057
diff changeset
253 )
be0d49134e4f * Unlink test files before and after each test; hopefully this will cut down on recent buildbot failures in test_islink.
Collin Winter <collinw@gmail.com>
parents: 41057
diff changeset
254 # If we don't have links, assume that os.stat doesn't return resonable
be0d49134e4f * Unlink test files before and after each test; hopefully this will cut down on recent buildbot failures in test_islink.
Collin Winter <collinw@gmail.com>
parents: 41057
diff changeset
255 # inode information and thus, that samefile() doesn't work
be0d49134e4f * Unlink test files before and after each test; hopefully this will cut down on recent buildbot failures in test_islink.
Collin Winter <collinw@gmail.com>
parents: 41057
diff changeset
256 if hasattr(os, "symlink"):
be0d49134e4f * Unlink test files before and after each test; hopefully this will cut down on recent buildbot failures in test_islink.
Collin Winter <collinw@gmail.com>
parents: 41057
diff changeset
257 os.symlink(
be0d49134e4f * Unlink test files before and after each test; hopefully this will cut down on recent buildbot failures in test_islink.
Collin Winter <collinw@gmail.com>
parents: 41057
diff changeset
258 test_support.TESTFN + "1",
be0d49134e4f * Unlink test files before and after each test; hopefully this will cut down on recent buildbot failures in test_islink.
Collin Winter <collinw@gmail.com>
parents: 41057
diff changeset
259 test_support.TESTFN + "2"
be0d49134e4f * Unlink test files before and after each test; hopefully this will cut down on recent buildbot failures in test_islink.
Collin Winter <collinw@gmail.com>
parents: 41057
diff changeset
260 )
be0d49134e4f * Unlink test files before and after each test; hopefully this will cut down on recent buildbot failures in test_islink.
Collin Winter <collinw@gmail.com>
parents: 41057
diff changeset
261 self.assertIs(
be0d49134e4f * Unlink test files before and after each test; hopefully this will cut down on recent buildbot failures in test_islink.
Collin Winter <collinw@gmail.com>
parents: 41057
diff changeset
262 posixpath.samefile(
be0d49134e4f * Unlink test files before and after each test; hopefully this will cut down on recent buildbot failures in test_islink.
Collin Winter <collinw@gmail.com>
parents: 41057
diff changeset
263 test_support.TESTFN + "1",
be0d49134e4f * Unlink test files before and after each test; hopefully this will cut down on recent buildbot failures in test_islink.
Collin Winter <collinw@gmail.com>
parents: 41057
diff changeset
264 test_support.TESTFN + "2"
be0d49134e4f * Unlink test files before and after each test; hopefully this will cut down on recent buildbot failures in test_islink.
Collin Winter <collinw@gmail.com>
parents: 41057
diff changeset
265 ),
be0d49134e4f * Unlink test files before and after each test; hopefully this will cut down on recent buildbot failures in test_islink.
Collin Winter <collinw@gmail.com>
parents: 41057
diff changeset
266 True
be0d49134e4f * Unlink test files before and after each test; hopefully this will cut down on recent buildbot failures in test_islink.
Collin Winter <collinw@gmail.com>
parents: 41057
diff changeset
267 )
be0d49134e4f * Unlink test files before and after each test; hopefully this will cut down on recent buildbot failures in test_islink.
Collin Winter <collinw@gmail.com>
parents: 41057
diff changeset
268 os.remove(test_support.TESTFN + "2")
be0d49134e4f * Unlink test files before and after each test; hopefully this will cut down on recent buildbot failures in test_islink.
Collin Winter <collinw@gmail.com>
parents: 41057
diff changeset
269 f = open(test_support.TESTFN + "2", "wb")
be0d49134e4f * Unlink test files before and after each test; hopefully this will cut down on recent buildbot failures in test_islink.
Collin Winter <collinw@gmail.com>
parents: 41057
diff changeset
270 f.write("bar")
29502
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
271 f.close()
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
272 self.assertIs(
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
273 posixpath.samefile(
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
274 test_support.TESTFN + "1",
41097
be0d49134e4f * Unlink test files before and after each test; hopefully this will cut down on recent buildbot failures in test_islink.
Collin Winter <collinw@gmail.com>
parents: 41057
diff changeset
275 test_support.TESTFN + "2"
29502
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
276 ),
41097
be0d49134e4f * Unlink test files before and after each test; hopefully this will cut down on recent buildbot failures in test_islink.
Collin Winter <collinw@gmail.com>
parents: 41057
diff changeset
277 False
29502
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
278 )
41097
be0d49134e4f * Unlink test files before and after each test; hopefully this will cut down on recent buildbot failures in test_islink.
Collin Winter <collinw@gmail.com>
parents: 41057
diff changeset
279 finally:
be0d49134e4f * Unlink test files before and after each test; hopefully this will cut down on recent buildbot failures in test_islink.
Collin Winter <collinw@gmail.com>
parents: 41057
diff changeset
280 if not f.close():
be0d49134e4f * Unlink test files before and after each test; hopefully this will cut down on recent buildbot failures in test_islink.
Collin Winter <collinw@gmail.com>
parents: 41057
diff changeset
281 f.close()
29502
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
282
41097
be0d49134e4f * Unlink test files before and after each test; hopefully this will cut down on recent buildbot failures in test_islink.
Collin Winter <collinw@gmail.com>
parents: 41057
diff changeset
283 self.assertRaises(TypeError, posixpath.samefile)
13555
9c74b8239ff8 new test file for posixpath module
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
284
29502
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
285 def test_samestat(self):
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
286 f = open(test_support.TESTFN + "1", "wb")
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
287 try:
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
288 f.write("foo")
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
289 f.close()
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
290 self.assertIs(
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
291 posixpath.samestat(
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
292 os.stat(test_support.TESTFN + "1"),
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
293 os.stat(test_support.TESTFN + "1")
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
294 ),
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
295 True
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
296 )
29668
36a17904a3ed Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 29540
diff changeset
297 # If we don't have links, assume that os.stat() doesn't return resonable
29502
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
298 # inode information and thus, that samefile() doesn't work
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
299 if hasattr(os, "symlink"):
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
300 if hasattr(os, "symlink"):
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
301 os.symlink(test_support.TESTFN + "1", test_support.TESTFN + "2")
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
302 self.assertIs(
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
303 posixpath.samestat(
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
304 os.stat(test_support.TESTFN + "1"),
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
305 os.stat(test_support.TESTFN + "2")
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
306 ),
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
307 True
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
308 )
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
309 os.remove(test_support.TESTFN + "2")
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
310 f = open(test_support.TESTFN + "2", "wb")
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
311 f.write("bar")
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
312 f.close()
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
313 self.assertIs(
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
314 posixpath.samestat(
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
315 os.stat(test_support.TESTFN + "1"),
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
316 os.stat(test_support.TESTFN + "2")
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
317 ),
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
318 False
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
319 )
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
320 finally:
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
321 if not f.close():
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
322 f.close()
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
323
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
324 self.assertRaises(TypeError, posixpath.samestat)
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
325
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
326 def test_ismount(self):
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
327 self.assertIs(posixpath.ismount("/"), True)
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
328
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
329 self.assertRaises(TypeError, posixpath.ismount)
13555
9c74b8239ff8 new test file for posixpath module
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
330
29502
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
331 def test_expanduser(self):
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
332 self.assertEqual(posixpath.expanduser("foo"), "foo")
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
333 try:
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
334 import pwd
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
335 except ImportError:
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
336 pass
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
337 else:
29540
c3bc7ed9d315 Use find() instead of looping over the string in expanduser().
Walter Dörwald <walter@livinglogic.de>
parents: 29502
diff changeset
338 self.assert_(isinstance(posixpath.expanduser("~/"), basestring))
29707
762e2a297ab9 Fix SF #763362, test_posixpath failed
Neal Norwitz <nnorwitz@gmail.com>
parents: 29668
diff changeset
339 # if home directory == root directory, this test makes no sense
762e2a297ab9 Fix SF #763362, test_posixpath failed
Neal Norwitz <nnorwitz@gmail.com>
parents: 29668
diff changeset
340 if posixpath.expanduser("~") != '/':
762e2a297ab9 Fix SF #763362, test_posixpath failed
Neal Norwitz <nnorwitz@gmail.com>
parents: 29668
diff changeset
341 self.assertEqual(
762e2a297ab9 Fix SF #763362, test_posixpath failed
Neal Norwitz <nnorwitz@gmail.com>
parents: 29668
diff changeset
342 posixpath.expanduser("~") + "/",
762e2a297ab9 Fix SF #763362, test_posixpath failed
Neal Norwitz <nnorwitz@gmail.com>
parents: 29668
diff changeset
343 posixpath.expanduser("~/")
762e2a297ab9 Fix SF #763362, test_posixpath failed
Neal Norwitz <nnorwitz@gmail.com>
parents: 29668
diff changeset
344 )
29502
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
345 self.assert_(isinstance(posixpath.expanduser("~root/"), basestring))
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
346 self.assert_(isinstance(posixpath.expanduser("~foo/"), basestring))
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
347
53485
0688e8b24632 Merged revisions 71984 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents: 52903
diff changeset
348 with test_support.EnvironmentVarGuard() as env:
53644
85542715a249 Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents: 53485
diff changeset
349 env['HOME'] = '/'
53485
0688e8b24632 Merged revisions 71984 via svnmerge from
Walter Dörwald <walter@livinglogic.de>
parents: 52903
diff changeset
350 self.assertEqual(posixpath.expanduser("~"), "/")
52903
3889d9528601 Merged revisions 71058,71149-71150,71212,71214-71216,71222,71225,71234,71237-71238,71240-71241,71243,71249,71251 via svnmerge from
Georg Brandl <georg@python.org>
parents: 44522
diff changeset
351
29502
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
352 self.assertRaises(TypeError, posixpath.expanduser)
13555
9c74b8239ff8 new test file for posixpath module
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
353
29502
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
354 def test_expandvars(self):
53644
85542715a249 Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents: 53485
diff changeset
355 with test_support.EnvironmentVarGuard() as env:
85542715a249 Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents: 53485
diff changeset
356 env.clear()
85542715a249 Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents: 53485
diff changeset
357 env["foo"] = "bar"
85542715a249 Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents: 53485
diff changeset
358 env["{foo"] = "baz1"
85542715a249 Make test.test_support.EnvironmentVarGuard behave like a dictionary.
Walter Dörwald <walter@livinglogic.de>
parents: 53485
diff changeset
359 env["{foo}"] = "baz2"
29502
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
360 self.assertEqual(posixpath.expandvars("foo"), "foo")
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
361 self.assertEqual(posixpath.expandvars("$foo bar"), "bar bar")
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
362 self.assertEqual(posixpath.expandvars("${foo}bar"), "barbar")
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
363 self.assertEqual(posixpath.expandvars("$[foo]bar"), "$[foo]bar")
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
364 self.assertEqual(posixpath.expandvars("$bar bar"), "$bar bar")
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
365 self.assertEqual(posixpath.expandvars("$?bar"), "$?bar")
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
366 self.assertEqual(posixpath.expandvars("${foo}bar"), "barbar")
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
367 self.assertEqual(posixpath.expandvars("$foo}bar"), "bar}bar")
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
368 self.assertEqual(posixpath.expandvars("${foo"), "${foo")
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
369 self.assertEqual(posixpath.expandvars("${{foo}}"), "baz1}")
40649
a4cdb9c8af25 Fixed ntpath.expandvars to not replace references to non-existing
Sjoerd Mullender <sjoerd@acm.org>
parents: 35040
diff changeset
370 self.assertEqual(posixpath.expandvars("$foo$foo"), "barbar")
a4cdb9c8af25 Fixed ntpath.expandvars to not replace references to non-existing
Sjoerd Mullender <sjoerd@acm.org>
parents: 35040
diff changeset
371 self.assertEqual(posixpath.expandvars("$bar$bar"), "$bar$bar")
29502
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
372
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
373 self.assertRaises(TypeError, posixpath.expandvars)
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
374
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
375 def test_normpath(self):
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
376 self.assertEqual(posixpath.normpath(""), ".")
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
377 self.assertEqual(posixpath.normpath("/"), "/")
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
378 self.assertEqual(posixpath.normpath("//"), "//")
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
379 self.assertEqual(posixpath.normpath("///"), "/")
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
380 self.assertEqual(posixpath.normpath("///foo/.//bar//"), "/foo/bar")
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
381 self.assertEqual(posixpath.normpath("///foo/.//bar//.//..//.//baz"), "/foo/baz")
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
382 self.assertEqual(posixpath.normpath("///..//./foo/.//bar"), "/foo/bar")
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
383
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
384 self.assertRaises(TypeError, posixpath.normpath)
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
385
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
386 def test_abspath(self):
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
387 self.assert_("foo" in posixpath.abspath("foo"))
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
388
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
389 self.assertRaises(TypeError, posixpath.abspath)
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
390
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
391 def test_realpath(self):
33129
ae9b47a8d648 bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents: 29707
diff changeset
392 self.assert_("foo" in realpath("foo"))
ae9b47a8d648 bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents: 29707
diff changeset
393 self.assertRaises(TypeError, posixpath.realpath)
33204
c18aca7e81cb Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33129
diff changeset
394
33129
ae9b47a8d648 bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents: 29707
diff changeset
395 if hasattr(os, "symlink"):
ae9b47a8d648 bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents: 29707
diff changeset
396 def test_realpath_basic(self):
ae9b47a8d648 bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents: 29707
diff changeset
397 # Basic operation.
ae9b47a8d648 bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents: 29707
diff changeset
398 try:
ae9b47a8d648 bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents: 29707
diff changeset
399 os.symlink(ABSTFN+"1", ABSTFN)
ae9b47a8d648 bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents: 29707
diff changeset
400 self.assertEqual(realpath(ABSTFN), ABSTFN+"1")
ae9b47a8d648 bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents: 29707
diff changeset
401 finally:
41097
be0d49134e4f * Unlink test files before and after each test; hopefully this will cut down on recent buildbot failures in test_islink.
Collin Winter <collinw@gmail.com>
parents: 41057
diff changeset
402 test_support.unlink(ABSTFN)
33204
c18aca7e81cb Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33129
diff changeset
403
33129
ae9b47a8d648 bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents: 29707
diff changeset
404 def test_realpath_symlink_loops(self):
ae9b47a8d648 bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents: 29707
diff changeset
405 # Bug #930024, return the path unchanged if we get into an infinite
ae9b47a8d648 bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents: 29707
diff changeset
406 # symlink loop.
ae9b47a8d648 bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents: 29707
diff changeset
407 try:
ae9b47a8d648 bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents: 29707
diff changeset
408 old_path = abspath('.')
ae9b47a8d648 bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents: 29707
diff changeset
409 os.symlink(ABSTFN, ABSTFN)
ae9b47a8d648 bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents: 29707
diff changeset
410 self.assertEqual(realpath(ABSTFN), ABSTFN)
ae9b47a8d648 bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents: 29707
diff changeset
411
ae9b47a8d648 bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents: 29707
diff changeset
412 os.symlink(ABSTFN+"1", ABSTFN+"2")
ae9b47a8d648 bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents: 29707
diff changeset
413 os.symlink(ABSTFN+"2", ABSTFN+"1")
ae9b47a8d648 bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents: 29707
diff changeset
414 self.assertEqual(realpath(ABSTFN+"1"), ABSTFN+"1")
ae9b47a8d648 bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents: 29707
diff changeset
415 self.assertEqual(realpath(ABSTFN+"2"), ABSTFN+"2")
ae9b47a8d648 bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents: 29707
diff changeset
416
ae9b47a8d648 bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents: 29707
diff changeset
417 # Test using relative path as well.
ae9b47a8d648 bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents: 29707
diff changeset
418 os.chdir(dirname(ABSTFN))
ae9b47a8d648 bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents: 29707
diff changeset
419 self.assertEqual(realpath(basename(ABSTFN)), ABSTFN)
ae9b47a8d648 bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents: 29707
diff changeset
420 finally:
ae9b47a8d648 bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents: 29707
diff changeset
421 os.chdir(old_path)
41097
be0d49134e4f * Unlink test files before and after each test; hopefully this will cut down on recent buildbot failures in test_islink.
Collin Winter <collinw@gmail.com>
parents: 41057
diff changeset
422 test_support.unlink(ABSTFN)
be0d49134e4f * Unlink test files before and after each test; hopefully this will cut down on recent buildbot failures in test_islink.
Collin Winter <collinw@gmail.com>
parents: 41057
diff changeset
423 test_support.unlink(ABSTFN+"1")
be0d49134e4f * Unlink test files before and after each test; hopefully this will cut down on recent buildbot failures in test_islink.
Collin Winter <collinw@gmail.com>
parents: 41057
diff changeset
424 test_support.unlink(ABSTFN+"2")
33129
ae9b47a8d648 bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents: 29707
diff changeset
425
33204
c18aca7e81cb Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33129
diff changeset
426 def test_realpath_resolve_parents(self):
33129
ae9b47a8d648 bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents: 29707
diff changeset
427 # We also need to resolve any symlinks in the parents of a relative
ae9b47a8d648 bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents: 29707
diff changeset
428 # path passed to realpath. E.g.: current working directory is
ae9b47a8d648 bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents: 29707
diff changeset
429 # /usr/doc with 'doc' being a symlink to /usr/share/doc. We call
ae9b47a8d648 bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents: 29707
diff changeset
430 # realpath("a"). This should return /usr/share/doc/a/.
ae9b47a8d648 bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents: 29707
diff changeset
431 try:
ae9b47a8d648 bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents: 29707
diff changeset
432 old_path = abspath('.')
ae9b47a8d648 bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents: 29707
diff changeset
433 os.mkdir(ABSTFN)
ae9b47a8d648 bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents: 29707
diff changeset
434 os.mkdir(ABSTFN + "/y")
ae9b47a8d648 bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents: 29707
diff changeset
435 os.symlink(ABSTFN + "/y", ABSTFN + "/k")
29502
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
436
33129
ae9b47a8d648 bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents: 29707
diff changeset
437 os.chdir(ABSTFN + "/k")
ae9b47a8d648 bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents: 29707
diff changeset
438 self.assertEqual(realpath("a"), ABSTFN + "/y/a")
ae9b47a8d648 bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents: 29707
diff changeset
439 finally:
ae9b47a8d648 bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents: 29707
diff changeset
440 os.chdir(old_path)
41097
be0d49134e4f * Unlink test files before and after each test; hopefully this will cut down on recent buildbot failures in test_islink.
Collin Winter <collinw@gmail.com>
parents: 41057
diff changeset
441 test_support.unlink(ABSTFN + "/k")
be0d49134e4f * Unlink test files before and after each test; hopefully this will cut down on recent buildbot failures in test_islink.
Collin Winter <collinw@gmail.com>
parents: 41057
diff changeset
442 safe_rmdir(ABSTFN + "/y")
be0d49134e4f * Unlink test files before and after each test; hopefully this will cut down on recent buildbot failures in test_islink.
Collin Winter <collinw@gmail.com>
parents: 41057
diff changeset
443 safe_rmdir(ABSTFN)
33129
ae9b47a8d648 bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents: 29707
diff changeset
444
ae9b47a8d648 bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents: 29707
diff changeset
445 def test_realpath_resolve_before_normalizing(self):
ae9b47a8d648 bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents: 29707
diff changeset
446 # Bug #990669: Symbolic links should be resolved before we
ae9b47a8d648 bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents: 29707
diff changeset
447 # normalize the path. E.g.: if we have directories 'a', 'k' and 'y'
ae9b47a8d648 bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents: 29707
diff changeset
448 # in the following hierarchy:
ae9b47a8d648 bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents: 29707
diff changeset
449 # a/k/y
ae9b47a8d648 bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents: 29707
diff changeset
450 #
ae9b47a8d648 bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents: 29707
diff changeset
451 # and a symbolic link 'link-y' pointing to 'y' in directory 'a',
ae9b47a8d648 bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents: 29707
diff changeset
452 # then realpath("link-y/..") should return 'k', not 'a'.
ae9b47a8d648 bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents: 29707
diff changeset
453 try:
ae9b47a8d648 bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents: 29707
diff changeset
454 old_path = abspath('.')
ae9b47a8d648 bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents: 29707
diff changeset
455 os.mkdir(ABSTFN)
ae9b47a8d648 bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents: 29707
diff changeset
456 os.mkdir(ABSTFN + "/k")
ae9b47a8d648 bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents: 29707
diff changeset
457 os.mkdir(ABSTFN + "/k/y")
ae9b47a8d648 bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents: 29707
diff changeset
458 os.symlink(ABSTFN + "/k/y", ABSTFN + "/link-y")
ae9b47a8d648 bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents: 29707
diff changeset
459
ae9b47a8d648 bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents: 29707
diff changeset
460 # Absolute path.
ae9b47a8d648 bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents: 29707
diff changeset
461 self.assertEqual(realpath(ABSTFN + "/link-y/.."), ABSTFN + "/k")
ae9b47a8d648 bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents: 29707
diff changeset
462 # Relative path.
ae9b47a8d648 bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents: 29707
diff changeset
463 os.chdir(dirname(ABSTFN))
ae9b47a8d648 bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents: 29707
diff changeset
464 self.assertEqual(realpath(basename(ABSTFN) + "/link-y/.."), ABSTFN + "/k")
ae9b47a8d648 bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents: 29707
diff changeset
465 finally:
ae9b47a8d648 bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents: 29707
diff changeset
466 os.chdir(old_path)
41097
be0d49134e4f * Unlink test files before and after each test; hopefully this will cut down on recent buildbot failures in test_islink.
Collin Winter <collinw@gmail.com>
parents: 41057
diff changeset
467 test_support.unlink(ABSTFN + "/link-y")
be0d49134e4f * Unlink test files before and after each test; hopefully this will cut down on recent buildbot failures in test_islink.
Collin Winter <collinw@gmail.com>
parents: 41057
diff changeset
468 safe_rmdir(ABSTFN + "/k/y")
be0d49134e4f * Unlink test files before and after each test; hopefully this will cut down on recent buildbot failures in test_islink.
Collin Winter <collinw@gmail.com>
parents: 41057
diff changeset
469 safe_rmdir(ABSTFN + "/k")
be0d49134e4f * Unlink test files before and after each test; hopefully this will cut down on recent buildbot failures in test_islink.
Collin Winter <collinw@gmail.com>
parents: 41057
diff changeset
470 safe_rmdir(ABSTFN)
35040
3b41bd878918 Tools/scripts/reindent.py is your friend
Anthony Baxter <anthonybaxter@gmail.com>
parents: 35007
diff changeset
471
35007
de76b343d9c8 Backport bug #1213894: os.path.realpath didn't resolve symlinks that were the first
Georg Brandl <georg@python.org>
parents: 33332
diff changeset
472 def test_realpath_resolve_first(self):
de76b343d9c8 Backport bug #1213894: os.path.realpath didn't resolve symlinks that were the first
Georg Brandl <georg@python.org>
parents: 33332
diff changeset
473 # Bug #1213894: The first component of the path, if not absolute,
de76b343d9c8 Backport bug #1213894: os.path.realpath didn't resolve symlinks that were the first
Georg Brandl <georg@python.org>
parents: 33332
diff changeset
474 # must be resolved too.
de76b343d9c8 Backport bug #1213894: os.path.realpath didn't resolve symlinks that were the first
Georg Brandl <georg@python.org>
parents: 33332
diff changeset
475
de76b343d9c8 Backport bug #1213894: os.path.realpath didn't resolve symlinks that were the first
Georg Brandl <georg@python.org>
parents: 33332
diff changeset
476 try:
de76b343d9c8 Backport bug #1213894: os.path.realpath didn't resolve symlinks that were the first
Georg Brandl <georg@python.org>
parents: 33332
diff changeset
477 old_path = abspath('.')
de76b343d9c8 Backport bug #1213894: os.path.realpath didn't resolve symlinks that were the first
Georg Brandl <georg@python.org>
parents: 33332
diff changeset
478 os.mkdir(ABSTFN)
de76b343d9c8 Backport bug #1213894: os.path.realpath didn't resolve symlinks that were the first
Georg Brandl <georg@python.org>
parents: 33332
diff changeset
479 os.mkdir(ABSTFN + "/k")
de76b343d9c8 Backport bug #1213894: os.path.realpath didn't resolve symlinks that were the first
Georg Brandl <georg@python.org>
parents: 33332
diff changeset
480 os.symlink(ABSTFN, ABSTFN + "link")
de76b343d9c8 Backport bug #1213894: os.path.realpath didn't resolve symlinks that were the first
Georg Brandl <georg@python.org>
parents: 33332
diff changeset
481 os.chdir(dirname(ABSTFN))
35040
3b41bd878918 Tools/scripts/reindent.py is your friend
Anthony Baxter <anthonybaxter@gmail.com>
parents: 35007
diff changeset
482
35007
de76b343d9c8 Backport bug #1213894: os.path.realpath didn't resolve symlinks that were the first
Georg Brandl <georg@python.org>
parents: 33332
diff changeset
483 base = basename(ABSTFN)
de76b343d9c8 Backport bug #1213894: os.path.realpath didn't resolve symlinks that were the first
Georg Brandl <georg@python.org>
parents: 33332
diff changeset
484 self.assertEqual(realpath(base + "link"), ABSTFN)
de76b343d9c8 Backport bug #1213894: os.path.realpath didn't resolve symlinks that were the first
Georg Brandl <georg@python.org>
parents: 33332
diff changeset
485 self.assertEqual(realpath(base + "link/k"), ABSTFN + "/k")
de76b343d9c8 Backport bug #1213894: os.path.realpath didn't resolve symlinks that were the first
Georg Brandl <georg@python.org>
parents: 33332
diff changeset
486 finally:
de76b343d9c8 Backport bug #1213894: os.path.realpath didn't resolve symlinks that were the first
Georg Brandl <georg@python.org>
parents: 33332
diff changeset
487 os.chdir(old_path)
41097
be0d49134e4f * Unlink test files before and after each test; hopefully this will cut down on recent buildbot failures in test_islink.
Collin Winter <collinw@gmail.com>
parents: 41057
diff changeset
488 test_support.unlink(ABSTFN + "link")
be0d49134e4f * Unlink test files before and after each test; hopefully this will cut down on recent buildbot failures in test_islink.
Collin Winter <collinw@gmail.com>
parents: 41057
diff changeset
489 safe_rmdir(ABSTFN + "/k")
be0d49134e4f * Unlink test files before and after each test; hopefully this will cut down on recent buildbot failures in test_islink.
Collin Winter <collinw@gmail.com>
parents: 41057
diff changeset
490 safe_rmdir(ABSTFN)
29502
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
491
41218
f3d4274d063d Patch 1339796: add a relpath() function to os.path.
Collin Winter <collinw@gmail.com>
parents: 41133
diff changeset
492 def test_relpath(self):
41293
1c3e0d2c36dc Make test_relpath() pass on Windows.
Collin Winter <collinw@gmail.com>
parents: 41218
diff changeset
493 (real_getcwd, os.getcwd) = (os.getcwd, lambda: r"/home/user/bar")
1c3e0d2c36dc Make test_relpath() pass on Windows.
Collin Winter <collinw@gmail.com>
parents: 41218
diff changeset
494 try:
1c3e0d2c36dc Make test_relpath() pass on Windows.
Collin Winter <collinw@gmail.com>
parents: 41218
diff changeset
495 curdir = os.path.split(os.getcwd())[-1]
1c3e0d2c36dc Make test_relpath() pass on Windows.
Collin Winter <collinw@gmail.com>
parents: 41218
diff changeset
496 self.assertRaises(ValueError, posixpath.relpath, "")
1c3e0d2c36dc Make test_relpath() pass on Windows.
Collin Winter <collinw@gmail.com>
parents: 41218
diff changeset
497 self.assertEqual(posixpath.relpath("a"), "a")
1c3e0d2c36dc Make test_relpath() pass on Windows.
Collin Winter <collinw@gmail.com>
parents: 41218
diff changeset
498 self.assertEqual(posixpath.relpath(posixpath.abspath("a")), "a")
1c3e0d2c36dc Make test_relpath() pass on Windows.
Collin Winter <collinw@gmail.com>
parents: 41218
diff changeset
499 self.assertEqual(posixpath.relpath("a/b"), "a/b")
1c3e0d2c36dc Make test_relpath() pass on Windows.
Collin Winter <collinw@gmail.com>
parents: 41218
diff changeset
500 self.assertEqual(posixpath.relpath("../a/b"), "../a/b")
1c3e0d2c36dc Make test_relpath() pass on Windows.
Collin Winter <collinw@gmail.com>
parents: 41218
diff changeset
501 self.assertEqual(posixpath.relpath("a", "../b"), "../"+curdir+"/a")
1c3e0d2c36dc Make test_relpath() pass on Windows.
Collin Winter <collinw@gmail.com>
parents: 41218
diff changeset
502 self.assertEqual(posixpath.relpath("a/b", "../c"), "../"+curdir+"/a/b")
1c3e0d2c36dc Make test_relpath() pass on Windows.
Collin Winter <collinw@gmail.com>
parents: 41218
diff changeset
503 self.assertEqual(posixpath.relpath("a", "b/c"), "../../a")
44522
4321a5abd6ce #1742: don't raise exception on os.path.relpath("a", "a"), but return os.curdir.
Georg Brandl <georg@python.org>
parents: 41701
diff changeset
504 self.assertEqual(posixpath.relpath("a", "a"), ".")
41293
1c3e0d2c36dc Make test_relpath() pass on Windows.
Collin Winter <collinw@gmail.com>
parents: 41218
diff changeset
505 finally:
1c3e0d2c36dc Make test_relpath() pass on Windows.
Collin Winter <collinw@gmail.com>
parents: 41218
diff changeset
506 os.getcwd = real_getcwd
41218
f3d4274d063d Patch 1339796: add a relpath() function to os.path.
Collin Winter <collinw@gmail.com>
parents: 41133
diff changeset
507
29502
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
508 def test_main():
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
509 test_support.run_unittest(PosixPathTest)
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
510
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
511 if __name__=="__main__":
71c06bc5a73e Complete rewrite of tests by Walter Dorwald as unittest tests.
Brett Cannon <bcannon@gmail.com>
parents: 26627
diff changeset
512 test_main()