Example #1
0
    def setUp(self):
        """ Initialize a fake filesystem and dirtools. """

        # First we create a fake filesystem in order to test dirtools
        fk = fake_filesystem.FakeFilesystem()
        fk.CreateDirectory('/test_dirtools')
        fk.CreateFile('/test_dirtools/file1', contents='contents1')
        fk.CreateFile('/test_dirtools/file2', contents='contents2')
        fk.CreateFile('/test_dirtools/file3.py', contents='print "ok"')
        fk.CreateFile('/test_dirtools/file3.pyc', contents='')
        fk.CreateFile('/test_dirtools/.exclude',
                      contents='excluded_dir/\n*.pyc')

        fk.CreateDirectory('/test_dirtools/excluded_dir')
        fk.CreateFile('/test_dirtools/excluded_dir/excluded_file',
                      contents='excluded')

        fk.CreateDirectory('/test_dirtools/dir1')
        fk.CreateDirectory('/test_dirtools/dir1/subdir1')
        fk.CreateFile('/test_dirtools/dir1/subdir1/file_subdir1',
                      contents='inside subdir1')
        fk.CreateFile('/test_dirtools/dir1/subdir1/.project')

        fk.CreateDirectory('/test_dirtools/dir2')
        fk.CreateFile('/test_dirtools/dir2/file_dir2', contents='inside dir2')

        # Sort of "monkey patch" to make dirtools use the fake filesystem
        dirtools.os = fake_filesystem.FakeOsModule(fk)
        dirtools.open = fake_filesystem.FakeFileOpen(fk)

        # Dirtools initialization
        self.dir = dirtools.Dir('/test_dirtools')
        self.os = dirtools.os
        self.open = dirtools.open
    def testLoadFromStorage_relativePath(self):
        fake_os = fake_filesystem.FakeOsModule(self.filesystem)
        yaml_contents = {'one': {'needed': 'd', 'keys': 'e'}}
        yaml_contents['one'].update(self._OAUTH_DICT)
        self.filesystem.CreateFile('/home/test/yaml/googleads.yaml',
                                   contents=yaml.dump(yaml_contents))
        fake_os.chdir('/home/test')

        with mock.patch(
                'googleads.oauth2.GoogleRefreshTokenClient') as mock_client:
            with mock.patch('googleads.common.os', fake_os):
                with mock.patch('googleads.common.open',
                                self.fake_open,
                                create=True):
                    rval = googleads.common.LoadFromStorage(
                        'yaml/googleads.yaml', 'one', ['needed', 'keys'],
                        ['other'])
                    mock_client.assert_called_once_with(
                        'a', 'b', 'c', None, True, None)
                    self.assertEqual(
                        {
                            'oauth2_client': mock_client.return_value,
                            'needed': 'd',
                            'keys': 'e',
                            'https_proxy': None
                        }, rval)
    def setUp(self):
        # Base paths in the real and test file systems.     We keep them different
        # so that missing features in the fake don't fall through to the base
        # operations and magically succeed.
        tsname = 'fakefs.%s' % time.time()
        # Fully expand the base_path - required on OS X.
        self.real_base = os.path.realpath(
            os.path.join(tempfile.gettempdir(), tsname))
        os.chdir(tempfile.gettempdir())
        if os.path.isdir(self.real_base):
            shutil.rmtree(self.real_base)
        os.mkdir(self.real_base)
        self.fake_base = self._FAKE_FS_BASE

        # Make sure we can write to the physical testing temp directory.
        self.assertTrue(os.access(self.real_base, os.W_OK))

        self.fake_filesystem = fake_filesystem.FakeFilesystem()
        self.fake_filesystem.CreateDirectory(self.fake_base)
        self.fake_os = fake_filesystem.FakeOsModule(self.fake_filesystem)
        self.fake_open = fake_filesystem.FakeFileOpen(self.fake_filesystem)
        self._created_files = []

        os.chdir(self.real_base)
        self.fake_os.chdir(self.fake_base)
 def replaceGlobs(self, globs_):
     globs = globs_.copy()
     if self._isStale:
         self._refresh()
     if 'os' in globs:
         globs['os'] = fake_filesystem.FakeOsModule(self.fs)
     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
     if 'shutil' in globs:
         globs['shutil'] = fake_filesystem_shutil.FakeShutilModule(self.fs)
     if 'tempfile' in globs:
         globs['tempfile'] = fake_tempfile.FakeTempfileModule(self.fs)
     return globs
    def setUp(self):
        self._fs = fake_filesystem.FakeFilesystem()
        self._os = fake_filesystem.FakeOsModule(self._fs)
        self._real_os_path = ptc_fetch.os.path
        ptc_fetch.os.path = self._os.path

        self._mox = mox.Mox()
        self._mox.StubOutWithMock(ptc_fetch, 'requests')
    def setUp(self):
        self._fs = fake_filesystem.FakeFilesystem()
        self._os = fake_filesystem.FakeOsModule(self._fs)
        self._real_os = ptc_fetch.os
        ptc_fetch.os = self._os
        self._subprocess_call_args = []

        self._raise_error_on_file = None
Example #7
0
 def setUp(self):
     self.real_tempfile = sys.modules['tempfile']
     self.real_os = sys.modules['os']
     self.filesystem = fake_filesystem.FakeFilesystem()
     sys.modules['tempfile'] = fake_tempfile.FakeTempfileModule(
         self.filesystem)
     sys.modules['os'] = fake_filesystem.FakeOsModule(self.filesystem)
     random.seed(32452)
