Skip to content

Commit 46758c0

Browse files
authored
Move file encoding (#7)
* remove b64 of file from xml creation * update b64 attribute name and check for data * store data as gzip * move file annotation out of init * update link annotation api
1 parent 245011f commit 46758c0

File tree

2 files changed

+57
-23
lines changed

2 files changed

+57
-23
lines changed

python_testspace_xml/testspace_xml.py

Lines changed: 54 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import print_function
22
import base64
33
import gzip
4+
import re
45
import os
56
import io
67
from io import BytesIO
@@ -33,44 +34,62 @@ def __init__(self, name, comment):
3334

3435

3536
class Annotation:
36-
def __init__(self, name='unknown', level='info', description='',
37-
file_path=None, mime_type='text/plain'):
37+
def __init__(self, name='unknown', level='info', description=''):
3838
self.name = name
3939
self.level = level
4040
self.description = description
41-
self.mimeType = mime_type
42-
self.file_path = file_path
41+
self.mimeType = None
42+
self.file_path = None
43+
self.link_file = False
44+
self.gzip_data = None
4345
self.comments = []
4446

4547
def add_comment(self, name, comment):
4648
comment = AnnotationComment(name, comment)
4749
self.comments.append(comment)
4850

51+
def add_file_annotation(self, file_path=None, mime_type='text/plain'):
52+
self.file_path = file_path
53+
self.mimeType = mime_type
54+
if file_path is not None:
55+
if not os.path.isfile(self.file_path):
56+
ta = Annotation(self.file_path, level='error')
57+
ta.description = 'File: ' + self.file_path + ' not found.'
58+
self.comments.append(ta)
59+
else:
60+
with io.open(self.file_path, 'rb') as inFile:
61+
out = BytesIO()
62+
with gzip.GzipFile(fileobj=out, mode="wb") as f:
63+
f.writelines(inFile)
64+
f.close()
65+
self.gzip_data = out.getvalue()
66+
67+
def add_link_annotation(self, file_path=None):
68+
self.link_file = True
69+
match_path = re.search(r'\\\w', file_path)
70+
if match_path and file_path:
71+
self.file_path = "file://" + file_path.replace('\\', '/')
72+
else:
73+
ta = Annotation(self.file_path, level='error')
74+
ta.description = 'Invalid network file path given:' + file_path
75+
self.comments.append(ta)
76+
4977
def write_xml(self, parent_element, dom):
5078
annotation = dom.createElement("annotation")
5179
annotation.setAttribute("description", self.description)
5280
annotation.setAttribute("level", self.level)
5381
annotation.setAttribute("name", self.name)
5482

5583
if self.file_path is not None:
56-
if not os.path.isfile(self.file_path):
57-
ta = Annotation(self.file_path, level='error')
58-
ta.description = 'File: ' + self.file_path + ' not found.'
59-
for com in self.comments:
60-
ta.comments.append(com)
61-
ta.write_xml(parent_element, dom)
62-
return
84+
if self.link_file:
85+
annotation.setAttribute("link_file", "true")
86+
annotation.setAttribute("file", self.file_path)
6387
else:
6488
annotation.setAttribute("link_file", "false")
65-
annotation.setAttribute("file", "file://" + self.file_path)
89+
annotation.setAttribute("file", self.file_path)
6690
annotation.setAttribute("mime_type", self.mimeType)
67-
with io.open(self.file_path, 'rb') as inFile:
68-
out = BytesIO()
69-
with gzip.GzipFile(fileobj=out, mode="wb") as f:
70-
f.writelines(inFile)
71-
f.close()
72-
gzip_data = out.getvalue()
73-
b64_data = base64.b64encode(gzip_data)
91+
if self.gzip_data is not None:
92+
b64_data = base64.b64encode(self.gzip_data)
7493
b64_data_string = b64_data.decode()
7594
cdata = dom.createCDATASection(b64_data_string)
7695
annotation.appendChild(cdata)
@@ -132,7 +151,14 @@ def add_custom_data(self, name, value):
132151

133152
def add_file_annotation(self, name, level='info', description='',
134153
file_path=None, mime_type='text/plain'):
135-
fa = Annotation(name, level, description, file_path, mime_type)
154+
fa = Annotation(name, level, description)
155+
fa.add_file_annotation(file_path, mime_type)
156+
self.annotations.append(fa)
157+
return fa
158+
159+
def add_link_annotation(self, level='info', description='', file_path=None):
160+
fa = Annotation(file_path, level, description)
161+
fa.add_link_annotation(file_path)
136162
self.annotations.append(fa)
137163
return fa
138164

@@ -178,7 +204,14 @@ def add_custom_data(self, name, value):
178204

179205
def add_file_annotation(self, name, level='info', description='',
180206
file_path=None, mime_type='text/plain'):
181-
fa = Annotation(name, level, description, file_path, mime_type)
207+
fa = Annotation(name, level, description)
208+
fa.add_file_annotation(file_path, mime_type)
209+
self.annotations.append(fa)
210+
return fa
211+
212+
def add_link_annotation(self, level='info', description='', file_path=None):
213+
fa = Annotation(file_path, level, description)
214+
fa.add_link_annotation(file_path)
182215
self.annotations.append(fa)
183216
return fa
184217

tests/test_TestspaceReport.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ def create_simple_testspace_xml(self):
1414
example_suite.add_test_case(test_case)
1515
test_case = testspace_xml.TestCase('passing case 2', 'passed')
1616
test_case.add_file_annotation('report_v1.xsd', file_path='tests/report_v1.xsd')
17+
test_case.add_link_annotation(file_path=r'\\machine/public')
1718
example_suite.add_test_case(test_case)
1819
test_case = testspace_xml.TestCase('failing case 1', 'failed')
1920
example_suite.add_test_case(test_case)
@@ -52,11 +53,11 @@ def test_number_failed_testcases(self):
5253

5354
def test_number_annotations(self):
5455
test_cases = self.testspace_xml_root.xpath("//test_suite/test_case/annotation")
55-
assert len(test_cases) is 2
56+
assert len(test_cases) is 3
5657

5758
def test_number_file_annotations(self):
5859
test_cases = self.testspace_xml_root.xpath("//test_suite/test_case/annotation[@file]")
59-
assert len(test_cases) is 1
60+
assert len(test_cases) is 2
6061

6162
def test_validate_xsd(self):
6263
assert xml_validator(self.testspace_xml_string, 'tests/report_v1.xsd')

0 commit comments

Comments
 (0)