Skip to main content

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

Metadata

Release files for mock-file-tree 1.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mock-file-tree 1.1.3
File Size Uploaded
mock-file-tree-1.1.3.tar.gz 6.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for mock-file-tree 1.1.3
File Interpreter ABI Platform
mock_file_tree-1.1.3-py3-none-any.whl Python 3 none any Details

Total release size: 14.3 kB

Release files / mock-file-tree-1.1.3.tar.gz

Download URL mock-file-tree-1.1.3.tar.gz
Size 6.3 kB
Tags Source
SHA-256 checksum
How to use checksums
c5927090a7cd9eab31efbbd30fc79781fb2f7532580271e37b5875f336ca6135
BLAKE2b-256 checksum
How to use checksums
25f6dc3da7bb9d0776a1c8a784f232ee0226dac3cafd00ad9462d355da8514cb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.6

Release files / mock_file_tree-1.1.3-py3-none-any.whl

Download URL mock_file_tree-1.1.3-py3-none-any.whl
Size 7.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8e3269c0cb5cd7048ec0478bcedc599b3bd95ef384c99930b00706ed1dac5fa1
BLAKE2b-256 checksum
How to use checksums
a57a8334edd45eb935c4e3c210e52c3a50e458a13db8931695fb7f0273291683
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.6

Release history Release notifications | RSS feed

This release

1.1.3 This release

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page