From 4cab26c2bb5e5f017c648a745ebb9d5c5e18282d Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sun, 8 Jun 2025 13:04:33 +0200 Subject: [PATCH 1/6] =?UTF-8?q?=F0=9F=A9=B9=20[Patch]:=20Refactor=20contex?= =?UTF-8?q?t=20functions=20for=20improved=20ID=20handling=20and=20debuggin?= =?UTF-8?q?g=20output?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/functions/public/Get-ContextInfo.ps1 | 6 ++++-- src/functions/public/Remove-Context.ps1 | 2 +- src/functions/public/Rename-Context.ps1 | 10 +++++----- src/functions/public/Set-Context.ps1 | 2 +- 4 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/functions/public/Get-ContextInfo.ps1 b/src/functions/public/Get-ContextInfo.ps1 index 69d99251..b958f08f 100644 --- a/src/functions/public/Get-ContextInfo.ps1 +++ b/src/functions/public/Get-ContextInfo.ps1 @@ -115,8 +115,10 @@ Write-Debug "[$stackPath] - Processing file: $($file.FullName)" $contextInfo | Format-List | Out-String -Stream | ForEach-Object { Write-Debug "[$stackPath] $_" } } - if ($contextInfo.ID -like $ID) { - $contextInfo + foreach ($IDItem in $ID) { + if ($contextInfo.ID -like $IDItem) { + $contextInfo + } } } } diff --git a/src/functions/public/Remove-Context.ps1 b/src/functions/public/Remove-Context.ps1 index 017b2b8e..e1ec1f7d 100644 --- a/src/functions/public/Remove-Context.ps1 +++ b/src/functions/public/Remove-Context.ps1 @@ -105,7 +105,7 @@ if ($PSCmdlet.ShouldProcess("Context '$contextId'", 'Remove')) { Write-Debug "[$stackPath] - Removing context [$contextId]" $contextInfo.Path | Remove-Item -Force -ErrorAction Stop - Write-Output "Removed item: $contextId" + Write-Debug "Removed item: $contextId" } } } diff --git a/src/functions/public/Rename-Context.ps1 b/src/functions/public/Rename-Context.ps1 index 295b8acf..3435ecba 100644 --- a/src/functions/public/Rename-Context.ps1 +++ b/src/functions/public/Rename-Context.ps1 @@ -30,7 +30,7 @@ Renames the context 'PSModule.GitHub' to 'PSModule.GitHub2' using pipeline input. .OUTPUTS - [System.String] + object .NOTES The confirmation message indicating the successful renaming of the context. @@ -38,7 +38,7 @@ .LINK https://psmodule.io/Context/Functions/Rename-Context/ #> - + [OutputType([object])] [CmdletBinding(SupportsShouldProcess)] param ( # The ID of the context to rename. @@ -72,15 +72,15 @@ process { $context = Get-Context -ID $ID -Vault $Vault if (-not $context) { - throw "Context with ID '$ID' not found$(if ($Vault) { " in vault '$Vault'" })." + throw "Context with ID '$ID' not found in vault '$Vault'" } $existingContext = Get-Context -ID $NewID -Vault $Vault if ($existingContext -and -not $Force) { - throw "Context with ID '$NewID' already exists$(if ($Vault) { " in vault '$Vault'" })." + throw "Context with ID '$NewID' already exists in vault '$Vault'" } - if ($PSCmdlet.ShouldProcess("Renaming context '$ID' to '$NewID'$(if ($Vault) { " in vault '$Vault'" })")) { + if ($PSCmdlet.ShouldProcess("Renaming context '$ID' to '$NewID' in vault '$Vault'")) { $context | Set-Context -ID $NewID -Vault $Vault Remove-Context -ID $ID -Vault $Vault } diff --git a/src/functions/public/Set-Context.ps1 b/src/functions/public/Set-Context.ps1 index f0fedc51..bbdb887b 100644 --- a/src/functions/public/Set-Context.ps1 +++ b/src/functions/public/Set-Context.ps1 @@ -77,7 +77,7 @@ function Set-Context { process { $vaultObject = Set-ContextVault -Name $Vault - Write-Verbose "$($vaultObject | Format-List | Out-String)" + $vaultObject | Format-List | Out-String -Stream | ForEach-Object { Write-Debug $_ } if ($context -is [System.Collections.IDictionary]) { $Context = [PSCustomObject]$Context From 83adcc68d9c3a554a7c9fc6934269c79e5951595 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sun, 8 Jun 2025 13:44:42 +0200 Subject: [PATCH 2/6] =?UTF-8?q?=F0=9F=A9=B9=20[Patch]:=20Add=20tests=20for?= =?UTF-8?q?=20retrieving=20contexts=20by=20multiple=20IDs=20in=20VaultA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/Context.Tests.ps1 | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/Context.Tests.ps1 b/tests/Context.Tests.ps1 index daa798f0..7d154f96 100644 --- a/tests/Context.Tests.ps1 +++ b/tests/Context.Tests.ps1 @@ -179,6 +179,15 @@ Describe 'Context' { { Get-Context -ID $null -Vault 'VaultA' } | Should -Not -Throw Get-Context -ID $null -Vault 'VaultA' | Should -BeNullOrEmpty } + It "Get-Context -ID array - Should return only specified contexts in VaultA" { + $ids = @('TestID1', 'TestID2') + $results = Get-Context -ID $ids -Vault 'VaultA' + $results | Should -Not -BeNullOrEmpty + $results.Count | Should -Be 2 + $results.ID | Should -Contain 'TestID1' + $results.ID | Should -Contain 'TestID2' + $results.ID | Should -Not -Contain 'TestID3' + } } Context 'Remove-Context' { @@ -304,5 +313,15 @@ Describe 'Context' { $result = Get-ContextInfo -ID 'NonExistentContext' -Vault 'VaultA' $result | Should -BeNullOrEmpty } + + It 'Should return only specified contexts for multiple IDs in VaultA' { + $ids = @('TestID1', 'TestID2') + $results = Get-ContextInfo -ID $ids -Vault 'VaultA' + $results | Should -Not -BeNullOrEmpty + $results.Count | Should -Be 2 + $results.ID | Should -Contain 'TestID1' + $results.ID | Should -Contain 'TestID2' + $results.ID | Should -Not -Contain 'TestID3' + } } } From a6b135867476e0465361f5552385ee0d93c81b5e Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sun, 8 Jun 2025 13:46:14 +0200 Subject: [PATCH 3/6] =?UTF-8?q?=F0=9F=A9=B9=20[Patch]:=20Fix=20test=20desc?= =?UTF-8?q?ription=20formatting=20for=20Get-Context=20with=20array=20IDs?= =?UTF-8?q?=20in=20VaultA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/Context.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Context.Tests.ps1 b/tests/Context.Tests.ps1 index 7d154f96..61888594 100644 --- a/tests/Context.Tests.ps1 +++ b/tests/Context.Tests.ps1 @@ -179,7 +179,7 @@ Describe 'Context' { { Get-Context -ID $null -Vault 'VaultA' } | Should -Not -Throw Get-Context -ID $null -Vault 'VaultA' | Should -BeNullOrEmpty } - It "Get-Context -ID array - Should return only specified contexts in VaultA" { + It 'Get-Context -ID array - Should return only specified contexts in VaultA' { $ids = @('TestID1', 'TestID2') $results = Get-Context -ID $ids -Vault 'VaultA' $results | Should -Not -BeNullOrEmpty From 9233caf57882eb6dfcb4d3eb91126bc475260dcb Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sun, 8 Jun 2025 13:49:58 +0200 Subject: [PATCH 4/6] =?UTF-8?q?=F0=9F=A9=B9=20[Patch]:=20Add=20tests=20for?= =?UTF-8?q?=20Get-Context=20to=20verify=20output=20leakage=20with=20single?= =?UTF-8?q?=20and=20array=20IDs=20in=20VaultA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/Context.Tests.ps1 | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tests/Context.Tests.ps1 b/tests/Context.Tests.ps1 index 61888594..bcae0bc2 100644 --- a/tests/Context.Tests.ps1 +++ b/tests/Context.Tests.ps1 @@ -188,6 +188,31 @@ Describe 'Context' { $results.ID | Should -Contain 'TestID2' $results.ID | Should -Not -Contain 'TestID3' } + It 'Get-Context -ID single - Should not have output leakage (returns only one context)' { + $id = 'TestID1' + $results = Get-Context -ID $id -Vault 'VaultA' + $results | Should -Not -BeNullOrEmpty + $results.Count | Should -Be 1 + $results.ID | Should -Be $id + } + It 'Get-Context -ID single (nonexistent) - Should not have output leakage (returns nothing)' { + $id = 'NonExistentContext' + $results = Get-Context -ID $id -Vault 'VaultA' + $results | Should -BeNullOrEmpty + } + It 'Get-Context -ID array with one valid and one invalid - Should not have output leakage (returns only valid)' { + $ids = @('TestID1', 'NonExistentContext') + $results = Get-Context -ID $ids -Vault 'VaultA' + $results | Should -Not -BeNullOrEmpty + $results.Count | Should -Be 1 + $results.ID | Should -Be 'TestID1' + } + It 'Get-Context -ID with whitespace or null - Should not have output leakage (returns nothing)' { + $results = Get-Context -ID ' ' -Vault 'VaultA' + $results | Should -BeNullOrEmpty + $results = Get-Context -ID $null -Vault 'VaultA' + $results | Should -BeNullOrEmpty + } } Context 'Remove-Context' { From a9ffb010872f04035dcc16b97ff28c0fca0d3e93 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sun, 8 Jun 2025 14:23:43 +0200 Subject: [PATCH 5/6] =?UTF-8?q?=F0=9F=A9=B9=20[Patch]:=20Standardize=20deb?= =?UTF-8?q?ug=20output=20messages=20to=20use=20"Begin"=20instead=20of=20"S?= =?UTF-8?q?tart"=20across=20multiple=20context=20functions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/functions/public/Get-Context.ps1 | 2 +- src/functions/public/Get-ContextInfo.ps1 | 21 ++++++------------- src/functions/public/Rename-Context.ps1 | 2 +- src/functions/public/Set-Context.ps1 | 2 +- .../public/Vault/Get-ContextVault.ps1 | 2 +- .../public/Vault/Remove-ContextVault.ps1 | 2 +- .../public/Vault/Reset-ContextVault.ps1 | 2 +- .../public/Vault/Set-ContextVault.ps1 | 2 +- 8 files changed, 13 insertions(+), 22 deletions(-) diff --git a/src/functions/public/Get-Context.ps1 b/src/functions/public/Get-Context.ps1 index c12cdd8b..9ce98be7 100644 --- a/src/functions/public/Get-Context.ps1 +++ b/src/functions/public/Get-Context.ps1 @@ -108,7 +108,7 @@ function Get-Context { begin { $stackPath = Get-PSCallStackPath - Write-Debug "[$stackPath] - Start" + Write-Debug "[$stackPath] - Begin" } process { diff --git a/src/functions/public/Get-ContextInfo.ps1 b/src/functions/public/Get-ContextInfo.ps1 index b958f08f..66fc2f1b 100644 --- a/src/functions/public/Get-ContextInfo.ps1 +++ b/src/functions/public/Get-ContextInfo.ps1 @@ -87,34 +87,25 @@ ) begin { - $debug = $DebugPreference -eq 'Continue' - if ($debug) { - $stackPath = Get-PSCallStackPath - Write-Debug "[$stackPath] - Start" - } + $stackPath = Get-PSCallStackPath + Write-Debug "[$stackPath] - Begin" } process { $vaults = foreach ($vaultName in $Vault) { Get-ContextVault -Name $vaultName -ErrorAction Stop } - if ($debug) { - Write-Debug "[$stackPath] - Found $($vaults.Count) vault(s) matching '$($Vault -join ', ')'." - } + Write-Debug "[$stackPath] - Found $($vaults.Count) vault(s) matching '$($Vault -join ', ')'." $files = foreach ($vaultObject in $vaults) { Get-ChildItem -Path $vaultObject.Path -Filter *.json -File } - if ($debug) { - Write-Debug "[$stackPath] - Found $($files.Count) context file(s) in vault(s)." - } + Write-Debug "[$stackPath] - Found $($files.Count) context file(s) in vault(s)." foreach ($file in $files) { $contextInfo = Get-Content -Path $file.FullName | ConvertFrom-Json - if ($debug) { - Write-Debug "[$stackPath] - Processing file: $($file.FullName)" - $contextInfo | Format-List | Out-String -Stream | ForEach-Object { Write-Debug "[$stackPath] $_" } - } + Write-Debug "[$stackPath] - Processing file: $($file.FullName)" + $contextInfo | Format-List | Out-String -Stream | ForEach-Object { Write-Debug "[$stackPath] $_" } foreach ($IDItem in $ID) { if ($contextInfo.ID -like $IDItem) { $contextInfo diff --git a/src/functions/public/Rename-Context.ps1 b/src/functions/public/Rename-Context.ps1 index 3435ecba..87fdb1ac 100644 --- a/src/functions/public/Rename-Context.ps1 +++ b/src/functions/public/Rename-Context.ps1 @@ -66,7 +66,7 @@ begin { $stackPath = Get-PSCallStackPath - Write-Debug "[$stackPath] - Start" + Write-Debug "[$stackPath] - Begin" } process { diff --git a/src/functions/public/Set-Context.ps1 b/src/functions/public/Set-Context.ps1 index bbdb887b..10237e0f 100644 --- a/src/functions/public/Set-Context.ps1 +++ b/src/functions/public/Set-Context.ps1 @@ -72,7 +72,7 @@ function Set-Context { begin { $stackPath = Get-PSCallStackPath - Write-Debug "[$stackPath] - Start" + Write-Debug "[$stackPath] - Begin" } process { diff --git a/src/functions/public/Vault/Get-ContextVault.ps1 b/src/functions/public/Vault/Get-ContextVault.ps1 index 5f41f45e..c29a1ab4 100644 --- a/src/functions/public/Vault/Get-ContextVault.ps1 +++ b/src/functions/public/Vault/Get-ContextVault.ps1 @@ -39,7 +39,7 @@ begin { $stackPath = Get-PSCallStackPath - Write-Debug "[$stackPath] - Start" + Write-Debug "[$stackPath] - Begin" if (-not (Test-Path -Path $script:Config.RootPath)) { return } diff --git a/src/functions/public/Vault/Remove-ContextVault.ps1 b/src/functions/public/Vault/Remove-ContextVault.ps1 index 59058eab..c0224e67 100644 --- a/src/functions/public/Vault/Remove-ContextVault.ps1 +++ b/src/functions/public/Vault/Remove-ContextVault.ps1 @@ -30,7 +30,7 @@ begin { $stackPath = Get-PSCallStackPath - Write-Debug "[$stackPath] - Start" + Write-Debug "[$stackPath] - Begin" $vaults = Get-ContextVault } diff --git a/src/functions/public/Vault/Reset-ContextVault.ps1 b/src/functions/public/Vault/Reset-ContextVault.ps1 index 87dc082c..dc4b6c81 100644 --- a/src/functions/public/Vault/Reset-ContextVault.ps1 +++ b/src/functions/public/Vault/Reset-ContextVault.ps1 @@ -34,7 +34,7 @@ begin { $stackPath = Get-PSCallStackPath - Write-Debug "[$stackPath] - Start" + Write-Debug "[$stackPath] - Begin" } process { diff --git a/src/functions/public/Vault/Set-ContextVault.ps1 b/src/functions/public/Vault/Set-ContextVault.ps1 index 43ba675d..59e6e18f 100644 --- a/src/functions/public/Vault/Set-ContextVault.ps1 +++ b/src/functions/public/Vault/Set-ContextVault.ps1 @@ -30,7 +30,7 @@ function Set-ContextVault { begin { $stackPath = Get-PSCallStackPath - Write-Debug "[$stackPath] - Start" + Write-Debug "[$stackPath] - Begin" } process { From d6e7ae5aa98d3182b17565bf18f3480ccadbd9eb Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sun, 8 Jun 2025 14:31:26 +0200 Subject: [PATCH 6/6] =?UTF-8?q?=F0=9F=A9=B9=20[Patch]:=20Replace=20Write-D?= =?UTF-8?q?ebug=20with=20Write-Verbose=20for=20improved=20logging=20in=20G?= =?UTF-8?q?et-ContextInfo,=20Remove-Context,=20and=20Set-Context=20functio?= =?UTF-8?q?ns?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/functions/public/Get-ContextInfo.ps1 | 8 ++++---- src/functions/public/Remove-Context.ps1 | 4 ++-- src/functions/public/Set-Context.ps1 | 6 +++--- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/functions/public/Get-ContextInfo.ps1 b/src/functions/public/Get-ContextInfo.ps1 index 66fc2f1b..a6531361 100644 --- a/src/functions/public/Get-ContextInfo.ps1 +++ b/src/functions/public/Get-ContextInfo.ps1 @@ -95,17 +95,17 @@ $vaults = foreach ($vaultName in $Vault) { Get-ContextVault -Name $vaultName -ErrorAction Stop } - Write-Debug "[$stackPath] - Found $($vaults.Count) vault(s) matching '$($Vault -join ', ')'." + Write-Verbose "[$stackPath] - Found $($vaults.Count) vault(s) matching '$($Vault -join ', ')'." $files = foreach ($vaultObject in $vaults) { Get-ChildItem -Path $vaultObject.Path -Filter *.json -File } - Write-Debug "[$stackPath] - Found $($files.Count) context file(s) in vault(s)." + Write-Verbose "[$stackPath] - Found $($files.Count) context file(s) in vault(s)." foreach ($file in $files) { $contextInfo = Get-Content -Path $file.FullName | ConvertFrom-Json - Write-Debug "[$stackPath] - Processing file: $($file.FullName)" - $contextInfo | Format-List | Out-String -Stream | ForEach-Object { Write-Debug "[$stackPath] $_" } + Write-Verbose "[$stackPath] - Processing file: $($file.FullName)" + $contextInfo | Format-List | Out-String -Stream | ForEach-Object { Write-Verbose "[$stackPath] $_" } foreach ($IDItem in $ID) { if ($contextInfo.ID -like $IDItem) { $contextInfo diff --git a/src/functions/public/Remove-Context.ps1 b/src/functions/public/Remove-Context.ps1 index e1ec1f7d..cb489940 100644 --- a/src/functions/public/Remove-Context.ps1 +++ b/src/functions/public/Remove-Context.ps1 @@ -103,9 +103,9 @@ $contextId = $contextInfo.ID if ($PSCmdlet.ShouldProcess("Context '$contextId'", 'Remove')) { - Write-Debug "[$stackPath] - Removing context [$contextId]" + Write-Verbose "[$stackPath] - Removing context [$contextId]" $contextInfo.Path | Remove-Item -Force -ErrorAction Stop - Write-Debug "Removed item: $contextId" + Write-Verbose "[$stackPath] - Removed item: $contextId" } } } diff --git a/src/functions/public/Set-Context.ps1 b/src/functions/public/Set-Context.ps1 index 10237e0f..41a09944 100644 --- a/src/functions/public/Set-Context.ps1 +++ b/src/functions/public/Set-Context.ps1 @@ -77,7 +77,7 @@ function Set-Context { process { $vaultObject = Set-ContextVault -Name $Vault - $vaultObject | Format-List | Out-String -Stream | ForEach-Object { Write-Debug $_ } + $vaultObject | Format-List | Out-String -Stream | ForEach-Object { Write-Verbose "[$stackPath] $_" } if ($context -is [System.Collections.IDictionary]) { $Context = [PSCustomObject]$Context @@ -92,7 +92,7 @@ function Set-Context { $contextInfo = Get-ContextInfo -ID $ID -Vault $Vault Write-Verbose 'Context info:' - $contextInfo | Format-List | Out-String -Stream | ForEach-Object { Write-Verbose $_ } + $contextInfo | Format-List | Out-String -Stream | ForEach-Object { Write-Verbose "[$stackPath] $_" } if (-not $contextInfo) { Write-Verbose "[$stackPath] - Creating context [$ID] in [$Vault]" $guid = [Guid]::NewGuid().Guid @@ -112,7 +112,7 @@ function Set-Context { Context = ConvertTo-SodiumSealedBox -Message $contextJson -PublicKey $keys.PublicKey } | ConvertTo-Json -Depth 5 Write-Verbose 'Content:' - $content | ConvertTo-Json -Depth 5 | Out-String -Stream | ForEach-Object { Write-Verbose $_ } + $content | ConvertTo-Json -Depth 5 | Out-String -Stream | ForEach-Object { Write-Verbose "[$stackPath] $_" } if ($PSCmdlet.ShouldProcess("file: [$contextPath]", 'Set content')) { Write-Verbose "[$stackPath] - Setting context [$ID] in vault [$Vault]"