Bug 776035 part 3 - Use the new MockedOpen in unit-Preprocessor.py. r=ted
authorMike Hommey <mh+mozilla@glandium.org>
Wed, 25 Jul 2012 07:44:08 +0200
changeset 100406 4c78a8cc97bcc1ee0e7c9061a356285bb444ff64
parent 100405 c94f0e69113c9c533d013cd742baf282268902f0
child 100407 64de88e184209ac8655842b4e37a080d9a9c81ee
push id12479
push usermh@glandium.org
push dateWed, 25 Jul 2012 11:07:07 +0000
treeherdermozilla-inbound@64de88e18420 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersted
bugs776035
milestone17.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 776035 part 3 - Use the new MockedOpen in unit-Preprocessor.py. r=ted
config/tests/unit-Preprocessor.py
--- a/config/tests/unit-Preprocessor.py
+++ b/config/tests/unit-Preprocessor.py
@@ -1,50 +1,23 @@
 from __future__ import with_statement
 import unittest
 
 from StringIO import StringIO
 import os
 import sys
 import os.path
-import mozunit
+from mozunit import main, MockedOpen
 
 from Preprocessor import Preprocessor
 
-class NamedIO(StringIO):
-  def __init__(self, name, content):
-    self.name = name
-    StringIO.__init__(self, content)
-
-class MockedOpen(object):
-  """
-  Context manager diverting the open builtin such that opening files
-  can open NamedIO instances given when creating a MockedOpen.
-
-  with MockedOpen(NamedIO('foo', 'foo'), NamedIO('bar', 'bar')):
-    f = open('foo', 'r')
-
-  will thus assign the NamedIO instance for the file 'foo' to f.
-  """
-  def __init__(self, *files):
-    self.files = {}
-    for f in files:
-      self.files[os.path.abspath(f.name)] = f
-  def __call__(self, name, args):
-    absname = os.path.abspath(name)
-    if absname in self.files:
-      return self.files[absname]
-    return self.open(name, args)
-  def __enter__(self):
-    import __builtin__
-    self.open = __builtin__.open
-    __builtin__.open = self
-  def __exit__(self, type, value, traceback):
-    import __builtin__
-    __builtin__.open = self.open
+def NamedIO(name, content):
+  with open(name, 'w') as f:
+    f.write(content)
+  return name
 
 class TestPreprocessor(unittest.TestCase):
   """
   Unit tests for the Context class
   """
 
   def setUp(self):
     self.pp = Preprocessor()
@@ -534,23 +507,23 @@ octal value is not equal
     try:
       self.pp.do_include(f)
     except Preprocessor.Error, exception:
       self.assertEqual(exception.key, 'UNDEFINED_VAR')
     else:
       self.fail("Expected a Preprocessor.Error")
 
   def test_include(self):
-    with MockedOpen(NamedIO("foo/test", """#define foo foobarbaz
+    with MockedOpen({"foo/test": """#define foo foobarbaz
 #include @inc@
 @bar@
-"""),
-                      NamedIO("bar", """#define bar barfoobaz
+""",
+                     "bar": """#define bar barfoobaz
 @foo@
-""")):
+"""}):
       f = NamedIO("include.in", """#filter substitution
 #define inc ../bar
 #include foo/test""")
       self.pp.do_include(f)
       self.assertEqual(self.pp.out.getvalue(), """foobarbaz
 barfoobaz
 """)
 
@@ -570,26 +543,26 @@ barfoobaz
     try:
       self.pp.do_include(f)
     except Preprocessor.Error, exception:
       self.assertEqual(exception.key, 'UNDEFINED_VAR')
     else:
       self.fail("Expected a Preprocessor.Error")
 
   def test_include_literal_at(self):
-    with MockedOpen(NamedIO("@foo@", "#define foo foobarbaz")):
+    with MockedOpen({"@foo@": "#define foo foobarbaz"}):
       f = NamedIO("include_literal_at.in", """#include @foo@
 #filter substitution
 @foo@
 """)
       self.pp.do_include(f)
       self.assertEqual(self.pp.out.getvalue(), """foobarbaz
 """)
 
   def test_command_line_literal_at(self):
-    with MockedOpen(NamedIO("@foo@.in", """@foo@
-""")):
+    with MockedOpen({"@foo@.in": """@foo@
+"""}):
       self.pp.handleCommandLine(['-Fsubstitution', '-Dfoo=foobarbaz', '@foo@.in'])
       self.assertEqual(self.pp.out.getvalue(), """foobarbaz
 """)
 
 if __name__ == '__main__':
-  mozunit.main()
+  main()