home *** CD-ROM | disk | FTP | other *** search
/ PC World 2005 June / PCWorld_2005-06_cd.bin / software / vyzkuste / firewally / firewally.exe / framework-2.3.exe / test_posixpath.py < prev    next >
Text File  |  2003-12-30  |  15KB  |  403 lines

  1. import unittest
  2. from test import test_support
  3.  
  4. import posixpath, os
  5.  
  6. class PosixPathTest(unittest.TestCase):
  7.  
  8.     def assertIs(self, a, b):
  9.         self.assert_(a is b)
  10.  
  11.     def test_normcase(self):
  12.         # Check that normcase() is idempotent
  13.         p = "FoO/./BaR"
  14.         p = posixpath.normcase(p)
  15.         self.assertEqual(p, posixpath.normcase(p))
  16.  
  17.         self.assertRaises(TypeError, posixpath.normcase)
  18.  
  19.     def test_join(self):
  20.         self.assertEqual(posixpath.join("/foo", "bar", "/bar", "baz"), "/bar/baz")
  21.         self.assertEqual(posixpath.join("/foo", "bar", "baz"), "/foo/bar/baz")
  22.         self.assertEqual(posixpath.join("/foo/", "bar/", "baz/"), "/foo/bar/baz/")
  23.  
  24.         self.assertRaises(TypeError, posixpath.join)
  25.  
  26.     def test_splitdrive(self):
  27.         self.assertEqual(posixpath.splitdrive("/foo/bar"), ("", "/foo/bar"))
  28.  
  29.         self.assertRaises(TypeError, posixpath.splitdrive)
  30.  
  31.     def test_split(self):
  32.         self.assertEqual(posixpath.split("/foo/bar"), ("/foo", "bar"))
  33.         self.assertEqual(posixpath.split("/"), ("/", ""))
  34.         self.assertEqual(posixpath.split("foo"), ("", "foo"))
  35.         self.assertEqual(posixpath.split("////foo"), ("////", "foo"))
  36.         self.assertEqual(posixpath.split("//foo//bar"), ("//foo", "bar"))
  37.  
  38.         self.assertRaises(TypeError, posixpath.split)
  39.  
  40.     def test_splitext(self):
  41.         self.assertEqual(posixpath.splitext("foo.ext"), ("foo", ".ext"))
  42.         self.assertEqual(posixpath.splitext("/foo/foo.ext"), ("/foo/foo", ".ext"))
  43.         self.assertEqual(posixpath.splitext(".ext"), ("", ".ext"))
  44.         self.assertEqual(posixpath.splitext("/foo.ext/foo"), ("/foo.ext/foo", ""))
  45.         self.assertEqual(posixpath.splitext("foo.ext/"), ("foo.ext/", ""))
  46.         self.assertEqual(posixpath.splitext(""), ("", ""))
  47.         self.assertEqual(posixpath.splitext("foo.bar.ext"), ("foo.bar", ".ext"))
  48.  
  49.         self.assertRaises(TypeError, posixpath.splitext)
  50.  
  51.     def test_isabs(self):
  52.         self.assertIs(posixpath.isabs(""), False)
  53.         self.assertIs(posixpath.isabs("/"), True)
  54.         self.assertIs(posixpath.isabs("/foo"), True)
  55.         self.assertIs(posixpath.isabs("/foo/bar"), True)
  56.         self.assertIs(posixpath.isabs("foo/bar"), False)
  57.  
  58.         self.assertRaises(TypeError, posixpath.isabs)
  59.  
  60.     def test_splitdrive(self):
  61.         self.assertEqual(posixpath.splitdrive("/foo/bar"), ("", "/foo/bar"))
  62.  
  63.         self.assertRaises(TypeError, posixpath.splitdrive)
  64.  
  65.     def test_basename(self):
  66.         self.assertEqual(posixpath.basename("/foo/bar"), "bar")
  67.         self.assertEqual(posixpath.basename("/"), "")
  68.         self.assertEqual(posixpath.basename("foo"), "foo")
  69.         self.assertEqual(posixpath.basename("////foo"), "foo")
  70.         self.assertEqual(posixpath.basename("//foo//bar"), "bar")
  71.  
  72.         self.assertRaises(TypeError, posixpath.basename)
  73.  
  74.     def test_dirname(self):
  75.         self.assertEqual(posixpath.dirname("/foo/bar"), "/foo")
  76.         self.assertEqual(posixpath.dirname("/"), "/")
  77.         self.assertEqual(posixpath.dirname("foo"), "")
  78.         self.assertEqual(posixpath.dirname("////foo"), "////")
  79.         self.assertEqual(posixpath.dirname("//foo//bar"), "//foo")
  80.  
  81.         self.assertRaises(TypeError, posixpath.dirname)
  82.  
  83.     def test_commonprefix(self):
  84.         self.assertEqual(
  85.             posixpath.commonprefix([]),
  86.             ""
  87.         )
  88.         self.assertEqual(
  89.             posixpath.commonprefix(["/home/swenson/spam", "/home/swen/spam"]),
  90.             "/home/swen"
  91.         )
  92.         self.assertEqual(
  93.             posixpath.commonprefix(["/home/swen/spam", "/home/swen/eggs"]),
  94.             "/home/swen/"
  95.         )
  96.         self.assertEqual(
  97.             posixpath.commonprefix(["/home/swen/spam", "/home/swen/spam"]),
  98.             "/home/swen/spam"
  99.         )
  100.  
  101.     def test_getsize(self):
  102.         f = open(test_support.TESTFN, "wb")
  103.         try:
  104.             f.write("foo")
  105.             f.close()
  106.             self.assertEqual(posixpath.getsize(test_support.TESTFN), 3)
  107.         finally:
  108.             if not f.closed:
  109.                 f.close()
  110.             os.remove(test_support.TESTFN)
  111.  
  112.     def test_time(self):
  113.         f = open(test_support.TESTFN, "wb")
  114.         try:
  115.             f.write("foo")
  116.             f.close()
  117.             f = open(test_support.TESTFN, "ab")
  118.             f.write("bar")
  119.             f.close()
  120.             f = open(test_support.TESTFN, "rb")
  121.             d = f.read()
  122.             f.close()
  123.             self.assertEqual(d, "foobar")
  124.  
  125.             self.assert_(
  126.                 posixpath.getctime(test_support.TESTFN) <=
  127.                 posixpath.getmtime(test_support.TESTFN)
  128.             )
  129.         finally:
  130.             if not f.closed:
  131.                 f.close()
  132.             os.remove(test_support.TESTFN)
  133.  
  134.     def test_islink(self):
  135.         self.assertIs(posixpath.islink(test_support.TESTFN + "1"), False)
  136.         f = open(test_support.TESTFN + "1", "wb")
  137.         try:
  138.             f.write("foo")
  139.             f.close()
  140.             self.assertIs(posixpath.islink(test_support.TESTFN + "1"), False)
  141.             if hasattr(os, "symlink"):
  142.                 os.symlink(test_support.TESTFN + "1", test_support.TESTFN + "2")
  143.                 self.assertIs(posixpath.islink(test_support.TESTFN + "2"), True)
  144.                 os.remove(test_support.TESTFN + "1")
  145.                 self.assertIs(posixpath.islink(test_support.TESTFN + "2"), True)
  146.                 self.assertIs(posixpath.exists(test_support.TESTFN + "2"), False)
  147.         finally:
  148.             if not f.close():
  149.                 f.close()
  150.             try:
  151.                 os.remove(test_support.TESTFN + "1")
  152.             except os.error:
  153.                 pass
  154.             try:
  155.                 os.remove(test_support.TESTFN + "2")
  156.             except os.error:
  157.                 pass
  158.  
  159.         self.assertRaises(TypeError, posixpath.islink)
  160.  
  161.     def test_exists(self):
  162.         self.assertIs(posixpath.exists(test_support.TESTFN), False)
  163.         f = open(test_support.TESTFN, "wb")
  164.         try:
  165.             f.write("foo")
  166.             f.close()
  167.             self.assertIs(posixpath.exists(test_support.TESTFN), True)
  168.         finally:
  169.             if not f.close():
  170.                 f.close()
  171.             try:
  172.                 os.remove(test_support.TESTFN)
  173.             except os.error:
  174.                 pass
  175.  
  176.         self.assertRaises(TypeError, posixpath.exists)
  177.  
  178.     def test_isdir(self):
  179.         self.assertIs(posixpath.isdir(test_support.TESTFN), False)
  180.         f = open(test_support.TESTFN, "wb")
  181.         try:
  182.             f.write("foo")
  183.             f.close()
  184.             self.assertIs(posixpath.isdir(test_support.TESTFN), False)
  185.             os.remove(test_support.TESTFN)
  186.             os.mkdir(test_support.TESTFN)
  187.             self.assertIs(posixpath.isdir(test_support.TESTFN), True)
  188.             os.rmdir(test_support.TESTFN)
  189.         finally:
  190.             if not f.close():
  191.                 f.close()
  192.             try:
  193.                 os.remove(test_support.TESTFN)
  194.             except os.error:
  195.                 pass
  196.             try:
  197.                 os.rmdir(test_support.TESTFN)
  198.             except os.error:
  199.                 pass
  200.  
  201.         self.assertRaises(TypeError, posixpath.isdir)
  202.  
  203.     def test_isfile(self):
  204.         self.assertIs(posixpath.isfile(test_support.TESTFN), False)
  205.         f = open(test_support.TESTFN, "wb")
  206.         try:
  207.             f.write("foo")
  208.             f.close()
  209.             self.assertIs(posixpath.isfile(test_support.TESTFN), True)
  210.             os.remove(test_support.TESTFN)
  211.             os.mkdir(test_support.TESTFN)
  212.             self.assertIs(posixpath.isfile(test_support.TESTFN), False)
  213.             os.rmdir(test_support.TESTFN)
  214.         finally:
  215.             if not f.close():
  216.                 f.close()
  217.             try:
  218.                 os.remove(test_support.TESTFN)
  219.             except os.error:
  220.                 pass
  221.             try:
  222.                 os.rmdir(test_support.TESTFN)
  223.             except os.error:
  224.                 pass
  225.  
  226.         self.assertRaises(TypeError, posixpath.isdir)
  227.  
  228.         def test_samefile(self):
  229.             f = open(test_support.TESTFN + "1", "wb")
  230.             try:
  231.                 f.write("foo")
  232.                 f.close()
  233.                 self.assertIs(
  234.                     posixpath.samefile(
  235.                         test_support.TESTFN + "1",
  236.                         test_support.TESTFN + "1"
  237.                     ),
  238.                     True
  239.                 )
  240.                 # If we don't have links, assume that os.stat doesn't return resonable
  241.                 # inode information and thus, that samefile() doesn't work
  242.                 if hasattr(os, "symlink"):
  243.                     os.symlink(
  244.                         test_support.TESTFN + "1",
  245.                         test_support.TESTFN + "2"
  246.                     )
  247.                     self.assertIs(
  248.                         posixpath.samefile(
  249.                             test_support.TESTFN + "1",
  250.                             test_support.TESTFN + "2"
  251.                         ),
  252.                         True
  253.                     )
  254.                     os.remove(test_support.TESTFN + "2")
  255.                     f = open(test_support.TESTFN + "2", "wb")
  256.                     f.write("bar")
  257.                     f.close()
  258.                     self.assertIs(
  259.                         posixpath.samefile(
  260.                             test_support.TESTFN + "1",
  261.                             test_support.TESTFN + "2"
  262.                         ),
  263.                         False
  264.                     )
  265.             finally:
  266.                 if not f.close():
  267.                     f.close()
  268.                 try:
  269.                     os.remove(test_support.TESTFN + "1")
  270.                 except os.error:
  271.                     pass
  272.                 try:
  273.                     os.remove(test_support.TESTFN + "2")
  274.                 except os.error:
  275.                     pass
  276.  
  277.             self.assertRaises(TypeError, posixpath.samefile)
  278.  
  279.     def test_samestat(self):
  280.         f = open(test_support.TESTFN + "1", "wb")
  281.         try:
  282.             f.write("foo")
  283.             f.close()
  284.             self.assertIs(
  285.                 posixpath.samestat(
  286.                     os.stat(test_support.TESTFN + "1"),
  287.                     os.stat(test_support.TESTFN + "1")
  288.                 ),
  289.                 True
  290.             )
  291.             # If we don't have links, assume that os.stat() doesn't return resonable
  292.             # inode information and thus, that samefile() doesn't work
  293.             if hasattr(os, "symlink"):
  294.                 if hasattr(os, "symlink"):
  295.                     os.symlink(test_support.TESTFN + "1", test_support.TESTFN + "2")
  296.                     self.assertIs(
  297.                         posixpath.samestat(
  298.                             os.stat(test_support.TESTFN + "1"),
  299.                             os.stat(test_support.TESTFN + "2")
  300.                         ),
  301.                         True
  302.                     )
  303.                     os.remove(test_support.TESTFN + "2")
  304.                 f = open(test_support.TESTFN + "2", "wb")
  305.                 f.write("bar")
  306.                 f.close()
  307.                 self.assertIs(
  308.                     posixpath.samestat(
  309.                         os.stat(test_support.TESTFN + "1"),
  310.                         os.stat(test_support.TESTFN + "2")
  311.                     ),
  312.                     False
  313.                 )
  314.         finally:
  315.             if not f.close():
  316.                 f.close()
  317.             try:
  318.                 os.remove(test_support.TESTFN + "1")
  319.             except os.error:
  320.                 pass
  321.             try:
  322.                 os.remove(test_support.TESTFN + "2")
  323.             except os.error:
  324.                 pass
  325.  
  326.         self.assertRaises(TypeError, posixpath.samestat)
  327.  
  328.     def test_ismount(self):
  329.         if os.name in ('mac',):
  330.             return
  331.         self.assertIs(posixpath.ismount("/"), True)
  332.  
  333.         self.assertRaises(TypeError, posixpath.ismount)
  334.  
  335.     def test_expanduser(self):
  336.         self.assertEqual(posixpath.expanduser("foo"), "foo")
  337.         try:
  338.             import pwd
  339.         except ImportError:
  340.             pass
  341.         else:
  342.             self.assert_(isinstance(posixpath.expanduser("~/"), basestring))
  343.             # if home directory == root directory, this test makes no sense
  344.             if posixpath.expanduser("~") != '/':
  345.                 self.assertEqual(
  346.                     posixpath.expanduser("~") + "/",
  347.                     posixpath.expanduser("~/")
  348.                 )
  349.             self.assert_(isinstance(posixpath.expanduser("~root/"), basestring))
  350.             self.assert_(isinstance(posixpath.expanduser("~foo/"), basestring))
  351.  
  352.         self.assertRaises(TypeError, posixpath.expanduser)
  353.  
  354.     def test_expandvars(self):
  355.         oldenv = os.environ.copy()
  356.         try:
  357.             os.environ.clear()
  358.             os.environ["foo"] = "bar"
  359.             os.environ["{foo"] = "baz1"
  360.             os.environ["{foo}"] = "baz2"
  361.             self.assertEqual(posixpath.expandvars("foo"), "foo")
  362.             self.assertEqual(posixpath.expandvars("$foo bar"), "bar bar")
  363.             self.assertEqual(posixpath.expandvars("${foo}bar"), "barbar")
  364.             self.assertEqual(posixpath.expandvars("$[foo]bar"), "$[foo]bar")
  365.             self.assertEqual(posixpath.expandvars("$bar bar"), "$bar bar")
  366.             self.assertEqual(posixpath.expandvars("$?bar"), "$?bar")
  367.             self.assertEqual(posixpath.expandvars("${foo}bar"), "barbar")
  368.             self.assertEqual(posixpath.expandvars("$foo}bar"), "bar}bar")
  369.             self.assertEqual(posixpath.expandvars("${foo"), "${foo")
  370.             self.assertEqual(posixpath.expandvars("${{foo}}"), "baz1}")
  371.         finally:
  372.             os.environ.clear()
  373.             os.environ.update(oldenv)
  374.  
  375.         self.assertRaises(TypeError, posixpath.expandvars)
  376.  
  377.     def test_normpath(self):
  378.         self.assertEqual(posixpath.normpath(""), ".")
  379.         self.assertEqual(posixpath.normpath("/"), "/")
  380.         self.assertEqual(posixpath.normpath("//"), "//")
  381.         self.assertEqual(posixpath.normpath("///"), "/")
  382.         self.assertEqual(posixpath.normpath("///foo/.//bar//"), "/foo/bar")
  383.         self.assertEqual(posixpath.normpath("///foo/.//bar//.//..//.//baz"), "/foo/baz")
  384.         self.assertEqual(posixpath.normpath("///..//./foo/.//bar"), "/foo/bar")
  385.  
  386.         self.assertRaises(TypeError, posixpath.normpath)
  387.  
  388.     def test_abspath(self):
  389.         self.assert_("foo" in posixpath.abspath("foo"))
  390.  
  391.         self.assertRaises(TypeError, posixpath.abspath)
  392.  
  393.     def test_realpath(self):
  394.         self.assert_("foo" in posixpath.realpath("foo"))
  395.  
  396.         self.assertRaises(TypeError, posixpath.realpath)
  397.  
  398. def test_main():
  399.     test_support.run_unittest(PosixPathTest)
  400.  
  401. if __name__=="__main__":
  402.     test_main()
  403.