File: conftest.py

package info (click to toggle)
python-get-version 2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 124 kB
  • sloc: python: 236; makefile: 3
file content (37 lines) | stat: -rw-r--r-- 1,021 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from pathlib import Path
from tempfile import TemporaryDirectory
from typing import Union, Callable, Dict, List

import pytest


Desc = Dict[str, Union["Desc", str, bytes]]


@pytest.fixture(scope="function")
def temp_tree() -> Callable[[Desc], Path]:
    def mk_tree(desc: Desc, parent: Path):
        parent.mkdir(parents=True, exist_ok=True)
        for name, content in desc.items():
            path = parent / name
            if isinstance(content, str):
                path.write_text(content)
            elif isinstance(content, bytes):
                path.write_bytes(content)
            else:
                assert isinstance(content, dict)
                mk_tree(content, path)

    dirs = []  # type: List[TemporaryDirectory]

    def get_temptree(desc: Desc) -> Path:
        d = TemporaryDirectory()
        mk_tree(desc, Path(d.name))
        dirs.append(d)
        return Path(d.name)

    yield get_temptree

    # Cleanup after the test function is through
    for d in dirs:
        d.cleanup()