From 37ce0412f527ec26e73a48853b241323f3ccbd17 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sun, 23 Feb 2025 12:12:12 +0100 Subject: [PATCH 1/2] Remove Get-PSModuleTest function and add Remove-Member function with tests --- src/functions/public/Get-PSModuleTest.ps1 | 20 ------- src/functions/public/Remove-Member.ps1 | 63 +++++++++++++++++++++++ tests/Object.Tests.ps1 | 27 ++++++++++ 3 files changed, 90 insertions(+), 20 deletions(-) delete mode 100644 src/functions/public/Get-PSModuleTest.ps1 create mode 100644 src/functions/public/Remove-Member.ps1 create mode 100644 tests/Object.Tests.ps1 diff --git a/src/functions/public/Get-PSModuleTest.ps1 b/src/functions/public/Get-PSModuleTest.ps1 deleted file mode 100644 index 0e9aacf..0000000 --- a/src/functions/public/Get-PSModuleTest.ps1 +++ /dev/null @@ -1,20 +0,0 @@ -#Requires -Modules Utilities - -function Get-PSModuleTest { - <# - .SYNOPSIS - Performs tests on a module. - - .EXAMPLE - Test-PSModule -Name 'World' - - "Hello, World!" - #> - [CmdletBinding()] - param ( - # Name of the person to greet. - [Parameter(Mandatory)] - [string] $Name - ) - Write-Output "Hello, $Name!" -} diff --git a/src/functions/public/Remove-Member.ps1 b/src/functions/public/Remove-Member.ps1 new file mode 100644 index 0000000..5fd0d0a --- /dev/null +++ b/src/functions/public/Remove-Member.ps1 @@ -0,0 +1,63 @@ +function Remove-Member { + <# + .SYNOPSIS + Removes specified members (properties) from an object. + + .DESCRIPTION + The Remove-Member function removes specified members (properties) from objects passed through the pipeline. + If the specified properties exist, they are removed; otherwise, a verbose message is displayed. + When the -PassThru switch is used, the modified object is returned. + + .EXAMPLE + $obj = [PSCustomObject]@{Name="John"; Age=30; City="New York"} + $obj | Remove-Member -Name "Age" -PassThru + + Output: + ```powershell + Name City + ---- ---- + John New York + ``` + + Removes the "Age" property from the PSCustomObject and returns the modified object. + + .OUTPUTS + PSCustomObject + + .NOTES + The modified object without the specified properties, if -PassThru is used. + + .LINK + https://psmodule.io/Object/Functions/Remove-Member + #> + [CmdletBinding()] + param( + # Accept input from the pipeline. + [Parameter(Mandatory, ValueFromPipeline)] + [System.Object] $InputObject, + + # Specify one or more member names to remove. + [Parameter(Mandatory)] + [string[]] $Name, + + # When specified, output the modified object. + [Parameter()] + [switch] $PassThru + ) + + process { + foreach ($obj in $InputObject) { + foreach ($member in $Name) { + if ($obj.PSObject.Properties[$member]) { + $null = $obj.PSObject.Properties.Remove($member) + } else { + Write-Verbose "Property '$member' not found on object of type $($obj.GetType().FullName)." + } + } + if ($PassThru) { + # Only output the object if -PassThru is used. + $obj + } + } + } +} diff --git a/tests/Object.Tests.ps1 b/tests/Object.Tests.ps1 new file mode 100644 index 0000000..c421086 --- /dev/null +++ b/tests/Object.Tests.ps1 @@ -0,0 +1,27 @@ +[Diagnostics.CodeAnalysis.SuppressMessageAttribute( + 'PSReviewUnusedParameter', '', + Justification = 'Required for Pester tests' +)] +[Diagnostics.CodeAnalysis.SuppressMessageAttribute( + 'PSUseDeclaredVarsMoreThanAssignments', '', + Justification = 'Required for Pester tests' +)] +[CmdletBinding()] +param() + +Describe 'Object' { + Context 'Function: Remove-Member' { + It 'Removes a member from an object' { + $object = [pscustomobject]@{ Name = 'Test'; Value = 'Value' } + $object | Remove-Member -Name Value + $object.PSObject.Properties | Should -Not -Contain 'Value' + } + + It 'Removes multiple members from an object' { + $object = [pscustomobject]@{ Name = 'Test'; Value = 'Value'; Other = 'Other' } + $object | Remove-Member -Name Value, Other + $object.PSObject.Properties | Should -Not -Contain 'Value' + $object.PSObject.Properties | Should -Not -Contain 'Other' + } + } +} From 0ca78d893b9ceef095225bd57006a8ba0c644f7e Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sun, 23 Feb 2025 12:23:20 +0100 Subject: [PATCH 2/2] Add suppression attribute to Remove-Member and enhance tests for property removal --- src/functions/public/Remove-Member.ps1 | 4 ++++ tests/Object.Tests.ps1 | 7 +++++++ tests/PSModuleTest.Tests.ps1 | 16 ---------------- 3 files changed, 11 insertions(+), 16 deletions(-) delete mode 100644 tests/PSModuleTest.Tests.ps1 diff --git a/src/functions/public/Remove-Member.ps1 b/src/functions/public/Remove-Member.ps1 index 5fd0d0a..39155b7 100644 --- a/src/functions/public/Remove-Member.ps1 +++ b/src/functions/public/Remove-Member.ps1 @@ -30,6 +30,10 @@ .LINK https://psmodule.io/Object/Functions/Remove-Member #> + [Diagnostics.CodeAnalysis.SuppressMessageAttribute( + 'PSUseShouldProcessForStateChangingFunctions', '', + Justification = 'Sets text in memory' + )] [CmdletBinding()] param( # Accept input from the pipeline. diff --git a/tests/Object.Tests.ps1 b/tests/Object.Tests.ps1 index c421086..ba1972d 100644 --- a/tests/Object.Tests.ps1 +++ b/tests/Object.Tests.ps1 @@ -23,5 +23,12 @@ Describe 'Object' { $object.PSObject.Properties | Should -Not -Contain 'Value' $object.PSObject.Properties | Should -Not -Contain 'Other' } + + It 'Removes properties of classes' { + $object = [System.Net.WebClient]::new() + $object | Add-Member -MemberType NoteProperty -Name Something -Value 'http://example.com' + $object | Remove-Member -Name BaseAddress + $object.PSObject.Properties | Should -Not -Contain 'BaseAddress' + } } } diff --git a/tests/PSModuleTest.Tests.ps1 b/tests/PSModuleTest.Tests.ps1 deleted file mode 100644 index 8258bb7..0000000 --- a/tests/PSModuleTest.Tests.ps1 +++ /dev/null @@ -1,16 +0,0 @@ -[Diagnostics.CodeAnalysis.SuppressMessageAttribute( - 'PSReviewUnusedParameter', '', - Justification = 'Required for Pester tests' -)] -[Diagnostics.CodeAnalysis.SuppressMessageAttribute( - 'PSUseDeclaredVarsMoreThanAssignments', '', - Justification = 'Required for Pester tests' -)] -[CmdletBinding()] -param() - -Describe 'Module' { - It 'Function: Get-PSModuleTest' { - Get-PSModuleTest -Name 'World' | Should -Be 'Hello, World!' - } -}