Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional reporters #424

Open
Gijsreyn opened this issue Mar 6, 2025 · 0 comments
Open

Additional reporters #424

Gijsreyn opened this issue Mar 6, 2025 · 0 comments

Comments

@Gijsreyn
Copy link

Gijsreyn commented Mar 6, 2025

As a user implementing the markdown-link-check in CI pipelines, it's helpful to have additional reporters like JUnit/XUnit to easily report on. The #364 PR addresses the JUnit reporter as additional option to the CLI, but this was later reverted.

Now I have to fallback to using regex and some PowerShell and convert it myself:

# Simple regex
$successPattern = [regex]::new('\[✓\] (http[^\s]+)')
$deadPattern = [regex]::new('\[✖\] (http[^\s]+) → Status: (\d+)')

# Do transformation on content generated and then use the following function to generate JUnit
function ConvertTo-JUnitXml {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [psobject[]]$InputObject
    )

    # Create XML document
    $xml = New-Object System.Xml.XmlDocument
    $xmlDeclaration = $xml.CreateXmlDeclaration("1.0", "UTF-8", $null)
    $xml.AppendChild($xmlDeclaration) | Out-Null

    # Create root element
    $root = $xml.CreateElement("testsuites")
    $root.SetAttribute("name", "Test run")
    $root.SetAttribute("tests", $InputObject.Count.ToString())
    $root.SetAttribute("failures", ($InputObject | Where-Object { $_.Status -eq 'Failure' }).Count.ToString())
    $root.SetAttribute("errors", "0")
    $root.SetAttribute("skipped", "0")
    $root.SetAttribute("assertions", "0")
    $root.SetAttribute("time", "0")
    $root.SetAttribute("timestamp", (Get-Date).ToString("yyyy-MM-ddTHH:mm:ss"))
    $xml.AppendChild($root) | Out-Null

    # Create a testsuite element
    $testsuite = $xml.CreateElement("testsuite")
    $testsuite.SetAttribute("name", "Brokenlink.Assertion")
    $testsuite.SetAttribute("time", "0")
    $root.AppendChild($testsuite) | Out-Null

    foreach ($item in $InputObject) {
        $testcase = $xml.CreateElement("testcase")
        $testcase.SetAttribute("classname", $item.URL)
        $testcase.SetAttribute("name", $item.URL)
        $testcase.SetAttribute("time", "0")

        if ($item.Status -eq 'Failure') {
            $failure = $xml.CreateElement("failure")
            $failure.SetAttribute("message", "Status Code: $($Item.StatusCode)")
            $failure.SetAttribute("type", "BrokenlinkError")
            $testcase.AppendChild($failure) | Out-Null
        }

        $testsuite.AppendChild($testcase) | Out-Null
    }

    return $xml.OuterXml
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant