3 ms·
Just as an anecdote, I recently had to generate some XLSX which, even for XML standards, is a horrible standard and the only library that seems to be able to ac
by Tainnor 6y ago
Just as an anecdote, I recently had to generate some XLSX which, even for XML standards, is a horrible standard and the only library that seems to be able to accomplish this (for Java) consists basically of some autogenerated classes from the XML schema itself that have zero documentation.
But the worst part was when I tried to add an integration test for it. I tried comparing the generated file with a fixture file, but that didn't work, because XLSX are zipped XML files and zip isn't deterministic. But even comparing the two file directories of the unzipped files and then comparing XML files pairwise didn't really work, because apparently, XML can be serialized in different ways and it turned out that my CI somehow serialized it differently than my machine (somehow the namespaces were handled differently - not sure if it was an OS issue or some transitive dependency issue). In the end I was only able to solve it by using another library that can specifically diff XML files.
- virtue3 6y agoif you smash the file permissions and dates on the file it can be deterministic.
- Tainnor 6y agoMaybe (didn't look into that), but that wouldn't have solved the "somehow, XML is serialized differently in some cases because of namespaces" issue which, honestly, I didn't even try to understand because it seemed like a pointless rabbit-hole.
- sk5t 6y agoIndeed there's generally no guarantee that two XML serializers will choose the same namespace aliases, or handle ignorable whitespace the same, etc. You gotta think of XML as an object graph (and not a bunch of tags and text nodes) to have any joy at all with it. Unit tests should not compare the serialized bytes, but rather deserialize and/or event-parse and compare the graphs or events. I'm not suggesting any of this is pleasant, but, there it is.
- loopz 6y agoThinking of it as graph helps due to its many branches, but it's still hierarchical.
- Tainnor 6y agoIt's actually an arborescence which is a kind of directed acyclic graph (it's a directed graph in which there is exactly one directed path from the root to any of its children).
- Tainnor 6y agoYep, I ended up using https://www.xmlunit.org/ https://www.xmlunit.org/, which seems to do the job well.
- Mikhail_Edoshin 6y agoNowadays there is a canonical serialization for XML. Not sure if all tools support it though.