addon-sdk/source/python-lib/cuddlefish/tests/test_init.py

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

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

mercurial