Skip to main content

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

Project description

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

Project details


Download files

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

Source Distribution

mock-file-tree-1.1.3.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

mock_file_tree-1.1.3-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file mock-file-tree-1.1.3.tar.gz.

File metadata

  • Download URL: mock-file-tree-1.1.3.tar.gz
  • Upload date:
  • Size: 6.3 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for mock-file-tree-1.1.3.tar.gz
Algorithm Hash digest
SHA256 c5927090a7cd9eab31efbbd30fc79781fb2f7532580271e37b5875f336ca6135
MD5 f425b2e7d773864258113657d7f11e3e
BLAKE2b-256 25f6dc3da7bb9d0776a1c8a784f232ee0226dac3cafd00ad9462d355da8514cb

See more details on using hashes here.

File details

Details for the file mock_file_tree-1.1.3-py3-none-any.whl.

File metadata

  • Download URL: mock_file_tree-1.1.3-py3-none-any.whl
  • Upload date:
  • Size: 7.9 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for mock_file_tree-1.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 8e3269c0cb5cd7048ec0478bcedc599b3bd95ef384c99930b00706ed1dac5fa1
MD5 34c41552a53dd812a2932d158264f497
BLAKE2b-256 a57a8334edd45eb935c4e3c210e52c3a50e458a13db8931695fb7f0273291683

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page