def _xml_filediff(a: pathlib.Path, b: pathlib.Path): """Print a unified diff of canonicalize XML files a and b.""" try: a_contents = etree.canonicalize(a.read_text()).splitlines() except etree.ParseError: print_error(f"File {a} could not be parsed.") return True try: b_contents = etree.canonicalize(b.read_text()).splitlines() except etree.ParseError: print_error(f"File {a} could not be parsed.") return True diff = list( difflib.unified_diff(a_contents, b_contents, fromfile=str(a), tofile=str(b))) if diff: print_error(f"Files {a} did not match:") for line in diff: print(line.strip()) return True return False
def assert_xml_equal(mini_lmf, dump_lmf): if hasattr(ET, 'canonicalize'): # available from Python 3.8 orig = ET.canonicalize(from_file=mini_lmf, strip_text=True) temp = ET.canonicalize(from_file=dump_lmf, strip_text=True) # additional transformation to help with debugging orig = orig.replace('<', '\n<') temp = temp.replace('<', '\n<') assert orig == temp
def test_xml_insert(): with open(os.path.join(DIR_PATH, "web.xml")) as base_xml: out = insert_file(base_xml.read(), os.path.join(DIR_PATH, "insert.xml")) with open(os.path.join(DIR_PATH, "new.xml")) as expected_xml_fp: out_xml_canonical = ET.canonicalize(out, strip_text=True) expected_xml_canonical = ET.canonicalize(from_file=expected_xml_fp, strip_text=True) assert out_xml_canonical == expected_xml_canonical
def assert_xml_equal(actual: XMLType, expected: XMLType) -> None: """Assert that the XML canonical forms of `expected` and `actual` are equal.""" def to_string(xml_data: XMLType) -> str: if isinstance(xml_data, str): return xml_data if isinstance(xml_data, ET.ElementTree): xml_data = xml_data.getroot() return ET.tostring(xml_data, encoding="unicode") assert ET.canonicalize(to_string(actual), strip_text=True) == ET.canonicalize( to_string(expected), strip_text=True)
def test_export(mini_lmf_1_0, tmp_path): tmpdir = tmp_path / 'test_export' tmpdir.mkdir() tmppath = tmpdir / 'mini_lmf_export.xml' lexicons = wn.lexicons(lexicon='test-en test-es') wn.export(lexicons, tmppath) if hasattr(ET, 'canonicalize'): # available from Python 3.8 # remove comments, indentation, etc. orig = ET.canonicalize(from_file=mini_lmf_1_0, strip_text=True) temp = ET.canonicalize(from_file=tmppath, strip_text=True) # additional transformation to help with debugging orig = orig.replace('<', '\n<') temp = temp.replace('<', '\n<') assert orig == temp
def clean_svg(svg: str) -> str: svg = ET.fromstring(svg.replace(' style=""', "")) del svg.attrib["viewBox"] del svg.find(f"./{{{SVGNS}}}g[@id='inner']").attrib["transform"] svg.remove(svg.find(f"./{{{SVGNS}}}g[@id='axisLabels']")) svg.remove(svg.find(f"./{{{SVGNS}}}rect[@id='xBlock']")) svg.remove(svg.find(f"./{{{SVGNS}}}rect[@id='yBlock']")) svg.remove(svg.find(f"./{{{SVGNS}}}path[@id='axis']")) grid = svg.find(f"./{{{SVGNS}}}g[@id='inner']/{{{SVGNS}}}rect[@id='grid']") for attrib in ["y", "width", "height"]: del grid.attrib[attrib] return ET.canonicalize(ET.tostring(svg))
def udiff(expect, got, expect_path): expect = expect.splitlines(1) got = got.splitlines(1) for line in difflib.unified_diff(expect, got, fromfile=expect_path, tofile='got'): sys.stderr.write(line) if not line.endswith('\n'): sys.stderr.write('[no-newline]\n') indent(element) #canonicalize() is only available in python3.8+, and we need it to have reliable string output: if hasattr(et, 'canonicalize'): got = et.canonicalize(et.tostring(element)).rstrip() exp_path = os.path.join(os.path.dirname(sys.argv[0]), 'expected_junit_output.xml') with open(exp_path, 'r') as f: exp = f.read().rstrip() udiff(exp, got, exp_path) # Uncomment to update exp_path: #with open(exp_path, 'w') as f: # f.write(got) #deleting generated tmp trial dir: shutil.rmtree(example_trial_dir, ignore_errors=True) # vim: expandtab tabstop=4 shiftwidth=4
def assert_xml_equal(self, output, expected): self.assertEqual(ET.canonicalize(output), ET.canonicalize(expected))