from galaxy.tool_util.format import (
format_xml,
main,
)
SIMPLE_TOOL_XML = """\
A test tool
echo hello
"""
EXPECTED_FORMATTED = """\
A test tool
echo hello
"""
CDATA_XML = """\
"""
COMMENT_XML = """\
echo hello
"""
def test_basic_formatting():
result = format_xml(SIMPLE_TOOL_XML)
assert result == EXPECTED_FORMATTED
def test_tab_size_2():
result = format_xml(SIMPLE_TOOL_XML, tab_size=2)
assert " " in result
assert " " in result
def test_invalid_xml_returned_unchanged():
bad_xml = ""
result = format_xml(bad_xml)
assert result == bad_xml
def test_idempotent():
first = format_xml(SIMPLE_TOOL_XML)
second = format_xml(first)
assert first == second
def test_cli_inplace(tmp_path):
tool_file = tmp_path / "tool.xml"
tool_file.write_text(SIMPLE_TOOL_XML)
main([str(tool_file)])
assert tool_file.read_text() == EXPECTED_FORMATTED
def test_cli_already_formatted(tmp_path, capsys):
tool_file = tmp_path / "tool.xml"
tool_file.write_text(EXPECTED_FORMATTED)
main([str(tool_file)])
assert tool_file.read_text() == EXPECTED_FORMATTED
assert "already formatted" in capsys.readouterr().out
def test_cli_diff_mode(tmp_path, capsys):
tool_file = tmp_path / "tool.xml"
tool_file.write_text(SIMPLE_TOOL_XML)
try:
main(["--diff", str(tool_file)])
except SystemExit as e:
assert e.code == 1
# file should be unchanged in diff mode
assert tool_file.read_text() == SIMPLE_TOOL_XML
assert "---" in capsys.readouterr().out
def test_cli_diff_mode_no_changes(tmp_path):
tool_file = tmp_path / "tool.xml"
tool_file.write_text(EXPECTED_FORMATTED)
# should not raise SystemExit
main(["--diff", str(tool_file)])
def test_cli_tab_size(tmp_path):
tool_file = tmp_path / "tool.xml"
tool_file.write_text(SIMPLE_TOOL_XML)
main(["--tab-size", "2", str(tool_file)])
result = tool_file.read_text()
assert " " in result
assert "