Bug 776035 - Make mozunit even easier to use, and use it in config/tests python unit tests. r=ted
authorMike Hommey <mh+mozilla@glandium.org>
Wed, 25 Jul 2012 07:44:08 +0200
changeset 100435 284ee7c9fb336aff59b3e6756725696bd3bf4540
parent 100434 e0a8b539d56218a91906ef1d82b3cf0b36e2fc6a
child 100436 c94f0e69113c9c533d013cd742baf282268902f0
push idunknown
push userunknown
push dateunknown
reviewersted
bugs776035
milestone17.0a1
Bug 776035 - Make mozunit even easier to use, and use it in config/tests python unit tests. r=ted
config/mozunit.py
config/tests/unit-Expression.py
config/tests/unit-JarMaker.py
config/tests/unit-LineEndings.py
config/tests/unit-Preprocessor.py
config/tests/unit-buildlist.py
config/tests/unit-expandlibs.py
config/tests/unit-nsinstall.py
config/tests/unit-printprereleasesuffix.py
config/tests/unit-writemozinfo.py
xpcom/idl-parser/runtests.py
xpcom/typelib/xpt/tools/runtests.py
--- a/config/mozunit.py
+++ b/config/mozunit.py
@@ -1,25 +1,26 @@
 # 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/.
 
 from unittest import TextTestRunner as _TestRunner, TestResult as _TestResult
+import unittest
 import inspect
 
 '''Helper to make python unit tests report the way that the Mozilla
 unit test infrastructure expects tests to report.
 
 Usage:
 
 import unittest
-from mozunit import MozTestRunner
+import mozunit
 
 if __name__ == '__main__':
-    unittest.main(testRunner=MozTestRunner())
+    mozunit.main()
 '''
 
 class _MozTestResult(_TestResult):
     def __init__(self, stream, descriptions):
         _TestResult.__init__(self)
         self.stream = stream
         self.descriptions = descriptions
 
@@ -63,8 +64,11 @@ class _MozTestResult(_TestResult):
 class MozTestRunner(_TestRunner):
     def _makeResult(self):
         return _MozTestResult(self.stream, self.descriptions)
     def run(self, test):
         result = self._makeResult()
         test(result)
         result.printErrorList()
         return result
+
+def main(*args):
+    unittest.main(testRunner=MozTestRunner(),*args)
--- a/config/tests/unit-Expression.py
+++ b/config/tests/unit-Expression.py
@@ -1,13 +1,13 @@
 import unittest
 
 import sys
 import os.path
-sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
+import mozunit
 
 from Expression import Expression, Context
 
 class TestContext(unittest.TestCase):
   """
   Unit tests for the Context class
   """
 
@@ -55,9 +55,9 @@ class TestExpression(unittest.TestCase):
     """ Test for the == operator"""
     self.assert_(Expression('FAIL == PASS').evaluate(self.c))
 
   def test_notequals(self):
     """ Test for the != operator"""
     self.assert_(Expression('FAIL != 1').evaluate(self.c))
 
 if __name__ == '__main__':
-  unittest.main()
+  mozunit.main()
--- a/config/tests/unit-JarMaker.py
+++ b/config/tests/unit-JarMaker.py
@@ -1,18 +1,16 @@
 import unittest
 
 import os, sys, os.path, time, inspect
 from filecmp import dircmp
 from tempfile import mkdtemp
 from shutil import rmtree, copy2
 from zipfile import ZipFile
-sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
-
-from mozunit import MozTestRunner
+import mozunit
 from JarMaker import JarMaker
 
 if sys.platform == "win32":
     import ctypes
     from ctypes import POINTER, WinError
     DWORD = ctypes.c_ulong
     LPDWORD = POINTER(DWORD)
     HANDLE = ctypes.c_void_p
@@ -275,9 +273,9 @@ class TestJarMaker(unittest.TestCase):
             open(os.path.join(ldir, relpath), 'w').write(relpath+" content\n")
         # call JarMaker
         difference = self._jar_and_compare(jars,
                                            (_mangle,),
                                            sourcedirs = [])
         self.assertTrue(not difference, difference)
 
 if __name__ == '__main__':
-    unittest.main(testRunner=MozTestRunner())
+    mozunit.main()
--- a/config/tests/unit-LineEndings.py
+++ b/config/tests/unit-LineEndings.py
@@ -1,15 +1,15 @@
 import unittest
 
 from StringIO import StringIO
 import os
 import sys
 import os.path
-sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
+import mozunit
 
 from Preprocessor import Preprocessor
 
 class TestLineEndings(unittest.TestCase):
   """
   Unit tests for the Context class
   """
 
@@ -38,9 +38,9 @@ class TestLineEndings(unittest.TestCase)
     self.assertEquals(self.pp.out.getvalue(), 'a\nb\nc\n')
 
   def testWindows(self):
     self.createFile(['\x0D\x0A']*3)
     self.pp.do_include(self.tempnam)
     self.assertEquals(self.pp.out.getvalue(), 'a\nb\nc\n')
 
 if __name__ == '__main__':
-  unittest.main()
+  mozunit.main()
--- a/config/tests/unit-Preprocessor.py
+++ b/config/tests/unit-Preprocessor.py
@@ -1,16 +1,16 @@
 from __future__ import with_statement
 import unittest
 
 from StringIO import StringIO
 import os
 import sys
 import os.path
-sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
+import mozunit
 
 from Preprocessor import Preprocessor
 
 class NamedIO(StringIO):
   def __init__(self, name, content):
     self.name = name
     StringIO.__init__(self, content)
 
@@ -587,9 +587,9 @@ barfoobaz
   def test_command_line_literal_at(self):
     with MockedOpen(NamedIO("@foo@.in", """@foo@
 """)):
       self.pp.handleCommandLine(['-Fsubstitution', '-Dfoo=foobarbaz', '@foo@.in'])
       self.assertEqual(self.pp.out.getvalue(), """foobarbaz
 """)
 
 if __name__ == '__main__':
-  unittest.main()
+  mozunit.main()
--- a/config/tests/unit-buildlist.py
+++ b/config/tests/unit-buildlist.py
@@ -1,14 +1,14 @@
 import unittest
 
 import os, sys, os.path, time
 from tempfile import mkdtemp
 from shutil import rmtree
-sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
+import mozunit
 
 from buildlist import addEntriesToListFile
 
 class TestBuildList(unittest.TestCase):
   """
   Unit tests for buildlist.py
   """
   def setUp(self):
