From b2e7dd7ba3d346383fed0b85f479d509302e7445 Mon Sep 17 00:00:00 2001 From: Mark Schofield Date: Sun, 5 Oct 2025 14:09:48 -0700 Subject: [PATCH 1/3] Update ReferenceBuild with scoped targets and an executable target --- Tests/BuildTargetsCompleter.Tests.ps1 | 20 ++++++++++++------- Tests/ReferenceBuild.ps1 | 9 +++++++++ Tests/ReferenceBuild/CMakeLists.txt | 3 +++ .../SubDirectory/CMakeLists.txt | 7 +++++++ .../SubDirectoryOther/CMakeLists.txt | 7 +++++++ Tests/Write-CMakeBuild.Tests.ps1 | 4 ++++ 6 files changed, 43 insertions(+), 7 deletions(-) create mode 100644 Tests/ReferenceBuild/SubDirectory/CMakeLists.txt create mode 100644 Tests/ReferenceBuild/SubDirectoryOther/CMakeLists.txt diff --git a/Tests/BuildTargetsCompleter.Tests.ps1 b/Tests/BuildTargetsCompleter.Tests.ps1 index 537a8c6..5742a65 100644 --- a/Tests/BuildTargetsCompleter.Tests.ps1 +++ b/Tests/BuildTargetsCompleter.Tests.ps1 @@ -20,13 +20,19 @@ Describe 'BuildTargetsCompleter' { Using-Location "$PSScriptRoot/ReferenceBuild" { $Completions = Get-CommandCompletion "Build-CMakeBuild -Targets " - $Completions.CompletionMatches.Count | Should -Be 6 - $Completions.CompletionMatches[0].CompletionText | Should -Be 'A_Library' - $Completions.CompletionMatches[1].CompletionText | Should -Be 'B_Library' - $Completions.CompletionMatches[2].CompletionText | Should -Be 'C_Library' - $Completions.CompletionMatches[3].CompletionText | Should -Be 'all' - $Completions.CompletionMatches[4].CompletionText | Should -Be 'clean' - $Completions.CompletionMatches[5].CompletionText | Should -Be 'install' + $Completions.CompletionMatches.Count | Should -Be 10 + $Completions.CompletionMatches.CompletionText | Should -Be @( + 'A_Library' + 'B_Library' + 'C_Library' + 'SubDirectoryOther_Executable' + 'SubDirectoryOther_Library' + 'SubDirectory_Executable' + 'SubDirectory_Library' + 'all' + 'clean' + 'install' + ) } } } diff --git a/Tests/ReferenceBuild.ps1 b/Tests/ReferenceBuild.ps1 index 0dd8f5b..476eb7d 100644 --- a/Tests/ReferenceBuild.ps1 +++ b/Tests/ReferenceBuild.ps1 @@ -41,3 +41,12 @@ function PrepareReferenceBuild() { "-DCMAKE_MAKE_PROGRAM=$CMAKE_MAKE_PROGRAM" ) } + +function GetReferenceBuildProperties() { + $BinaryDirectory = "$PSScriptRoot/ReferenceBuild/__output/windows-x64" + + [PSCustomObject]@{ + BinaryDirectory = $BinaryDirectory + CodeModelFile = Get-CMakeBuildCodeModel $BinaryDirectory + } +} diff --git a/Tests/ReferenceBuild/CMakeLists.txt b/Tests/ReferenceBuild/CMakeLists.txt index de4bf11..f737040 100644 --- a/Tests/ReferenceBuild/CMakeLists.txt +++ b/Tests/ReferenceBuild/CMakeLists.txt @@ -16,3 +16,6 @@ add_library(B_Library add_library(C_Library Reference.cpp ) + +add_subdirectory(SubDirectory) +add_subdirectory(SubDirectoryOther) \ No newline at end of file diff --git a/Tests/ReferenceBuild/SubDirectory/CMakeLists.txt b/Tests/ReferenceBuild/SubDirectory/CMakeLists.txt new file mode 100644 index 0000000..12ace25 --- /dev/null +++ b/Tests/ReferenceBuild/SubDirectory/CMakeLists.txt @@ -0,0 +1,7 @@ +add_executable(SubDirectory_Executable + ../Reference.cpp +) + +add_library(SubDirectory_Library + ../Reference.cpp +) diff --git a/Tests/ReferenceBuild/SubDirectoryOther/CMakeLists.txt b/Tests/ReferenceBuild/SubDirectoryOther/CMakeLists.txt new file mode 100644 index 0000000..473e828 --- /dev/null +++ b/Tests/ReferenceBuild/SubDirectoryOther/CMakeLists.txt @@ -0,0 +1,7 @@ +add_executable(SubDirectoryOther_Executable + ../Reference.cpp +) + +add_library(SubDirectoryOther_Library + ../Reference.cpp +) diff --git a/Tests/Write-CMakeBuild.Tests.ps1 b/Tests/Write-CMakeBuild.Tests.ps1 index 15444fb..14f730a 100644 --- a/Tests/Write-CMakeBuild.Tests.ps1 +++ b/Tests/Write-CMakeBuild.Tests.ps1 @@ -23,6 +23,10 @@ digraph CodeModel { "A_Library::@6890427a1f51a3e7e1df" [label="A_Library"] "B_Library::@6890427a1f51a3e7e1df" [label="B_Library"] "C_Library::@6890427a1f51a3e7e1df" [label="C_Library"] + "SubDirectoryOther_Executable::@01210d55993b56455dd6" [label="SubDirectoryOther_Executable"] + "SubDirectoryOther_Library::@01210d55993b56455dd6" [label="SubDirectoryOther_Library"] + "SubDirectory_Executable::@c68b9f6dab07fa391196" [label="SubDirectory_Executable"] + "SubDirectory_Library::@c68b9f6dab07fa391196" [label="SubDirectory_Library"] } '@ ((Write-CMakeBuild) -join '') | From bccb8afa38ba3cdc10b4b4d0a6f48cbb133e7cbb Mon Sep 17 00:00:00 2001 From: Mark Schofield Date: Sun, 5 Oct 2025 14:10:00 -0700 Subject: [PATCH 2/3] Add 'GetScopedTargets.Tests.ps1' with expected GetScopedTargets behavior --- Tests/GetScopedTargets.Tests.ps1 | 57 ++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 Tests/GetScopedTargets.Tests.ps1 diff --git a/Tests/GetScopedTargets.Tests.ps1 b/Tests/GetScopedTargets.Tests.ps1 new file mode 100644 index 0000000..4c5358e --- /dev/null +++ b/Tests/GetScopedTargets.Tests.ps1 @@ -0,0 +1,57 @@ +#Requires -PSEdition Core + +Set-StrictMode -Version Latest +$ErrorActionPreference = 'Stop' + +BeforeAll { + . $PSScriptRoot/TestUtilities.ps1 + . $PSScriptRoot/ReferenceBuild.ps1 + + $Properties = PrepareReferenceBuild + + $CMake = "$env:ProgramFiles/CMake/bin/cmake.exe" + & $CMake @Properties + + . $PSScriptRoot/../PSCMake/Common/CMake.ps1 + + $ReferenceBuildProperties = GetReferenceBuildProperties + $CodeModel = Get-CMakeBuildCodeModel $ReferenceBuildProperties.BinaryDirectory + $script:SourceLocation = $CodeModel.paths.source +} + +Describe 'GetScopedTargets' { + It 'Returns all targets when the ScopeLocation is the SourceLocation' { + $ScopeLocation = $SourceLocation + $Targets = GetScopedTargets $CodeModel $null $ScopeLocation + $Targets.name | + Should -Be @( + 'A_Library' + 'B_Library' + 'C_Library' + 'SubDirectoryOther_Executable' + 'SubDirectoryOther_Library' + 'SubDirectory_Executable' + 'SubDirectory_Library' + ) + } + + It 'Returns scoped targets when the ScopeLocation is a subdirectory of the SourceLocation' { + $ScopeLocation = Join-Path -Path $SourceLocation -ChildPath 'SubDirectoryOther' + $Targets = GetScopedTargets $CodeModel $null $ScopeLocation + $Targets.name | + Should -Be @( + 'SubDirectoryOther_Executable' + 'SubDirectoryOther_Library' + ) + } + + It 'Returns scoped targets when the ScopeLocation is a subdirectory of the SourceLocation, that is also a prefix of another subdirectory' { + $ScopeLocation = Join-Path -Path $SourceLocation -ChildPath 'SubDirectory' + $Targets = GetScopedTargets $CodeModel $null $ScopeLocation + $Targets.name | + Should -Be @( + 'SubDirectory_Executable' + 'SubDirectory_Library' + ) + } +} From 24c5317e47eb09af36dab99e6d76cf6940cb8906 Mon Sep 17 00:00:00 2001 From: Mark Schofield Date: Sun, 5 Oct 2025 14:10:03 -0700 Subject: [PATCH 3/3] Finding targets by scope should limit to the scoped directory --- PSCMake/Common/CMake.ps1 | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/PSCMake/Common/CMake.ps1 b/PSCMake/Common/CMake.ps1 index bfc7188..9e77ca9 100644 --- a/PSCMake/Common/CMake.ps1 +++ b/PSCMake/Common/CMake.ps1 @@ -507,6 +507,10 @@ function GetScopedTargets { $Configuration, $ScopeLocation ) + function CanonicalizeDirectoryPath($Path) { + Resolve-Path -Path (Join-Path -Path $Path -ChildPath '/') + } + $ScopeLocation = CanonicalizeDirectoryPath $ScopeLocation $CodeModelConfiguration = if ($Configuration) { $CodeModel.configurations | Where-Object { $_.name -eq $Configuration } } else { @@ -521,7 +525,8 @@ function GetScopedTargets { } else { Join-Path -Path $SourceDir -ChildPath $Folder } - $Folder.StartsWith($ScopeLocation) + $Folder = CanonicalizeDirectoryPath $Folder + $Folder.Path.StartsWith($ScopeLocation.Path) } }