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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
# 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
import zipfile
from StringIO import StringIO
from cuddlefish import initializer
from cuddlefish.templates import 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 admitted
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["result"], 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(),"")
self.assertEqual(open(package_json,"r").read() % {"id":"tmp_addon_id" },
PACKAGE_JSON % {"name":"tmp_addon_sample",
"title": "tmp_addon_SAMPLE",
"id":init_run["jid"] })
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["result"],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", "specified-dirname", "extra-arg"], out, err)
out, err = out.getvalue(), err.getvalue()
self.failIfEqual(init_run["result"], 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["result"], 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)
def test_init_subdir(self):
parent = os.path.abspath(os.path.join(".test_tmp", self.id()))
basedir = os.path.join(parent, "init-basedir")
if os.path.exists(parent):
shutil.rmtree(parent)
os.makedirs(parent)
# if the basedir exists and is not empty, init should refuse
os.makedirs(basedir)
f = open(os.path.join(basedir, "boo"), "w")
f.write("stuff")
f.close()
out, err = StringIO(), StringIO()
rc = initializer(None, ["init", basedir], out, err)
out, err = out.getvalue(), err.getvalue()
self.assertEqual(rc["result"], 1)
self.assertTrue("testing if directory is empty" in out, out)
self.assertTrue("This command must be run in an empty directory." in err,
err)
# a .dotfile should be tolerated
os.rename(os.path.join(basedir, "boo"), os.path.join(basedir, ".phew"))
out, err = StringIO(), StringIO()
rc = initializer(None, ["init", basedir], out, err)
out, err = out.getvalue(), err.getvalue()
self.assertEqual(rc["result"], 0)
self.assertTrue("* data directory created" in out, out)
self.assertTrue("Have fun!" in out)
self.assertEqual(err,"")
self.assertTrue(os.listdir(basedir))
main_js = os.path.join(basedir,"lib","main.js")
package_json = os.path.join(basedir,"package.json")
self.assertTrue(os.path.exists(main_js))
self.assertTrue(os.path.exists(package_json))
shutil.rmtree(basedir)
# init should create directories that don't exist already
out, err = StringIO(), StringIO()
rc = initializer(None, ["init", basedir], out, err)
out, err = out.getvalue(), err.getvalue()
self.assertEqual(rc["result"], 0)
self.assertTrue("* data directory created" in out)
self.assertTrue("Have fun!" in out)
self.assertEqual(err,"")
self.assertTrue(os.listdir(basedir))
main_js = os.path.join(basedir,"lib","main.js")
package_json = os.path.join(basedir,"package.json")
self.assertTrue(os.path.exists(main_js))
self.assertTrue(os.path.exists(package_json))
class TestCfxQuits(unittest.TestCase):
def run_cfx(self, addon_path, command):
old_cwd = os.getcwd()
os.chdir(addon_path)
import sys
old_stdout = sys.stdout
old_stderr = sys.stderr
sys.stdout = out = StringIO()
sys.stderr = err = StringIO()
rc = 0
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_cfx_init(self):
# Create an empty test directory
addon_path = os.path.abspath(os.path.join(".test_tmp", "test-cfx-init"))
if os.path.isdir(addon_path):
shutil.rmtree(addon_path)
os.makedirs(addon_path)
# Fake a call to cfx init
old_cwd = os.getcwd()
os.chdir(addon_path)
out, err = StringIO(), StringIO()
rc = initializer(None, ["init"], out, err)
os.chdir(old_cwd)
out, err = out.getvalue(), err.getvalue()
self.assertEqual(rc["result"], 0)
self.assertTrue("Have fun!" in out)
self.assertEqual(err,"")
# run cfx test
rc, out, err = self.run_cfx(addon_path, ["test"])
self.assertEqual(rc, 0)
self.assertIn("6 of 6 tests passed.", err)
self.assertIn("Program terminated successfully.", err)
if __name__ == "__main__":
unittest.main()
|