Skip to content

libtest's junit formatter does not produce valid XML for doctests #99813

Open
@therealfrauholle

Description

@therealfrauholle

This issue is related to #85563. The junit formatter of libtest does not produce correct XML for some doctests. Take the following as an example:

use std::marker::PhantomData;


pub struct WithGeneric<A> {
    marker: PhantomData<A>
}

impl<A> WithGeneric<A> {
    /// 
    /// This is a doctest:
    /// 
    /// ```
    /// let _ = hello_world::WithGeneric::<()>::new();
    /// ```
    /// 
    pub fn new() -> Self {
        WithGeneric { marker: PhantomData }
    }
}

When running cargo +nightly-2022-07-26 test --doc -- -Z unstable-options --format junit, the following (invalid) XML is generated (formatted with an external tool):

<?xml version="1.0" encoding="UTF-8"?>
<testsuites>
	<testsuite name="test" package="test" id="0" errors="0" failures="0" tests="1" skipped="0" >
		<testcase classname="src/lib.rs" name="WithGeneric<A>::new (line 12)" time="0"/>
		<system-out/>
		<system-err/>
	</testsuite>
</testsuites>

The XML is invalid because of the name attribute of the testcase element. It contains the invalid characters < and >, which should be escaped to &lt; and &gt; as far as I understand.

Proposed solution

In a quick and dirty way I would implement something similar to what the JSON formatter does to escape the strings to valid XML attributes - I just did not have time to make a PR so far.

Apart from that I never contributed to this repository, nor am I specifically familiar with libtest. What would it take to implement the serialization based on serde and a crate like serde_xml_rs? Could I just add these crates as a dependency? I feel like this would be much cleaner than just repeating what the json formatter does.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-libtestArea: `#[test]` / the `test` libraryC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-testing-devexRelevant to the testing devex team (testing DX), which will review and decide on the PR/issue.

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions