Skip to content

Commit

Permalink
XSD test for NLog.Schema (#3487)
Browse files Browse the repository at this point in the history
* Added XSD test

* Integrate into build script
  • Loading branch information
304NotModified committed Jun 13, 2019
1 parent 1568606 commit 712f994
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
57 changes: 57 additions & 0 deletions Test-XmlFile.ps1
@@ -0,0 +1,57 @@
function Test-XmlFile
{
<#
from: https://stackoverflow.com/a/16618560/201303
.Synopsis
Validates an xml file against an xml schema file.
.Example
PS> dir *.xml | Test-XmlFile schema.xsd
#>
[CmdletBinding()]
param (
[Parameter(Mandatory=$true)]
[string] $SchemaFile,

[Parameter(ValueFromPipeline=$true, Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
[alias('Fullname')]
[string] $XmlFile,

[scriptblock] $ValidationEventHandler = { Write-Error $args[1].Exception }
)

begin {
$schemaReader = New-Object System.Xml.XmlTextReader $SchemaFile
$schema = [System.Xml.Schema.XmlSchema]::Read($schemaReader, $ValidationEventHandler)
}

process {
$ret = $true
try {
$xml = New-Object System.Xml.XmlDocument
$xml.Schemas.Add($schema) | Out-Null
$xml.Load($XmlFile)
$xml.Validate({
throw ([PsCustomObject] @{
SchemaFile = $SchemaFile
XmlFile = $XmlFile
Exception = $args[1].Exception
})
})
} catch {
Write-Error $_
$ret = $false
}
$ret
}

end {
$schemaReader.Close()
}
}

# Needs absolute paths. Will throw a error if one of the files is not found
$pwd = get-location;

# Returns true if valid
return Test-XmlFile "$pwd\src\NLog\bin\Release\NLog.xsd" "$pwd\src\NuGet\NLog.Config\content\NLog.config"
8 changes: 8 additions & 0 deletions run-tests.ps1
@@ -1,3 +1,11 @@

if(.\Test-XmlFile.ps1){
Write-Output "Valid XSD"
}else {
exit 400;
}

return
dotnet restore .\src\NLog\
if (-Not $LastExitCode -eq 0)
{ exit $LastExitCode }
Expand Down

0 comments on commit 712f994

Please sign in to comment.