@@ -72,9 +72,9 @@ class TestBuildList(unittest.TestCase):
     only one entry being added."""
     testfile = os.path.join(self.tmpdir, "test.list")
     addEntriesToListFile(testfile, ["a","b","a","a","b"])
     self.assertFileContains(testfile, ["a","b"])
     addEntriesToListFile(testfile, ["c","a","c","b","c"])
     self.assertFileContains(testfile, ["a","b","c"])
 
 if __name__ == '__main__':
-  unittest.main()
+  mozunit.main()
--- a/config/tests/unit-expandlibs.py
+++ b/config/tests/unit-expandlibs.py
@@ -1,18 +1,17 @@
 from __future__ import with_statement
 import subprocess
 import unittest
 import sys
 import os
 import imp
 from tempfile import mkdtemp
 from shutil import rmtree
-sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
-from mozunit import MozTestRunner
+import mozunit
 
 from UserString import UserString
 # Create a controlled configuration for use by expandlibs
 config_win = {
     'AR_EXTRACT': '',
     'DLL_PREFIX': '',
     'LIB_PREFIX': '',
     'OBJ_SUFFIX': '.obj',
@@ -380,9 +379,9 @@ class TestSymbolOrder(unittest.TestCase)
         config.LD_PRINT_ICF_SECTIONS = '-Wl,--print-icf-sections'
         args = ExpandArgsMore(['foo', '-bar', 'bar.o', 'foo.o'])
         self.assertEqual(args._getOrderedSections(['hello', '_Z6barbazv']), ['.text.hi', '.text.hello', '.text.hot._Z6barbazv'])
         self.assertEqual(args._getOrderedSections(['_ZThn4_6foobarv', 'hi', '_Z6barbazv']), ['.text._Z6foobarv', '.text._ZThn4_6foobarv', '.text.hi', '.text.hello', '.text.hot._Z6barbazv'])
         subprocess.Popen = subprocess_popen
 
 
 if __name__ == '__main__':
-    unittest.main(testRunner=MozTestRunner())
+    mozunit.main()
--- a/config/tests/unit-nsinstall.py
+++ b/config/tests/unit-nsinstall.py
@@ -1,14 +1,14 @@
 import unittest
 
 import os, sys, os.path, time
 from tempfile import mkdtemp
 from shutil import rmtree
-sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
+import mozunit
 from mozprocess import processhandler
 
 from nsinstall import nsinstall
 import nsinstall as nsinstall_module
 NSINSTALL_PATH = nsinstall_module.__file__
 
 # Run the non-ASCII tests on (a) Windows, or (b) any platform with
 # sys.stdin.encoding set to UTF-8
@@ -165,9 +165,9 @@ class TestNsinstall(unittest.TestCase):
 
             self.assertEqual(rv, 0)
             destfile = os.path.join(testdir, filename)
             self.assert_(os.path.isfile(destfile))
 
     #TODO: implement -R, -l, -L and test them!
 
 if __name__ == '__main__':
-  unittest.main()
+  mozunit.main()
--- a/config/tests/unit-printprereleasesuffix.py
+++ b/config/tests/unit-printprereleasesuffix.py
@@ -1,13 +1,13 @@
 import unittest
 
 import sys
 import os.path
-sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
+import mozunit
 
 from printprereleasesuffix import get_prerelease_suffix
 
 class TestGetPreReleaseSuffix(unittest.TestCase):
   """
   Unit tests for the get_prerelease_suffix function
   """
 
@@ -72,9 +72,9 @@ class TestGetPreReleaseSuffix(unittest.T
     self.assertEqual(self.c, '')
 
   def test_plus(self):
     """test 1.2+ version string """
     self.c = get_prerelease_suffix('1.2+')
     self.assertEqual(self.c, '')
 
 if __name__ == '__main__':
-  unittest.main()
+  mozunit.main()
--- a/config/tests/unit-writemozinfo.py
+++ b/config/tests/unit-writemozinfo.py
@@ -1,15 +1,14 @@
 #!/usr/bin/env python
 from __future__ import with_statement
 import unittest
 import os, sys, time, tempfile
 from StringIO import StringIO
-
-sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
+import mozunit
 
 from writemozinfo import build_dict, write_json, JsonValue, jsonify
 
 class TestBuildDict(unittest.TestCase):
     def testMissing(self):
         """
         Test that missing required values raises.
         """
@@ -234,10 +233,9 @@ class TestWriteJson(unittest.TestCase):
                            'MOZ_WIDGET_TOOLKIT':'windows'})
         d = parse_json(s.getvalue())
         self.assertEqual('win', d['os'])
         self.assertEqual('x86', d['processor'])
         self.assertEqual('windows', d['toolkit'])
         self.assertEqual(32, d['bits'])
 
 if __name__ == '__main__':
-    unittest.main()
-  
+    mozunit.main()
--- a/xpcom/idl-parser/runtests.py
+++ b/xpcom/idl-parser/runtests.py
@@ -1,15 +1,16 @@
 #!/usr/bin/env python
 #
 # Any copyright is dedicated to the Public Domain.
 # http://creativecommons.org/publicdomain/zero/1.0/
 #
 # Unit tests for xpidl.py
 
+import mozunit
 import unittest
 import xpidl
 
 class TestParser(unittest.TestCase):
     def setUp(self):
         self.p = xpidl.IDLParser()
     
     def testEmpty(self):
@@ -89,9 +90,9 @@ attribute long bar;
         self.assertTrue(isinstance(i.productions[0], xpidl.Interface))
         iface = i.productions[0]
         a = iface.members[0]
         self.assertTrue(isinstance(a, xpidl.Attribute))
         self.assertEqual("bar", a.name)
         self.assertEqual("long", a.type)
 
 if __name__ == '__main__':
-    unittest.main()
+    mozunit.main()
--- a/xpcom/typelib/xpt/tools/runtests.py
+++ b/xpcom/typelib/xpt/tools/runtests.py
@@ -32,16 +32,17 @@
 
 import difflib
 import os
 import shutil
 from StringIO import StringIO
 import subprocess
 import sys
 import tempfile
+import mozunit
 import unittest
 import xpt
 
 def get_output(bin, file):
     p = subprocess.Popen([bin, file], stdout=subprocess.PIPE)
     stdout, _ = p.communicate()
     return stdout
 
@@ -758,9 +759,9 @@ class TestXPTLink(unittest.TestCase):
         self.assert_(t3.interfaces[1].resolved)
         # Ensure that IRetval's method's param type has been updated.
         self.assertEqual(1, len(t3.interfaces[0].methods))
         self.assert_(t3.interfaces[0].methods[0].params[0].type.element_type.iface.resolved)
         self.assertEqual(t3.interfaces[1],
                          t3.interfaces[0].methods[0].params[0].type.element_type.iface)
 
 if __name__ == '__main__':
-    unittest.main()
+    mozunit.main()