Bug 1874684 - Part 28: Return DateDuration from DifferenceISODateTime. r=mgaudet
[gecko.git] / python / mozbuild / mozbuild / test / test_mozinfo.py
blob0d966b3dcc4735463cdc956ba7fadebb8f660923
1 #!/usr/bin/env python
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/.
6 import json
7 import os
8 import tempfile
9 import unittest
11 import mozunit
12 import six
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
20 class Base(object):
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):
28 """
29 Test that missing required values raises.
30 """
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")))
41 def test_win(self):
42 d = build_dict(
43 self._config(
44 dict(
45 OS_TARGET="WINNT",
46 TARGET_CPU="i386",
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"])
56 def test_linux(self):
57 d = build_dict(
58 self._config(
59 dict(
60 OS_TARGET="Linux",
61 TARGET_CPU="i386",
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"])
71 d = build_dict(
72 self._config(
73 dict(
74 OS_TARGET="Linux",
75 TARGET_CPU="x86_64",
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"])
85 def test_mac(self):
86 d = build_dict(
87 self._config(
88 dict(
89 OS_TARGET="Darwin",
90 TARGET_CPU="i386",
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"])
100 d = build_dict(
101 self._config(
102 dict(
103 OS_TARGET="Darwin",
104 TARGET_CPU="x86_64",
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):
115 d = build_dict(
116 self._config(
117 dict(
118 OS_TARGET="Android",
119 TARGET_CPU="arm",
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"])
129 def test_x86(self):
131 Test that various i?86 values => x86.
133 d = build_dict(
134 self._config(
135 dict(
136 OS_TARGET="WINNT",
137 TARGET_CPU="i486",
138 MOZ_WIDGET_TOOLKIT="windows",
142 self.assertEqual("x86", d["processor"])
144 d = build_dict(
145 self._config(
146 dict(
147 OS_TARGET="WINNT",
148 TARGET_CPU="i686",
149 MOZ_WIDGET_TOOLKIT="windows",
153 self.assertEqual("x86", d["processor"])
155 def test_arm(self):
157 Test that all arm CPU architectures => arm.
159 d = build_dict(
160 self._config(
161 dict(
162 OS_TARGET="Linux",
163 TARGET_CPU="arm",
164 MOZ_WIDGET_TOOLKIT="gtk",
168 self.assertEqual("arm", d["processor"])
170 d = build_dict(
171 self._config(
172 dict(
173 OS_TARGET="Linux",
174 TARGET_CPU="armv7",
175 MOZ_WIDGET_TOOLKIT="gtk",
179 self.assertEqual("arm", d["processor"])
181 def test_unknown(self):
183 Test that unknown values pass through okay.
185 d = build_dict(
186 self._config(
187 dict(
188 OS_TARGET="RandOS",
189 TARGET_CPU="cptwo",
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.
204 d = build_dict(
205 self._config(
206 dict(
207 OS_TARGET="Linux",
208 TARGET_CPU="i386",
209 MOZ_WIDGET_TOOLKIT="gtk",
213 self.assertEqual(False, d["debug"])
215 d = build_dict(
216 self._config(
217 dict(
218 OS_TARGET="Linux",
219 TARGET_CPU="i386",
220 MOZ_WIDGET_TOOLKIT="gtk",
221 MOZ_DEBUG="1",
225 self.assertEqual(True, d["debug"])
227 def test_crashreporter(self):
229 Test that crashreporter values are properly detected.
231 d = build_dict(
232 self._config(
233 dict(
234 OS_TARGET="Linux",
235 TARGET_CPU="i386",
236 MOZ_WIDGET_TOOLKIT="gtk",
240 self.assertEqual(False, d["crashreporter"])
242 d = build_dict(
243 self._config(
244 dict(
245 OS_TARGET="Linux",
246 TARGET_CPU="i386",
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.
260 def setUp(self):
261 fd, f = tempfile.mkstemp()
262 self.f = six.ensure_text(f)
263 os.close(fd)
265 def tearDown(self):
266 os.unlink(self.f)
268 def test_basic(self):
270 Test that writing to a file produces correct output.
272 c = self._config(
273 dict(
274 OS_TARGET="WINNT",
275 TARGET_CPU="i386",
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"
283 ) as mozconfig:
284 mozconfig.write("unused contents")
285 mozconfig.flush()
286 c.mozconfig = mozconfig.name
287 write_mozinfo(self.f, c)
288 with open(self.f) as f:
289 d = json.load(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.
301 s = StringIO()
302 c = self._config(
303 dict(
304 OS_TARGET="WINNT",
305 TARGET_CPU="i386",
306 MOZ_WIDGET_TOOLKIT="windows",
309 write_mozinfo(s, c)
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__":
318 mozunit.main()