File: conftest.py

package info (click to toggle)
duplicity 3.0.5.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,260 kB
  • sloc: python: 25,089; sh: 934; ansic: 392; makefile: 83
file content (65 lines) | stat: -rw-r--r-- 1,733 bytes parent folder | download | duplicates (2)
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4; encoding:utf-8 -*-
#

import io
import os
import sys

import pytest

from testing import _runtest_dir


@pytest.fixture(scope="function")
def redirect_stdin():
    """
    GPG requires stdin to be open and have real file descriptor, which interferes with pytest's capture facility.
    Work around this by redirecting /dev/null to stdin temporarily.

    Activate this fixture on unittest test methods and classes by means of:
    @pytest.mark.usefixtures("redirect_stdin").
    """
    try:
        targetfd_save = os.dup(0)
        stdin_save = sys.stdin

        nullfile = open(os.devnull, "r")
        sys.stdin = nullfile
        os.dup2(nullfile.fileno(), 0)
        yield
    finally:
        os.dup2(targetfd_save, 0)  # pylint: disable=used-before-assignment
        sys.stdin = stdin_save  # pylint: disable=used-before-assignment
        os.close(targetfd_save)
        nullfile.close()  # pylint: disable=used-before-assignment


@pytest.fixture(scope="function")
def fake_input():
    orig_stdin = sys.stdin
    sys.stdin = io.StringIO("yes\n")
    yield
    sys.stdin = orig_stdin


@pytest.hookimpl()
def pytest_sessionfinish(session, exitstatus):
    """
    Called after whole test run finished, right before
    returning the exit status to the system.  Since tests
    kill duplicity and cause errors intentionally this is
    necessary to keep a clean test system.
    """
    cleanup = [
        "backup-metadata",
        "duplicity-*-tempdir",
        "duptest",
        "foo",
        "full",
        "inc",
        "log.txt",
        "target_url",
        "testbackend.log",
    ]
    for fn in cleanup:
        os.system(f"rm -rf {_runtest_dir}/{fn}")