Skip to content
This repository has been archived by the owner on Sep 23, 2024. It is now read-only.

Pretty and sanitizier xml #32

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion kpet/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import tempfile
import shutil
import os
import subprocess
import distutils
from kpet import utils, targeted


Expand Down Expand Up @@ -50,6 +52,28 @@ def print_test_cases(patches, dbdir, pw_cookie=None):
print(test_case)


def pretty_xml(xml_content):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer this was at least named maybe_pretty_xml(), try_prettify_xml(), or something like that, or, better, that kpet would have an option to turn this off (or on), and the function doing it unconditionally. Otherwise kpet users as well as developers would find it confusing that this sometimes works and sometimes doesn't without apparent reason.

Another way could be requiring xmllint, but we can't do that with just Python packaging, as you know.

"""
Check, reformat and reindent the xml content if xmllint is available,
otherwise return the content unchanged.
Args:
xml_content: Jinja template rendered
Returns:
An xml reformatted and reindented if xmllint is available.
"""
cmd = distutils.spawn.find_executable('xmllint')
if not cmd:
return xml_content
args = [cmd, '--format', '-']
proc = subprocess.Popen(args, stdin=subprocess.PIPE,
stdout=subprocess.PIPE)
proc.stdin.write(xml_content.encode('utf-8'))
stdout, _ = proc.communicate()
if proc.poll() != 0:
raise subprocess.CalledProcessError(proc.returncode, ' '.join(args))
return stdout.decode('utf-8')


# pylint: disable=too-many-arguments
def generate(template, template_params, patches, dbdir, output,
pw_cookie=None):
Expand Down Expand Up @@ -78,7 +102,7 @@ def generate(template, template_params, patches, dbdir, output,
template_params['TEST_CASES_KICKSTART'] = sorted(
targeted.get_property('kickstart', test_names, dbdir)
)
content = template.render(template_params)
content = pretty_xml(template.render(template_params))
if not output:
print(content)
else:
Expand Down
3 changes: 2 additions & 1 deletion tests/test_kpet.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ def test_exec_command(self):
mock.call('Not implemented yet'),
)

def test_main(self):
@mock.patch('distutils.spawn.find_executable', return_value=None)
def test_main(self, mock_find_executable):
"""
Check --db is required and also run generate command executes
successfully
Expand Down
21 changes: 20 additions & 1 deletion tests/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
import os
import tempfile
import unittest
import subprocess
import mock
from kpet import run, utils, exceptions


class RunTest(unittest.TestCase):
"""Test cases for run module."""
def test_generate(self):
@mock.patch('distutils.spawn.find_executable', return_value=None)
def test_generate(self, mock_find_executable):
"""
Check the success case, if it raises the proper exception when
the type is not found and if it saves the output in a file instead
Expand Down Expand Up @@ -113,3 +115,20 @@ def test_get_test_cases(self, mock_get_test_cases):
mock_get_test_cases.return_value = ['default/ltplite', 'fs/xfs']
test_cases = run.get_test_cases("", "")
self.assertEqual(mock_get_test_cases.return_value, test_cases)

@mock.patch('subprocess.Popen')
@mock.patch('distutils.spawn.find_executable')
def test_pretty_xml(self, mock_find_executable, mock_popen):
"""
Check command (xmllint mocked) is called if it is available and content
is modified.
"""
mock_find_executable.return_value = None
self.assertEqual('foobar', run.pretty_xml('foobar'))

mock_find_executable.return_value = 'binary'
mock_popen().communicate.return_value = (b'stdout', b'')
self.assertRaises(subprocess.CalledProcessError, run.pretty_xml, 'bar')

mock_popen().poll.return_value = 0
self.assertEqual('stdout', run.pretty_xml('foobar'))