/qooxdoo/tool/test/misc/copytool.py

https://github.com/Wkasel/qooxdoo · Python · 238 lines · 173 code · 42 blank · 23 comment · 1 complexity · 99b80d605071dc19170a8e56a6a93157 MD5 · raw file

  1. #! /usr/bin/env python
  2. ################################################################################
  3. #
  4. # qooxdoo - the new era of web development
  5. #
  6. # http://qooxdoo.org
  7. #
  8. # Copyright:
  9. # 2006-2010 1&1 Internet AG, Germany, http://www.1und1.de
  10. #
  11. # License:
  12. # LGPL: http://www.gnu.org/licenses/lgpl.html
  13. # EPL: http://www.eclipse.org/org/documents/epl-v10.php
  14. # See the LICENSE file in the project's top-level directory for details.
  15. #
  16. # Authors:
  17. # * Daniel Wagner (d_wagner)
  18. #
  19. ################################################################################
  20. import unittest
  21. import os
  22. import sys
  23. import shutil
  24. import stat
  25. import filecmp
  26. import tempfile
  27. libDir = os.path.abspath(os.path.join(os.pardir, os.pardir, "pylib"))
  28. sys.path.append(libDir)
  29. from misc.copytool import CopyTool
  30. class TestCopyTool(unittest.TestCase):
  31. def setUp(self):
  32. self.copier = CopyTool()
  33. self.tempDir = tempfile.mkdtemp()
  34. self.file1 = os.path.join(self.tempDir, "file1")
  35. file(self.file1, "w")
  36. self.file2 = os.path.join(self.tempDir, "file2")
  37. file(self.file2, "w")
  38. self.dir1 = os.path.join(self.tempDir, "dir1")
  39. os.mkdir(self.dir1)
  40. self.dir2 = os.path.join(self.tempDir, "dir2")
  41. os.mkdir(self.dir2)
  42. def tearDown(self):
  43. shutil.rmtree(self.tempDir)
  44. def testNoSource(self):
  45. target = os.getcwd()
  46. self.copier.parse_args(["nothing.txt", target])
  47. self.failUnlessRaises(IOError, self.copier.do_work)
  48. def testNoTarget(self):
  49. self.copier.parse_args(["--no-new-dirs", self.file1, "nothing"])
  50. self.failUnlessRaises(IOError, self.copier.do_work)
  51. def testInvalidTarget(self):
  52. self.copier.parse_args([self.file1, self.file2])
  53. self.failUnlessRaises(Exception, self.copier.do_work)
  54. def testFile(self):
  55. self.copier.parse_args([self.file1, self.dir1])
  56. self.copier.do_work()
  57. self.failUnless(os.path.isfile(os.path.join(self.dir1, self.file1)), "Source file was not copied to target directory!")
  58. def testCreateDir(self):
  59. self.copier.parse_args([self.file1, "newDir"])
  60. self.copier.do_work()
  61. self.failUnless(os.path.isdir("newDir"), "Directory was not created!")
  62. shutil.rmtree("newDir")
  63. def testOverwriteProtectedFile(self):
  64. source = file(self.file1, "w")
  65. source.write("source")
  66. source.close()
  67. targetPath = os.path.join(self.dir2, "file1")
  68. target = file(targetPath, "w")
  69. # set target read-only
  70. os.chmod(targetPath, stat.S_IRUSR)
  71. self.copier.parse_args([self.file1, self.dir2])
  72. self.copier.do_work()
  73. copied = file(targetPath, "r")
  74. copiedContent = copied.read()
  75. shutil.rmtree(targetPath, True)
  76. self.failUnless(copiedContent == "source", "Failed to overwrite readonly file!")
  77. def testContinueAfterFailedFileCopy(self):
  78. os.makedirs(os.path.join(self.dir1, "aaa"))
  79. file(os.path.join(self.dir1, "aaa", "file"), "w")
  80. os.makedirs(os.path.join(self.dir1, "zzz"))
  81. file(os.path.join(self.dir1, "zzz", "file"), "w")
  82. os.makedirs(os.path.join(self.dir2, "dir1"))
  83. os.makedirs(os.path.join(self.dir2, "dir1", "aaa"), 0444)
  84. os.makedirs(os.path.join(self.dir2, "dir1", "zzz"))
  85. self.copier.parse_args([self.dir1, self.dir2])
  86. self.copier.do_work()
  87. self.failUnless(os.path.exists(os.path.join(self.dir2, "dir1", "zzz", "file")), "Stopped working after failed copy!")
  88. def testUpdate(self):
  89. source = file(self.file1, "w")
  90. source.write("source")
  91. source.close()
  92. sourceMtime = os.stat(self.file1).st_mtime
  93. # make source file older
  94. os.utime(self.file1, (sourceMtime - 20000000, sourceMtime - 20000000))
  95. targetPath = os.path.join(self.dir2, "file1")
  96. file(targetPath, "w")
  97. self.copier.parse_args(["--update-only", self.file1, self.dir2])
  98. self.copier.do_work()
  99. target = file(targetPath, "r")
  100. targetContent = target.read()
  101. shutil.rmtree(targetPath, True)
  102. self.failIf(targetContent == "source", "Newer target file was overwritten!")
  103. copier = CopyTool()
  104. copier.parse_args([self.file1, self.dir2])
  105. copier.do_work()
  106. target = file(targetPath, "r")
  107. targetContent = target.read()
  108. shutil.rmtree(targetPath, True)
  109. self.failIf(targetContent != "source", "Newer target file was not overwritten!")
  110. def testDirectorySimple(self):
  111. self.copier.parse_args([self.dir1, self.dir2])
  112. self.copier.do_work()
  113. self.failUnless(os.path.isdir(os.path.join(self.dir2, "dir1")), "Source file was not copied to target!")
  114. def testDirectoryDeep(self):
  115. file(os.path.join(self.dir1, "file1"), "w")
  116. file(os.path.join(self.dir1, "file2"), "w")
  117. os.makedirs(os.path.join(self.dir1, "dir1_1", "dir1_1_1"))
  118. file(os.path.join(self.dir1, "dir1_1", "file1"), "w")
  119. file(os.path.join(self.dir1, "dir1_1", "file2"), "w")
  120. file(os.path.join(self.dir1, "dir1_1", "dir1_1_1", "file1"), "w")
  121. file(os.path.join(self.dir1, "dir1_1", "dir1_1_1", "file2"), "w")
  122. self.copier.parse_args([self.dir1, self.dir2])
  123. self.copier.do_work()
  124. comp = filecmp.cmp(os.path.join(self.dir1, "file1"), os.path.join(self.dir2, "dir1", "file1"))
  125. self.failUnless(comp, "Source file was not copied to target!")
  126. comp = filecmp.cmp(os.path.join(self.dir1, "file2"), os.path.join(self.dir2, "dir1", "file2"))
  127. self.failUnless(comp, "Source file was not copied to target!")
  128. comp = filecmp.cmp(os.path.join(self.dir1, "dir1_1", "file1"), os.path.join(self.dir2, "dir1", "dir1_1", "file1"))
  129. self.failUnless(comp, "Source file was not copied to target!")
  130. comp = filecmp.cmp(os.path.join(self.dir1, "dir1_1", "file2"), os.path.join(self.dir2, "dir1", "dir1_1", "file2"))
  131. self.failUnless(comp, "Source file was not copied to target!")
  132. comp = filecmp.cmp(os.path.join(self.dir1, "dir1_1", "dir1_1_1", "file1"), os.path.join(self.dir2, "dir1", "dir1_1", "dir1_1_1", "file1"))
  133. self.failUnless(comp, "Source file was not copied to target!")
  134. comp = filecmp.cmp(os.path.join(self.dir1, "dir1_1", "dir1_1_1", "file2"), os.path.join(self.dir2, "dir1", "dir1_1", "dir1_1_1", "file2"))
  135. self.failUnless(comp, "Source file was not copied to target!")
  136. def testDirectoryUpdate(self):
  137. sourceDir = os.path.join(self.dir1, "dir1_1")
  138. os.mkdir(sourceDir)
  139. sourceOlderPath = os.path.join(sourceDir, "file1")
  140. sourceOlder = file(sourceOlderPath, "w")
  141. sourceOlder.write("sourceOlder")
  142. sourceOlder.close()
  143. sourceMtime = os.stat(sourceOlderPath).st_mtime
  144. #make source file older
  145. os.utime(sourceOlderPath, (sourceMtime - 20000000, sourceMtime - 20000000))
  146. sourceNewer = file(os.path.join(sourceDir, "file2"), "w")
  147. sourceNewer.write("sourceNewer")
  148. sourceNewer.close()
  149. targetDir = os.path.join(self.dir2, "dir1", "dir1_1")
  150. os.makedirs(targetDir)
  151. targetNewerPath = os.path.join(targetDir, "file1")
  152. targetNewer = file(targetNewerPath, "w")
  153. targetNewer.write("targetNewer")
  154. targetNewer.close()
  155. targetOlderPath = os.path.join(targetDir, "file2")
  156. targetOlder = file(targetOlderPath, "w")
  157. targetOlder.write("targetOlder")
  158. targetOlder.close()
  159. targetMtime = os.stat(targetOlderPath).st_mtime
  160. #make source file older
  161. os.utime(targetOlderPath, (sourceMtime - 20000000, sourceMtime - 20000000))
  162. self.copier.parse_args(["--update-only", self.dir1, self.dir2])
  163. self.copier.do_work()
  164. targetOlder = file(targetOlderPath, "r")
  165. targetOlderContent = targetOlder.read()
  166. self.failIf(targetOlderContent == "targetOlder", "Older target file was not updated!")
  167. targetNewer = file(targetNewerPath, "r")
  168. targetNewerContent = targetNewer.read()
  169. self.failIf(targetNewerContent == "sourceOlder", "Newer target file was overwritten!")
  170. def testExclude(self):
  171. self.copier.parse_args(["-x", "dir1", self.dir1, self.dir2])
  172. self.copier.do_work()
  173. self.failIf(os.path.exists(os.path.join(self.dir2, "dir1")), "Excluded directory was copied!")
  174. copier = CopyTool()
  175. copier.parse_args(["-x", "file1", self.dir1, self.dir2])
  176. copier.do_work()
  177. self.failIf(os.path.exists(os.path.join(self.dir2, "file1")), "Excluded file was copied!")
  178. def testSynchronizeDirectories(self):
  179. syncSourcePath = os.path.join(self.dir1, "sourceFile")
  180. syncSource = file(syncSourcePath, "w")
  181. syncTargetPath = os.path.join(self.dir2, "sourceFile")
  182. self.copier.parse_args(["--synchronize", self.dir1, self.dir2])
  183. self.copier.do_work()
  184. self.failIf(os.path.isdir(os.path.join(self.dir2, "dir1")), "Directory was copied but should have been synchronized!")
  185. self.failUnless(os.path.isfile(syncTargetPath), "Directory contents not synchronized!")
  186. def testSynchronizeDirectoriesCreate(self):
  187. syncSourceDir = os.path.join(self.dir1, "subDir")
  188. os.makedirs(syncSourceDir)
  189. syncSourcePath = os.path.join(syncSourceDir, "sourceFile")
  190. syncSource = file(syncSourcePath, "w")
  191. syncTargetPath = os.path.join(self.dir2, "subDir", "sourceFile")
  192. self.copier.parse_args(["--synchronize", self.dir1, self.dir2])
  193. self.copier.do_work()
  194. self.failUnless(os.path.isfile(syncTargetPath), "Directory contents not synchronized!")
  195. if __name__ == '__main__':
  196. unittest.main()