From ac80ea2c5a63a84d79a5659f36514b497b87660e Mon Sep 17 00:00:00 2001 From: Santiago Squarzon Date: Wed, 6 Nov 2024 18:08:31 -0300 Subject: [PATCH 1/2] fixes encoding transformation when string wrapped in psobject is passed as argument --- ...ingArgumentCompleter.cs => EncodingCompleter.cs} | 0 ...tTransformation.cs => EncodingTransformation.cs} | 13 +++++-------- src/PSCompression/ZipArchiveCache.cs | 2 +- src/PSCompression/ZipEntryBase.cs | 6 ------ src/PSCompression/ZipEntryCache.cs | 2 +- src/PSCompression/ZipEntryMoveCache.cs | 4 ++-- src/PSCompression/ZipEntryType.cs | 7 +++++++ tests/EncodingTransformation.tests.ps1 | 9 +++++++-- tests/ZipEntryCmdlets.tests.ps1 | 7 +++++++ 9 files changed, 30 insertions(+), 20 deletions(-) rename src/PSCompression/{EncodingArgumentCompleter.cs => EncodingCompleter.cs} (100%) rename src/PSCompression/{EncodingArgumentTransformation.cs => EncodingTransformation.cs} (84%) create mode 100644 src/PSCompression/ZipEntryType.cs diff --git a/src/PSCompression/EncodingArgumentCompleter.cs b/src/PSCompression/EncodingCompleter.cs similarity index 100% rename from src/PSCompression/EncodingArgumentCompleter.cs rename to src/PSCompression/EncodingCompleter.cs diff --git a/src/PSCompression/EncodingArgumentTransformation.cs b/src/PSCompression/EncodingTransformation.cs similarity index 84% rename from src/PSCompression/EncodingArgumentTransformation.cs rename to src/PSCompression/EncodingTransformation.cs index 683e7a5..0d48acb 100644 --- a/src/PSCompression/EncodingArgumentTransformation.cs +++ b/src/PSCompression/EncodingTransformation.cs @@ -9,21 +9,19 @@ public sealed class EncodingTransformation : ArgumentTransformationAttribute { public override object Transform( EngineIntrinsics engineIntrinsics, - object inputData) - { - return inputData switch + object inputData) => + 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) - { - return str.ToLowerInvariant() switch + private Encoding ParseStringEncoding(string str) => + str.ToLowerInvariant() switch { "ascii" => new ASCIIEncoding(), "bigendianunicode" => new UnicodeEncoding(true, true), @@ -37,7 +35,6 @@ private Encoding ParseStringEncoding(string str) "ansi" => Encoding.GetEncoding(GetACP()), _ => Encoding.GetEncoding(str), }; - } [DllImport("Kernel32.dll")] private static extern int GetACP(); diff --git a/src/PSCompression/ZipArchiveCache.cs b/src/PSCompression/ZipArchiveCache.cs index 8d7599c..566895f 100644 --- a/src/PSCompression/ZipArchiveCache.cs +++ b/src/PSCompression/ZipArchiveCache.cs @@ -14,7 +14,7 @@ internal sealed class ZipArchiveCache : IDisposable internal ZipArchiveCache(ZipArchiveMode mode) { - _cache = new Dictionary(); + _cache = []; _mode = mode; } diff --git a/src/PSCompression/ZipEntryBase.cs b/src/PSCompression/ZipEntryBase.cs index e0b664d..18e92bb 100644 --- a/src/PSCompression/ZipEntryBase.cs +++ b/src/PSCompression/ZipEntryBase.cs @@ -6,12 +6,6 @@ namespace PSCompression; -public enum ZipEntryType -{ - Directory = 0, - Archive = 1 -} - public abstract class ZipEntryBase { protected string? _formatDirectoryPath; diff --git a/src/PSCompression/ZipEntryCache.cs b/src/PSCompression/ZipEntryCache.cs index 20f5b97..e8c3a67 100644 --- a/src/PSCompression/ZipEntryCache.cs +++ b/src/PSCompression/ZipEntryCache.cs @@ -14,7 +14,7 @@ internal List WithSource(string source) { if (!_cache.ContainsKey(source)) { - _cache[source] = new(); + _cache[source] = []; } return _cache[source]; diff --git a/src/PSCompression/ZipEntryMoveCache.cs b/src/PSCompression/ZipEntryMoveCache.cs index 5a82e91..2e0f389 100644 --- a/src/PSCompression/ZipEntryMoveCache.cs +++ b/src/PSCompression/ZipEntryMoveCache.cs @@ -16,14 +16,14 @@ internal sealed class ZipEntryMoveCache internal ZipEntryMoveCache() { _cache = new(StringComparer.InvariantCultureIgnoreCase); - _mappings = new(); + _mappings = []; } private Dictionary WithSource(ZipEntryBase entry) { if (!_cache.ContainsKey(entry.Source)) { - _cache[entry.Source] = new(); + _cache[entry.Source] = []; } return _cache[entry.Source]; diff --git a/src/PSCompression/ZipEntryType.cs b/src/PSCompression/ZipEntryType.cs new file mode 100644 index 0000000..0b875b6 --- /dev/null +++ b/src/PSCompression/ZipEntryType.cs @@ -0,0 +1,7 @@ +namespace PSCompression; + +public enum ZipEntryType +{ + Directory = 0, + Archive = 1 +} diff --git a/tests/EncodingTransformation.tests.ps1 b/tests/EncodingTransformation.tests.ps1 index fba5337..ec9e4fe 100644 --- a/tests/EncodingTransformation.tests.ps1 +++ b/tests/EncodingTransformation.tests.ps1 @@ -36,7 +36,12 @@ Describe 'EncodingTransformation Class' { $transform | Out-Null } - It 'Transform a completion set to their Encoding Representations' { + It 'Transforms Encoding to Encoding' { + $transform.Transform($ExecutionContext, [System.Text.Encoding]::UTF8) | + Should -BeExactly ([System.Text.Encoding]::UTF8) + } + + It 'Transforms a completion set to their Encoding Representations' { $encodings.GetEnumerator() | ForEach-Object { $transform.Transform($ExecutionContext, $_.Key) | Should -BeExactly $_.Value @@ -50,7 +55,7 @@ Describe 'EncodingTransformation Class' { } } - It 'Throws if it cant transform' { + It 'Throws if input value cannot be transformed' { { $transform.Transform($ExecutionContext, 'doesnotexist') } | Should -Throw } diff --git a/tests/ZipEntryCmdlets.tests.ps1 b/tests/ZipEntryCmdlets.tests.ps1 index e2107f3..53cf085 100644 --- a/tests/ZipEntryCmdlets.tests.ps1 +++ b/tests/ZipEntryCmdlets.tests.ps1 @@ -158,6 +158,13 @@ Describe 'ZipEntry Cmdlets' { Should -BeOfType ([string]) } + It 'Should not throw when a string wrapped in PSObject is passed-in as Encdoing' { + $enc = Write-Output utf8 + $zip | Get-ZipEntry -Type Archive | + Get-ZipEntryContent -Encoding $enc | + Should -BeOfType ([string]) + } + It 'Can read bytes from zip file entries' { $zip | Get-ZipEntry -Type Archive | Get-ZipEntryContent -AsByteStream | From af26a09ab63869e8ed5d94fc475b8455a2d1f06e Mon Sep 17 00:00:00 2001 From: Santiago Squarzon Date: Wed, 6 Nov 2024 18:08:56 -0300 Subject: [PATCH 2/2] fixes encoding transformation when string wrapped in psobject is passed as argument --- tests/ZipEntryCmdlets.tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ZipEntryCmdlets.tests.ps1 b/tests/ZipEntryCmdlets.tests.ps1 index 53cf085..31627aa 100644 --- a/tests/ZipEntryCmdlets.tests.ps1 +++ b/tests/ZipEntryCmdlets.tests.ps1 @@ -158,7 +158,7 @@ Describe 'ZipEntry Cmdlets' { Should -BeOfType ([string]) } - It 'Should not throw when a string wrapped in PSObject is passed-in as Encdoing' { + It 'Should not throw when a string wrapped in PSObject is passed as Encdoing argument' { $enc = Write-Output utf8 $zip | Get-ZipEntry -Type Archive | Get-ZipEntryContent -Encoding $enc |