Index: tools/telemetry/third_party/pyfakefs/pyfakefs/fake_filesystem_unittest.py |
diff --git a/tools/telemetry/third_party/pyfakefs/pyfakefs/fake_filesystem_unittest.py b/tools/telemetry/third_party/pyfakefs/pyfakefs/fake_filesystem_unittest.py |
index 1c4624dfa6d31af57b77c31037fa86e93645f289..692fb2f23cc0aea76925399db2b65657de0b18b6 100644 |
--- a/tools/telemetry/third_party/pyfakefs/pyfakefs/fake_filesystem_unittest.py |
+++ b/tools/telemetry/third_party/pyfakefs/pyfakefs/fake_filesystem_unittest.py |
@@ -1,7 +1,5 @@ |
# Copyright 2014 Altera Corporation. All Rights Reserved. |
-# Author: John McGehee |
-# |
-# Copyright 2014 John McGehee. All Rights Reserved. |
+# Copyright 2015 John McGehee |
# |
# Licensed under the Apache License, Version 2.0 (the "License"); |
# you may not use this file except in compliance with the License. |
@@ -36,7 +34,7 @@ that even in your test, you can use familiar functions like `open()` and |
`os.makedirs()` to manipulate the fake file system. |
This also means existing unit tests that use the real file system can be |
-retro-fitted to use `pyfakefs` by simply changing their base class from |
+retrofitted to use `pyfakefs` by simply changing their base class from |
`:py:class`unittest.TestCase` to |
`:py:class`pyfakefs.fake_filesystem_unittest.TestCase`. |
""" |
@@ -44,17 +42,20 @@ retro-fitted to use `pyfakefs` by simply changing their base class from |
import sys |
import unittest |
import doctest |
-import inspect |
import fake_filesystem |
import fake_filesystem_glob |
import fake_filesystem_shutil |
import fake_tempfile |
+if sys.version_info < (3,): |
+ import __builtin__ as builtins |
+else: |
+ import builtins |
-import mock |
+import mox3.stubout |
def load_doctests(loader, tests, ignore, module): |
'''Load the doctest tests for the specified module into unittest.''' |
- _patcher = _Patcher() |
+ _patcher = Patcher() |
globs = _patcher.replaceGlobs(vars(module)) |
tests.addTests(doctest.DocTestSuite(module, |
globs=globs, |
@@ -66,7 +67,7 @@ def load_doctests(loader, tests, ignore, module): |
class TestCase(unittest.TestCase): |
def __init__(self, methodName='runTest'): |
super(TestCase, self).__init__(methodName) |
- self._stubber = _Patcher() |
+ self._stubber = Patcher() |
@property |
def fs(self): |
@@ -90,19 +91,18 @@ class TestCase(unittest.TestCase): |
def tearDownPyfakefs(self): |
''':meth:`pyfakefs.fake_filesystem_unittest.setUpPyfakefs` registers the |
- tear down procedure using :meth:unittest.TestCase.addCleanup`. Thus this |
+ tear down procedure using :meth:`unittest.TestCase.addCleanup`. Thus this |
method is deprecated, and remains just for backward compatibility. |
''' |
pass |
-class _Patcher(object): |
+class Patcher(object): |
''' |
Instantiate a stub creator to bind and un-bind the file-related modules to |
- the :py:module:`pyfakefs` fake modules. |
+ the :py:mod:`pyfakefs` fake modules. |
''' |
SKIPMODULES = set([None, fake_filesystem, fake_filesystem_glob, |
- fake_filesystem_shutil, fake_tempfile, unittest, |
- sys]) |
+ fake_filesystem_shutil, fake_tempfile, sys]) |
'''Stub nothing that is imported within these modules. |
`sys` is included to prevent `sys.path` from being stubbed with the fake |
`os.path`. |
@@ -113,16 +113,17 @@ class _Patcher(object): |
def __init__(self): |
# Attributes set by _findModules() |
- self._osModuleNames = None |
- self._globModuleNames = None |
- self._pathModuleNames = None |
- self._shutilModuleNames = None |
- self._tempfileModuleNames = None |
+ self._osModules = None |
+ self._globModules = None |
+ self._pathModules = None |
+ self._shutilModules = None |
+ self._tempfileModules = None |
self._findModules() |
assert None not in vars(self).values(), \ |
"_findModules() missed the initialization of an instance variable" |
# Attributes set by _refresh() |
+ self._stubs = None |
self.fs = None |
self.fake_os = None |
self.fake_glob = None |
@@ -142,29 +143,31 @@ class _Patcher(object): |
Later, `setUp()` will stub these with the fake file system |
modules. |
''' |
- self._osModuleNames = set() |
- self._globModuleNames = set() |
- self._pathModuleNames = set() |
- self._shutilModuleNames = set() |
- self._tempfileModuleNames = set() |
+ self._osModules = set() |
+ self._globModules = set() |
+ self._pathModules = set() |
+ self._shutilModules = set() |
+ self._tempfileModules = set() |
for name, module in set(sys.modules.items()): |
- if module in self.SKIPMODULES or name in self.SKIPNAMES or (not inspect.ismodule(module)): |
+ if module in self.SKIPMODULES or name in self.SKIPNAMES: |
continue |
- if 'os' in module.__dict__ and inspect.ismodule(module.__dict__['os']): |
- self._osModuleNames.add(name + '.os') |
+ if 'os' in module.__dict__: |
+ self._osModules.add(module) |
if 'glob' in module.__dict__: |
- self._globModuleNames.add(name + '.glob') |
+ self._globModules.add(module) |
if 'path' in module.__dict__: |
- self._pathModuleNames.add(name + '.path') |
+ self._pathModules.add(module) |
if 'shutil' in module.__dict__: |
- self._shutilModuleNames.add(name + '.shutil') |
+ self._shutilModules.add(module) |
if 'tempfile' in module.__dict__: |
- self._tempfileModuleNames.add(name + '.tempfile') |
- |
+ self._tempfileModules.add(module) |
+ |
def _refresh(self): |
'''Renew the fake file system and set the _isStale flag to `False`.''' |
- mock.patch.stopall() |
- |
+ if self._stubs is not None: |
+ self._stubs.SmartUnsetAll() |
+ self._stubs = mox3.stubout.StubOutForTesting() |
+ |
self.fs = fake_filesystem.FakeFilesystem() |
self.fake_os = fake_filesystem.FakeOsModule(self.fs) |
self.fake_glob = fake_filesystem_glob.FakeGlobModule(self.fs) |
@@ -176,7 +179,7 @@ class _Patcher(object): |
self._isStale = False |
def setUp(self, doctester=None): |
- '''Bind the file-related modules to the :py:module:`pyfakefs` fake |
+ '''Bind the file-related modules to the :py:mod:`pyfakefs` fake |
modules real ones. Also bind the fake `file()` and `open()` functions. |
''' |
if self._isStale: |
@@ -185,33 +188,22 @@ class _Patcher(object): |
if doctester is not None: |
doctester.globs = self.replaceGlobs(doctester.globs) |
- def startPatch(self, realModuleName, fakeModule): |
- if realModuleName == 'unittest.main.os': |
- # Known issue with unittest.main resolving to unittest.main.TestProgram |
- # See mock module bug 250, https://code.google.com/p/mock/issues/detail?id=250. |
- return |
- patch = mock.patch(realModuleName, new=fakeModule) |
- try: |
- patch.start() |
- except: |
- target, attribute = realModuleName.rsplit('.', 1) |
- print("Warning: Could not patch '{}' on module '{}' because '{}' resolves to {}".format(attribute, target, target, patch.getter())) |
- print(" See mock module bug 250, https://code.google.com/p/mock/issues/detail?id=250") |
- |
- startPatch(self, '__builtin__.file', self.fake_open) |
- startPatch(self, '__builtin__.open', self.fake_open) |
- |
- for module in self._osModuleNames: |
- startPatch(self, module, self.fake_os) |
- for module in self._globModuleNames: |
- startPatch(self, module, self.fake_glob) |
- for module in self._pathModuleNames: |
- startPatch(self, module, self.fake_path) |
- for module in self._shutilModuleNames: |
- startPatch(self, module, self.fake_shutil) |
- for module in self._tempfileModuleNames: |
- startPatch(self, module, self.fake_tempfile_) |
- |
+ if sys.version_info < (3,): |
+ # No file() in Python3 |
+ self._stubs.SmartSet(builtins, 'file', self.fake_open) |
+ self._stubs.SmartSet(builtins, 'open', self.fake_open) |
+ |
+ for module in self._osModules: |
+ self._stubs.SmartSet(module, 'os', self.fake_os) |
+ for module in self._globModules: |
+ self._stubs.SmartSet(module, 'glob', self.fake_glob) |
+ for module in self._pathModules: |
+ self._stubs.SmartSet(module, 'path', self.fake_path) |
+ for module in self._shutilModules: |
+ self._stubs.SmartSet(module, 'shutil', self.fake_shutil) |
+ for module in self._tempfileModules: |
+ self._stubs.SmartSet(module, 'tempfile', self.fake_tempfile_) |
+ |
def replaceGlobs(self, globs_): |
globs = globs_.copy() |
if self._isStale: |
@@ -221,9 +213,7 @@ class _Patcher(object): |
if 'glob' in globs: |
globs['glob'] = fake_filesystem_glob.FakeGlobModule(self.fs) |
if 'path' in globs: |
- fake_os = globs['os'] if 'os' in globs \ |
- else fake_filesystem.FakeOsModule(self.fs) |
- globs['path'] = fake_os.path |
+ globs['path'] = fake_filesystem.FakePathModule(self.fs) |
if 'shutil' in globs: |
globs['shutil'] = fake_filesystem_shutil.FakeShutilModule(self.fs) |
if 'tempfile' in globs: |
@@ -233,4 +223,4 @@ class _Patcher(object): |
def tearDown(self, doctester=None): |
'''Clear the fake filesystem bindings created by `setUp()`.''' |
self._isStale = True |
- mock.patch.stopall() |
+ self._stubs.SmartUnsetAll() |