Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix EncodingTransformation for Encoding and int instances wrapped in PSObject #37

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion module/PSCompression.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
RootModule = 'bin/netstandard2.0/PSCompression.dll'

# Version number of this module.
ModuleVersion = '2.0.9'
ModuleVersion = '2.0.10'

# Supported PSEditions
# CompatiblePSEditions = @()
Expand Down
11 changes: 8 additions & 3 deletions src/PSCompression/EncodingTransformation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,21 @@ public sealed class EncodingTransformation : ArgumentTransformationAttribute
{
public override object Transform(
EngineIntrinsics engineIntrinsics,
object inputData) =>
inputData switch
object inputData)
{
inputData = inputData is PSObject pso
? pso.BaseObject
: inputData;

return inputData switch
{
Encoding enc => enc,
int num => Encoding.GetEncoding(num),
string str => ParseStringEncoding(str),
PSObject pso when pso.BaseObject is string str => ParseStringEncoding(str),
_ => throw new ArgumentTransformationMetadataException(
$"Could not convert input '{inputData}' to a valid Encoding object."),
};
}

private Encoding ParseStringEncoding(string str) =>
str.ToLowerInvariant() switch
Expand Down
15 changes: 11 additions & 4 deletions tests/ZipEntryCmdlets.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,18 @@ Describe 'ZipEntry Cmdlets' {
Should -BeOfType ([string])
}

It 'Should not throw when a string wrapped in PSObject is passed as Encdoing argument' {
It 'Should not throw when an instance wrapped in PSObject is passed as Encdoing argument' {
$enc = Write-Output utf8
$zip | Get-ZipEntry -Type Archive |
Get-ZipEntryContent -Encoding $enc |
Should -BeOfType ([string])
{ $zip | Get-ZipEntry -Type Archive | Get-ZipEntryContent -Encoding $enc } |
Should -Not -Throw

$enc = [System.Text.Encoding]::UTF8 | Write-Output
{ $zip | Get-ZipEntry -Type Archive | Get-ZipEntryContent -Encoding $enc } |
Should -Not -Throw

$enc = [System.Text.Encoding]::UTF8.CodePage | Write-Output
{ $zip | Get-ZipEntry -Type Archive | Get-ZipEntryContent -Encoding $enc } |
Should -Not -Throw
}

It 'Can read bytes from zip file entries' {
Expand Down