forked from allure-framework/allure-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add file attachment_worker.py with AttachmentWorker class Now PytestBDDListener use AttachmentWorker for deleting duplicate of attachments
- Loading branch information
Denis Bautin
committed
Jan 9, 2021
1 parent
43a6419
commit c8b6e0d
Showing
2 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import os.path | ||
import os | ||
|
||
|
||
class AttachmentWorker: | ||
|
||
def __init__(self, test_result, item): | ||
self.test_result = test_result | ||
self.attachments_dir = AttachmentWorker.get_path_to_attachments(item) | ||
|
||
@staticmethod | ||
def get_path_to_attachments(item): | ||
allure_dir = os.path.normpath( | ||
item.config.invocation_params.args[0].split('=')[1]) | ||
if os.path.isabs(allure_dir): | ||
return allure_dir | ||
else: | ||
project_dir = str(item.config.invocation_params.dir) | ||
return os.path.join(project_dir, allure_dir.lstrip("\\")) | ||
|
||
def delete_duplicates(self): | ||
if len(self.test_result.attachments) == 0: | ||
return | ||
|
||
for step in self.test_result.steps: | ||
for attach in step.attachments: | ||
to_delete = self._find_duplicate(attach) | ||
if to_delete is not None: | ||
self.test_result.attachments.remove(to_delete) | ||
os.remove(os.path.join(self.attachments_dir, to_delete.source)) | ||
|
||
def _find_duplicate(self, attachment_from_step): | ||
for attachment in self.test_result.attachments: | ||
if self._are_attachments_equal(attachment, attachment_from_step): | ||
return attachment | ||
|
||
return None | ||
|
||
def _are_attachments_equal(self, first, second): | ||
first_file = open(os.path.join(self.attachments_dir, first.source), 'br') | ||
first_content = first_file.read() | ||
first_file.close() | ||
|
||
second_file = open(os.path.join(self.attachments_dir, second.source), 'br') | ||
second_content = second_file.read() | ||
second_file.close() | ||
|
||
return \ | ||
first.name == second.name and \ | ||
first.type == second.type and \ | ||
first_content == second_content |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters