1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
#!/usr/bin/env python3
#
# Copyright (c) 2023 Red Hat, Inc.
#
# SPDX-License-Identifier: MIT
"""Unit tests for xml-preprocess"""
import contextlib
import importlib
import os
import platform
import subprocess
import tempfile
import unittest
from io import StringIO
xmlpp = importlib.import_module("xml-preprocess")
class TestXmlPreprocess(unittest.TestCase):
"""Tests for xml-preprocess.Preprocessor"""
def test_preprocess_xml(self):
with tempfile.NamedTemporaryFile(mode="w", delete=False) as temp_file:
temp_file.write("<root></root>")
temp_file_name = temp_file.name
result = xmlpp.preprocess_xml(temp_file_name)
self.assertEqual(result, "<root></root>")
os.remove(temp_file_name)
def test_save_xml(self):
with tempfile.NamedTemporaryFile(mode="w", delete=False) as temp_file:
temp_file_name = temp_file.name
xmlpp.save_xml("<root></root>", temp_file_name)
self.assertTrue(os.path.isfile(temp_file_name))
os.remove(temp_file_name)
def test_include(self):
with tempfile.NamedTemporaryFile(mode="w", delete=False) as inc_file:
inc_file.write("<included>Content from included file</included>")
inc_file_name = inc_file.name
xml_str = f"<?include {inc_file_name} ?>"
expected = "<included>Content from included file</included>"
xpp = xmlpp.Preprocessor()
result = xpp.preprocess(xml_str)
self.assertEqual(result, expected)
os.remove(inc_file_name)
self.assertRaises(FileNotFoundError, xpp.preprocess, xml_str)
def test_envvar(self):
os.environ["TEST_ENV_VAR"] = "TestValue"
xml_str = "<root>$(env.TEST_ENV_VAR)</root>"
expected = "<root>TestValue</root>"
xpp = xmlpp.Preprocessor()
result = xpp.preprocess(xml_str)
self.assertEqual(result, expected)
self.assertRaises(KeyError, xpp.preprocess, "$(env.UNKNOWN)")
def test_sys_var(self):
xml_str = "<root>$(sys.ARCH)</root>"
expected = f"<root>{platform.architecture()[0]}</root>"
xpp = xmlpp.Preprocessor()
result = xpp.preprocess(xml_str)
self.assertEqual(result, expected)
self.assertRaises(KeyError, xpp.preprocess, "$(sys.UNKNOWN)")
def test_cus_var(self):
xml_str = "<root>$(var.USER)</root>"
expected = "<root></root>"
xpp = xmlpp.Preprocessor()
result = xpp.preprocess(xml_str)
self.assertEqual(result, expected)
xml_str = "<?define USER=FOO?><root>$(var.USER)</root>"
expected = "<root>FOO</root>"
xpp = xmlpp.Preprocessor()
result = xpp.preprocess(xml_str)
self.assertEqual(result, expected)
def test_error_warning(self):
xml_str = "<root><?warning \"test warn\"?></root>"
expected = "<root></root>"
xpp = xmlpp.Preprocessor()
out = StringIO()
with contextlib.redirect_stdout(out):
result = xpp.preprocess(xml_str)
self.assertEqual(result, expected)
self.assertEqual(out.getvalue(), "[Warning]: test warn\n")
self.assertRaises(RuntimeError, xpp.preprocess, "<?error \"test\"?>")
def test_cmd(self):
xpp = xmlpp.Preprocessor()
result = xpp.preprocess('<root><?cmd "echo hello world"?></root>')
self.assertEqual(result, "<root>hello world</root>")
self.assertRaises(
subprocess.CalledProcessError,
xpp.preprocess, '<?cmd "test-unknown-cmd"?>'
)
def test_foreach(self):
xpp = xmlpp.Preprocessor()
result = xpp.preprocess(
'<root><?foreach x in a;b;c?>$(var.x)<?endforeach?></root>'
)
self.assertEqual(result, "<root>abc</root>")
def test_if_elseif(self):
xpp = xmlpp.Preprocessor()
result = xpp.preprocess('<root><?if True?>ok<?endif?></root>')
self.assertEqual(result, "<root>ok</root>")
result = xpp.preprocess('<root><?if False?>ok<?endif?></root>')
self.assertEqual(result, "<root></root>")
result = xpp.preprocess('<root><?if True?>ok<?else?>ko<?endif?></root>')
self.assertEqual(result, "<root>ok</root>")
result = xpp.preprocess('<root><?if False?>ok<?else?>ko<?endif?></root>')
self.assertEqual(result, "<root>ko</root>")
result = xpp.preprocess(
'<root><?if False?>ok<?elseif True?>ok2<?else?>ko<?endif?></root>'
)
self.assertEqual(result, "<root>ok2</root>")
result = xpp.preprocess(
'<root><?if False?>ok<?elseif False?>ok<?else?>ko<?endif?></root>'
)
self.assertEqual(result, "<root>ko</root>")
def test_ifdef(self):
xpp = xmlpp.Preprocessor()
result = xpp.preprocess('<root><?ifdef USER?>ok<?else?>ko<?endif?></root>')
self.assertEqual(result, "<root>ko</root>")
result = xpp.preprocess(
'<?define USER=FOO?><root><?ifdef USER?>ok<?else?>ko<?endif?></root>'
)
self.assertEqual(result, "<root>ok</root>")
if __name__ == "__main__":
unittest.main()
|