Skip to content

Commit a704f11

Browse files
authored
refactor logging (connects #88 via #99)
refactor logging
1 parent 35efcfc commit a704f11

File tree

9 files changed

+211
-158
lines changed

9 files changed

+211
-158
lines changed

allure-behave/src/listener.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
from allure_commons.logger import AllureLogger
1+
from allure_commons import register
2+
from allure_commons.logger import AllureFileLogger
3+
from allure_commons.reporter import AllureReporter
24
from allure_commons.utils import uuid4
35
from allure_commons.utils import now
46
from allure_commons.types import LabelType, AttachmentType
@@ -19,7 +21,10 @@
1921

2022
class AllureListener(object):
2123
def __init__(self, result_dir):
22-
self.logger = AllureLogger(result_dir)
24+
self.logger = AllureReporter()
25+
file_logger = AllureFileLogger(result_dir)
26+
register(file_logger)
27+
2328
self.current_group_uuid = None
2429
self.current_before_uuid = None
2530
self.current_scenario_uuid = None

allure-pytest/src/listener.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from allure_commons.utils import md5
66
from allure_commons.utils import uuid4
77

8-
from allure_commons.logger import AllureLogger
8+
from allure_commons.reporter import AllureReporter
99

1010
from allure_commons.model2 import TestStepResult, TestResult, TestBeforeResult, TestAfterResult
1111
from allure_commons.model2 import TestResultContainer
@@ -23,7 +23,7 @@ class AllureListener(object):
2323

2424
def __init__(self, config):
2525
self.config = config
26-
self.allure_logger = AllureLogger(config.option.allure_report_dir)
26+
self.allure_logger = AllureReporter()
2727
self._cache = ItemCache()
2828

2929
@allure_commons.hookimpl

allure-pytest/src/plugin.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
import allure
66
import allure_commons
7+
78
from allure_commons.types import LabelType
9+
from allure_commons.logger import AllureFileLogger
10+
811
from allure_pytest.utils import allure_labels
912
from allure_pytest.helper import AllureTestHelper
1013
from allure_pytest.listener import AllureListener
@@ -89,12 +92,15 @@ def pytest_configure(config):
8992
report_dir = config.option.allure_report_dir
9093

9194
if report_dir:
95+
test_helper = AllureTestHelper(config)
96+
allure_commons.register(test_helper)
97+
9298
test_listener = AllureListener(config)
9399
config.pluginmanager.register(test_listener)
94-
95-
test_helper = AllureTestHelper(config)
96100
allure_commons.register(test_listener)
97-
allure_commons.register(test_helper)
101+
102+
file_logger = AllureFileLogger(report_dir)
103+
allure_commons.register(file_logger)
98104

99105

100106
def pytest_runtest_setup(item):

allure-python-commons-test/src/report.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,21 @@
7777

7878

7979
class AllureReport(object):
80-
def __init__(self, report_dir):
81-
self.report_dir = report_dir
82-
self.test_cases = [json.load(item) for item in self._report_items('*result.json')]
83-
self.test_containers = [json.load(item) for item in self._report_items('*container.json')]
84-
self.attachments = [item.read() for item in self._report_items('*attachment.*')]
85-
86-
def _report_items(self, glob):
87-
for _file in os.listdir(self.report_dir):
80+
def __init__(self, result):
81+
if isinstance(result, dict):
82+
self.test_cases = result['test_cases']
83+
self.test_containers = result['test_containers']
84+
self.attachments = result['attachments']
85+
else:
86+
self.test_cases = [json.load(item) for item in self._report_items(result, '*result.json')]
87+
self.test_containers = [json.load(item) for item in self._report_items(result, '*container.json')]
88+
self.attachments = [item.read() for item in self._report_items(result, '*attachment.*')]
89+
90+
@staticmethod
91+
def _report_items(report_dir, glob):
92+
for _file in os.listdir(report_dir):
8893
if fnmatch.fnmatch(_file, glob):
89-
with open(os.path.join(self.report_dir, _file)) as report_file:
94+
with open(os.path.join(report_dir, _file)) as report_file:
9095
yield report_file
9196

9297

allure-python-commons/src/_core.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55

66
_storage = threading.local()
77
_storage.plugin_manager = PluginManager('allure')
8-
_storage.plugin_manager.add_hookspecs(_hooks)
8+
_storage.plugin_manager.add_hookspecs(_hooks.AllureUserHooks)
9+
_storage.plugin_manager.add_hookspecs(_hooks.AllureDeveloperHooks)
10+
911

1012
plugin_manager = _storage.plugin_manager
1113
register = plugin_manager.register

allure-python-commons/src/_hooks.py

Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,51 +4,63 @@
44
hookimpl = HookimplMarker("allure")
55

66

7-
@hookspec
8-
def decorate_as_label(label_type, labels):
9-
""" label """
7+
class AllureUserHooks(object):
108

9+
@hookspec
10+
def decorate_as_label(self, label_type, labels):
11+
""" label """
1112

12-
@hookspec
13-
def add_label(label_type, labels):
14-
""" label """
13+
@hookspec
14+
def add_label(self, label_type, labels):
15+
""" label """
1516

17+
@hookspec
18+
def decorate_as_link(self, url, link_type, name):
19+
""" url """
1620

17-
@hookspec
18-
def decorate_as_link(url, link_type, name):
19-
""" url """
21+
@hookspec
22+
def add_link(self, url, link_type, name):
23+
""" url """
2024

25+
@hookspec
26+
def start_step(self, uuid, title, params):
27+
""" step """
2128

22-
@hookspec
23-
def add_link(url, link_type, name):
24-
""" url """
29+
@hookspec
30+
def stop_step(self, uuid, exc_type, exc_val, exc_tb):
31+
""" step """
2532

33+
@hookspec
34+
def attach_data(self, body, name, attachment_type, extension):
35+
""" attach data """
2636

27-
@hookspec
28-
def start_step(uuid, title, params):
29-
""" step """
37+
@hookspec
38+
def attach_file(self, source, name, attachment_type, extension):
39+
""" attach file """
3040

3141

32-
@hookspec
33-
def stop_step(uuid, exc_type, exc_val, exc_tb):
34-
""" step """
42+
class AllureDeveloperHooks(object):
3543

44+
@hookspec
45+
def start_fixture(self, parent_uuid, uuid, name):
46+
""" start fixture"""
3647

37-
@hookspec
38-
def attach_data(body, name, attachment_type, extension):
39-
""" attach data """
48+
@hookspec
49+
def stop_fixture(self, uuid, exc_type, exc_val, exc_tb):
50+
""" stop fixture """
4051

52+
@hookspec
53+
def report_result(self, result):
54+
""" reporting """
4155

42-
@hookspec
43-
def attach_file(source, name, attachment_type, extension):
44-
""" attach file """
56+
@hookspec
57+
def report_container(self, container):
58+
""" reporting """
4559

60+
@hookspec
61+
def report_attached_file(self, source, file_name):
62+
""" reporting """
4663

47-
@hookspec
48-
def start_fixture(parent_uuid, uuid, name):
49-
""" start fixture"""
50-
51-
52-
@hookspec
53-
def stop_fixture(uuid, exc_type, exc_val, exc_tb):
54-
""" stop fixture """
64+
@hookspec
65+
def report_attached_data(self, body, file_name):
66+
""" reporting """

allure-python-commons/src/logger.py

Lines changed: 29 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,107 +1,50 @@
1+
import io
12
import os
3+
import sys
4+
import json
5+
import uuid
26
import shutil
37
from six import text_type
4-
from collections import OrderedDict
8+
from attr import asdict
9+
from allure_commons import hookimpl
510

6-
from allure_commons.types import AttachmentType
7-
from allure_commons.model2 import ExecutableItem
8-
from allure_commons.model2 import Attachment, ATTACHMENT_PATTERN
9-
from allure_commons.utils import now
11+
INDENT = 4
1012

1113

12-
class AllureLogger(object):
14+
class AllureFileLogger(object):
15+
1316
def __init__(self, report_dir):
14-
self._items = OrderedDict()
1517
self._report_dir = report_dir
18+
1619
if not os.path.exists(report_dir):
1720
os.makedirs(report_dir)
1821

19-
def _update_item(self, uuid, **kwargs):
20-
item = self._items[uuid] if uuid else self._items[next(reversed(self._items))]
21-
for name, value in kwargs.items():
22-
attr = getattr(item, name)
23-
if isinstance(attr, list):
24-
attr.append(value)
22+
def _report_item(self, item):
23+
indent = INDENT if os.environ.get("ALLURE_INDENT_OUTPUT") else None
24+
filename = item.file_pattern.format(prefix=uuid.uuid4())
25+
data = asdict(item, filter=lambda attr, value: not (type(value) != bool and not bool(value)))
26+
with io.open(os.path.join(self._report_dir, filename), 'w', encoding='utf8') as json_file:
27+
if sys.version_info.major < 3:
28+
json_file.write(unicode(json.dumps(data, indent=indent, ensure_ascii=False, encoding='utf8')))
2529
else:
26-
setattr(item, name, value)
27-
28-
def get_item(self, uuid):
29-
return self._items.get(uuid)
30-
31-
def start_group(self, uuid, group):
32-
self._items[uuid] = group
33-
34-
def stop_group(self, uuid, **kwargs):
35-
self._update_item(uuid, **kwargs)
36-
group = self._items.pop(uuid)
37-
group.write(self._report_dir)
38-
39-
def update_group(self, uuid, **kwargs):
40-
self._update_item(uuid, **kwargs)
41-
42-
def start_before_fixture(self, parent_uuid, uuid, fixture):
43-
self._items.get(parent_uuid).befores.append(fixture)
44-
self._items[uuid] = fixture
45-
46-
def stop_before_fixture(self, uuid, **kwargs):
47-
self._update_item(uuid, **kwargs)
48-
self._items.pop(uuid)
49-
50-
def start_after_fixture(self, parent_uuid, uuid, fixture):
51-
self._items.get(parent_uuid).afters.append(fixture)
52-
self._items[uuid] = fixture
53-
54-
def stop_after_fixture(self, uuid, **kwargs):
55-
self._update_item(uuid, **kwargs)
56-
fixture = self._items.pop(uuid)
57-
fixture.stop = now()
30+
json.dump(data, json_file, indent=indent, ensure_ascii=False)
5831

59-
def schedule_test(self, uuid, test_case):
60-
self._items[uuid] = test_case
32+
@hookimpl
33+
def report_result(self, result):
34+
self._report_item(result)
6135

62-
def update_test(self, uuid, **kwargs):
63-
self._update_item(uuid, **kwargs)
36+
@hookimpl
37+
def report_container(self, container):
38+
self._report_item(container)
6439

65-
def close_test(self, uuid):
66-
test_case = self._items.pop(uuid)
67-
test_case.write(self._report_dir)
68-
69-
def start_step(self, parent_uuid, uuid, step):
70-
if not parent_uuid:
71-
for _uuid in reversed(self._items):
72-
if isinstance(self._items[_uuid], ExecutableItem):
73-
parent_uuid = _uuid
74-
break
75-
self._items[parent_uuid].steps.append(step)
76-
self._items[uuid] = step
77-
78-
def stop_step(self, uuid, **kwargs):
79-
self._update_item(uuid, **kwargs)
80-
self._items.pop(uuid)
81-
82-
def _attach(self, uuid, name=None, attachment_type=None, extension=None):
83-
mime_type = attachment_type
84-
extension = extension if extension else 'attach'
85-
86-
if type(attachment_type) is AttachmentType:
87-
extension = attachment_type.extension
88-
mime_type = attachment_type.mime_type
89-
90-
file_name = ATTACHMENT_PATTERN.format(prefix=uuid, ext=extension)
40+
@hookimpl
41+
def report_attached_file(self, source, file_name):
9142
destination = os.path.join(self._report_dir, file_name)
92-
attachment = Attachment(source=file_name, name=name, type=mime_type)
93-
last_uuid = next(reversed(self._items))
94-
self._items[last_uuid].attachments.append(attachment)
95-
96-
return file_name, destination
97-
98-
def attach_file(self, uuid, source, name=None, attachment_type=None, extension=None):
99-
file_name, destination = self._attach(uuid, name=name, attachment_type=attachment_type, extension=extension)
10043
shutil.copy2(source, destination)
10144

102-
def attach_data(self, uuid, body, name=None, attachment_type=None, extension=None):
103-
file_name, destination = self._attach(uuid, name=name, attachment_type=attachment_type, extension=extension)
104-
45+
@hookimpl
46+
def report_attached_data(self, body, file_name):
47+
destination = os.path.join(self._report_dir, file_name)
10548
with open(destination, 'wb') as attached_file:
10649
if isinstance(body, text_type):
10750
attached_file.write(body.encode('utf-8'))

0 commit comments

Comments
 (0)