From 8423d81e6241dfea5e522015c3a6b77a6b264db5 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 27 May 2025 22:57:38 +0000 Subject: [PATCH 1/5] Initial plan for issue From 532c5b0f1ae7e9246e3b640760debd0dd998919d Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 27 May 2025 23:03:16 +0000 Subject: [PATCH 2/5] Update functions to take array input via pipeline Co-authored-by: MariusStorhaug <17722253+MariusStorhaug@users.noreply.github.com> --- src/functions/public/ConvertFrom-Base64.ps1 | 29 ++++++++++++++----- src/functions/public/ConvertTo-Base64.ps1 | 32 +++++++++++++++------ tests/Base64.Tests.ps1 | 28 ++++++++++++++++++ 3 files changed, 74 insertions(+), 15 deletions(-) diff --git a/src/functions/public/ConvertFrom-Base64.ps1 b/src/functions/public/ConvertFrom-Base64.ps1 index 1162800..65fd60b 100644 --- a/src/functions/public/ConvertFrom-Base64.ps1 +++ b/src/functions/public/ConvertFrom-Base64.ps1 @@ -1,10 +1,10 @@ -filter ConvertFrom-Base64 { +function ConvertFrom-Base64 { <# .SYNOPSIS - Decodes a base64-encoded string into a UTF-8 string. + Decodes a base64-encoded string or array of strings into UTF-8 strings. .DESCRIPTION - Converts a base64-encoded string into a human-readable UTF-8 string. The function accepts input from the pipeline + Converts a base64-encoded string or array of strings into human-readable UTF-8 strings. The function accepts input from the pipeline and validates the input using the `Test-Base64` function before decoding. .EXAMPLE @@ -17,11 +17,22 @@ Decodes the base64-encoded string "U29tZSBkYXRh" into its original UTF-8 representation. + .EXAMPLE + @("SGVsbG8=", "V29ybGQ=") | ConvertFrom-Base64 + + Output: + ```powershell + Hello + World + ``` + + Decodes each base64-encoded string in the array into its original UTF-8 representation. + .OUTPUTS System.String .NOTES - The decoded UTF-8 string. + The decoded UTF-8 string(s). .LINK https://psmodule.io/Base64/Functions/ConvertFrom-Base64/ @@ -30,14 +41,14 @@ [OutputType([string])] [CmdletBinding()] param( - # The base64-encoded string to be decoded. + # The base64-encoded string or array of strings to be decoded. [Parameter( Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName )] [ValidateScript({ Test-Base64 -Base64String $_ }, ErrorMessage = 'Invalid Base64 string')] - [string] $Base64String, + [string[]] $Base64String, # The encoding to use when converting the string to bytes. [Parameter()] @@ -45,5 +56,9 @@ [string] $Encoding = 'UTF8' ) - [System.Text.Encoding]::$Encoding.GetString([Convert]::FromBase64String($Base64String)) + process { + foreach ($item in $Base64String) { + [System.Text.Encoding]::$Encoding.GetString([Convert]::FromBase64String($item)) + } + } } diff --git a/src/functions/public/ConvertTo-Base64.ps1 b/src/functions/public/ConvertTo-Base64.ps1 index 40dbd13..06185f6 100644 --- a/src/functions/public/ConvertTo-Base64.ps1 +++ b/src/functions/public/ConvertTo-Base64.ps1 @@ -1,11 +1,11 @@ -filter ConvertTo-Base64 { +function ConvertTo-Base64 { <# .SYNOPSIS - Converts a string to its base64 encoded representation. + Converts a string or array of strings to their base64 encoded representation. .DESCRIPTION - This function takes a string as input and converts it to a base64 encoded string using UTF-8 encoding. - It accepts input from the pipeline and can process string values directly. + This function takes a string or array of strings as input and converts them to base64 encoded strings using UTF-8 encoding. + It accepts input from the pipeline and can process string values directly or as an array. .EXAMPLE "Hello World" | ConvertTo-Base64 @@ -17,11 +17,22 @@ Converts the string "Hello World" to its base64 encoded equivalent. + .EXAMPLE + @("Hello", "World") | ConvertTo-Base64 + + Output: + ```powershell + SGVsbG8= + V29ybGQ= + ``` + + Converts each string in the array to its base64 encoded equivalent. + .OUTPUTS System.String .NOTES - The base64 encoded representation of the input string. + The base64 encoded representation of the input string(s). .LINK https://psmodule.io/Base64/Functions/ConvertTo-Base64/ @@ -30,18 +41,23 @@ [OutputType([string])] [CmdletBinding()] param( - # The input string to be converted to base64 encoding. + # The input string or array of strings to be converted to base64 encoding. [Parameter( Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName )] - [string] $String, + [string[]] $String, # The encoding to use when converting the string to bytes. [Parameter()] [ValidateSet('UTF8', 'UTF7', 'UTF32', 'ASCII', 'Unicode', 'BigEndianUnicode', 'Latin1')] [string] $Encoding = 'UTF8' ) - [Convert]::ToBase64String([System.Text.Encoding]::$Encoding.GetBytes($String)) + + process { + foreach ($item in $String) { + [Convert]::ToBase64String([System.Text.Encoding]::$Encoding.GetBytes($item)) + } + } } diff --git a/tests/Base64.Tests.ps1 b/tests/Base64.Tests.ps1 index 457967d..28becee 100644 --- a/tests/Base64.Tests.ps1 +++ b/tests/Base64.Tests.ps1 @@ -15,6 +15,20 @@ It "'Hello World' | ConvertTo-Base64 -> SGVsbG8gV29ybGQ=" { 'Hello World' | ConvertTo-Base64 | Should -Be 'SGVsbG8gV29ybGQ=' } + + It "ConvertTo-Base64 -String @('Hello', 'World') -> @('SGVsbG8=', 'V29ybGQ=')" { + $result = ConvertTo-Base64 -String @('Hello', 'World') + $result.Count | Should -Be 2 + $result[0] | Should -Be 'SGVsbG8=' + $result[1] | Should -Be 'V29ybGQ=' + } + + It "@('Hello', 'World') | ConvertTo-Base64 -> @('SGVsbG8=', 'V29ybGQ=')" { + $result = @('Hello', 'World') | ConvertTo-Base64 + $result.Count | Should -Be 2 + $result[0] | Should -Be 'SGVsbG8=' + $result[1] | Should -Be 'V29ybGQ=' + } } Context 'Function: ConvertFrom-Base64' { It "ConvertFrom-Base64 -Base64String 'VGhpc0lzQU5pY2VTdHJpbmc=' -> ThisIsANiceString" { @@ -24,5 +38,19 @@ It "'SGVsbG8gV29ybGQ=' | ConvertFrom-Base64 -> Hello World" { 'SGVsbG8gV29ybGQ=' | ConvertFrom-Base64 | Should -Be 'Hello World' } + + It "ConvertFrom-Base64 -Base64String @('SGVsbG8=', 'V29ybGQ=') -> @('Hello', 'World')" { + $result = ConvertFrom-Base64 -Base64String @('SGVsbG8=', 'V29ybGQ=') + $result.Count | Should -Be 2 + $result[0] | Should -Be 'Hello' + $result[1] | Should -Be 'World' + } + + It "@('SGVsbG8=', 'V29ybGQ=') | ConvertFrom-Base64 -> @('Hello', 'World')" { + $result = @('SGVsbG8=', 'V29ybGQ=') | ConvertFrom-Base64 + $result.Count | Should -Be 2 + $result[0] | Should -Be 'Hello' + $result[1] | Should -Be 'World' + } } } From 7621f82f7ecb6318d488ae2b2b9bc1e327beac0a Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 27 May 2025 23:19:01 +0000 Subject: [PATCH 3/5] Fix linting issues by removing trailing whitespace in tests Co-authored-by: MariusStorhaug <17722253+MariusStorhaug@users.noreply.github.com> --- tests/Base64.Tests.ps1 | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/Base64.Tests.ps1 b/tests/Base64.Tests.ps1 index 28becee..a295d42 100644 --- a/tests/Base64.Tests.ps1 +++ b/tests/Base64.Tests.ps1 @@ -15,7 +15,6 @@ It "'Hello World' | ConvertTo-Base64 -> SGVsbG8gV29ybGQ=" { 'Hello World' | ConvertTo-Base64 | Should -Be 'SGVsbG8gV29ybGQ=' } - It "ConvertTo-Base64 -String @('Hello', 'World') -> @('SGVsbG8=', 'V29ybGQ=')" { $result = ConvertTo-Base64 -String @('Hello', 'World') $result.Count | Should -Be 2 @@ -38,7 +37,6 @@ It "'SGVsbG8gV29ybGQ=' | ConvertFrom-Base64 -> Hello World" { 'SGVsbG8gV29ybGQ=' | ConvertFrom-Base64 | Should -Be 'Hello World' } - It "ConvertFrom-Base64 -Base64String @('SGVsbG8=', 'V29ybGQ=') -> @('Hello', 'World')" { $result = ConvertFrom-Base64 -Base64String @('SGVsbG8=', 'V29ybGQ=') $result.Count | Should -Be 2 From 5115fc6bbc3152d7f69c030f857a625a51769901 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 27 May 2025 23:41:11 +0000 Subject: [PATCH 4/5] Add array variable and file content tests Co-authored-by: MariusStorhaug <17722253+MariusStorhaug@users.noreply.github.com> --- tests/Base64.Tests.ps1 | 35 +++++++++++++++++++++++++++++++++++ tests/Run-Tests.ps1 | 25 +++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 tests/Run-Tests.ps1 diff --git a/tests/Base64.Tests.ps1 b/tests/Base64.Tests.ps1 index a295d42..4cc989b 100644 --- a/tests/Base64.Tests.ps1 +++ b/tests/Base64.Tests.ps1 @@ -28,6 +28,23 @@ $result[0] | Should -Be 'SGVsbG8=' $result[1] | Should -Be 'V29ybGQ=' } + + It "Variable containing multiple strings piped to ConvertTo-Base64" { + $strings = @('Hello', 'World') + $result = $strings | ConvertTo-Base64 + $result.Count | Should -Be 2 + $result[0] | Should -Be 'SGVsbG8=' + $result[1] | Should -Be 'V29ybGQ=' + } + + It "File content piped to ConvertTo-Base64" { + $testFilePath = '/tmp/base64_test/test_content.txt' + $result = Get-Content -Path $testFilePath | ConvertTo-Base64 + $result.Count | Should -Be 3 + $result[0] | Should -Be 'TGluZSAx' + $result[1] | Should -Be 'TGluZSAy' + $result[2] | Should -Be 'TGluZSAz' + } } Context 'Function: ConvertFrom-Base64' { It "ConvertFrom-Base64 -Base64String 'VGhpc0lzQU5pY2VTdHJpbmc=' -> ThisIsANiceString" { @@ -50,5 +67,23 @@ $result[0] | Should -Be 'Hello' $result[1] | Should -Be 'World' } + + It "Variable containing multiple Base64 strings piped to ConvertFrom-Base64" { + $base64Strings = @('SGVsbG8=', 'V29ybGQ=') + $result = $base64Strings | ConvertFrom-Base64 + $result.Count | Should -Be 2 + $result[0] | Should -Be 'Hello' + $result[1] | Should -Be 'World' + } + + It "File content with Base64 strings piped to ConvertFrom-Base64" { + $testBase64FilePath = '/tmp/base64_test/test_base64_content.txt' + @('TGluZSAx', 'TGluZSAy', 'TGluZSAz') | Out-File -FilePath $testBase64FilePath + $result = Get-Content -Path $testBase64FilePath | ConvertFrom-Base64 + $result.Count | Should -Be 3 + $result[0] | Should -Be 'Line 1' + $result[1] | Should -Be 'Line 2' + $result[2] | Should -Be 'Line 3' + } } } diff --git a/tests/Run-Tests.ps1 b/tests/Run-Tests.ps1 new file mode 100644 index 0000000..1d8c426 --- /dev/null +++ b/tests/Run-Tests.ps1 @@ -0,0 +1,25 @@ +# Import functions directly +. "$PSScriptRoot/../src/functions/public/Test-Base64.ps1" +. "$PSScriptRoot/../src/functions/public/ConvertTo-Base64.ps1" +. "$PSScriptRoot/../src/functions/public/ConvertFrom-Base64.ps1" + +# Create test files +$testContentPath = '/tmp/base64_test/test_content.txt' +if (-not (Test-Path $testContentPath)) { + New-Item -ItemType Directory -Path '/tmp/base64_test' -Force | Out-Null + @" +Line 1 +Line 2 +Line 3 +"@ | Out-File -FilePath $testContentPath -Encoding UTF8 +} + +$testBase64Path = '/tmp/base64_test/test_base64_content.txt' +@" +TGluZSAx +TGluZSAy +TGluZSAz +"@ | Out-File -FilePath $testBase64Path -Encoding UTF8 + +# Run tests +Invoke-Pester -Path "$PSScriptRoot/Base64.Tests.ps1" -PassThru \ No newline at end of file From de305c69515c05cfef8bc7e6c5bc92ffc76a8a6c Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 28 May 2025 00:43:08 +0000 Subject: [PATCH 5/5] Move Run-Tests.ps1 to BeforeAll block in tests Co-authored-by: MariusStorhaug <17722253+MariusStorhaug@users.noreply.github.com> --- tests/Base64.Tests.ps1 | 27 ++++++++++++++++++++++++++- tests/Run-Tests.ps1 | 25 ------------------------- 2 files changed, 26 insertions(+), 26 deletions(-) delete mode 100644 tests/Run-Tests.ps1 diff --git a/tests/Base64.Tests.ps1 b/tests/Base64.Tests.ps1 index 4cc989b..9d0002d 100644 --- a/tests/Base64.Tests.ps1 +++ b/tests/Base64.Tests.ps1 @@ -1,4 +1,29 @@ -Describe 'Base64' { +BeforeAll { + # Import functions directly + . "$PSScriptRoot/../src/functions/public/Test-Base64.ps1" + . "$PSScriptRoot/../src/functions/public/ConvertTo-Base64.ps1" + . "$PSScriptRoot/../src/functions/public/ConvertFrom-Base64.ps1" + + # Create test files + $testContentPath = '/tmp/base64_test/test_content.txt' + if (-not (Test-Path $testContentPath)) { + New-Item -ItemType Directory -Path '/tmp/base64_test' -Force | Out-Null + @" +Line 1 +Line 2 +Line 3 +"@ | Out-File -FilePath $testContentPath -Encoding UTF8 + } + + $testBase64Path = '/tmp/base64_test/test_base64_content.txt' + @" +TGluZSAx +TGluZSAy +TGluZSAz +"@ | Out-File -FilePath $testBase64Path -Encoding UTF8 +} + +Describe 'Base64' { Context 'Function: Test-Base64' { It "Test-Base64 -Base64String 'VGhpc0lzQU5pY2VTdHJpbmc=' -> true" { Test-Base64 -Base64String 'VGhpc0lzQU5pY2VTdHJpbmc=' | Should -Be $true diff --git a/tests/Run-Tests.ps1 b/tests/Run-Tests.ps1 deleted file mode 100644 index 1d8c426..0000000 --- a/tests/Run-Tests.ps1 +++ /dev/null @@ -1,25 +0,0 @@ -# Import functions directly -. "$PSScriptRoot/../src/functions/public/Test-Base64.ps1" -. "$PSScriptRoot/../src/functions/public/ConvertTo-Base64.ps1" -. "$PSScriptRoot/../src/functions/public/ConvertFrom-Base64.ps1" - -# Create test files -$testContentPath = '/tmp/base64_test/test_content.txt' -if (-not (Test-Path $testContentPath)) { - New-Item -ItemType Directory -Path '/tmp/base64_test' -Force | Out-Null - @" -Line 1 -Line 2 -Line 3 -"@ | Out-File -FilePath $testContentPath -Encoding UTF8 -} - -$testBase64Path = '/tmp/base64_test/test_base64_content.txt' -@" -TGluZSAx -TGluZSAy -TGluZSAz -"@ | Out-File -FilePath $testBase64Path -Encoding UTF8 - -# Run tests -Invoke-Pester -Path "$PSScriptRoot/Base64.Tests.ps1" -PassThru \ No newline at end of file