|
| 1 | +import io |
1 | 2 | import os |
| 3 | +import sys |
| 4 | +import json |
| 5 | +import uuid |
2 | 6 | import shutil |
3 | 7 | from six import text_type |
4 | | -from collections import OrderedDict |
| 8 | +from attr import asdict |
| 9 | +from allure_commons import hookimpl |
5 | 10 |
|
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 |
10 | 12 |
|
11 | 13 |
|
12 | | -class AllureLogger(object): |
| 14 | +class AllureFileLogger(object): |
| 15 | + |
13 | 16 | def __init__(self, report_dir): |
14 | | - self._items = OrderedDict() |
15 | 17 | self._report_dir = report_dir |
| 18 | + |
16 | 19 | if not os.path.exists(report_dir): |
17 | 20 | os.makedirs(report_dir) |
18 | 21 |
|
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'))) |
25 | 29 | 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) |
58 | 31 |
|
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) |
61 | 35 |
|
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) |
64 | 39 |
|
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): |
91 | 42 | 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) |
100 | 43 | shutil.copy2(source, destination) |
101 | 44 |
|
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) |
105 | 48 | with open(destination, 'wb') as attached_file: |
106 | 49 | if isinstance(body, text_type): |
107 | 50 | attached_file.write(body.encode('utf-8')) |
|
0 commit comments