def _get_writer(self, path, rpa, generator): if not path: return NullMarkupWriter() writer = XmlWriter(path, write_empty=False, usage='output') writer.start('robot', {'generator': get_full_version(generator), 'generated': get_timestamp(), 'rpa': 'true' if rpa else 'false'}) return writer
def _get_writer(self, path, generator): if not path: return NullMarkupWriter() try: writer = XmlWriter(path, encoding='UTF-8') except EnvironmentError as err: raise DataError("Opening output file '%s' failed: %s" % (path, err.strerror)) writer.start('robot', {'generator': get_full_version(generator), 'generated': get_timestamp()}) return writer
def _get_writer(self, path, generator): if not path: return NullMarkupWriter() try: writer = XmlWriter(path, encoding='UTF-8', write_empty=False) except EnvironmentError as err: raise DataError("Opening output file '%s' failed: %s" % (path, err.strerror)) writer.start('robot', {'generator': get_full_version(generator), 'generated': get_timestamp()}) return writer
def write(self, libdoc, outfile): writer = XmlWriter(outfile, encoding='UTF-8') writer.start('keywordspec', {'name': libdoc.name, 'type': libdoc.type, 'generated': get_timestamp(millissep=None)}) writer.element('version', libdoc.version) writer.element('scope', libdoc.scope) writer.element('namedargs', 'yes' if libdoc.named_args else 'no') writer.element('doc', libdoc.doc) self._write_keywords('init', libdoc.inits, writer) self._write_keywords('kw', libdoc.keywords, writer) writer.end('keywordspec') writer.close()
class XUnitWriter(ResultVisitor): """Provides an xUnit-compatible result file. Attempts to adhere to the de facto schema guessed by Peter Reilly, see: http://marc.info/?l=ant-dev&m=123551933508682 """ def __init__(self, output): self._writer = XmlWriter(output, encoding='UTF-8') self._root_suite = None def start_suite(self, suite): if self._root_suite: return self._root_suite = suite attrs = { 'name': suite.name, 'tests': str(suite.statistics.all.total), 'errors': '0', 'failures': str(suite.statistics.all.failed), 'skip': '0' } self._writer.start('testsuite', attrs) def end_suite(self, suite): if suite is self._root_suite: self._writer.end('testsuite') def start_test(self, test): attrs = { 'classname': test.parent.longname, 'name': test.name, 'time': self._time_as_seconds(test.elapsedtime) } self._writer.start('testcase', attrs) if not test.passed: test.visit(TestFailureWriter(self._writer)) def _time_as_seconds(self, millis): return str(int(round(millis, -3) / 1000)) def end_test(self, test): self._writer.end('testcase') def visit_keyword(self, kw): pass def end_result(self, result): self._writer.close()
class XUnitWriter(ResultVisitor): """Provides an xUnit-compatible result file. Attempts to adhere to the de facto schema guessed by Peter Reilly, see: http://marc.info/?l=ant-dev&m=123551933508682 """ def __init__(self, output): self._writer = XmlWriter(output, encoding='UTF-8') self._root_suite = None def start_suite(self, suite): if self._root_suite: return self._root_suite = suite attrs = {'name': suite.name, 'tests': str(suite.statistics.all.total), 'errors': '0', 'failures': str(suite.statistics.all.failed), 'skip': '0'} self._writer.start('testsuite', attrs) def end_suite(self, suite): if suite is self._root_suite: self._writer.end('testsuite') def start_test(self, test): attrs = {'classname': test.parent.longname, 'name': test.name, 'time': self._time_as_seconds(test.elapsedtime)} self._writer.start('testcase', attrs) if not test.passed: test.visit(TestFailureWriter(self._writer)) def _time_as_seconds(self, millis): return str(int(round(millis, -3) / 1000)) def end_test(self, test): self._writer.end('testcase') def visit_keyword(self, kw): pass def end_result(self, result): self._writer.close()
def write(self, libdoc, outfile): writer = XmlWriter(outfile, encoding="UTF-8") writer.start( "keywordspec", { "name": libdoc.name, "type": libdoc.type, "format": libdoc.doc_format, "generated": get_timestamp(millissep=None), }, ) writer.element("version", libdoc.version) writer.element("scope", libdoc.scope) writer.element("namedargs", "yes" if libdoc.named_args else "no") writer.element("doc", libdoc.doc) self._write_keywords("init", libdoc.inits, writer) self._write_keywords("kw", libdoc.keywords, writer) writer.end("keywordspec") writer.close()
class TestXmlWriter(unittest.TestCase): def setUp(self): self.writer = XmlWriter(PATH) def tearDown(self): self.writer.close() os.remove(PATH) def test_write_element_in_pieces(self): self.writer.start('name', {'attr': 'value'}, newline=False) self.writer.content('Some content here!!') self.writer.end('name') self._verify_node(None, 'name', 'Some content here!!', {'attr': 'value'}) def test_calling_content_multiple_times(self): self.writer.start(u'robot-log', newline=False) self.writer.content(u'Hello world!\n') self.writer.content(u'Hi again!') self.writer.content('\tMy name is John') self.writer.end('robot-log') self._verify_node(None, 'robot-log', 'Hello world!\nHi again!\tMy name is John') def test_write_element(self): self.writer.element('foo', 'Node\n content', { 'a1': 'attr1', 'a2': 'attr2' }) self._verify_node(None, 'foo', 'Node\n content', { 'a1': 'attr1', 'a2': 'attr2' }) def test_write_many_elements(self): self.writer.start('root', {'version': 'test'}) self.writer.start('child1', {'my-attr': 'my value'}) self.writer.element('leaf1.1', 'leaf content', {'type': 'kw'}) self.writer.element('leaf1.2') self.writer.end('child1') self.writer.element('child2', attrs={'class': 'foo'}) self.writer.end('root') root = self._get_root() self._verify_node(root, 'root', attrs={'version': 'test'}) self._verify_node(root.find('child1'), 'child1', attrs={'my-attr': 'my value'}) self._verify_node(root.find('child1/leaf1.1'), 'leaf1.1', 'leaf content', {'type': 'kw'}) self._verify_node(root.find('child1/leaf1.2'), 'leaf1.2') self._verify_node(root.find('child2'), 'child2', attrs={'class': 'foo'}) def test_newline_insertion(self): self.writer.start('root') self.writer.start('suite', {'type': 'directory_suite'}) self.writer.element('test', attrs={'name': 'my_test'}, newline=False) self.writer.element('test', attrs={'name': 'my_2nd_test'}) self.writer.end('suite', False) self.writer.start('suite', {'name': 'another suite'}, newline=False) self.writer.content('Suite 2 content') self.writer.end('suite') self.writer.end('root') content = self._get_content() lines = [line for line in content.splitlines() if line != '\n'] assert_equal(len(lines), 6) def test_none_content(self): self.writer.element(u'robot-log', None) self._verify_node(None, 'robot-log') def test_content_with_invalid_command_char(self): self.writer.element('robot-log', '\033[31m\033[32m\033[33m\033[m') self._verify_node(None, 'robot-log', '[31m[32m[33m[m') def test_content_with_invalid_command_char_unicode(self): self.writer.element('robot-log', u'\x1b[31m\x1b[32m\x1b[33m\x1b[m') self._verify_node(None, 'robot-log', '[31m[32m[33m[m') def test_content_with_non_ascii(self): self.writer.start('root') self.writer.element(u'e', u'Circle is 360\xB0') self.writer.element(u'f', u'Hyv\xE4\xE4 \xFC\xF6t\xE4') self.writer.end('root') root = self._get_root() self._verify_node(root.find('e'), 'e', u'Circle is 360\xB0') self._verify_node(root.find('f'), 'f', u'Hyv\xE4\xE4 \xFC\xF6t\xE4') def test_content_with_entities(self): self.writer.element(u'robot-log', 'Me, Myself & I > you') self._verify_content('Me, Myself & I > you') def test_remove_illegal_chars(self): assert_equals(self.writer._escape(u'\x1b[31m'), '[31m') assert_equals(self.writer._escape(u'\x00'), '') def test_ioerror_when_file_is_invalid(self): assert_raises(IOError, XmlWriter, os.path.dirname(__file__)) def test_custom_encoding(self): self.writer.close() self.writer = XmlWriter(PATH, encoding='ISO-8859-1') self.writer.element('test', u'hyv\xe4') self._verify_content('encoding="ISO-8859-1"') self._verify_node(None, 'test', u'hyv\xe4') def _verify_node(self, node, name, text=None, attrs={}): if node is None: node = self._get_root() assert_equals(node.tag, name) if text is not None: assert_equals(node.text, text) assert_equals(node.attrib, attrs) def _verify_content(self, expected): content = self._get_content() assert_true(expected in content, 'Failed to find:\n%s\n\nfrom:\n%s' % (expected, content)) def _get_root(self): self.writer.close() with ETSource(PATH) as source: return ET.parse(source).getroot() def _get_content(self): self.writer.close() with open(PATH) as f: return f.read()
class TestXmlWriter(unittest.TestCase): def setUp(self): self.writer = XmlWriter(PATH) def tearDown(self): self.writer.close() os.remove(PATH) def test_write_element_in_pieces(self): self.writer.start('name', {'attr': 'value'}, newline=False) self.writer.content('Some content here!!') self.writer.end('name') self._verify_node(None, 'name', 'Some content here!!', {'attr': 'value'}) def test_calling_content_multiple_times(self): self.writer.start(u'robot-log', newline=False) self.writer.content(u'Hello world!\n') self.writer.content(u'Hi again!') self.writer.content('\tMy name is John') self.writer.end('robot-log') self._verify_node(None, 'robot-log', 'Hello world!\nHi again!\tMy name is John') def test_write_element(self): self.writer.element('foo', 'Node\n content', {'a1': 'attr1', 'a2': 'attr2'}) self._verify_node(None, 'foo', 'Node\n content', {'a1': 'attr1', 'a2': 'attr2'}) def test_write_many_elements(self): self.writer.start('root', {'version': 'test'}) self.writer.start('child1', {'my-attr': 'my value'}) self.writer.element('leaf1.1', 'leaf content', {'type': 'kw'}) self.writer.element('leaf1.2') self.writer.end('child1') self.writer.element('child2', attrs={'class': 'foo'}) self.writer.end('root') root = self._get_root() self._verify_node(root, 'root', attrs={'version': 'test'}) self._verify_node(root.find('child1'), 'child1', attrs={'my-attr': 'my value'}) self._verify_node(root.find('child1/leaf1.1'), 'leaf1.1', 'leaf content', {'type': 'kw'}) self._verify_node(root.find('child1/leaf1.2'), 'leaf1.2') self._verify_node(root.find('child2'), 'child2', attrs={'class': 'foo'}) def test_newline_insertion(self): self.writer.start('root') self.writer.start('suite', {'type': 'directory_suite'}) self.writer.element('test', attrs={'name': 'my_test'}, newline=False) self.writer.element('test', attrs={'name': 'my_2nd_test'}) self.writer.end('suite', False) self.writer.start('suite', {'name': 'another suite'}, newline=False) self.writer.content('Suite 2 content') self.writer.end('suite') self.writer.end('root') content = self._get_content() lines = [line for line in content.splitlines() if line != '\n'] assert_equal(len(lines), 6) def test_none_content(self): self.writer.element(u'robot-log', None) self._verify_node(None, 'robot-log') def test_content_with_invalid_command_char(self): self.writer.element('robot-log', '\033[31m\033[32m\033[33m\033[m') self._verify_node(None, 'robot-log', '[31m[32m[33m[m') def test_content_with_invalid_command_char_unicode(self): self.writer.element('robot-log', u'\x1b[31m\x1b[32m\x1b[33m\x1b[m') self._verify_node(None, 'robot-log', '[31m[32m[33m[m') def test_content_with_non_ascii(self): self.writer.start('root') self.writer.element(u'e', u'Circle is 360\xB0') self.writer.element(u'f', u'Hyv\xE4\xE4 \xFC\xF6t\xE4') self.writer.end('root') root = self._get_root() self._verify_node(root.find('e'), 'e', u'Circle is 360\xB0') self._verify_node(root.find('f'), 'f', u'Hyv\xE4\xE4 \xFC\xF6t\xE4') def test_content_with_entities(self): self.writer.element(u'robot-log', 'Me, Myself & I > you') self._verify_content('Me, Myself & I > you') def test_remove_illegal_chars(self): assert_equals(self.writer._escape(u'\x1b[31m'), '[31m') assert_equals(self.writer._escape(u'\x00'), '') def test_ioerror_when_file_is_invalid(self): assert_raises(IOError, XmlWriter, os.path.dirname(__file__)) def test_custom_encoding(self): self.writer.close() self.writer = XmlWriter(PATH, encoding='ISO-8859-1') self.writer.element('test', u'hyv\xe4') self._verify_content('encoding="ISO-8859-1"') self._verify_node(None, 'test', u'hyv\xe4') def _verify_node(self, node, name, text=None, attrs={}): if node is None: node = self._get_root() assert_equals(node.tag, name) if text is not None: assert_equals(node.text, text) assert_equals(node.attrib, attrs) def _verify_content(self, expected): content = self._get_content() assert_true(expected in content, 'Failed to find:\n%s\n\nfrom:\n%s' % (expected, content)) def _get_root(self): self.writer.close() with ETSource(PATH) as source: return ET.parse(source).getroot() def _get_content(self): self.writer.close() with open(PATH) as f: return f.read()
class TestXmlWriter(unittest.TestCase): def setUp(self): self.writer = XmlWriter(PATH, encoding="UTF-8") def tearDown(self): self.writer.close() os.remove(PATH) def test_write_element_in_pieces(self): self.writer.start("name", {"attr": "value"}, newline=False) self.writer.content("Some content here!!") self.writer.end("name") self.writer.close() self._verify_node(None, "name", "Some content here!!", {"attr": "value"}) def test_calling_content_multiple_times(self): self.writer.start(u"robot-log", newline=False) self.writer.content(u"Hello world!\n") self.writer.content(u"Hi again!") self.writer.content("\tMy name is John") self.writer.end("robot-log") self.writer.close() self._verify_node(None, "robot-log", "Hello world!\nHi again!\tMy name is John") def test_write_element(self): self.writer.element("foo", "Node\n content", {"a1": "attr1", "a2": "attr2"}) self.writer.close() self._verify_node(None, "foo", "Node\n content", {"a1": "attr1", "a2": "attr2"}) def test_write_many_elements(self): self.writer.start("root", {"version": "test"}) self.writer.start("child1", {"my-attr": "my value"}) self.writer.element("leaf1.1", "leaf content", {"type": "kw"}) self.writer.element("leaf1.2") self.writer.end("child1") self.writer.element("child2", attrs={"class": "foo"}) self.writer.end("root") self.writer.close() with ETSource(PATH) as source: root = ET.parse(source).getroot() self._verify_node(root, "root", attrs={"version": "test"}) self._verify_node(root.find("child1"), "child1", attrs={"my-attr": "my value"}) self._verify_node(root.find("child1/leaf1.1"), "leaf1.1", "leaf content", {"type": "kw"}) self._verify_node(root.find("child1/leaf1.2"), "leaf1.2") self._verify_node(root.find("child2"), "child2", attrs={"class": "foo"}) def test_newline_insertion(self): self.writer.start("root") self.writer.start("suite", {"type": "directory_suite"}) self.writer.element("test", attrs={"name": "my_test"}, newline=False) self.writer.element("test", attrs={"name": "my_2nd_test"}) self.writer.end("suite", False) self.writer.start("suite", {"name": "another suite"}, newline=False) self.writer.content("Suite 2 content") self.writer.end("suite") self.writer.end("root") self.writer.close() with open(PATH) as file: lines = [line for line in file if line != "\n"] assert_equal(len(lines), 6) def test_none_content(self): self.writer.element(u"robot-log", None) self.writer.close() self._verify_node(None, "robot-log") def test_content_with_invalid_command_char(self): self.writer.element("robot-log", "\033[31m\033[32m\033[33m\033[m") self.writer.close() self._verify_node(None, "robot-log", "[31m[32m[33m[m") def test_content_with_invalid_command_char_unicode(self): self.writer.element("robot-log", u"\x1b[31m\x1b[32m\x1b[33m\x1b[m") self.writer.close() self._verify_node(None, "robot-log", "[31m[32m[33m[m") def test_content_with_unicode(self): self.writer.start("root") self.writer.element(u"e", u"Circle is 360\u00B0") self.writer.element(u"f", u"Hyv\u00E4\u00E4 \u00FC\u00F6t\u00E4") self.writer.end("root") self.writer.close() with ETSource(PATH) as source: root = ET.parse(source).getroot() self._verify_node(root.find("e"), "e", u"Circle is 360\u00B0") self._verify_node(root.find("f"), "f", u"Hyv\u00E4\u00E4 \u00FC\u00F6t\u00E4") def test_content_with_entities(self): self.writer.element(u"robot-log", "Me, Myself & I > you") self.writer.close() with open(PATH) as file: content = file.read() assert_true("Me, Myself & I > you" in content) def test_remove_illegal_chars(self): assert_equals(self.writer._escape(u"\x1b[31m"), "[31m") assert_equals(self.writer._escape(u"\x00"), "") def test_ioerror_when_file_is_invalid(self): assert_raises(IOError, XmlWriter, os.path.dirname(__file__)) def _verify_node(self, node, name, text=None, attrs={}): if node is None: with ETSource(PATH) as source: node = ET.parse(source).getroot() assert_equals(node.tag, name) if text is not None: assert_equals(node.text, text) assert_equals(node.attrib, attrs)