[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[PATCH 1/2] Add doctests and integrate them into unittest system Add test for isEfiPartition Add dummy implementation for some file methods Add Mock class



---
 anaconda                      |    4 +-
 tests/__init__.py             |   14 ++-
 tests/doctests/bootloader.txt |   57 +++++++++
 tests/doctests/language.txt   |    3 +
 tests/doctests/options.txt    |    6 +
 tests/doctests/run_tests.py   |   65 ++++++++++
 tests/doctests/selftest.txt   |   42 +++++++
 tests/mock/__init__.py        |    2 +
 tests/mock/disk.py            |   68 ++++++++++
 tests/mock/mock.py            |  271 +++++++++++++++++++++++++++++++++++++++++
 10 files changed, 524 insertions(+), 8 deletions(-)
 create mode 100644 tests/doctests/__init__.py
 create mode 100644 tests/doctests/bootloader.txt
 create mode 100644 tests/doctests/language.txt
 create mode 100644 tests/doctests/options.txt
 create mode 100755 tests/doctests/run_tests.py
 create mode 100644 tests/doctests/selftest.txt
 create mode 100644 tests/mock/__init__.py
 create mode 100644 tests/mock/disk.py
 create mode 100644 tests/mock/mock.py

diff --git a/anaconda b/anaconda
index 191dab0..b5532b0 100755
--- a/anaconda
+++ b/anaconda
@@ -151,7 +151,7 @@ def setupPythonUpdates():
         target = "/etc/udev/rules.d/" + rule.split('/')[-1]
         shutil.copyfile(rule, target)
 
-def parseOptions():
+def parseOptions(argv = None):
     def resolution_cb (option, opt_str, value, parser):
         parser.values.runres = value
 
@@ -229,7 +229,7 @@ def parseOptions():
     op.add_option("-t", "--test", dest="unsupportedMode",
                   action="store_const", const="test")
 
-    return op.parse_args()
+    return op.parse_args(argv)
 
 def setupPythonPath():
     haveUpdates = False
diff --git a/tests/__init__.py b/tests/__init__.py
index d5b53a8..784c932 100644
--- a/tests/__init__.py
+++ b/tests/__init__.py
@@ -1,15 +1,12 @@
 import os
 
-# this has to be imported before running anything
-import anaconda_log
-import upgrade
-
-
 def getAvailableSuites():
-    root, tests_dir = os.path.split(os.path.dirname(__file__))
+    root_dir, tests_dir = os.path.split(os.path.dirname(__file__))
     modules = []
 
     for root, dirs, files in os.walk(tests_dir):
+        if os.path.exists(os.path.join(root, ".disabled")):
+            continue
         for filename in files:
             if filename.endswith(".py") and filename != "__init__.py":
                 basename, extension = os.path.splitext(filename)
@@ -27,3 +24,8 @@ def getAvailableSuites():
             available_suites[module] = suite()
 
     return available_suites
+
+if __name__ == '__main__':
+    s = getAvailableSuites()
+    unittest.TextTestRunner(verbosity=2).run(s)
+    
diff --git a/tests/doctests/__init__.py b/tests/doctests/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/tests/doctests/bootloader.txt b/tests/doctests/bootloader.txt
new file mode 100644
index 0000000..3ea3065
--- /dev/null
+++ b/tests/doctests/bootloader.txt
@@ -0,0 +1,57 @@
+Prepare dummy object
+
+>>> class O(object): pass
+
+Create dummy partition object
+
+>>> part = O()
+>>> part.active = False
+>>> part.disk = O()
+>>> part.disk.type = "gpt"
+>>> part.name = "EFI System Partition"
+>>> from parted import PARTITION_BOOT
+>>> part.getFlag = lambda x: x==PARTITION_BOOT
+>>> part.fileSystem = O()
+>>> part.fileSystem.type = "fat16"
+>>> part.getDeviceNodeName = lambda: "device"
+
+Override isys module
+
+>>> isys = O()
+>>> isys.readFSLabel = lambda x: "not anaconda"
+
+Override storage modules
+
+>>> storage = O()
+>>> storage.devices = O()
+>>> storage.devices.devicePathToName = lambda: None
+>>> storage.getReleaseString = lambda: None
+
+Inject the overriden modules into python
+
+>>> import sys
+>>> sys.modules["isys"] = isys
+>>> sys.modules["storage"] = storage
+>>> sys.modules["storage.devices"] = storage.devices
+
+Import tested method
+
+>>> from bootloader import isEfiSystemPartition
+
+Run the test case
+
+>>> isEfiSystemPartition(part)
+False
+>>> part.active = True
+>>> isEfiSystemPartition(part)
+True
+>>> part.fileSystem.type = "fat32"
+>>> isEfiSystemPartition(part)
+True
+>>> part.fileSystem.type = "xfs"
+>>> isEfiSystemPartition(part)
+False
+>>> part.fileSystem.type = "jfs"
+>>> isEfiSystemPartition(part)
+False
+
diff --git a/tests/doctests/language.txt b/tests/doctests/language.txt
new file mode 100644
index 0000000..63cebad
--- /dev/null
+++ b/tests/doctests/language.txt
@@ -0,0 +1,3 @@
+>> from language import expandLangs
+>> expandLangs("fr_FR utf8 euro") 
+["fr_FR utf8 euro", "fr_FR.utf8", "fr_FR", "fr"]
diff --git a/tests/doctests/options.txt b/tests/doctests/options.txt
new file mode 100644
index 0000000..4c10a4d
--- /dev/null
+++ b/tests/doctests/options.txt
@@ -0,0 +1,6 @@
+>>> from anaconda import parseOptions
+>>> options, argv = parseOptions()
+>>> options.noipv4
+False
+>>> options.noipv6
+False
diff --git a/tests/doctests/run_tests.py b/tests/doctests/run_tests.py
new file mode 100755
index 0000000..2abd39d
--- /dev/null
+++ b/tests/doctests/run_tests.py
@@ -0,0 +1,65 @@
+#!/usr/bin/python
+import os.path
+import unittest
+import doctest
+import sys
+
+def get_tests():
+    tests_dir = os.path.dirname(__file__)
+    if not tests_dir:
+        tests_dir = "./"
+    sidx = len(tests_dir)+1
+        
+    test_pool = []
+    for root, dirs, files in os.walk(tests_dir):
+        for f in files:
+            if not f.endswith(".txt"):
+                continue
+            test_pool.append(os.path.join(root, f)[sidx:])
+    return test_pool
+
+    
+
+def run():
+    s = sys.modules
+    b = __builtins__
+    
+    for f in get_tests():
+        doctest.testfile(f, module_relative=True, raise_on_error = True, report=False)
+        sys.modules = s
+        __builtins__ = b
+        
+    if doctest.master:
+        doctest.master.summarize(True)
+    else:
+        print "No tests found"
+
+class Case(unittest.TestCase):
+    def __init__(self, file):
+        unittest.TestCase.__init__(self)
+        self.file = file
+        
+    def runfile(self):
+        s = sys.modules
+        b = __builtins__
+        
+        (failures, tests) = doctest.testfile(self.file, module_relative = True, raise_on_error = True)
+
+        sys.modules  = s
+        __builtins__ = b
+                
+        return failures
+
+    def __str__(self):
+        return """Test interactive session %s""" % (self.file)
+
+    def runTest(self):
+        
+        self.assertEqual(self.runfile(), 0)
+
+def suite():
+    return unittest.TestSuite([Case(f) for f in get_tests()])
+
+if __name__ == '__main__':
+    run()
+    
diff --git a/tests/doctests/selftest.txt b/tests/doctests/selftest.txt
new file mode 100644
index 0000000..28e9e07
--- /dev/null
+++ b/tests/doctests/selftest.txt
@@ -0,0 +1,42 @@
+>>> from tests.dummies import O, InitTest
+>>> 1+1
+2
+>>> 2*9
+18
+>>> raise Exception("lala")
+Traceback (most recent call last):
+...
+Exception: lala
+>>> Test = InitTest()
+>>> f = file("/tmp/test.txt", "r")
+Traceback (most recent call last):
+...
+IOError: [Errno 2] No such file or directory: '/tmp/test.txt'
+>>> f = open("/tmp/test.txt", "w")
+>>> f.write("test")
+>>> Test.fs["/tmp/test.txt"]
+''
+>>> f.flush()
+>>> Test.fs["/tmp/test.txt"]
+'test'
+>>> import os
+>>> f.seek(-2, os.SEEK_CUR)
+>>> f.write("02")
+>>> f.close()
+>>> __testenv__.fs["/tmp/test.txt"]
+'te02'
+>>> os.path.exists("/tmp/test.txt")
+True
+>>> os.path.exists("/etc/passwd")
+False
+>>> os.access('/tmp/test.txt', os.R_OK)
+True
+>>> os.remove("/tmp/test.txt")
+>>> os.path.exists("/tmp/test.txt")
+False
+>>> os.remove("/tmp/test.txt")
+Traceback (most recent call last):
+...
+OSError: [Errno 2] No such file or directory: '/tmp/test.txt'
+>>> os.access('/tmp/test.txt', os.R_OK)
+False
diff --git a/tests/mock/__init__.py b/tests/mock/__init__.py
new file mode 100644
index 0000000..5e2c84d
--- /dev/null
+++ b/tests/mock/__init__.py
@@ -0,0 +1,2 @@
+from disk import *
+from mock import *
diff --git a/tests/mock/disk.py b/tests/mock/disk.py
new file mode 100644
index 0000000..ac10e47
--- /dev/null
+++ b/tests/mock/disk.py
@@ -0,0 +1,68 @@
+from StringIO import StringIO
+
+class DiskIO(object):
+    class TestFile(StringIO):
+        def __init__(self, store, path, content = ""):
+            StringIO.__init__(self, content)
+            self._store = store
+            self._path = path
+            self._ro = False
+
+        def flush(self):
+            self._store[self._path] = self.getvalue()
+
+        def close(self):
+            self.flush()
+            return StringIO.close(self)
+
+    class Dir(object):
+        pass
+
+    class Link(object):
+        pass
+ 
+    def __init__(self):
+        self.reset()
+
+    def reset(self):
+        self.fs = {
+            "/proc": self.Dir,
+            "/proc/cmdline": "linux",
+        }
+        self._pwd = "/"
+
+    #Emulate file objects
+    def open(self, filename, mode = "r"):
+        path = os.path.join(self._pwd, filename)
+        content = self.fs.get(path, None)
+        if content == self.Dir:
+            raise IOError("[Errno 21] Is a directory: '%s'" % (path))
+        elif not content and mode.startswith("w"):
+            self.fs[path] = ""
+            f = self.IO(self.fs, path, self.fs[path])
+        elif not content:
+            raise IOError("[Errno 2] No such file or directory: '%s'" % (path,))
+        elif mode.endswith("+") or mode.endswith("a"):
+            f = self.IO(self.fs, path, content)
+            f.seek(0, os.SEEK_END)
+        else:
+            f = self.IO(self.fs, path, content)
+        
+        return f
+            
+    #Emulate os.path calls
+    def os_path_exists(self, path):
+        path = os.path.join(self._pwd, path)
+        return self.fs.has_key(path)
+
+    #Emulate os calls
+    def os_remove(self, path):
+        path = os.path.join(self._pwd, path)
+        try:
+            del self.fs[path]
+        except KeyError:
+            raise OSError("[Errno 2] No such file or directory: '%s'" % (path,))
+
+    def os_access(self, path, mode):
+        return self.path_exists(path)
+
diff --git a/tests/mock/mock.py b/tests/mock/mock.py
new file mode 100644
index 0000000..03871d6
--- /dev/null
+++ b/tests/mock/mock.py
@@ -0,0 +1,271 @@
+# mock.py
+# Test tools for mocking and patching.
+# Copyright (C) 2007-2009 Michael Foord
+# E-mail: fuzzyman AT voidspace DOT org DOT uk
+
+# mock 0.6.0
+# http://www.voidspace.org.uk/python/mock/
+
+# Released subject to the BSD License
+# Please see http://www.voidspace.org.uk/python/license.shtml
+
+# Scripts maintained at http://www.voidspace.org.uk/python/index.shtml
+# Comments, suggestions and bug reports welcome.
+
+
+__all__ = (
+    'Mock',
+    'patch',
+    'patch_object',
+    'sentinel',
+    'DEFAULT'
+)
+
+__version__ = '0.6.0'
+
+class SentinelObject(object):
+    def __init__(self, name):
+        self.name = name
+        
+    def __repr__(self):
+        return '<SentinelObject "%s">' % self.name
+
+
+class Sentinel(object):
+    def __init__(self):
+        self._sentinels = {}
+        
+    def __getattr__(self, name):
+        return self._sentinels.setdefault(name, SentinelObject(name))
+    
+    
+sentinel = Sentinel()
+
+DEFAULT = sentinel.DEFAULT
+
+class OldStyleClass:
+    pass
+ClassType = type(OldStyleClass)
+
+def _is_magic(name):
+    return '__%s__' % name[2:-2] == name
+
+def _copy(value):
+    if type(value) in (dict, list, tuple, set):
+        return type(value)(value)
+    return value
+
+
+class Mock(object):
+
+    def __init__(self, spec=None, side_effect=None, return_value=DEFAULT, 
+                 name=None, parent=None, wraps=None):
+        self._parent = parent
+        self._name = name
+        if spec is not None and not isinstance(spec, list):
+            spec = [member for member in dir(spec) if not _is_magic(member)]
+        
+        self._methods = spec
+        self._children = {}
+        self._return_value = return_value
+        self.side_effect = side_effect
+        self._wraps = wraps
+        
+        self.reset_mock()
+        
+
+    def reset_mock(self):
+        self.called = False
+        self.call_args = None
+        self.call_count = 0
+        self.call_args_list = []
+        self.method_calls = []
+        for child in self._children.itervalues():
+            child.reset_mock()
+        if isinstance(self._return_value, Mock):
+            self._return_value.reset_mock()
+        
+    
+    def __get_return_value(self):
+        if self._return_value is DEFAULT:
+            self._return_value = Mock()
+        return self._return_value
+    
+    def __set_return_value(self, value):
+        self._return_value = value
+        
+    return_value = property(__get_return_value, __set_return_value)
+
+
+    def __call__(self, *args, **kwargs):
+        self.called = True
+        self.call_count += 1
+        self.call_args = (args, kwargs)
+        self.call_args_list.append((args, kwargs))
+        
+        parent = self._parent
+        name = self._name
+        while parent is not None:
+            parent.method_calls.append((name, args, kwargs))
+            if parent._parent is None:
+                break
+            name = parent._name + '.' + name
+            parent = parent._parent
+        
+        ret_val = DEFAULT
+        if self.side_effect is not None:
+            if (isinstance(self.side_effect, Exception) or 
+                isinstance(self.side_effect, (type, ClassType)) and
+                issubclass(self.side_effect, Exception)):
+                raise self.side_effect
+            
+            ret_val = self.side_effect(*args, **kwargs)
+            if ret_val is DEFAULT:
+                ret_val = self.return_value
+        
+        if self._wraps is not None and self._return_value is DEFAULT:
+            return self._wraps(*args, **kwargs)
+        if ret_val is DEFAULT:
+            ret_val = self.return_value
+        return ret_val
+    
+    
+    def __getattr__(self, name):
+        if self._methods is not None:
+            if name not in self._methods:
+                raise AttributeError("Mock object has no attribute '%s'" % name)
+        elif _is_magic(name):
+            raise AttributeError(name)
+        
+        if name not in self._children:
+            wraps = None
+            if self._wraps is not None:
+                wraps = getattr(self._wraps, name)
+            self._children[name] = Mock(parent=self, name=name, wraps=wraps)
+            
+        return self._children[name]
+    
+    
+    def assert_called_with(self, *args, **kwargs):
+        assert self.call_args == (args, kwargs), 'Expected: %s\nCalled with: %s' % ((args, kwargs), self.call_args)
+        
+
+def _dot_lookup(thing, comp, import_path):
+    try:
+        return getattr(thing, comp)
+    except AttributeError:
+        __import__(import_path)
+        return getattr(thing, comp)
+
+
+def _importer(target):
+    components = target.split('.')
+    import_path = components.pop(0)
+    thing = __import__(import_path)
+
+    for comp in components:
+        import_path += ".%s" % comp
+        thing = _dot_lookup(thing, comp, import_path)
+    return thing
+
+
+class _patch(object):
+    def __init__(self, target, attribute, new, spec, create):
+        self.target = target
+        self.attribute = attribute
+        self.new = new
+        self.spec = spec
+        self.create = create
+        self.has_local = False
+
+
+    def __call__(self, func):
+        if hasattr(func, 'patchings'):
+            func.patchings.append(self)
+            return func
+
+        def patched(*args, **keywargs):
+            # don't use a with here (backwards compatability with 2.5)
+            extra_args = []
+            for patching in patched.patchings:
+                arg = patching.__enter__()
+                if patching.new is DEFAULT:
+                    extra_args.append(arg)
+            args += tuple(extra_args)
+            try:
+                return func(*args, **keywargs)
+            finally:
+                for patching in getattr(patched, 'patchings', []):
+                    patching.__exit__()
+
+        patched.patchings = [self]
+        patched.__name__ = func.__name__ 
+        patched.compat_co_firstlineno = getattr(func, "compat_co_firstlineno", 
+                                                func.func_code.co_firstlineno)
+        return patched
+
+
+    def get_original(self):
+        target = self.target
+        name = self.attribute
+        create = self.create
+        
+        original = DEFAULT
+        if _has_local_attr(target, name):
+            try:
+                original = target.__dict__[name]
+            except AttributeError:
+                # for instances of classes with slots, they have no __dict__
+                original = getattr(target, name)
+        elif not create and not hasattr(target, name):
+            raise AttributeError("%s does not have the attribute %r" % (target, name))
+        return original
+
+    
+    def __enter__(self):
+        new, spec, = self.new, self.spec
+        original = self.get_original()
+        if new is DEFAULT:
+            # XXXX what if original is DEFAULT - shouldn't use it as a spec
+            inherit = False
+            if spec == True:
+                # set spec to the object we are replacing
+                spec = original
+                if isinstance(spec, (type, ClassType)):
+                    inherit = True
+            new = Mock(spec=spec)
+            if inherit:
+                new.return_value = Mock(spec=spec)
+        self.temp_original = original
+        setattr(self.target, self.attribute, new)
+        return new
+
+
+    def __exit__(self, *_):
+        if self.temp_original is not DEFAULT:
+            setattr(self.target, self.attribute, self.temp_original)
+        else:
+            delattr(self.target, self.attribute)
+        del self.temp_original
+            
+                
+def patch_object(target, attribute, new=DEFAULT, spec=None, create=False):
+    return _patch(target, attribute, new, spec, create)
+
+
+def patch(target, new=DEFAULT, spec=None, create=False):
+    try:
+        target, attribute = target.rsplit('.', 1)    
+    except (TypeError, ValueError):
+        raise TypeError("Need a valid target to patch. You supplied: %r" % (target,))
+    target = _importer(target)
+    return _patch(target, attribute, new, spec, create)
+
+
+
+def _has_local_attr(obj, name):
+    try:
+        return name in vars(obj)
+    except TypeError:
+        # objects without a __dict__
+        return hasattr(obj, name)
-- 
1.6.6.1


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]