2 # This Source Code Form is subject to the terms of the Mozilla Public
3 # License, v. 2.0. If a copy of the MPL was not distributed with this file,
4 # You can obtain one at http://mozilla.org/MPL/2.0/.
13 from mozfile
.mozfile
import NamedTemporaryFile
14 from six
import StringIO
16 from mozbuild
.backend
.configenvironment
import ConfigEnvironment
17 from mozbuild
.mozinfo
import build_dict
, write_mozinfo
21 def _config(self
, substs
={}):
22 d
= os
.path
.dirname(__file__
)
23 return ConfigEnvironment(d
, d
, substs
=substs
)
26 class TestBuildDict(unittest
.TestCase
, Base
):
27 def test_missing(self
):
29 Test that missing required values raises.
32 with self
.assertRaises(Exception):
33 build_dict(self
._config
(substs
=dict(OS_TARGET
="foo")))
35 with self
.assertRaises(Exception):
36 build_dict(self
._config
(substs
=dict(TARGET_CPU
="foo")))
38 with self
.assertRaises(Exception):
39 build_dict(self
._config
(substs
=dict(MOZ_WIDGET_TOOLKIT
="foo")))
47 MOZ_WIDGET_TOOLKIT
="windows",
51 self
.assertEqual("win", d
["os"])
52 self
.assertEqual("x86", d
["processor"])
53 self
.assertEqual("windows", d
["toolkit"])
54 self
.assertEqual(32, d
["bits"])
62 MOZ_WIDGET_TOOLKIT
="gtk",
66 self
.assertEqual("linux", d
["os"])
67 self
.assertEqual("x86", d
["processor"])
68 self
.assertEqual("gtk", d
["toolkit"])
69 self
.assertEqual(32, d
["bits"])
76 MOZ_WIDGET_TOOLKIT
="gtk",
80 self
.assertEqual("linux", d
["os"])
81 self
.assertEqual("x86_64", d
["processor"])
82 self
.assertEqual("gtk", d
["toolkit"])
83 self
.assertEqual(64, d
["bits"])
91 MOZ_WIDGET_TOOLKIT
="cocoa",
95 self
.assertEqual("mac", d
["os"])
96 self
.assertEqual("x86", d
["processor"])
97 self
.assertEqual("cocoa", d
["toolkit"])
98 self
.assertEqual(32, d
["bits"])
105 MOZ_WIDGET_TOOLKIT
="cocoa",
109 self
.assertEqual("mac", d
["os"])
110 self
.assertEqual("x86_64", d
["processor"])
111 self
.assertEqual("cocoa", d
["toolkit"])
112 self
.assertEqual(64, d
["bits"])
114 def test_android(self
):
120 MOZ_WIDGET_TOOLKIT
="android",
124 self
.assertEqual("android", d
["os"])
125 self
.assertEqual("arm", d
["processor"])
126 self
.assertEqual("android", d
["toolkit"])
127 self
.assertEqual(32, d
["bits"])
131 Test that various i?86 values => x86.
138 MOZ_WIDGET_TOOLKIT
="windows",
142 self
.assertEqual("x86", d
["processor"])
149 MOZ_WIDGET_TOOLKIT
="windows",
153 self
.assertEqual("x86", d
["processor"])
157 Test that all arm CPU architectures => arm.
164 MOZ_WIDGET_TOOLKIT
="gtk",
168 self
.assertEqual("arm", d
["processor"])
175 MOZ_WIDGET_TOOLKIT
="gtk",
179 self
.assertEqual("arm", d
["processor"])
181 def test_unknown(self
):
183 Test that unknown values pass through okay.
190 MOZ_WIDGET_TOOLKIT
="foobar",
194 self
.assertEqual("randos", d
["os"])
195 self
.assertEqual("cptwo", d
["processor"])
196 self
.assertEqual("foobar", d
["toolkit"])
197 # unknown CPUs should not get a bits value
198 self
.assertFalse("bits" in d
)
200 def test_debug(self
):
202 Test that debug values are properly detected.
209 MOZ_WIDGET_TOOLKIT
="gtk",
213 self
.assertEqual(False, d
["debug"])
220 MOZ_WIDGET_TOOLKIT
="gtk",
225 self
.assertEqual(True, d
["debug"])
227 def test_crashreporter(self
):
229 Test that crashreporter values are properly detected.
236 MOZ_WIDGET_TOOLKIT
="gtk",
240 self
.assertEqual(False, d
["crashreporter"])
247 MOZ_WIDGET_TOOLKIT
="gtk",
248 MOZ_CRASHREPORTER
="1",
252 self
.assertEqual(True, d
["crashreporter"])
255 class TestWriteMozinfo(unittest
.TestCase
, Base
):
257 Test the write_mozinfo function.
261 fd
, f
= tempfile
.mkstemp()
262 self
.f
= six
.ensure_text(f
)
268 def test_basic(self
):
270 Test that writing to a file produces correct output.
276 MOZ_WIDGET_TOOLKIT
="windows",
279 tempdir
= tempfile
.gettempdir()
280 c
.topsrcdir
= tempdir
281 with
NamedTemporaryFile(
282 dir=os
.path
.normpath(c
.topsrcdir
), mode
="wt"
284 mozconfig
.write("unused contents")
286 c
.mozconfig
= mozconfig
.name
287 write_mozinfo(self
.f
, c
)
288 with
open(self
.f
) as f
:
290 self
.assertEqual("win", d
["os"])
291 self
.assertEqual("x86", d
["processor"])
292 self
.assertEqual("windows", d
["toolkit"])
293 self
.assertEqual(tempdir
, d
["topsrcdir"])
294 self
.assertEqual(mozconfig
.name
, d
["mozconfig"])
295 self
.assertEqual(32, d
["bits"])
297 def test_fileobj(self
):
299 Test that writing to a file-like object produces correct output.
306 MOZ_WIDGET_TOOLKIT
="windows",
310 d
= json
.loads(s
.getvalue())
311 self
.assertEqual("win", d
["os"])
312 self
.assertEqual("x86", d
["processor"])
313 self
.assertEqual("windows", d
["toolkit"])
314 self
.assertEqual(32, d
["bits"])
317 if __name__
== "__main__":