Name | tdir JSON |
Version |
1.8.2
JSON |
| download |
home_page | https://github.com/rec/tdir |
Summary | 🗃 Create and fill a temporary directory 🗃 |
upload_time | 2024-01-25 10:42:28 |
maintainer | |
docs_url | None |
author | Tom Ritchford |
requires_python | >=3.8 |
license | MIT |
keywords |
|
VCS |
 |
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
|
coveralls test coverage |
No coveralls.
|
# 🗃 tdir - create and fill a temporary directory 🗃
Run code inside a temporary directory filled with zero or more files.
Very convenient for writing tests: you can decorate individual tests or a whole
test suite.
`tdir()` runs code in a temporary directory pre-filled with files: it can
either be used as a context manager, or a decorator for functions or classes.
`tdir.fill()` is a tiny function that recursively fills a directory.
## Example: as a context manager
from pathlib import Path
import tdir
cwd = Path.cwd()
# Simplest invocation.
with tdir():
# Do a lot of things in a temporary directory
# Everything is gone!
# With a single file
with tdir('hello') as td:
# The file `hello` is there
assert Path('hello').read_text() = 'hello\n'
# We're in a temporary directory
assert td == Path.cwd()
assert td != cwd
# Write some other file
Path('junk.txt').write_text('hello, world\n')
# The temporary directory and the files are gone
assert not td.exists()
assert cwd == Path.cwd()
# A more complex example:
#
with tdir(
'one.txt',
three='some information',
four=Path('existing/file'), # Copy a file into the tempdir
sub1={
'file.txt': 'blank lines\n\n\n\n',
'sub2': [
'a', 'b', 'c'
]
},
):
assert Path('one.txt').exists()
assert Path('four').read_text() == Path('/existing/file').read_text()
assert Path('sub1/sub2/a').exists()
# All files gone!
## Example: as a decorator
from pathlib import Path
import tdir
import unittest
@tdir
def my_function():
pass # my_function() always operates in a temporary directory
# Decorate a TestCase so each test runs in a new temporary directory
# with two files
@tdir('a', foo='bar')
class MyTest(unittest.TestCast):
def test_something(self):
assert Path('a').read_text() = 'a\n'
def test_something_else(self):
assert Path('foo').read_text() = 'bar\n'
class MyTest2(unittest.TestCast):
# Decorate just one test in a unitttest
@tdir(foo='bar', baz=bytes(range(4))) # binary files are possible
def test_something(self):
assert Path('foo').read_text() = 'bar\n'
assert Path('baz').read_bytes() = bytes(range(4)))
# Run test in an empty temporary directory
@tdir
def test_something_else(self):
assert not Path('a').exists()
assert Path().absolute() != self.ORIGINAL_PATH
ORIGINAL_PATH = Path().absolute()
### [API Documentation](https://rec.github.io/tdir#tdir--api-documentation)
Raw data
{
"_id": null,
"home_page": "https://github.com/rec/tdir",
"name": "tdir",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.8",
"maintainer_email": "",
"keywords": "",
"author": "Tom Ritchford",
"author_email": "tom@swirly.com",
"download_url": "https://files.pythonhosted.org/packages/17/07/3fd96b8a9fa51601214d16bb0c8645a8835112d44306a76fa49999923ea9/tdir-1.8.2.tar.gz",
"platform": null,
"description": "# \ud83d\uddc3 tdir - create and fill a temporary directory \ud83d\uddc3\n\nRun code inside a temporary directory filled with zero or more files.\n\nVery convenient for writing tests: you can decorate individual tests or a whole\ntest suite.\n\n`tdir()` runs code in a temporary directory pre-filled with files: it can\neither be used as a context manager, or a decorator for functions or classes.\n\n`tdir.fill()` is a tiny function that recursively fills a directory.\n\n## Example: as a context manager\n\n from pathlib import Path\n import tdir\n\n cwd = Path.cwd()\n\n # Simplest invocation.\n\n with tdir():\n # Do a lot of things in a temporary directory\n\n # Everything is gone!\n\n # With a single file\n with tdir('hello') as td:\n # The file `hello` is there\n assert Path('hello').read_text() = 'hello\\n'\n\n # We're in a temporary directory\n assert td == Path.cwd()\n assert td != cwd\n\n # Write some other file\n Path('junk.txt').write_text('hello, world\\n')\n\n # The temporary directory and the files are gone\n assert not td.exists()\n assert cwd == Path.cwd()\n\n # A more complex example:\n #\n with tdir(\n 'one.txt',\n three='some information',\n four=Path('existing/file'), # Copy a file into the tempdir\n sub1={\n 'file.txt': 'blank lines\\n\\n\\n\\n',\n 'sub2': [\n 'a', 'b', 'c'\n ]\n },\n ):\n assert Path('one.txt').exists()\n assert Path('four').read_text() == Path('/existing/file').read_text()\n assert Path('sub1/sub2/a').exists()\n\n # All files gone!\n\n## Example: as a decorator\n\n from pathlib import Path\n import tdir\n import unittest\n\n @tdir\n def my_function():\n pass # my_function() always operates in a temporary directory\n\n\n # Decorate a TestCase so each test runs in a new temporary directory\n # with two files\n @tdir('a', foo='bar')\n class MyTest(unittest.TestCast):\n def test_something(self):\n assert Path('a').read_text() = 'a\\n'\n\n def test_something_else(self):\n assert Path('foo').read_text() = 'bar\\n'\n\n\n class MyTest2(unittest.TestCast):\n # Decorate just one test in a unitttest\n @tdir(foo='bar', baz=bytes(range(4))) # binary files are possible\n def test_something(self):\n assert Path('foo').read_text() = 'bar\\n'\n assert Path('baz').read_bytes() = bytes(range(4)))\n\n # Run test in an empty temporary directory\n @tdir\n def test_something_else(self):\n assert not Path('a').exists()\n assert Path().absolute() != self.ORIGINAL_PATH\n\n ORIGINAL_PATH = Path().absolute()\n\n\n### [API Documentation](https://rec.github.io/tdir#tdir--api-documentation)\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "\ud83d\uddc3 Create and fill a temporary directory \ud83d\uddc3",
"version": "1.8.2",
"project_urls": {
"Documentation": "https://rec.github.io/tdir",
"Homepage": "https://github.com/rec/tdir",
"Repository": "https://github.com/rec/tdir"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "f31e31af55a256a6b3e2bccde9044fadede081d1b96e802095b7ae576c4aefbe",
"md5": "846dfb1558eb4c36d268133eb4e21b41",
"sha256": "6f8cd459b6a10470515aff70219caeea740180c0cfa566e38cceee82bfc14c50"
},
"downloads": -1,
"filename": "tdir-1.8.2-py3-none-any.whl",
"has_sig": false,
"md5_digest": "846dfb1558eb4c36d268133eb4e21b41",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8",
"size": 5998,
"upload_time": "2024-01-25T10:42:25",
"upload_time_iso_8601": "2024-01-25T10:42:25.761949Z",
"url": "https://files.pythonhosted.org/packages/f3/1e/31af55a256a6b3e2bccde9044fadede081d1b96e802095b7ae576c4aefbe/tdir-1.8.2-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "17073fd96b8a9fa51601214d16bb0c8645a8835112d44306a76fa49999923ea9",
"md5": "348501cf745691b3672a2681c0ab5ee3",
"sha256": "cc0cbbb8f2495efe024fc841140e25f1094313de1b29002f65eb0ade3ce7ada0"
},
"downloads": -1,
"filename": "tdir-1.8.2.tar.gz",
"has_sig": false,
"md5_digest": "348501cf745691b3672a2681c0ab5ee3",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8",
"size": 4900,
"upload_time": "2024-01-25T10:42:28",
"upload_time_iso_8601": "2024-01-25T10:42:28.316362Z",
"url": "https://files.pythonhosted.org/packages/17/07/3fd96b8a9fa51601214d16bb0c8645a8835112d44306a76fa49999923ea9/tdir-1.8.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-01-25 10:42:28",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "rec",
"github_project": "tdir",
"travis_ci": true,
"coveralls": false,
"github_actions": true,
"tox": true,
"lcname": "tdir"
}