test_init.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. # This Source Code Form is subject to the terms of the Mozilla Public
  2. # License, v. 2.0. If a copy of the MPL was not distributed with this
  3. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
  4. import os, unittest, shutil
  5. import zipfile
  6. from StringIO import StringIO
  7. from cuddlefish import initializer
  8. from cuddlefish.templates import TEST_MAIN_JS, PACKAGE_JSON
  9. tests_path = os.path.abspath(os.path.dirname(__file__))
  10. class TestInit(unittest.TestCase):
  11. def run_init_in_subdir(self, dirname, f, *args, **kwargs):
  12. top = os.path.abspath(os.getcwd())
  13. basedir = os.path.abspath(os.path.join(".test_tmp",self.id(),dirname))
  14. if os.path.isdir(basedir):
  15. assert basedir.startswith(top)
  16. shutil.rmtree(basedir)
  17. os.makedirs(basedir)
  18. try:
  19. os.chdir(basedir)
  20. return f(basedir, *args, **kwargs)
  21. finally:
  22. os.chdir(top)
  23. def do_test_init(self,basedir):
  24. # Let's init the addon, no error admitted
  25. f = open(".ignoreme","w")
  26. f.write("stuff")
  27. f.close()
  28. out, err = StringIO(), StringIO()
  29. init_run = initializer(None, ["init"], out, err)
  30. out, err = out.getvalue(), err.getvalue()
  31. self.assertEqual(init_run["result"], 0)
  32. self.assertTrue("* lib directory created" in out)
  33. self.assertTrue("* data directory created" in out)
  34. self.assertTrue("Have fun!" in out)
  35. self.assertEqual(err,"")
  36. self.assertTrue(len(os.listdir(basedir))>0)
  37. main_js = os.path.join(basedir,"lib","main.js")
  38. package_json = os.path.join(basedir,"package.json")
  39. test_main_js = os.path.join(basedir,"test","test-main.js")
  40. self.assertTrue(os.path.exists(main_js))
  41. self.assertTrue(os.path.exists(package_json))
  42. self.assertTrue(os.path.exists(test_main_js))
  43. self.assertEqual(open(main_js,"r").read(),"")
  44. self.assertEqual(open(package_json,"r").read() % {"id":"tmp_addon_id" },
  45. PACKAGE_JSON % {"name":"tmp_addon_sample",
  46. "title": "tmp_addon_SAMPLE",
  47. "id":init_run["jid"] })
  48. self.assertEqual(open(test_main_js,"r").read(),TEST_MAIN_JS)
  49. # Let's check that the addon is initialized
  50. out, err = StringIO(), StringIO()
  51. init_run = initializer(None, ["init"], out, err)
  52. out, err = out.getvalue(), err.getvalue()
  53. self.failIfEqual(init_run["result"],0)
  54. self.assertTrue("This command must be run in an empty directory." in err)
  55. def test_initializer(self):
  56. self.run_init_in_subdir("tmp_addon_SAMPLE",self.do_test_init)
  57. def do_test_args(self, basedir):
  58. # check that running it with spurious arguments will fail
  59. out,err = StringIO(), StringIO()
  60. init_run = initializer(None, ["init", "specified-dirname", "extra-arg"], out, err)
  61. out, err = out.getvalue(), err.getvalue()
  62. self.failIfEqual(init_run["result"], 0)
  63. self.assertTrue("Too many arguments" in err)
  64. def test_args(self):
  65. self.run_init_in_subdir("tmp_addon_sample", self.do_test_args)
  66. def _test_existing_files(self, basedir):
  67. f = open("pay_attention_to_me","w")
  68. f.write("stuff")
  69. f.close()
  70. out,err = StringIO(), StringIO()
  71. rc = initializer(None, ["init"], out, err)
  72. out, err = out.getvalue(), err.getvalue()
  73. self.assertEqual(rc["result"], 1)
  74. self.failUnless("This command must be run in an empty directory" in err,
  75. err)
  76. self.failIf(os.path.exists("lib"))
  77. def test_existing_files(self):
  78. self.run_init_in_subdir("existing_files", self._test_existing_files)
  79. def test_init_subdir(self):
  80. parent = os.path.abspath(os.path.join(".test_tmp", self.id()))
  81. basedir = os.path.join(parent, "init-basedir")
  82. if os.path.exists(parent):
  83. shutil.rmtree(parent)
  84. os.makedirs(parent)
  85. # if the basedir exists and is not empty, init should refuse
  86. os.makedirs(basedir)
  87. f = open(os.path.join(basedir, "boo"), "w")
  88. f.write("stuff")
  89. f.close()
  90. out, err = StringIO(), StringIO()
  91. rc = initializer(None, ["init", basedir], out, err)
  92. out, err = out.getvalue(), err.getvalue()
  93. self.assertEqual(rc["result"], 1)
  94. self.assertTrue("testing if directory is empty" in out, out)
  95. self.assertTrue("This command must be run in an empty directory." in err,
  96. err)
  97. # a .dotfile should be tolerated
  98. os.rename(os.path.join(basedir, "boo"), os.path.join(basedir, ".phew"))
  99. out, err = StringIO(), StringIO()
  100. rc = initializer(None, ["init", basedir], out, err)
  101. out, err = out.getvalue(), err.getvalue()
  102. self.assertEqual(rc["result"], 0)
  103. self.assertTrue("* data directory created" in out, out)
  104. self.assertTrue("Have fun!" in out)
  105. self.assertEqual(err,"")
  106. self.assertTrue(os.listdir(basedir))
  107. main_js = os.path.join(basedir,"lib","main.js")
  108. package_json = os.path.join(basedir,"package.json")
  109. self.assertTrue(os.path.exists(main_js))
  110. self.assertTrue(os.path.exists(package_json))
  111. shutil.rmtree(basedir)
  112. # init should create directories that don't exist already
  113. out, err = StringIO(), StringIO()
  114. rc = initializer(None, ["init", basedir], out, err)
  115. out, err = out.getvalue(), err.getvalue()
  116. self.assertEqual(rc["result"], 0)
  117. self.assertTrue("* data directory created" in out)
  118. self.assertTrue("Have fun!" in out)
  119. self.assertEqual(err,"")
  120. self.assertTrue(os.listdir(basedir))
  121. main_js = os.path.join(basedir,"lib","main.js")
  122. package_json = os.path.join(basedir,"package.json")
  123. self.assertTrue(os.path.exists(main_js))
  124. self.assertTrue(os.path.exists(package_json))
  125. class TestCfxQuits(unittest.TestCase):
  126. def run_cfx(self, addon_path, command):
  127. old_cwd = os.getcwd()
  128. os.chdir(addon_path)
  129. import sys
  130. old_stdout = sys.stdout
  131. old_stderr = sys.stderr
  132. sys.stdout = out = StringIO()
  133. sys.stderr = err = StringIO()
  134. rc = 0
  135. try:
  136. import cuddlefish
  137. args = list(command)
  138. # Pass arguments given to cfx so that cfx can find firefox path
  139. # if --binary option is given:
  140. args.extend(sys.argv[1:])
  141. cuddlefish.run(arguments=args)
  142. except SystemExit, e:
  143. if "code" in e:
  144. rc = e.code
  145. elif "args" in e and len(e.args)>0:
  146. rc = e.args[0]
  147. else:
  148. rc = 0
  149. finally:
  150. sys.stdout = old_stdout
  151. sys.stderr = old_stderr
  152. os.chdir(old_cwd)
  153. out.flush()
  154. err.flush()
  155. return rc, out.getvalue(), err.getvalue()
  156. # this method doesn't exists in python 2.5,
  157. # implements our own
  158. def assertIn(self, member, container):
  159. """Just like self.assertTrue(a in b), but with a nicer default message."""
  160. if member not in container:
  161. standardMsg = '"%s" not found in "%s"' % (member,
  162. container)
  163. self.fail(standardMsg)
  164. def test_cfx_run(self):
  165. addon_path = os.path.join(tests_path,
  166. "addons", "simplest-test")
  167. rc, out, err = self.run_cfx(addon_path, ["run"])
  168. self.assertEqual(rc, 0)
  169. self.assertIn("Program terminated successfully.", err)
  170. def test_cfx_test(self):
  171. addon_path = os.path.join(tests_path,
  172. "addons", "simplest-test")
  173. rc, out, err = self.run_cfx(addon_path, ["test"])
  174. self.assertEqual(rc, 0)
  175. self.assertIn("1 of 1 tests passed.", err)
  176. self.assertIn("Program terminated successfully.", err)
  177. def test_cfx_xpi(self):
  178. addon_path = os.path.join(tests_path,
  179. "addons", "simplest-test")
  180. rc, out, err = self.run_cfx(addon_path, \
  181. ["xpi", "--manifest-overload", "manifest-overload.json"])
  182. self.assertEqual(rc, 0)
  183. # Ensure that the addon version from our manifest overload is used
  184. # in install.rdf
  185. xpi_path = os.path.join(addon_path, "simplest-test.xpi")
  186. xpi = zipfile.ZipFile(xpi_path, "r")
  187. manifest = xpi.read("install.rdf")
  188. self.assertIn("<em:version>1.0-nightly</em:version>", manifest)
  189. xpi.close()
  190. os.remove(xpi_path)
  191. def test_cfx_init(self):
  192. # Create an empty test directory
  193. addon_path = os.path.abspath(os.path.join(".test_tmp", "test-cfx-init"))
  194. if os.path.isdir(addon_path):
  195. shutil.rmtree(addon_path)
  196. os.makedirs(addon_path)
  197. # Fake a call to cfx init
  198. old_cwd = os.getcwd()
  199. os.chdir(addon_path)
  200. out, err = StringIO(), StringIO()
  201. rc = initializer(None, ["init"], out, err)
  202. os.chdir(old_cwd)
  203. out, err = out.getvalue(), err.getvalue()
  204. self.assertEqual(rc["result"], 0)
  205. self.assertTrue("Have fun!" in out)
  206. self.assertEqual(err,"")
  207. # run cfx test
  208. rc, out, err = self.run_cfx(addon_path, ["test"])
  209. self.assertEqual(rc, 0)
  210. self.assertIn("2 of 2 tests passed.", err)
  211. self.assertIn("Program terminated successfully.", err)
  212. if __name__ == "__main__":
  213. unittest.main()