Skip to main content

🗃 Create and fill a temporary directory 🗃

Project description

🗃 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

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

tdir-1.9.0.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tdir-1.9.0-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file tdir-1.9.0.tar.gz.

File metadata

  • Download URL: tdir-1.9.0.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.28 {"installer":{"name":"uv","version":"0.9.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for tdir-1.9.0.tar.gz
Algorithm Hash digest
SHA256 c420f23c4681893015aba4b628949379b1bb7f149f9ad4eab9e8e9cbf6e97d1e
MD5 b7a385a0dfbef568c92262cf37d1cef1
BLAKE2b-256 a6dbddacce8a1e807c9abd070e4ce40ce7f7754bec2a3ec50a79c43cd9d560f1

See more details on using hashes here.

File details

Details for the file tdir-1.9.0-py3-none-any.whl.

File metadata

  • Download URL: tdir-1.9.0-py3-none-any.whl
  • Upload date:
  • Size: 5.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.28 {"installer":{"name":"uv","version":"0.9.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for tdir-1.9.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0042f68c2d306bba47b32918dbbfd884cbc95e016a70d4e7f06454e7cd8211df
MD5 b678fca8860d77d064dda2a5a9d2a3e0
BLAKE2b-256 039c68d13a89c3d91fc54ad074ec28296eb39c7b1e46b09f7b157f7eb0fd4b88

See more details on using hashes here.

Supported by

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