aboutsummaryrefslogtreecommitdiff
path: root/tools/addon-sdk-1.5/python-lib/cuddlefish/tests/test_init.py
blob: 33c0059f3fece4ba5035f340c71a388b5fa1d6d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import os, unittest, shutil
from StringIO import StringIO
from cuddlefish import initializer
from cuddlefish.templates import MAIN_JS, TEST_MAIN_JS, PACKAGE_JSON

tests_path = os.path.abspath(os.path.dirname(__file__))

class TestInit(unittest.TestCase):

    def run_init_in_subdir(self, dirname, f, *args, **kwargs):
        top = os.path.abspath(os.getcwd())
        basedir = os.path.abspath(os.path.join(".test_tmp",self.id(),dirname))
        if os.path.isdir(basedir):
            assert basedir.startswith(top)
            shutil.rmtree(basedir)
        os.makedirs(basedir)
        try:
            os.chdir(basedir)
            return f(basedir, *args, **kwargs)
        finally:
            os.chdir(top)

    def do_test_init(self,basedir):
        # Let's init the addon, no error admited
        f = open(".ignoreme","w")
        f.write("stuff")
        f.close()

        out, err = StringIO(), StringIO()
        init_run = initializer(None, ["init"], out, err)
        out, err = out.getvalue(), err.getvalue()
        self.assertEqual(init_run, 0)
        self.assertTrue("* lib directory created" in out)
        self.assertTrue("* data directory created" in out)
        self.assertTrue("Have fun!" in out)
        self.assertEqual(err,"")
        self.assertTrue(len(os.listdir(basedir))>0)
        main_js = os.path.join(basedir,"lib","main.js")
        package_json = os.path.join(basedir,"package.json")
        test_main_js = os.path.join(basedir,"test","test-main.js")
        self.assertTrue(os.path.exists(main_js))
        self.assertTrue(os.path.exists(package_json))
        self.assertTrue(os.path.exists(test_main_js))
        self.assertEqual(open(main_js,"r").read(),MAIN_JS)
        self.assertEqual(open(package_json,"r").read(),
                         PACKAGE_JSON % {"name":"tmp_addon_sample",
                                         "fullName": "tmp_addon_SAMPLE" })
        self.assertEqual(open(test_main_js,"r").read(),TEST_MAIN_JS)

        # Let's check that the addon is initialized
        out, err = StringIO(), StringIO()
        init_run = initializer(None, ["init"], out, err)
        out, err = out.getvalue(), err.getvalue()
        self.failIfEqual(init_run,0)
        self.assertTrue("This command must be run in an empty directory." in err)

    def test_initializer(self):
        self.run_init_in_subdir("tmp_addon_SAMPLE",self.do_test_init)

    def do_test_args(self, basedir):
        # check that running it with spurious arguments will fail
        out,err = StringIO(), StringIO()
        init_run = initializer(None, ["init", "ignored-dirname"], out, err)
        out, err = out.getvalue(), err.getvalue()
        self.failIfEqual(init_run, 0)
        self.assertTrue("Too many arguments" in err)

    def test_args(self):
        self.run_init_in_subdir("tmp_addon_sample", self.do_test_args)

    def _test_existing_files(self, basedir):
        f = open("pay_attention_to_me","w")
        f.write("stuff")
        f.close()
        out,err = StringIO(), StringIO()
        rc = initializer(None, ["init"], out, err)
        out, err = out.getvalue(), err.getvalue()
        self.assertEqual(rc, 1)
        self.failUnless("This command must be run in an empty directory" in err,
                        err)
        self.failIf(os.path.exists("lib"))

    def test_existing_files(self):
        self.run_init_in_subdir("existing_files", self._test_existing_files)



class TestCfxQuits(unittest.TestCase):

    def run_cfx(self, addon_name, command):
        old_cwd = os.getcwd()
        addon_path = os.path.join(tests_path,
                                  "addons", addon_name)
        os.chdir(addon_path)
        import sys
        old_stdout = sys.stdout
        old_stderr = sys.stderr
        sys.stdout = out = StringIO()
        sys.stderr = err = StringIO()
        try:
            import cuddlefish
            args = list(command)
            # Pass arguments given to cfx so that cfx can find firefox path
            # if --binary option is given:
            args.extend(sys.argv[1:])
            cuddlefish.run(arguments=args)
        except SystemExit, e:
            if "code" in e:
                rc = e.code
            elif "args" in e and len(e.args)>0:
                rc = e.args[0]
            else:
                rc = 0
        finally:
            sys.stdout = old_stdout
            sys.stderr = old_stderr
            os.chdir(old_cwd)
        out.flush()
        err.flush()
        return rc, out.getvalue(), err.getvalue()

    # this method doesn't exists in python 2.5,
    # implements our own
    def assertIn(self, member, container):
        """Just like self.assertTrue(a in b), but with a nicer default message."""
        if member not in container:
            standardMsg = '"%s" not found in "%s"' % (member,
                                                  container)
            self.fail(standardMsg)

    def test_run(self):
        rc, out, err = self.run_cfx("simplest-test", ["run"])
        self.assertEqual(rc, 0)
        self.assertIn("Program terminated successfully.", err)

    def test_test(self):
        rc, out, err = self.run_cfx("simplest-test", ["test"])
        self.assertEqual(rc, 0)
        self.assertIn("1 of 1 tests passed.", err)
        self.assertIn("Program terminated successfully.", err)


if __name__ == "__main__":
    unittest.main()