From c4b242753946e7d90554d058ac5aead580a52eae Mon Sep 17 00:00:00 2001 From: DrIOS <58635327+DrIOSX@users.noreply.github.com> Date: Fri, 14 Jun 2024 13:37:38 -0500 Subject: [PATCH] add: Function to remove test rows with no results --- .../Public/Remove-RowsWithEmptyCSVStatus.ps1 | 34 +++++++++ .../Remove-RowsWithEmptyCSVStatus.tests.ps1 | 71 +++++++++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 source/Public/Remove-RowsWithEmptyCSVStatus.ps1 create mode 100644 tests/Unit/Public/Remove-RowsWithEmptyCSVStatus.tests.ps1 diff --git a/source/Public/Remove-RowsWithEmptyCSVStatus.ps1 b/source/Public/Remove-RowsWithEmptyCSVStatus.ps1 new file mode 100644 index 0000000..7258500 --- /dev/null +++ b/source/Public/Remove-RowsWithEmptyCSVStatus.ps1 @@ -0,0 +1,34 @@ +function Remove-RowsWithEmptyCSVStatus { + [CmdletBinding()] + param ( + [Parameter(Mandatory = $true)] + [string]$FilePath, + + [Parameter(Mandatory = $true)] + [string]$WorksheetName + ) + + # Import the Excel file + $ExcelData = Import-Excel -Path $FilePath -WorksheetName $WorksheetName + + # Check if CSV_Status column exists + if (-not $ExcelData.PSObject.Properties.Match("CSV_Status")) { + throw "CSV_Status column not found in the worksheet." + } + + # Filter rows where CSV_Status is not empty + $FilteredData = $ExcelData | Where-Object { $null -ne $_.CSV_Status -and $_.CSV_Status -ne '' } + + # Get the original file name and directory + $OriginalFileName = [System.IO.Path]::GetFileNameWithoutExtension($FilePath) + $Directory = [System.IO.Path]::GetDirectoryName($FilePath) + + # Create a new file name for the filtered data + $NewFileName = "$OriginalFileName-Filtered.xlsx" + $NewFilePath = Join-Path -Path $Directory -ChildPath $NewFileName + + # Export the filtered data to a new Excel file + $FilteredData | Export-Excel -Path $NewFilePath -WorksheetName $WorksheetName -Show + + Write-Output "Filtered Excel file created at $NewFilePath" +} \ No newline at end of file diff --git a/tests/Unit/Public/Remove-RowsWithEmptyCSVStatus.tests.ps1 b/tests/Unit/Public/Remove-RowsWithEmptyCSVStatus.tests.ps1 new file mode 100644 index 0000000..5998a20 --- /dev/null +++ b/tests/Unit/Public/Remove-RowsWithEmptyCSVStatus.tests.ps1 @@ -0,0 +1,71 @@ +BeforeAll { + $script:moduleName = '<% $PLASTER_PARAM_ModuleName %>' + + # If the module is not found, run the build task 'noop'. + if (-not (Get-Module -Name $script:moduleName -ListAvailable)) + { + # Redirect all streams to $null, except the error stream (stream 2) + & "$PSScriptRoot/../../build.ps1" -Tasks 'noop' 2>&1 4>&1 5>&1 6>&1 > $null + } + + # Re-import the module using force to get any code changes between runs. + Import-Module -Name $script:moduleName -Force -ErrorAction 'Stop' + + $PSDefaultParameterValues['InModuleScope:ModuleName'] = $script:moduleName + $PSDefaultParameterValues['Mock:ModuleName'] = $script:moduleName + $PSDefaultParameterValues['Should:ModuleName'] = $script:moduleName +} + +AfterAll { + $PSDefaultParameterValues.Remove('Mock:ModuleName') + $PSDefaultParameterValues.Remove('InModuleScope:ModuleName') + $PSDefaultParameterValues.Remove('Should:ModuleName') + + Remove-Module -Name $script:moduleName +} + +Describe Get-Something { + + Context 'Return values' { + BeforeEach { + $return = Get-Something -Data 'value' + } + + It 'Returns a single object' { + ($return | Measure-Object).Count | Should -Be 1 + } + + } + + Context 'Pipeline' { + It 'Accepts values from the pipeline by value' { + $return = 'value1', 'value2' | Get-Something + + $return[0] | Should -Be 'value1' + $return[1] | Should -Be 'value2' + } + + It 'Accepts value from the pipeline by property name' { + $return = 'value1', 'value2' | ForEach-Object { + [PSCustomObject]@{ + Data = $_ + OtherProperty = 'other' + } + } | Get-Something + + + $return[0] | Should -Be 'value1' + $return[1] | Should -Be 'value2' + } + } + + Context 'ShouldProcess' { + It 'Supports WhatIf' { + (Get-Command Get-Something).Parameters.ContainsKey('WhatIf') | Should -Be $true + { Get-Something -Data 'value' -WhatIf } | Should -Not -Throw + } + + + } +} +