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.

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

mercurial