aboutsummaryrefslogtreecommitdiff
blob: 76bdaa3819123d9708d3e48a82a9e3dd8f1cf660 (plain)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env python
# Copyright 2006-2023 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

import grp
import os
import os.path as osp
import pwd
import signal
import tempfile
import shutil
import sys

import pytest

import portage
from portage.util._eventloop.global_event_loop import global_event_loop
from portage.const import PORTAGE_BIN_PATH


def debug_signal(signum, frame):
    import pdb

    pdb.set_trace()


signal.signal(signal.SIGUSR1, debug_signal)


@pytest.fixture(autouse=True, scope="session")
def prepare_environment():
    # Pretend that the current user's uid/gid are the 'portage' uid/gid,
    # so things go smoothly regardless of the current user and global
    # user/group configuration.
    os.environ["PORTAGE_USERNAME"] = pwd.getpwuid(os.getuid()).pw_name
    os.environ["PORTAGE_GRPNAME"] = grp.getgrgid(os.getgid()).gr_name

    # Insert our parent dir so we can do shiny import "tests"
    # This line courtesy of Marienz and Pkgcore ;)
    sys.path.insert(0, osp.dirname(osp.dirname(osp.dirname(osp.realpath(__file__)))))

    portage._internal_caller = True

    # Ensure that we don't instantiate portage.settings, so that tests should
    # work the same regardless of global configuration file state/existence.
    portage._disable_legacy_globals()

    if portage.util.no_color(os.environ):
        portage.output.nocolor()

    # import portage.tests as tests

    path = os.environ.get("PATH", "").split(":")
    path = [x for x in path if x]

    insert_bin_path = True
    try:
        insert_bin_path = not path or not os.path.samefile(path[0], PORTAGE_BIN_PATH)
    except OSError:
        pass

    if insert_bin_path:
        path.insert(0, PORTAGE_BIN_PATH)
        os.environ["PATH"] = ":".join(path)

    try:
        # Copy GPG test keys to temporary directory
        gpg_path = tempfile.mkdtemp(prefix="gpg_")

        shutil.copytree(
            os.path.join(os.path.dirname(os.path.realpath(__file__)), ".gnupg"),
            gpg_path,
            dirs_exist_ok=True,
        )

        os.chmod(gpg_path, 0o700)
        os.environ["PORTAGE_GNUPGHOME"] = gpg_path

        yield

    finally:
        global_event_loop().close()
        shutil.rmtree(gpg_path, ignore_errors=True)


# if __name__ == "__main__":
#     try:
#         sys.exit(tests.main())
#     finally:
#         global_event_loop().close()
#         shutil.rmtree(gpg_path, ignore_errors=True)