Example #8
0
    def setupFilesystem(self, module, path_specs=None, from_module=None):
        """Configure module so that it uses a fake filesystem.

    Raises:
      Error: if the method is called on a module multiple times.
      Error: if from_module is not None and has not been setup.
    Returns fake_filesystem.FakeFilesystem

    Args:
      module: module
        The module to configure.
      path_specs: *list of {str_or_two-tuple_(str/_dict)}
        The paths to create.  If any element is a two-tuple, the
        first element is the path, and the second element is a dict
        of keyword args to send to fake_filesystem.CreateFile().
      from_module: *module
        If specified, the faux filesystem objects are obtained from
        the given module, rather than being created anew.  This means
        that the same filesystem objects are shared across multiple
        modules (arguably the most sensible way to test things).

    Returns: any
    """
        mname = module.__name__
        fsinfo = self._fsinfo
        if mname in fsinfo:
            raise Error(
                'Attempt to setupFilesystem on %s when it is already setup' %
                mname)

        fsinfo[mname] = {'module': module, 'objs': {}}
        info = fsinfo[mname]

        if from_module:
            oname = from_module.__name__
            if oname not in fsinfo:
                raise Error(
                    'Request to setup faux filesystem for module %s based on module %s'
                    ' which has not been set up.' % (mname, oname))
            info['objs'] = fsinfo[oname]['objs']
            objs = info['objs']
            fs = objs['fs']
        else:
            fs = fake_filesystem.FakeFilesystem()
            objs = info['objs']
            objs['fs'] = fs
            objs['fcntl'] = FakeFcntl(fs)
            objs['open'] = fake_filesystem.FakeFileOpen(fs)
            objs['os'] = fake_filesystem.FakeOsModule(fs)

        module.open = objs['open']
        for modname in ('os', 'fcntl'):
            if hasattr(module, modname):
                self._stubs.Set(module, modname, objs[modname])
        if path_specs:
            self.populateFilesystem(fs, path_specs)
        return fs
Example #9
0
    def __init__(self, filesystem):
        """Construct fake glob module using the fake filesystem.

    Args:
      filesystem:  FakeFilesystem used to provide file system information
    """
        self._glob_module = glob
        self._os_module = fake_filesystem.FakeOsModule(filesystem)
        self._path_module = self._os_module.path
Example #10
0
 def setUp(self):
     self.filesystem = fake_filesystem.FakeFilesystem()
     self.os_path = os.path
     sdk_dir = './sdks'
     self.sdks = ['java', 'rails', 'ruby', 'whatever']
     self.filesystem.CreateDirectory(sdk_dir)
     for sdk in self.sdks:
         self.filesystem.CreateDirectory(os.path.join(sdk_dir, sdk))
     self.os = fake_filesystem.FakeOsModule(self.filesystem)
Example #11
0
    def setUp(self):
        fake_fs = pyfakefs.FakeFilesystem()
        fake_os = pyfakefs.FakeOsModule(fake_fs)
        fake_fs.CreateFile(HdfsTest._site_config,
                           contents="this is not a real file.")
        fake_fs.CreateFile(
            "src",
            contents="heh. before pyfakefs this was unintentionally a dir.")

        self.original_os = twitter.common.fs.hdfs.os
        twitter.common.fs.hdfs.os = fake_os
    def _refresh(self):
        '''Renew the fake file system and set the _isStale flag to `False`.'''
        mock.patch.stopall()

        self.fs = fake_filesystem.FakeFilesystem()
        self.fake_os = fake_filesystem.FakeOsModule(self.fs)
        self.fake_glob = fake_filesystem_glob.FakeGlobModule(self.fs)
        self.fake_path = self.fake_os.path
        self.fake_shutil = fake_filesystem_shutil.FakeShutilModule(self.fs)
        self.fake_tempfile_ = fake_tempfile.FakeTempfileModule(self.fs)
        self.fake_open = fake_filesystem.FakeFileOpen(self.fs)

        self._isStale = False
Example #13
0
    def _refresh(self):
        '''Renew the fake file system and set the _isStale flag to `False`.'''
        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)
        self.fake_path = self.fake_os.path
        self.fake_shutil = fake_filesystem_shutil.FakeShutilModule(self.fs)
        self.fake_tempfile_ = fake_tempfile.FakeTempfileModule(self.fs)
        self.fake_open = fake_filesystem.FakeFileOpen(self.fs)

        self._isStale = False
Example #14
0
# ./data_handler/provider/local_file

from data_handler.provider import local_file

import tests.fakers

from nose.tools import assert_equal
import mox
import fake_filesystem

filesystem = fake_filesystem.FakeFilesystem()
local_file.os = fake_filesystem.FakeOsModule(filesystem)
local_file.open = fake_filesystem.FakeFileOpen(filesystem)


class TestLocalFile(object):
    @classmethod
    def setup_class(klass):
        """This method is run once for each class before any tests are run"""
        klass.test_file_path = "/test/file"
        klass.test_file_data = "{hostname': 'abc', 'password': '******'}"
        filesystem.CreateFile(klass.test_file_path,
                              contents=klass.test_file_data)

        klass._fakeparser = tests.fakers.FakeParser()
        klass._local_file = local_file.LocalFile(parser=klass._fakeparser,
                                                 filepath=klass.test_file_path)

    def setUp(self):
        self.m = mox.Mox()
        self.m.StubOutWithMock(self._local_file, 'parse')
Example #15
0
 def __init__(self, filesystem=None):
     self.filesystem = fake_filesystem.FakeFilesystem()
     self.os = fake_filesystem.FakeOsModule(self.filesystem)
     self._open = fake_filesystem.FakeFileOpen(self.filesystem)
     self.shutil = fake_filesystem_shutil.FakeShutilModule(self.filesystem)