1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
# Copyright (C) 2019 Greenbone Networks GmbH |
3
|
|
|
# |
4
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later |
5
|
|
|
# |
6
|
|
|
# This program is free software: you can redistribute it and/or modify |
7
|
|
|
# it under the terms of the GNU General Public License as published by |
8
|
|
|
# the Free Software Foundation, either version 3 of the License, or |
9
|
|
|
# (at your option) any later version. |
10
|
|
|
# |
11
|
|
|
# This program is distributed in the hope that it will be useful, |
12
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14
|
|
|
# GNU General Public License for more details. |
15
|
|
|
# |
16
|
|
|
# You should have received a copy of the GNU General Public License |
17
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
18
|
|
|
|
19
|
|
|
import sys |
20
|
|
|
import unittest |
21
|
|
|
|
22
|
|
|
from unittest.mock import patch |
23
|
|
|
from io import StringIO |
24
|
|
|
|
25
|
|
|
import defusedxml.lxml as secET |
26
|
|
|
from gvm.xml import pretty_print |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
class PrettyPrintTestCase(unittest.TestCase): |
30
|
|
|
def test_pretty_print_to_file(self): |
31
|
|
|
xml_str = '<test><this><with id="a">and text</with></this></test>' |
32
|
|
|
elem = secET.fromstring(xml_str) |
33
|
|
|
expected_xml_string = ( |
34
|
|
|
'<test>\n' |
35
|
|
|
' <this>\n' |
36
|
|
|
' <with id="a">and text</with>\n' |
37
|
|
|
' </this>\n' |
38
|
|
|
'</test>\n' |
39
|
|
|
) |
40
|
|
|
|
41
|
|
|
with open('test.file', 'w') as f: |
42
|
|
|
pretty_print(elem, file=f) |
43
|
|
|
|
44
|
|
|
with open('test.file', 'r') as f: |
45
|
|
|
xml_string = f.read() |
46
|
|
|
|
47
|
|
|
self.assertEqual(xml_string, expected_xml_string) |
48
|
|
|
|
49
|
|
|
def test_pretty_print_to_stringio(self): |
50
|
|
|
xml_str = '<test><this><with id="a">and text</with></this></test>' |
51
|
|
|
elem = secET.fromstring(xml_str) |
52
|
|
|
expected_xml_string = ( |
53
|
|
|
'<test>\n' |
54
|
|
|
' <this>\n' |
55
|
|
|
' <with id="a">and text</with>\n' |
56
|
|
|
' </this>\n' |
57
|
|
|
'</test>\n' |
58
|
|
|
) |
59
|
|
|
|
60
|
|
|
stringio = StringIO() |
61
|
|
|
pretty_print(elem, file=stringio) |
62
|
|
|
|
63
|
|
|
xml_string = stringio.getvalue() |
64
|
|
|
|
65
|
|
|
self.assertEqual(xml_string, expected_xml_string) |
66
|
|
|
|
67
|
|
|
# I don't know why: But this test is only working if sys.stdout is passed. |
68
|
|
|
# But printing to sys.stdout is working, too using the default argument ... |
69
|
|
|
@patch('sys.stdout', new_callable=StringIO) |
70
|
|
|
def test_pretty_print_to_stdout(self, mock_stdout): |
71
|
|
|
xml_str = '<test><this><with id="a">and text</with></this></test>' |
72
|
|
|
elem = secET.fromstring(xml_str) |
73
|
|
|
expected_xml_string = ( |
74
|
|
|
'<test>\n' |
75
|
|
|
' <this>\n' |
76
|
|
|
' <with id="a">and text</with>\n' |
77
|
|
|
' </this>\n' |
78
|
|
|
'</test>\n' |
79
|
|
|
) |
80
|
|
|
|
81
|
|
|
pretty_print(elem, sys.stdout) |
82
|
|
|
xml_string = mock_stdout.getvalue() |
83
|
|
|
|
84
|
|
|
self.assertEqual(xml_string, expected_xml_string) |
85
|
|
|
|
86
|
|
|
def test_pretty_print_with_string(self): |
87
|
|
|
xml_str = '<test><this><with id="a">and text</with></this></test>' |
88
|
|
|
# elem = secET.fromstring(xml_str) |
89
|
|
|
expected_xml_string = ( |
90
|
|
|
'<test>\n' |
91
|
|
|
' <this>\n' |
92
|
|
|
' <with id="a">and text</with>\n' |
93
|
|
|
' </this>\n' |
94
|
|
|
'</test>\n' |
95
|
|
|
) |
96
|
|
|
|
97
|
|
|
stringio = StringIO() |
98
|
|
|
pretty_print(xml_str, file=stringio) |
99
|
|
|
|
100
|
|
|
xml_string = stringio.getvalue() |
101
|
|
|
|
102
|
|
|
self.assertEqual(xml_string, expected_xml_string) |
103
|
|
|
|
104
|
|
|
def test_pretty_print_with_dict(self): |
105
|
|
|
xml_str = '<test><this><with id="a">and text</with></this></test>' |
106
|
|
|
elem = secET.fromstring(xml_str) |
107
|
|
|
expected_xml_string = ( |
108
|
|
|
'<test>\n' |
109
|
|
|
' <this>\n' |
110
|
|
|
' <with id="a">and text</with>\n' |
111
|
|
|
' </this>\n' |
112
|
|
|
'</test>\n' |
113
|
|
|
'<test>\n' |
114
|
|
|
' <this>\n' |
115
|
|
|
' <with id="a">and text</with>\n' |
116
|
|
|
' </this>\n' |
117
|
|
|
'</test>\n' |
118
|
|
|
) |
119
|
|
|
|
120
|
|
|
stringio = StringIO() |
121
|
|
|
pretty_print([elem, elem], file=stringio) |
122
|
|
|
|
123
|
|
|
xml_string = stringio.getvalue() |
124
|
|
|
|
125
|
|
|
self.assertEqual(xml_string, expected_xml_string) |
126
|
|
|
|
127
|
|
|
def test_pretty_print_with_dict_str(self): |
128
|
|
|
xml_str = '<test><this><with id="a">and text</with></this></test>' |
129
|
|
|
no_xml = '</test>' |
130
|
|
|
elem = secET.fromstring(xml_str) |
131
|
|
|
expected_xml_string = ( |
132
|
|
|
'<test>\n' |
133
|
|
|
' <this>\n' |
134
|
|
|
' <with id="a">and text</with>\n' |
135
|
|
|
' </this>\n' |
136
|
|
|
'</test>\n' |
137
|
|
|
'</test>\n' |
138
|
|
|
) |
139
|
|
|
|
140
|
|
|
stringio = StringIO() |
141
|
|
|
pretty_print([elem, no_xml], file=stringio) |
142
|
|
|
|
143
|
|
|
xml_string = stringio.getvalue() |
144
|
|
|
|
145
|
|
|
self.assertEqual(xml_string, expected_xml_string) |
146
|
|
|
|
147
|
|
|
def test_pretty_print_type_error(self): |
148
|
|
|
xml_str = '<test><this><with id="a">and text</with></this></test>' |
149
|
|
|
elem = secET.fromstring(xml_str) |
150
|
|
|
|
151
|
|
|
with self.assertRaises(TypeError): |
152
|
|
|
pretty_print(elem, file='string') |
153
|
|
|
|
154
|
|
|
|
155
|
|
|
if __name__ == '__main__': |
156
|
|
|
unittest.main() |
157
|
|
|
|