You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

231 lines
6.8 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # vim: set fileencoding=utf-8 :
  4. """test runner for sync tests
  5. Start this with:
  6. $ python3 sync_test.py
  7. """
  8. # Sync test 2.0
  9. # Copyleft 2018, 2019, p≡p foundation
  10. # this file is under GNU General Public License 3.0
  11. import os
  12. import sys
  13. import shutil
  14. import pathlib
  15. def test_for(path, color=None, end_on=None, mt=False, imap=False, own_ident=1):
  16. cwd = os.getcwd();
  17. os.chdir(path)
  18. os.environ["HOME"] = os.getcwd()
  19. print("running tests for " + path)
  20. import sync_handshake
  21. if end_on:
  22. sync_handshake.end_on = end_on
  23. sync_handshake.multithreaded = mt
  24. sync_handshake.run(path, color, imap, own_ident)
  25. def setup(path):
  26. cwd = os.getcwd();
  27. os.makedirs(path, exist_ok=True)
  28. os.chdir(path)
  29. lib_path = os.path.join(os.environ["HOME"], "lib")
  30. Library_path = os.path.join(os.environ["HOME"], "Library")
  31. try:
  32. os.symlink(lib_path, "lib", True)
  33. except FileExistsError:
  34. pass
  35. try:
  36. os.symlink(Library_path, "Library", True)
  37. except FileExistsError:
  38. pass
  39. os.chdir(cwd)
  40. def rmrf(path):
  41. try:
  42. for p in pathlib.Path(path).iterdir():
  43. if p.is_dir() and not p.is_symlink():
  44. rmrf(str(p))
  45. else:
  46. p.unlink()
  47. os.rmdir(path)
  48. except FileNotFoundError:
  49. pass
  50. EINTR = 4
  51. if __name__ == "__main__":
  52. from optparse import OptionParser
  53. optParser = OptionParser()
  54. optParser.description = __doc__
  55. optParser.add_option("-c", "--clean", action="store_true", dest="clean",
  56. help="remove all generated files")
  57. optParser.add_option("-b", "--backup", action="store_true", dest="backup",
  58. help="make a backup of all generated files (old backup will be overwritten)")
  59. optParser.add_option("-r", "--restore", action="store_true", dest="restore",
  60. help="restore generated files from backup")
  61. optParser.add_option("-C", "--clean-all", action="store_true", dest="cleanall",
  62. help="remove all generated files including backup files")
  63. optParser.add_option("-s", "--setup", action="store_true", dest="setup_only",
  64. help="setup environment, then stop")
  65. optParser.add_option("-p", "--print", action="store_true", dest="print",
  66. help="print sync message trace in inbox")
  67. optParser.add_option("-n", "--noend", action="store_true", dest="noend",
  68. help="do not end")
  69. optParser.add_option("-E", "--end-on", dest="notifications",
  70. help="end test on these notifications")
  71. optParser.add_option("-3", "--third-device", action="store_true", dest="third",
  72. help="start Pad as third device")
  73. optParser.add_option("-j", "--multi-threaded", action="store_true",
  74. dest="multithreaded",
  75. help="use multithreaded instead of single threaded implementation")
  76. optParser.add_option("-i", "--imap", action="store_true",
  77. dest="imap",
  78. help="use imap instead of minimail")
  79. optParser.add_option("-A", "--add-account-after-sync", action="store_true",
  80. dest="add_account",
  81. help="after sync add an account")
  82. options, args = optParser.parse_args()
  83. if options.imap:
  84. import miniimap
  85. if options.cleanall:
  86. options.clean = True
  87. if options.clean:
  88. if options.imap:
  89. miniimap.clean_inbox()
  90. if options.cleanall:
  91. rmrf("Backup")
  92. else:
  93. rmrf("TestInbox")
  94. rmrf("Phone")
  95. rmrf("Laptop")
  96. rmrf("Pad")
  97. if options.cleanall:
  98. rmrf("Backup")
  99. if options.setup_only:
  100. os.makedirs("TestInbox", exist_ok=True)
  101. setup("Phone")
  102. setup("Laptop")
  103. if options.third:
  104. setup("Pad")
  105. elif options.backup:
  106. rmrf("Backup")
  107. try:
  108. os.mkdir("Backup")
  109. except FileExistsError:
  110. pass
  111. if options.imap:
  112. try:
  113. os.mkdir("Backup/TestInbox")
  114. except FileExistsError:
  115. pass
  116. miniimap.backup_inbox()
  117. else:
  118. shutil.copytree("Phone", "Backup/Phone", symlinks=True, copy_function=shutil.copy2)
  119. shutil.copytree("Laptop", "Backup/Laptop", symlinks=True, copy_function=shutil.copy2)
  120. shutil.copytree("TestInbox", "Backup/TestInbox", symlinks=True, copy_function=shutil.copy2)
  121. try:
  122. shutil.copytree("Pad", "Backup/Pad", symlinks=True, copy_function=shutil.copy2)
  123. except FileNotFoundError:
  124. pass
  125. elif options.restore:
  126. if options.imap:
  127. miniimap.clean_inbox()
  128. miniimap.restore_inbox()
  129. else:
  130. rmrf("TestInbox")
  131. rmrf("Phone")
  132. rmrf("Laptop")
  133. rmrf("Pad")
  134. shutil.copytree("Backup/Phone", "Phone", symlinks=True, copy_function=shutil.copy2)
  135. shutil.copytree("Backup/Laptop", "Laptop", symlinks=True, copy_function=shutil.copy2)
  136. shutil.copytree("Backup/TestInbox", "TestInbox", symlinks=True, copy_function=shutil.copy2)
  137. try:
  138. shutil.copytree("Backup/Pad", "Pad", symlinks=True, copy_function=shutil.copy2)
  139. except FileNotFoundError:
  140. pass
  141. elif options.print:
  142. from sync_handshake import print_msg
  143. inbox = pathlib.Path("TestInbox")
  144. l = [ path for path in inbox.glob("*.eml") ]
  145. l.sort(key=(lambda p: p.stat().st_mtime))
  146. for p in l:
  147. print_msg(p)
  148. else:
  149. from multiprocessing import Process
  150. os.makedirs("TestInbox", exist_ok=True)
  151. setup("Phone")
  152. setup("Laptop")
  153. if options.third:
  154. setup("Pad")
  155. if not options.setup_only:
  156. end_on = None
  157. if options.notifications:
  158. end_on = eval(options.notifications)
  159. try: None in end_on
  160. except TypeError:
  161. end_on = (end_on,)
  162. elif options.noend:
  163. end_on = (None,)
  164. # Phone runs with own_ident = 2
  165. Phone = Process(target=test_for, args=("Phone", "red", end_on,
  166. options.multithreaded, options.imap, 2))
  167. # others run with own_ident = 1
  168. Laptop = Process(target=test_for, args=("Laptop", "green", end_on,
  169. options.multithreaded, options.imap))
  170. if options.third:
  171. Pad = Process(target=test_for, args=("Pad", "cyan", end_on,
  172. options.multithreaded, options.imap))
  173. Phone.start()
  174. Laptop.start()
  175. if options.third:
  176. Pad.start()
  177. Phone.join()
  178. Laptop.join()
  179. if options.third:
  180. Pad.join()