Skip to content

Additional reporters #424

Description

@Gijsreyn

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
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature request or implementation

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions