forked from oamg/leapp-repository
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conftest.py
94 lines (83 loc) · 3.41 KB
/
conftest.py
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
92
93
94
import logging
import os
from leapp.repository.manager import RepositoryManager
from leapp.repository.scan import find_and_scan_repositories
from leapp.utils.repository import find_repository_basedir, get_repository_id
logger = logging.getLogger(__name__)
logging.getLogger("asyncio").setLevel(logging.INFO)
logging.getLogger("parso").setLevel(logging.INFO)
def _load_and_add_repo(manager, repo_path):
repo = find_and_scan_repositories(
repo_path,
include_locals=True
)
unloaded = set()
loaded = {r.repo_id for r in manager.repos}
if hasattr(repo, 'repos'):
for repo in repo.repos:
if not manager.repo_by_id(repo.repo_id):
manager.add_repo(repo)
unloaded.add(repo.repo_id)
else:
manager.add_repo(repo)
if not loaded:
manager.load(skip_actors_discovery=True)
else:
for repo_id in unloaded:
manager.repo_by_id(repo_id).load(skip_actors_discovery=True)
def pytest_collectstart(collector):
if collector.nodeid:
current_repo_basedir = find_repository_basedir(str(collector.fspath))
if not hasattr(collector.session, "leapp_repository"):
collector.session.leapp_repository = RepositoryManager()
collector.session.repo_base_dir = current_repo_basedir
_load_and_add_repo(collector.session.leapp_repository, current_repo_basedir)
else:
if not collector.session.leapp_repository.repo_by_id(
get_repository_id(current_repo_basedir)
):
_load_and_add_repo(collector.session.leapp_repository, current_repo_basedir)
# we're forcing the actor context switch only when traversing new
# actor
if "/actors/" in str(collector.fspath) and (
not hasattr(collector.session, "current_actor_path")
or collector.session.current_actor_path + os.sep
not in str(collector.fspath)
):
actor = None
for a in collector.session.leapp_repository.actors:
if a.full_path == collector.fspath.dirpath().dirname:
actor = a
break
if not actor:
logger.info("No actor found, exiting collection...")
return
# we need to tear down the context from the previous
# actor
try:
collector.session.current_actor_context.__exit__(
None, None, None
)
except AttributeError:
pass
else:
logger.info(
"Actor %r context teardown complete",
collector.session.current_actor.name,
)
logger.info("Injecting actor context for %r", actor.name)
collector.session.current_actor = actor
collector.session.current_actor_context = actor.injected_context()
collector.session.current_actor_context.__enter__()
collector.session.current_actor_path = (
collector.session.current_actor.full_path
)
logger.info("Actor %r context injected", actor.name)
def pytest_runtestloop(session):
try:
session.current_actor_context.__exit__(None, None, None)
logger.info(
"Actor %r context teardown complete", session.current_actor.name,
)
except AttributeError:
pass