test_zippath.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. """
  4. Test cases covering L{twisted.python.zippath}.
  5. """
  6. from __future__ import absolute_import, division
  7. import os
  8. import zipfile
  9. from twisted.test.test_paths import AbstractFilePathTests
  10. from twisted.python.zippath import ZipArchive
  11. from twisted.python.filepath import _coerceToFilesystemEncoding
  12. def zipit(dirname, zfname):
  13. """
  14. Create a zipfile on zfname, containing the contents of dirname'
  15. """
  16. dirname = _coerceToFilesystemEncoding('', dirname)
  17. zfname = _coerceToFilesystemEncoding('', zfname)
  18. with zipfile.ZipFile(zfname, "w") as zf:
  19. for root, ignored, files, in os.walk(dirname):
  20. for fname in files:
  21. fspath = os.path.join(root, fname)
  22. arcpath = os.path.join(root, fname)[len(dirname)+1:]
  23. zf.write(fspath, arcpath)
  24. class ZipFilePathTests(AbstractFilePathTests):
  25. """
  26. Test various L{ZipPath} path manipulations as well as reprs for L{ZipPath}
  27. and L{ZipArchive}.
  28. """
  29. def setUp(self):
  30. AbstractFilePathTests.setUp(self)
  31. zipit(self.cmn, self.cmn + b'.zip')
  32. self.nativecmn = _coerceToFilesystemEncoding('', self.cmn)
  33. self.path = ZipArchive(self.cmn + b'.zip')
  34. self.root = self.path
  35. self.all = [x.replace(self.cmn, self.cmn + b'.zip')
  36. for x in self.all]
  37. def test_zipPathRepr(self):
  38. """
  39. Make sure that invoking ZipPath's repr prints the correct class name
  40. and an absolute path to the zip file.
  41. """
  42. child = self.path.child("foo")
  43. pathRepr = "ZipPath(%r)" % (
  44. os.path.abspath(self.nativecmn + ".zip" + os.sep + 'foo'),)
  45. # Check for an absolute path
  46. self.assertEqual(repr(child), pathRepr)
  47. # Create a path to the file rooted in the current working directory
  48. relativeCommon = self.nativecmn.replace(os.getcwd() + os.sep,
  49. "", 1) + ".zip"
  50. relpath = ZipArchive(relativeCommon)
  51. child = relpath.child("foo")
  52. # Check using a path without the cwd prepended
  53. self.assertEqual(repr(child), pathRepr)
  54. def test_zipPathReprParentDirSegment(self):
  55. """
  56. The repr of a ZipPath with C{".."} in the internal part of its path
  57. includes the C{".."} rather than applying the usual parent directory
  58. meaning.
  59. """
  60. child = self.path.child("foo").child("..").child("bar")
  61. pathRepr = "ZipPath(%r)" % (
  62. self.nativecmn + ".zip" + os.sep.join(["", "foo", "..", "bar"]))
  63. self.assertEqual(repr(child), pathRepr)
  64. def test_zipArchiveRepr(self):
  65. """
  66. Make sure that invoking ZipArchive's repr prints the correct class
  67. name and an absolute path to the zip file.
  68. """
  69. path = ZipArchive(self.nativecmn + '.zip')
  70. pathRepr = 'ZipArchive(%r)' % (os.path.abspath(
  71. self.nativecmn + '.zip'),)
  72. # Check for an absolute path
  73. self.assertEqual(repr(path), pathRepr)
  74. # Create a path to the file rooted in the current working directory
  75. relativeCommon = self.nativecmn.replace(os.getcwd() + os.sep,
  76. "", 1) + ".zip"
  77. relpath = ZipArchive(relativeCommon)
  78. # Check using a path without the cwd prepended
  79. self.assertEqual(repr(relpath), pathRepr)