/config/tests/unit-writemozinfo.py

https://github.com/diogogmt/mozilla-central · Python · 243 lines · 172 code · 33 blank · 38 comment · 2 complexity · 47b3b641913ce36fd49dbd9a53df0dd6 MD5 · raw file

  1. #!/usr/bin/env python
  2. from __future__ import with_statement
  3. import unittest
  4. import os, sys, time, tempfile
  5. from StringIO import StringIO
  6. sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
  7. from writemozinfo import build_dict, write_json, JsonValue, jsonify
  8. class TestBuildDict(unittest.TestCase):
  9. def testMissing(self):
  10. """
  11. Test that missing required values raises.
  12. """
  13. self.assertRaises(Exception, build_dict, {})
  14. self.assertRaises(Exception, build_dict, {'OS_TARGET':'foo'})
  15. self.assertRaises(Exception, build_dict, {'TARGET_CPU':'foo'})
  16. self.assertRaises(Exception, build_dict, {'MOZ_WIDGET_TOOLKIT':'foo'})
  17. def testWin(self):
  18. d = build_dict({'OS_TARGET':'WINNT',
  19. 'TARGET_CPU':'i386',
  20. 'MOZ_WIDGET_TOOLKIT':'windows'})
  21. self.assertEqual('win', d['os'])
  22. self.assertEqual('x86', d['processor'])
  23. self.assertEqual('windows', d['toolkit'])
  24. self.assertEqual(32, d['bits'])
  25. def testLinux(self):
  26. d = build_dict({'OS_TARGET':'Linux',
  27. 'TARGET_CPU':'i386',
  28. 'MOZ_WIDGET_TOOLKIT':'gtk2'})
  29. self.assertEqual('linux', d['os'])
  30. self.assertEqual('x86', d['processor'])
  31. self.assertEqual('gtk2', d['toolkit'])
  32. self.assertEqual(32, d['bits'])
  33. d = build_dict({'OS_TARGET':'Linux',
  34. 'TARGET_CPU':'x86_64',
  35. 'MOZ_WIDGET_TOOLKIT':'gtk2'})
  36. self.assertEqual('linux', d['os'])
  37. self.assertEqual('x86_64', d['processor'])
  38. self.assertEqual('gtk2', d['toolkit'])
  39. self.assertEqual(64, d['bits'])
  40. def testMac(self):
  41. d = build_dict({'OS_TARGET':'Darwin',
  42. 'TARGET_CPU':'i386',
  43. 'MOZ_WIDGET_TOOLKIT':'cocoa'})
  44. self.assertEqual('mac', d['os'])
  45. self.assertEqual('x86', d['processor'])
  46. self.assertEqual('cocoa', d['toolkit'])
  47. self.assertEqual(32, d['bits'])
  48. d = build_dict({'OS_TARGET':'Darwin',
  49. 'TARGET_CPU':'x86_64',
  50. 'MOZ_WIDGET_TOOLKIT':'cocoa'})
  51. self.assertEqual('mac', d['os'])
  52. self.assertEqual('x86_64', d['processor'])
  53. self.assertEqual('cocoa', d['toolkit'])
  54. self.assertEqual(64, d['bits'])
  55. def testMacUniversal(self):
  56. d = build_dict({'OS_TARGET':'Darwin',
  57. 'TARGET_CPU':'i386',
  58. 'MOZ_WIDGET_TOOLKIT':'cocoa',
  59. 'UNIVERSAL_BINARY': '1'})
  60. self.assertEqual('mac', d['os'])
  61. self.assertEqual('universal-x86-x86_64', d['processor'])
  62. self.assertEqual('cocoa', d['toolkit'])
  63. self.assertFalse('bits' in d)
  64. d = build_dict({'OS_TARGET':'Darwin',
  65. 'TARGET_CPU':'x86_64',
  66. 'MOZ_WIDGET_TOOLKIT':'cocoa',
  67. 'UNIVERSAL_BINARY': '1'})
  68. self.assertEqual('mac', d['os'])
  69. self.assertEqual('universal-x86-x86_64', d['processor'])
  70. self.assertEqual('cocoa', d['toolkit'])
  71. self.assertFalse('bits' in d)
  72. def testAndroid(self):
  73. d = build_dict({'OS_TARGET':'Android',
  74. 'TARGET_CPU':'arm',
  75. 'MOZ_WIDGET_TOOLKIT':'android'})
  76. self.assertEqual('android', d['os'])
  77. self.assertEqual('arm', d['processor'])
  78. self.assertEqual('android', d['toolkit'])
  79. self.assertEqual(32, d['bits'])
  80. def testX86(self):
  81. """
  82. Test that various i?86 values => x86.
  83. """
  84. d = build_dict({'OS_TARGET':'WINNT',
  85. 'TARGET_CPU':'i486',
  86. 'MOZ_WIDGET_TOOLKIT':'windows'})
  87. self.assertEqual('x86', d['processor'])
  88. d = build_dict({'OS_TARGET':'WINNT',
  89. 'TARGET_CPU':'i686',
  90. 'MOZ_WIDGET_TOOLKIT':'windows'})
  91. self.assertEqual('x86', d['processor'])
  92. def testARM(self):
  93. """
  94. Test that all arm CPU architectures => arm.
  95. """
  96. d = build_dict({'OS_TARGET':'Linux',
  97. 'TARGET_CPU':'arm',
  98. 'MOZ_WIDGET_TOOLKIT':'gtk2'})
  99. self.assertEqual('arm', d['processor'])
  100. d = build_dict({'OS_TARGET':'Linux',
  101. 'TARGET_CPU':'armv7',
  102. 'MOZ_WIDGET_TOOLKIT':'gtk2'})
  103. self.assertEqual('arm', d['processor'])
  104. def testUnknown(self):
  105. """
  106. Test that unknown values pass through okay.
  107. """
  108. d = build_dict({'OS_TARGET':'RandOS',
  109. 'TARGET_CPU':'cptwo',
  110. 'MOZ_WIDGET_TOOLKIT':'foobar'})
  111. self.assertEqual("randos", d["os"])
  112. self.assertEqual("cptwo", d["processor"])
  113. self.assertEqual("foobar", d["toolkit"])
  114. # unknown CPUs should not get a bits value
  115. self.assertFalse("bits" in d)
  116. def testDebug(self):
  117. """
  118. Test that debug values are properly detected.
  119. """
  120. d = build_dict({'OS_TARGET':'Linux',
  121. 'TARGET_CPU':'i386',
  122. 'MOZ_WIDGET_TOOLKIT':'gtk2'})
  123. self.assertEqual(False, d['debug'])
  124. d = build_dict({'OS_TARGET':'Linux',
  125. 'TARGET_CPU':'i386',
  126. 'MOZ_WIDGET_TOOLKIT':'gtk2',
  127. 'MOZ_DEBUG':'1'})
  128. self.assertEqual(True, d['debug'])
  129. def testCrashreporter(self):
  130. """
  131. Test that crashreporter values are properly detected.
  132. """
  133. d = build_dict({'OS_TARGET':'Linux',
  134. 'TARGET_CPU':'i386',
  135. 'MOZ_WIDGET_TOOLKIT':'gtk2'})
  136. self.assertEqual(False, d['crashreporter'])
  137. d = build_dict({'OS_TARGET':'Linux',
  138. 'TARGET_CPU':'i386',
  139. 'MOZ_WIDGET_TOOLKIT':'gtk2',
  140. 'MOZ_CRASHREPORTER':'1'})
  141. self.assertEqual(True, d['crashreporter'])
  142. class TestJsonValue(unittest.TestCase):
  143. def testNone(self):
  144. self.assertEqual("null", repr(JsonValue(None)))
  145. def testBool(self):
  146. self.assertEqual("true", repr(JsonValue(True)))
  147. self.assertEqual("false", repr(JsonValue(False)))
  148. def testStr(self):
  149. self.assertEqual("'abc'", repr(JsonValue("abc")))
  150. def testInt(self):
  151. self.assertEqual("100", repr(JsonValue(100)))
  152. def testInvalid(self):
  153. self.assertRaises(Exception, JsonValue, unicode("abc"))
  154. self.assertRaises(Exception, JsonValue, 123.45)
  155. def parse_json(j):
  156. """
  157. Awful hack to parse a restricted subset of JSON strings into Python dicts.
  158. """
  159. return eval(j, {'true':True,'false':False,'null':None})
  160. class TestJsonify(unittest.TestCase):
  161. """
  162. Test the jsonify function.
  163. """
  164. def testBasic(self):
  165. """
  166. Sanity check the set of accepted Python value types.
  167. """
  168. j = parse_json(jsonify({'a':True,'b':False,'c':None,'d':100,'e':"abc"}))
  169. self.assertEquals(True, j['a'])
  170. self.assertEquals(False, j['b'])
  171. self.assertEquals(None, j['c'])
  172. self.assertEquals(100, j['d'])
  173. self.assertEquals("abc", j['e'])
  174. class TestWriteJson(unittest.TestCase):
  175. """
  176. Test the write_json function.
  177. """
  178. def setUp(self):
  179. fd, self.f = tempfile.mkstemp()
  180. os.close(fd)
  181. def tearDown(self):
  182. os.unlink(self.f)
  183. def testBasic(self):
  184. """
  185. Test that writing to a file produces correct output.
  186. """
  187. write_json(self.f, env={'OS_TARGET':'WINNT',
  188. 'TARGET_CPU':'i386',
  189. 'MOZ_WIDGET_TOOLKIT':'windows'})
  190. with open(self.f) as f:
  191. d = parse_json(f.read())
  192. self.assertEqual('win', d['os'])
  193. self.assertEqual('x86', d['processor'])
  194. self.assertEqual('windows', d['toolkit'])
  195. self.assertEqual(32, d['bits'])
  196. def testFileObj(self):
  197. """
  198. Test that writing to a file-like object produces correct output.
  199. """
  200. s = StringIO()
  201. write_json(s, env={'OS_TARGET':'WINNT',
  202. 'TARGET_CPU':'i386',
  203. 'MOZ_WIDGET_TOOLKIT':'windows'})
  204. d = parse_json(s.getvalue())
  205. self.assertEqual('win', d['os'])
  206. self.assertEqual('x86', d['processor'])
  207. self.assertEqual('windows', d['toolkit'])
  208. self.assertEqual(32, d['bits'])
  209. if __name__ == '__main__':
  210. unittest.main()