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

XSD test for NLog.Schema #3487

Merged
merged 2 commits into from Jun 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: fixed in 1fb0b97

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