Skip to content

JoelLefkowitz/mock-file-tree

Repository files navigation

Mock file tree

A simple interface to mock the os module with a virtual file tree.

Status

Source Shields
Project release license lines languages
Health codacy readthedocs github_review codacy_coverage
Repository issues issues_closed pulls pulls_closed
Publishers pypi python_versions pypi_downloads
Activity contributors monthly_commits last_commit

Installing

pip install mock-file-tree

Usage

Create a virtual file tree:

from mock_file_tree import FileTree

tree = FileTree.from_paths("file1", "dir1/file2")

Now we can import and overwrite the os module. This allows us to make all file interactions use our virtual tree.

import os
from mock_file_tree import MockFileTree

with MockFileTree(os, tree):
     os.listdir(".")

['file1', 'dir1']

Similarly:

os.listdir("dir1")

['file2']

And so on:

os.path.isfile("dir1")

False

Motivation

Mocking the file system is a typical task when writing unit tests. It is necessary to have a simple interface to declare paths to mock. It is not trivial to implement and so there has long been a need for an open source package to provide the functionality.

Advanced

To mock file system without using a context manager:

mock = MockFileTree(os, tree)
mock.apply()

And to restore the os module:

mock.restore()

We would usually call the action of replacing methods in the os module as stubbing. However, the standard library refers to such tasks as mocking and so this package conforms to that naming scheme.

You may want to replace the os module with the stubbed methods only and explicitly restrict access to any un-stubbed methods that interact with the file system (analogous to replacing the os module with a fake). This can be achieved by setting the safe parameter:

with MockFileTree(os, tree, safe=True):
     os.remove("file1")

... NotImplementedError

To cover all file system interactions the builtin 'open' function should be unset too.

Roadmap

In models.unsupported there is a list of methods that interact with the file system that do not yet have mock implementations. Most notable are the methods that add and remove files from the file tree such as 'mkdir' and 'remove'.

Tests

To run unit tests and generate a coverage report:

grunt test

Documentation

This repository's documentation is hosted on readthedocs.

To use quickdocs to generate a sphinx documentation configuration:

grunt docs

Tooling

To run linters:

grunt lint

To run formatters:

grunt format

Continuous integration

This repository uses github actions to lint and test each commit. Formatting tasks and writing/generating documentation must be done before committing new code.

Versioning

This repository adheres to semantic versioning standards. For more information on semantic versioning visit SemVer.

Bump2version is used to version and tag changes. For example:

bump2version patch

Changelog

Please read this repository's CHANGELOG for details on changes that have been made.

Contributing

Please read this repository's guidelines on CONTRIBUTING for details on our code of conduct and the process for submitting pull requests.

Contributors

Buy Me A Coffee

Remarks

Lots of love to the open source community!

Be kind

About

A simple interface to mock the os module with a virtual file tree.

Resources

License

Stars

Watchers

Forks

Packages

No packages published