Skip to main content

Helper to create fake filesystem and quick capture its state (or state of a real one).

Project description

FsForge - file system tests helper

fsforge - is a toolset defining fake or real filesystem layout.

Gives among others a functionality:

  • for creating artificial fs using pyfakefs.
  • for taking a snapshot of either real of faked file system.

In short it is some kind of syntax’ extension to pyfakefs and is intended to use with pytest framework. Allows for absolute transparency in fs operations, so that any kind of tests: untit, functional or end-to-end can be performed in memory - instead of real hard disc operations (SSDs can breathe and relax), without any headache nor enormous setup nor teardown.

The main difference against bare pyfakefs is that fsforge uses nice and clean dict literals instead of lists of paths. It also allows for reverse operation - to create the same kind of nested dictionary structure defining given fs with just single function call.

Such a result is immediately ready to make assertions on it.

Works with python 2.7, 3.4, 3.5, 3.6, 3.7, pypy and pypy3. Created with pyfakefs==3.4.3.


Capture real or faked fs snapshot.

Let’s use following structure originated in /tmp/ex for all further examples:

bash>$ tree /tmp/ex
├── dir_a
│   ├── sub_empty_dir
│   ├── sub_dir_with_a_file
│   │   ├── app_dump.json
│   │   └── file_1.txt
│   └── file_2.txt
├── dir_b
│   ├── special_file.txt
│   └── file_4.bin
├── empty_dir
└── special_file_2.txt

We can collect a snapshot of this layout with:

import pprint
from fsforge import take_fs_snapshot

tree = take_fs_snapshot('/tmp/ex')

will output such a tree:

    'dir_a': {
        'sub_empty_dir': {},
        'sub_dir_with_a_file': {
            'app_dump.json': None,
            'file_1.txt': None
        'file_2.txt': None,
    'dir_b': {
        'app_dump.json': None,
        'special_file.txt': None,
        'file_4.bin': None
    'empty_dir': {},
    'special_file_2.txt`: None


The resulting tree is a pure dict. fsforge uses magic relation and similarity of directory to a python’s dictionary.

Directories are recognized by being dict instances. Any other value type in the tree is treat as a file. fsforge distinguishes only dict (as directories) and non dict (files) while traversing the tree.

Create forged file system

Now fsforge can use such kind of tree to perform needed pyfakefs’ calls to recreate the structure in memory for some pytest tests:

import os

from fsforge import create_fs

def test_that(fs):

    create_fs(fs, tree, "/tmp/ex"):

    # everything is now set up:
    assert os.path.isdir("/tmp/ex/dir_a/sub_dir_with_a_file")
    assert os.path.isfile("/tmp/ex/dir_a/file_2.txt")

In the code above:

  • fs is a fixture automatically accessible in tests as soon as you have pyfakefs package installed. It can also be a `FakeFilesyst

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for fsforge, version 0.3.7
Filename, size File type Python version Upload date Hashes
Filename, size fsforge-0.3.7-py2.py3-none-any.whl (12.0 kB) File type Wheel Python version py2.py3 Upload date Hashes View
Filename, size fsforge-0.3.7-py3-none-any.whl (12.0 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size fsforge-0.3.7.tar.gz (8.5 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page