# python-fixturify-project
[![Build status](https://github.com/scalvert/python-fixturify-project/workflows/build/badge.svg?branch=main&event=push)](https://github.com/scalvert/python-fixturify-project/actions?query=workflow%3Abuild)
[![Python Version](https://img.shields.io/pypi/pyversions/python-fixturify-project.svg)](https://pypi.org/project/python-fixturify-project/)
[![Dependencies Status](https://img.shields.io/badge/dependencies-up%20to%20date-brightgreen.svg)](https://github.com/scalvert/python-fixturify-project/pulls?utf8=%E2%9C%93&q=is%3Apr%20author%3Aapp%2Fdependabot)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
[![Security: bandit](https://img.shields.io/badge/security-bandit-green.svg)](https://github.com/PyCQA/bandit)
[![Pre-commit](https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white)](https://github.com/scalvert/python-fixturify-project/blob/master/.pre-commit-config.yaml)
[![Semantic Versions](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--versions-e10079.svg)](https://github.com/scalvert/python-fixturify-project/releases)
[![License](https://img.shields.io/github/license/scalvert/python-fixturify-project)](https://github.com/scalvert/python-fixturify-project/blob/master/LICENSE)
![Coverage Report](assets/images/coverage.svg)
> Dynamic fixture creation for your tests
_A Python port of [node-fixturify-project](https://github.com/stefanpenner/node-fixturify-project)_
## Installation
```bash
pip install -U python-fixturify-project
```
or install with `Poetry`
```bash
poetry add python-fixturify-project --dev
```
## Usage
`python-fixturify-project` is a Python package that provides a way to create dynamic fixtures for your tests. Fixtures are real directories and files, written to a temporary directory.
```python
from python_fixturify_project import Project
dir_json = {
"valid_file.txt": "some text",
"nested_dir": {
"valid_empty_file.txt": "",
"another_nested_empty_dir": {},
"another_nested_dir": {
"last_nested_empty_dir": {},
"final_text_file.txt": "some text",
},
},
}
# create a new project with the given directory structure
project = Project(files=dir_json)
# add new files to the project, merging with the existing directory structure
p.write({
"new_file.txt": "some text"
})
# read the actual contents on disc
actual_dir_json = p.read()
```
### Ignore Files
By default, the `read()` function will ignore all hidden files and directories in your Project file structure. This can be overridden by using the `ignore_patterns` constructor argument, which
takes a list of glob pattern strings. Any patterns provided to the `ignore_patterns` argument will be used in an _exclusive_ manner. For example:
```python
files = {
".git": {
"a_nested_dir": {}
},
".github": {
"ignore_me": {},
"do_not_ignore_me": {
"a_file": "some text"
}
},
"ignore_me": "some text",
"do_not_ignore_me": "some text",
}
# Default ignore_patterns is ["**/.git", "**/.git/**"]
project = Project(ignore_patterns=["**/.git", "**/.git/**", "**/ignore_me"])
project.write(files)
assert project.read() == {
'.github': {
'do_not_ignore_me': {
'a_file': 'some text',
},
},
'do_not_ignore_me': 'some text',
}
```
### Recommended Usage Patterns
`python-fixutrify-project` becomes even more useful when combining it with tools like `pytest` and something like [`syrupy`](https://github.com/tophat/syrupy), which uses `jest`-like snapshots for testing. The example below combines `python-fixturify-project` with `pytest`'s fixtures, and `syrupy` to create a snapshot test.
First, we define a fixture to setup and teardown our `Project` instance:
```python
# conftest.py
import pytest
from python_fixturify_project import Project
@pytest.fixture
def project():
project = Project()
yield project
project.dispose()
```
This fixture uses `pytest`'s `yield` fixture pattern, which allows us to run some code after the test has completed. In this case, we use the `dispose()` method to remove the temporary directory created by `python-fixturify-project`.
```python
from python_fixturify_project import Project
def test_mutating_project(project, snapshot):
project.write({
"a_file.txt": "some text",
"a_dir": {
"another_file.txt": "some text",
},
"path": {
"to": {
"a_file.py": "# some python code",
},
},
})
mutate_files_for_some_reason(p.base_dir)
# ensure mutations were as expected
assert project.files == snapshot
```
Or you can use the `project.get` method to get the path to a file in the project.
```python
from python_fixturify_project import Project
def test_mutating_project(snapshot):
project.write({
"a_file.txt": "some text",
"a_dir": {
"another_file.txt": "some text",
},
"path": {
"to": {
"a_file.py": "# some python code",
},
},
})
mutate_files_for_some_reason(p.base_dir)
# ensure mutations were as for single file
assert project.get('path/to/a_file.py') == snapshot(name='path/to/a_file.py')
```
## Skip Dispose (for debugging)
If you want to skip the `dispose()` call, you can set the `FIXTURIFY_SKIP_DISPOSE` environment variable to `1`.
```bash
FIXTURIFY_SKIP_DISPOSE=1 pytest
```
This can be useful if you want to inspect the contents of the temporary directory after the test has completed.
## 🛡 License
[![License](https://img.shields.io/github/license/scalvert/python-fixturify-project)](https://github.com/scalvert/python-fixturify-project/blob/master/LICENSE)
This project is licensed under the terms of the `MIT` license. See [LICENSE](https://github.com/scalvert/python-fixturify-project/blob/master/LICENSE) for more details.
Raw data
{
"_id": null,
"home_page": "https://github.com/scalvert/python-fixturify-project",
"name": "python-fixturify-project",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.8,<4.0",
"maintainer_email": "",
"keywords": "fixtures,dynamic fixtures,test fixtures",
"author": "python-fixturify-project",
"author_email": "steve.calvert@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/3d/f7/3cbe8027728d43e3f3dd952eab9b5c24160fc648a2df02dbb68589542148/python_fixturify_project-1.0.0.tar.gz",
"platform": null,
"description": "# python-fixturify-project\n\n[![Build status](https://github.com/scalvert/python-fixturify-project/workflows/build/badge.svg?branch=main&event=push)](https://github.com/scalvert/python-fixturify-project/actions?query=workflow%3Abuild)\n[![Python Version](https://img.shields.io/pypi/pyversions/python-fixturify-project.svg)](https://pypi.org/project/python-fixturify-project/)\n[![Dependencies Status](https://img.shields.io/badge/dependencies-up%20to%20date-brightgreen.svg)](https://github.com/scalvert/python-fixturify-project/pulls?utf8=%E2%9C%93&q=is%3Apr%20author%3Aapp%2Fdependabot)\n\n[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)\n[![Security: bandit](https://img.shields.io/badge/security-bandit-green.svg)](https://github.com/PyCQA/bandit)\n[![Pre-commit](https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white)](https://github.com/scalvert/python-fixturify-project/blob/master/.pre-commit-config.yaml)\n[![Semantic Versions](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--versions-e10079.svg)](https://github.com/scalvert/python-fixturify-project/releases)\n[![License](https://img.shields.io/github/license/scalvert/python-fixturify-project)](https://github.com/scalvert/python-fixturify-project/blob/master/LICENSE)\n![Coverage Report](assets/images/coverage.svg)\n\n> Dynamic fixture creation for your tests\n\n_A Python port of [node-fixturify-project](https://github.com/stefanpenner/node-fixturify-project)_\n\n## Installation\n\n```bash\npip install -U python-fixturify-project\n```\n\nor install with `Poetry`\n\n```bash\npoetry add python-fixturify-project --dev\n```\n\n## Usage\n\n`python-fixturify-project` is a Python package that provides a way to create dynamic fixtures for your tests. Fixtures are real directories and files, written to a temporary directory.\n\n```python\nfrom python_fixturify_project import Project\n\ndir_json = {\n \"valid_file.txt\": \"some text\",\n \"nested_dir\": {\n \"valid_empty_file.txt\": \"\",\n \"another_nested_empty_dir\": {},\n \"another_nested_dir\": {\n \"last_nested_empty_dir\": {},\n \"final_text_file.txt\": \"some text\",\n },\n },\n}\n\n# create a new project with the given directory structure\nproject = Project(files=dir_json)\n# add new files to the project, merging with the existing directory structure\np.write({\n \"new_file.txt\": \"some text\"\n})\n\n# read the actual contents on disc\nactual_dir_json = p.read()\n```\n\n### Ignore Files\n\nBy default, the `read()` function will ignore all hidden files and directories in your Project file structure. This can be overridden by using the `ignore_patterns` constructor argument, which\ntakes a list of glob pattern strings. Any patterns provided to the `ignore_patterns` argument will be used in an _exclusive_ manner. For example:\n\n```python\nfiles = {\n \".git\": {\n \"a_nested_dir\": {}\n },\n \".github\": {\n \"ignore_me\": {},\n \"do_not_ignore_me\": {\n \"a_file\": \"some text\"\n }\n },\n \"ignore_me\": \"some text\",\n \"do_not_ignore_me\": \"some text\",\n}\n\n# Default ignore_patterns is [\"**/.git\", \"**/.git/**\"]\nproject = Project(ignore_patterns=[\"**/.git\", \"**/.git/**\", \"**/ignore_me\"])\n\nproject.write(files)\n\nassert project.read() == {\n '.github': {\n 'do_not_ignore_me': {\n 'a_file': 'some text',\n },\n },\n 'do_not_ignore_me': 'some text',\n}\n```\n\n### Recommended Usage Patterns\n\n`python-fixutrify-project` becomes even more useful when combining it with tools like `pytest` and something like [`syrupy`](https://github.com/tophat/syrupy), which uses `jest`-like snapshots for testing. The example below combines `python-fixturify-project` with `pytest`'s fixtures, and `syrupy` to create a snapshot test.\n\nFirst, we define a fixture to setup and teardown our `Project` instance:\n\n```python\n# conftest.py\nimport pytest\n\nfrom python_fixturify_project import Project\n\n\n@pytest.fixture\ndef project():\n project = Project()\n\n yield project\n\n project.dispose()\n```\n\nThis fixture uses `pytest`'s `yield` fixture pattern, which allows us to run some code after the test has completed. In this case, we use the `dispose()` method to remove the temporary directory created by `python-fixturify-project`.\n\n```python\nfrom python_fixturify_project import Project\n\n\ndef test_mutating_project(project, snapshot):\n project.write({\n \"a_file.txt\": \"some text\",\n \"a_dir\": {\n \"another_file.txt\": \"some text\",\n },\n \"path\": {\n \"to\": {\n \"a_file.py\": \"# some python code\",\n },\n },\n })\n\n mutate_files_for_some_reason(p.base_dir)\n\n # ensure mutations were as expected\n assert project.files == snapshot\n```\n\nOr you can use the `project.get` method to get the path to a file in the project.\n\n```python\nfrom python_fixturify_project import Project\n\ndef test_mutating_project(snapshot):\n project.write({\n \"a_file.txt\": \"some text\",\n \"a_dir\": {\n \"another_file.txt\": \"some text\",\n },\n \"path\": {\n \"to\": {\n \"a_file.py\": \"# some python code\",\n },\n },\n })\n\n mutate_files_for_some_reason(p.base_dir)\n\n # ensure mutations were as for single file\n assert project.get('path/to/a_file.py') == snapshot(name='path/to/a_file.py')\n```\n\n## Skip Dispose (for debugging)\n\nIf you want to skip the `dispose()` call, you can set the `FIXTURIFY_SKIP_DISPOSE` environment variable to `1`.\n\n```bash\nFIXTURIFY_SKIP_DISPOSE=1 pytest\n```\n\nThis can be useful if you want to inspect the contents of the temporary directory after the test has completed.\n\n## \ud83d\udee1 License\n\n[![License](https://img.shields.io/github/license/scalvert/python-fixturify-project)](https://github.com/scalvert/python-fixturify-project/blob/master/LICENSE)\n\nThis project is licensed under the terms of the `MIT` license. See [LICENSE](https://github.com/scalvert/python-fixturify-project/blob/master/LICENSE) for more details.\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Dynamic fixture creation for your tests",
"version": "1.0.0",
"split_keywords": [
"fixtures",
"dynamic fixtures",
"test fixtures"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "571c9b7bc1c968d73de9c4ab69af847c348b19d38b90e4d350e1408e85dd70b2",
"md5": "deca26b4b96d80e9ef01b14a0cbdaf1a",
"sha256": "204475d118c3ebe970f996b058224f471f2af6947915ba89b253298d4e0bcc9f"
},
"downloads": -1,
"filename": "python_fixturify_project-1.0.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "deca26b4b96d80e9ef01b14a0cbdaf1a",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8,<4.0",
"size": 7806,
"upload_time": "2023-01-14T19:53:37",
"upload_time_iso_8601": "2023-01-14T19:53:37.203248Z",
"url": "https://files.pythonhosted.org/packages/57/1c/9b7bc1c968d73de9c4ab69af847c348b19d38b90e4d350e1408e85dd70b2/python_fixturify_project-1.0.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "3df73cbe8027728d43e3f3dd952eab9b5c24160fc648a2df02dbb68589542148",
"md5": "f6967b94752435bbf8d0b375d2928865",
"sha256": "9898a4316957e269ba7666f221bf248b0b096a24f4b2d23a4ba4dd2f7d181406"
},
"downloads": -1,
"filename": "python_fixturify_project-1.0.0.tar.gz",
"has_sig": false,
"md5_digest": "f6967b94752435bbf8d0b375d2928865",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8,<4.0",
"size": 8682,
"upload_time": "2023-01-14T19:53:38",
"upload_time_iso_8601": "2023-01-14T19:53:38.636315Z",
"url": "https://files.pythonhosted.org/packages/3d/f7/3cbe8027728d43e3f3dd952eab9b5c24160fc648a2df02dbb68589542148/python_fixturify_project-1.0.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-01-14 19:53:38",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "scalvert",
"github_project": "python-fixturify-project",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [],
"lcname": "python-fixturify-project"
}