How to use the pyfakefs.fake_filesystem.FakeFilesystem function in pyfakefs

To help you get started, we’ve selected a few pyfakefs examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github KanoComputing / kano-settings / tests / display / display.py View on Github external
# Tests for the `kano_settings.system.display` module
#

import contextlib
import unittest

try:
    import unittest.mock as mock
except ImportError:
    import mock

import pyfakefs.fake_filesystem as fake_fs


# Import fails if the kano-settings directory doesn't exist
fs = fake_fs.FakeFilesystem()
fs.CreateDirectory('/usr/share/kano-settings')
fake_os = fake_fs.FakeOsModule(fs)
with mock.patch('kano.utils.file_operations.os', fake_os):
    import kano_settings.system.display as display


SETTINGS_FUNCTION_TEMPLATE = 'kano_settings.system.display.{}'
SETTINGS_RUN_CMD = SETTINGS_FUNCTION_TEMPLATE.format('run_cmd')
SETTINGS_SUBPROCESS_CMD = SETTINGS_FUNCTION_TEMPLATE.format(
    'subprocess.check_output'
)
SETTINGS_ISFILE_CMD = SETTINGS_FUNCTION_TEMPLATE.format('os.path.isfile')


def mock_run_cmd(stdout, stderr='', returncode=0):
    def run_cmd(dummy):
github gisinc / slap / tests / test_config_builder.py View on Github external
def setUp(self):
        self.fs = fake_filesystem.FakeFilesystem()
        self.fake_os = fake_filesystem.FakeOsModule(self.fs)
github SUSE / DeepSea / tests / unit / runners / test_changed.py View on Github external
from mock import patch, MagicMock, mock_open
import pytest
from pyfakefs import fake_filesystem as fake_fs

from srv.modules.runners import changed

fs = fake_fs.FakeFilesystem()
f_os = fake_fs.FakeOsModule(fs)
f_open = fake_fs.FakeFileOpen(fs)
base_dir = '/srv/salt/ceph/configuration/files/'
conf_dir = '/srv/salt/ceph/configuration/files/ceph.conf.d/'
checksum_dir = '/srv/salt/ceph/configuration/files/ceph.conf.checksum/'


class TestChanged():
    """
    Testing 'changed' runner
    """

    @pytest.fixture(scope='class')
    def cfg(self):
        with patch('srv.modules.runners.changed.salt.client', new_callable=MagicMock):
            yield changed.Config
github SUSE / DeepSea / tests / unit / _modules / test_osd.py View on Github external
    @mock.patch('srv.salt._modules.osd.time')
    def test_readlink_samename(self, mock_time):
        osd.__salt__ = {}
        osd.__salt__['helper.run'] = mock.Mock()
        osd.__salt__['helper.run'].return_value = ('', '/dev/disk/by-id/wwn-0x12345-part1', '')
        result = osd.readlink("/dev/disk/by-id/wwn-0x12345-part1")

        assert result == "/dev/disk/by-id/wwn-0x12345-part1"



@pytest.mark.skip(reason="Low priority: skipped")
class TetstOSDState():
    pass

fs = fake_fs.FakeFilesystem()
f_glob = fake_glob.FakeGlobModule(fs)
f_os = fake_fs.FakeOsModule(fs)
f_open = fake_fs.FakeFileOpen(fs)

class TestOSDWeight():
    """
    Initial checks for the wait method.  Override the __init__ funciton to
    avoid the rados logic.  Set osd_id and settings directly.
    """

    @patch('builtins.open', new=f_open)
    @patch('srv.salt._modules.osd.OSDWeight.osd_df')
    def test_save_defaults(self, osd_df):
        """
        No files created with default values
        """
github dmsimard / ara-archive / ara / utils.py View on Github external
def playbook_treeview(playbook):
    """
    Creates a fake filesystem with playbook files and uses generate_tree() to
    recurse and return a JSON structure suitable for bootstrap-treeview.
    """
    fs = fake_filesystem.FakeFilesystem()
    mock_os = fake_filesystem.FakeOsModule(fs)

    files = models.File.query.filter(models.File.playbook_id.in_([playbook]))

    paths = {}
    for file in files:
        fs.CreateFile(file.path)
        paths[file.path] = file.id

    return jsonutils.dumps(generate_tree('/', paths, mock_os),
                           sort_keys=True,
                           indent=2)
github kobayashi / s3monkey / s3monkey / pyfakefs / fake_pathlib.py View on Github external
chmod = _wrap_strfunc(FakeFilesystem.chmod)

    if sys.version_info >= (3, 6):
        scandir = _wrap_strfunc(fake_scandir.scandir)

    if hasattr(os, "lchmod"):
        lchmod = _wrap_strfunc(lambda fs, path, mode: FakeFilesystem.chmod(
            fs, path, mode, follow_symlinks=False))
    else:
        def lchmod(self, pathobj, mode):
            """Raises not implemented for Windows systems."""
            raise NotImplementedError("lchmod() not available on this system")

    mkdir = _wrap_strfunc(FakeFilesystem.makedir)

    unlink = _wrap_strfunc(FakeFilesystem.remove)

    rmdir = _wrap_strfunc(FakeFilesystem.rmdir)

    rename = _wrap_binary_strfunc(FakeFilesystem.rename)

    replace = _wrap_binary_strfunc(lambda fs, old_path, new_path:
                                   FakeFilesystem.rename(
                                       fs, old_path, new_path, force_replace=True))

    symlink = _wrap_binary_strfunc_reverse(
        lambda fs, file_path, link_target, target_is_directory:
        FakeFilesystem.create_symlink(fs, file_path, link_target,
                                      create_missing_dirs=False))

    utime = _wrap_strfunc(FakeFilesystem.utime)