From 712f994c81a7019bfc68cadc12562a4f1a24135f Mon Sep 17 00:00:00 2001 From: Julian Verdurmen <304NotModified@users.noreply.github.com> Date: Fri, 14 Jun 2019 01:18:54 +0200 Subject: [PATCH] XSD test for NLog.Schema (#3487) * Added XSD test * Integrate into build script --- Test-XmlFile.ps1 | 57 ++++++++++++++++++++++++++++++++++++++++++++++++ run-tests.ps1 | 8 +++++++ 2 files changed, 65 insertions(+) create mode 100644 Test-XmlFile.ps1 diff --git a/Test-XmlFile.ps1 b/Test-XmlFile.ps1 new file mode 100644 index 0000000000..ef898afa50 --- /dev/null +++ b/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" diff --git a/run-tests.ps1 b/run-tests.ps1 index 51468be92d..8a4a62ae94 100644 --- a/run-tests.ps1 +++ b/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 }