test_tool.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. import unittest
  2. from unittest.mock import patch
  3. import os
  4. import shutil
  5. from programy.admin.tool import AdminTool
  6. class MockAdminTool(AdminTool):
  7. def __init__(self):
  8. AdminTool.__init__(self)
  9. self.text = ""
  10. def display(self, text):
  11. self.text += text
  12. class AdminToolTests(unittest.TestCase):
  13. def get_temp_dir(self):
  14. if os.name == 'posix':
  15. return '/tmp'
  16. elif os.name == 'nt':
  17. import tempfile
  18. return tempfile.gettempdir()
  19. else:
  20. raise Exception("Unknown operating system [%s]" % os.name)
  21. def create_file(self, filename):
  22. with open(filename, "w+") as file:
  23. file.writelines(["line1", "line2", "line3"])
  24. file.flush()
  25. file.close()
  26. def test_recursive_copy(self):
  27. tmp_dir = self.get_temp_dir() + os.sep +"programy"
  28. if os.path.exists(tmp_dir):
  29. shutil.rmtree(tmp_dir)
  30. os.mkdir(tmp_dir)
  31. src_dir = tmp_dir + os.sep + "src"
  32. os.mkdir(src_dir)
  33. src_sub_dir = tmp_dir + os.sep + "src" + os.sep + "sub"
  34. os.mkdir(src_sub_dir)
  35. src_sub_dir2 = tmp_dir + os.sep + "src" + os.sep + "sub2"
  36. os.mkdir(src_sub_dir2)
  37. dest_dir = tmp_dir + os.sep + "dest"
  38. os.mkdir(dest_dir)
  39. self.create_file(src_dir + os.sep + "file1.txt")
  40. self.create_file(src_dir + os.sep + "file2.txt")
  41. self.create_file(src_dir + os.sep + "file3.txt")
  42. self.create_file(src_dir + os.sep + "sub" + os.sep + "file4.txt")
  43. AdminTool.recursive_copy(src_dir, dest_dir)
  44. self.assertTrue(os.path.exists(src_dir + os.sep + "file1.txt"))
  45. self.assertTrue(os.path.exists(src_dir + os.sep + "file2.txt"))
  46. self.assertTrue(os.path.exists(src_dir + os.sep + "file3.txt"))
  47. self.assertTrue(os.path.exists(src_dir + os.sep + "sub" + os.sep + "file4.txt"))
  48. self.assertTrue(os.path.exists(src_dir + os.sep + "sub2"))
  49. if os.path.exists(tmp_dir):
  50. shutil.rmtree(tmp_dir)
  51. def test_delete_folder_contents(self):
  52. tmp_dir = self.get_temp_dir() + os.sep +"programy"
  53. if os.path.exists(tmp_dir):
  54. shutil.rmtree(tmp_dir)
  55. os.mkdir(tmp_dir)
  56. src_dir = tmp_dir + os.sep + "src"
  57. os.mkdir(src_dir)
  58. src_sub_dir = tmp_dir + os.sep + "src" + os.sep + "sub"
  59. os.mkdir(src_sub_dir)
  60. dest_dir = tmp_dir + os.sep + "dest"
  61. os.mkdir(dest_dir)
  62. self.create_file(src_dir + os.sep + "file1.txt")
  63. self.create_file(src_dir + os.sep + "file2.txt")
  64. self.create_file(src_dir + os.sep + "file3.txt")
  65. self.create_file(src_dir + os.sep + "sub" + os.sep + "file4.txt")
  66. self.assertTrue(os.path.exists(src_dir + os.sep + "file1.txt"))
  67. self.assertTrue(os.path.exists(src_dir + os.sep + "file2.txt"))
  68. self.assertTrue(os.path.exists(src_dir + os.sep + "file3.txt"))
  69. self.assertTrue(os.path.exists(src_dir + os.sep + "sub" + os.sep + "file4.txt"))
  70. AdminTool.delete_folder_contents(tmp_dir)
  71. self.assertFalse(os.path.exists(src_dir + os.sep + "file1.txt"))
  72. self.assertFalse(os.path.exists(src_dir + os.sep + "file2.txt"))
  73. self.assertFalse(os.path.exists(src_dir + os.sep + "file3.txt"))
  74. self.assertFalse(os.path.exists(src_dir + os.sep + "sub" + os.sep + "file4.txt"))
  75. if os.path.exists(tmp_dir):
  76. shutil.rmtree(tmp_dir)
  77. def test_make_executable(self):
  78. tmp_dir = self.get_temp_dir() + os.sep +"programy"
  79. if os.path.exists(tmp_dir):
  80. shutil.rmtree(tmp_dir)
  81. os.mkdir(tmp_dir)
  82. filepath = tmp_dir + os.sep + "file1.txt"
  83. self.create_file(filepath)
  84. self.assertTrue(os.path.exists(filepath))
  85. AdminTool.make_executable(filepath)
  86. if os.path.exists(tmp_dir):
  87. shutil.rmtree(tmp_dir)
  88. def test_make_all_executable(self):
  89. tmp_dir = self.get_temp_dir() + os.sep +"programy"
  90. if os.path.exists(tmp_dir):
  91. shutil.rmtree(tmp_dir)
  92. os.mkdir(tmp_dir)
  93. src_dir = tmp_dir + os.sep + "src"
  94. os.mkdir(src_dir)
  95. src_sub_dir = tmp_dir + os.sep + "src" + os.sep + "sub"
  96. os.mkdir(src_sub_dir)
  97. dest_dir = tmp_dir + os.sep + "dest"
  98. os.mkdir(dest_dir)
  99. self.create_file(src_dir + os.sep + "file1.txt")
  100. self.create_file(src_dir + os.sep + "file2.txt")
  101. self.create_file(src_dir + os.sep + "file3.txt")
  102. self.create_file(src_dir + os.sep + "sub" + os.sep + "file4.txt")
  103. AdminTool.make_all_executable(tmp_dir)
  104. if os.path.exists(tmp_dir):
  105. shutil.rmtree(tmp_dir)
  106. def test_list_bots(self):
  107. tool = MockAdminTool()
  108. self.assertEqual("", tool.text)
  109. tool.list_bots()
  110. self.assertEquals("""Available bots are:
  111. alice2-y professor-y rosie-y talk-y y-bot servusai-y template-y traintimes-y
  112. To download use 'python3 -m programy.admin.tool download <bot-name>'
  113. Additional components are:
  114. textblob
  115. To install use 'python3 -m programy.admin.tool install <component>'""", tool.text)
  116. def patch_wget_download(self, url):
  117. return "mock.bot"
  118. @patch("programy.admin.tool.AdminTool.wget_download", patch_wget_download)
  119. def test_download_bot(self):
  120. tool = MockAdminTool()
  121. self.assertEqual("", tool.text)
  122. filename = tool.download_bot("y-bot")
  123. self.assertEqual("mock.bot", filename)
  124. self.assertEqual("""Downloading [y-bot] from [https://github.com/keiffster/y-bot/archive/master.zip]
  125. Download complete""", tool.text)
  126. def test_zip_dir_name_from_filename(self):
  127. self.assertEqual("filename", AdminTool.zip_dir_name_from_filename('filename.zip'))
  128. self.assertEqual("filename", AdminTool.zip_dir_name_from_filename('filename'))
  129. def test_extract_bot_no_remove(self):
  130. tool = AdminTool()
  131. tmp_dir = self.get_temp_dir() + os.sep +"programy"
  132. if os.path.exists(tmp_dir):
  133. shutil.rmtree(tmp_dir)
  134. os.mkdir(tmp_dir)
  135. shutil.copyfile(os.path.dirname(__file__) + os.sep + "bot.zip", tmp_dir + os.sep + "bot.zip")
  136. tool.extract_bot(tmp_dir + os.sep + "bot.zip", path=tmp_dir, remove_after=False)
  137. self.assertTrue(os.path.exists(tmp_dir + os.sep + "bot.zip"))
  138. self.assertTrue(os.path.exists(tmp_dir + os.sep + "test1.txt"))
  139. self.assertTrue(os.path.exists(tmp_dir + os.sep + "test2.txt"))
  140. shutil.rmtree(tmp_dir)
  141. def test_extract_bot_with_remove(self):
  142. tool = AdminTool()
  143. tmp_dir = self.get_temp_dir() + os.sep +"programy"
  144. if os.path.exists(tmp_dir):
  145. shutil.rmtree(tmp_dir)
  146. os.mkdir(tmp_dir)
  147. shutil.copyfile(os.path.dirname(__file__) + os.sep + "bot.zip", tmp_dir + os.sep + "bot.zip")
  148. tool.extract_bot(tmp_dir + os.sep + "bot.zip", path=tmp_dir, remove_after=True)
  149. self.assertFalse(os.path.exists(tmp_dir + os.sep + "bot.zip"))
  150. self.assertTrue(os.path.exists(tmp_dir + os.sep + "test1.txt"))
  151. self.assertTrue(os.path.exists(tmp_dir + os.sep + "test2.txt"))
  152. shutil.rmtree(tmp_dir)
  153. def patch_download_and_make_active(self, bot_name):
  154. pass # Do nothing
  155. @patch("programy.admin.tool.AdminTool.download_and_make_active", patch_download_and_make_active)
  156. def test_install_bot(self):
  157. tool = MockAdminTool()
  158. self.assertEquals("", tool.text)
  159. tool.install_bot(["test", "y-bot"])
  160. self.assertEqual("""
  161. To run y-bot bot in console mode, use the following commands
  162. \tcd scripts/xnix\t./y-bot.sh""", tool.text)
  163. def test_install_bot_unknown(self):
  164. tool = MockAdminTool()
  165. self.assertEquals("", tool.text)
  166. with self.assertRaises(Exception):
  167. tool.install_bot(["test", "unknown"])
  168. def patch_install_textblob(self):
  169. pass # Do nothing
  170. @patch("programy.admin.tool.AdminTool.install_textblob", patch_install_textblob)
  171. def test_install_additional(self):
  172. tool = MockAdminTool()
  173. self.assertEquals("", tool.text)
  174. tool.install_additional(["test", "textblob"])
  175. self.assertEqual("Installing additional components for textblob", tool.text)
  176. def test_install_additional_invalid(self):
  177. tool = MockAdminTool()
  178. self.assertEquals("", tool.text)
  179. with self.assertRaises(Exception):
  180. tool.install_additional(["test", "xxxxxxx"])
  181. def test_show_execute_help(self):
  182. tool = MockAdminTool()
  183. self.assertEqual("", tool.text)
  184. tool.show_execute_help("y-bot")
  185. self.assertEqual("""
  186. To run y-bot bot in console mode, use the following commands
  187. \tcd scripts/xnix\t./y-bot.sh""", tool.text)
  188. def test_show_help(self):
  189. tool = MockAdminTool()
  190. self.assertEqual("", tool.text)
  191. tool.show_help()
  192. self.assertEqual("""Available commands are:
  193. \thelp list download <bot-name> install <component>""", tool.text)
  194. def test_run_no_words(self):
  195. tool = MockAdminTool()
  196. tool.run([])
  197. self.assertIsNotNone(tool.text)
  198. self.assertTrue(tool.text.startswith("Available commands are:"))
  199. def test_run_unknown_primary_command(self):
  200. tool = MockAdminTool()
  201. tool.run(['unknown'])
  202. self.assertIsNotNone(tool.text)
  203. self.assertTrue(tool.text.startswith("Unknown primary command [unknown]"))
  204. def test_run_missing_bot_name(self):
  205. tool = MockAdminTool()
  206. tool.run(['download'])
  207. self.assertIsNotNone(tool.text)
  208. self.assertTrue(tool.text.startswith("Missing bot name from download command"))
  209. def test_run_list(self):
  210. tool = MockAdminTool()
  211. tool.run(['list'])
  212. self.assertIsNotNone(tool.text)
  213. def test_run_download(self):
  214. tool = MockAdminTool()
  215. tool.run(['download'])
  216. self.assertIsNotNone(tool.text)
  217. def test_run_install(self):
  218. tool = MockAdminTool()
  219. tool.run(['install'])
  220. self.assertIsNotNone(tool.text)
  221. def test_run_help(self):
  222. tool = MockAdminTool()
  223. tool.run(['help'])
  224. self.assertIsNotNone(tool.text)