From 1a81690524f3309d61be0784ece62c6a0ce5ac10 Mon Sep 17 00:00:00 2001 From: Mark Schofield Date: Sun, 5 Oct 2025 21:25:25 -0700 Subject: [PATCH] Add tests for 'Write-CMakeBuild' --- Tests/ReferenceBuild.ps1 | 1 + Tests/TestUtilities.ps1 | 22 ++++++ Tests/Write-CMakeBuild.Tests.ps1 | 19 ++++- Tests/Write-CMakeBuild.dgml | 122 +++++++++++++++++++++++++++++++ Tests/XmlUtilities.ps1 | 32 ++++++++ 5 files changed, 195 insertions(+), 1 deletion(-) create mode 100644 Tests/Write-CMakeBuild.dgml create mode 100644 Tests/XmlUtilities.ps1 diff --git a/Tests/ReferenceBuild.ps1 b/Tests/ReferenceBuild.ps1 index 476eb7d..cb32a20 100644 --- a/Tests/ReferenceBuild.ps1 +++ b/Tests/ReferenceBuild.ps1 @@ -48,5 +48,6 @@ function GetReferenceBuildProperties() { [PSCustomObject]@{ BinaryDirectory = $BinaryDirectory CodeModelFile = Get-CMakeBuildCodeModel $BinaryDirectory + SourceDirectory = (Resolve-Path -Path "$PSScriptRoot/ReferenceBuild/").Path } } diff --git a/Tests/TestUtilities.ps1 b/Tests/TestUtilities.ps1 index 6f25df1..4dc94c2 100644 --- a/Tests/TestUtilities.ps1 +++ b/Tests/TestUtilities.ps1 @@ -6,3 +6,25 @@ $ErrorActionPreference = 'Stop' function Get-CommandCompletion([string] $InputScript) { [System.Management.Automation.CommandCompletion]::CompleteInput($InputScript, $InputScript.Length, $null) } + +function Zip-Object { + $Objects = $args + + if ($Objects.Count -lt 2) { + throw "At least two collections must be specified" + } + + $First = $Objects[0] + $Objects | ForEach-Object { + if ($_.Count -ne $First.Count) { + throw "All collections must have the same number of elements" + } + } + for ($i = 0; $i -lt $First.Count; $i++) { + [array]$Current = @() + $Objects | ForEach-Object { + $Current += $_[$i] + } + (, $Current) + } +} diff --git a/Tests/Write-CMakeBuild.Tests.ps1 b/Tests/Write-CMakeBuild.Tests.ps1 index 14f730a..00058f6 100644 --- a/Tests/Write-CMakeBuild.Tests.ps1 +++ b/Tests/Write-CMakeBuild.Tests.ps1 @@ -5,9 +5,11 @@ $ErrorActionPreference = 'Stop' BeforeAll { . $PSScriptRoot/TestUtilities.ps1 + . $PSScriptRoot/XmlUtilities.ps1 . $PSScriptRoot/ReferenceBuild.ps1 $Properties = PrepareReferenceBuild + $script:BuildProperties = GetReferenceBuildProperties $CMake = "$env:ProgramFiles/CMake/bin/cmake.exe" & $CMake @Properties @@ -17,7 +19,7 @@ BeforeAll { Describe 'Write-CMakeBuild' { It 'Writes the build with no parameters' { - Using-Location "$PSScriptRoot/ReferenceBuild" { + Using-Location $BuildProperties.SourceDirectory { $ExpectedDotFile = @' digraph CodeModel { "A_Library::@6890427a1f51a3e7e1df" [label="A_Library"] @@ -33,4 +35,19 @@ digraph CodeModel { Should -Be ($ExpectedDotFile -replace '\r\n', '') } } + It 'Writes the DGML when specified' { + Using-Location $BuildProperties.SourceDirectory { + [xml]$ExpectedDgml = Get-Content "$PSScriptRoot/Write-CMakeBuild.dgml" + SortChildElements $ExpectedDgml.DirectedGraph.Links { $_.Target } + + [xml]$ActualDgml = Write-CMakeBuild -As Dgml + $ActualDgml.DirectedGraph.Nodes.Node | + Where-Object { Get-MemberValue $_ Definition } | + ForEach-Object { $_.Definition = $_.Definition.Replace($BuildProperties.SourceDirectory, "") } + SortChildElements $ActualDgml.DirectedGraph.Links { $_.Target } + + $ActualDgml.OuterXml | + Should -Be $ExpectedDgml.OuterXml + } + } } diff --git a/Tests/Write-CMakeBuild.dgml b/Tests/Write-CMakeBuild.dgml new file mode 100644 index 0000000..3603306 --- /dev/null +++ b/Tests/Write-CMakeBuild.dgml @@ -0,0 +1,122 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Tests/XmlUtilities.ps1 b/Tests/XmlUtilities.ps1 new file mode 100644 index 0000000..6d17017 --- /dev/null +++ b/Tests/XmlUtilities.ps1 @@ -0,0 +1,32 @@ +#Requires -PSEdition Core + +Set-StrictMode -Version Latest +$ErrorActionPreference = 'Stop' + +$TypeAccelerators = [psobject].Assembly.GetType("System.Management.Automation.TypeAccelerators") +$TypeAccelerators::Add("xmlelement", [System.Xml.XmlElement]) + +function RemoveChildElements([xmlelement] $XmlElement) { + $ChildNodes = $XmlElement.ChildNodes + for ($Index = 0; $Index -lt $ChildNodes.Count; ) { + $ChildXmlElement = $ChildNodes.Item($Index) + if ($ChildXmlElement.NodeType -eq 'Element') { + $XmlElement.RemoveChild($ChildXmlElement) + } else { + $Index++ + } + } +} + +function AddChildElements([xmlelement]$XmlElement, [array] $ChildElements) { + $ChildElements | + ForEach-Object { + $null = $XmlElement.AppendChild($_) + } +} + +function SortChildElements([xmlelement] $LinksElement, [scriptblock] $SortExpression) { + $LinkArray = RemoveChildElements $LinksElement + $LinkArray = $LinkArray | Sort-Object $SortExpression + AddChildElements $LinksElement $LinkArray +}