Skip to content

Commit 15cf8e4

Browse files
authored
Merge pull request #37 from santisq/36-fix-encodingtransformation-for-encoding-and-int-instances-wrapped-in-psobject
Fix `EncodingTransformation` for `Encoding` and `int` instances wrapped in `PSObject`
2 parents ef826cd + f397433 commit 15cf8e4

3 files changed

Lines changed: 20 additions & 8 deletions

File tree

module/PSCompression.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
RootModule = 'bin/netstandard2.0/PSCompression.dll'
1212

1313
# Version number of this module.
14-
ModuleVersion = '2.0.9'
14+
ModuleVersion = '2.0.10'
1515

1616
# Supported PSEditions
1717
# CompatiblePSEditions = @()

src/PSCompression/EncodingTransformation.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,21 @@ public sealed class EncodingTransformation : ArgumentTransformationAttribute
99
{
1010
public override object Transform(
1111
EngineIntrinsics engineIntrinsics,
12-
object inputData) =>
13-
inputData switch
12+
object inputData)
13+
{
14+
inputData = inputData is PSObject pso
15+
? pso.BaseObject
16+
: inputData;
17+
18+
return inputData switch
1419
{
1520
Encoding enc => enc,
1621
int num => Encoding.GetEncoding(num),
1722
string str => ParseStringEncoding(str),
18-
PSObject pso when pso.BaseObject is string str => ParseStringEncoding(str),
1923
_ => throw new ArgumentTransformationMetadataException(
2024
$"Could not convert input '{inputData}' to a valid Encoding object."),
2125
};
26+
}
2227

2328
private Encoding ParseStringEncoding(string str) =>
2429
str.ToLowerInvariant() switch

tests/ZipEntryCmdlets.tests.ps1

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,18 @@ Describe 'ZipEntry Cmdlets' {
158158
Should -BeOfType ([string])
159159
}
160160

161-
It 'Should not throw when a string wrapped in PSObject is passed as Encdoing argument' {
161+
It 'Should not throw when an instance wrapped in PSObject is passed as Encdoing argument' {
162162
$enc = Write-Output utf8
163-
$zip | Get-ZipEntry -Type Archive |
164-
Get-ZipEntryContent -Encoding $enc |
165-
Should -BeOfType ([string])
163+
{ $zip | Get-ZipEntry -Type Archive | Get-ZipEntryContent -Encoding $enc } |
164+
Should -Not -Throw
165+
166+
$enc = [System.Text.Encoding]::UTF8 | Write-Output
167+
{ $zip | Get-ZipEntry -Type Archive | Get-ZipEntryContent -Encoding $enc } |
168+
Should -Not -Throw
169+
170+
$enc = [System.Text.Encoding]::UTF8.CodePage | Write-Output
171+
{ $zip | Get-ZipEntry -Type Archive | Get-ZipEntryContent -Encoding $enc } |
172+
Should -Not -Throw
166173
}
167174

168175
It 'Can read bytes from zip file entries' {

0 commit comments

Comments
 (0)