Skip to content

Commit

Permalink
Merge pull request #35 from santisq/34-fix-encodingtransformation-whe…
Browse files Browse the repository at this point in the history
…n-passing-a-string-wrapped-in-psobject

fixes #34
  • Loading branch information
santisq authored Nov 6, 2024
2 parents 886c9e2 + af26a09 commit df0c22d
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 20 deletions.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -37,7 +35,6 @@ private Encoding ParseStringEncoding(string str)
"ansi" => Encoding.GetEncoding(GetACP()),
_ => Encoding.GetEncoding(str),
};
}

[DllImport("Kernel32.dll")]
private static extern int GetACP();
Expand Down
2 changes: 1 addition & 1 deletion src/PSCompression/ZipArchiveCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ internal sealed class ZipArchiveCache : IDisposable

internal ZipArchiveCache(ZipArchiveMode mode)
{
_cache = new Dictionary<string, ZipArchive>();
_cache = [];
_mode = mode;
}

Expand Down
6 changes: 0 additions & 6 deletions src/PSCompression/ZipEntryBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@

namespace PSCompression;

public enum ZipEntryType
{
Directory = 0,
Archive = 1
}

public abstract class ZipEntryBase
{
protected string? _formatDirectoryPath;
Expand Down
2 changes: 1 addition & 1 deletion src/PSCompression/ZipEntryCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ internal List<PathWithType> WithSource(string source)
{
if (!_cache.ContainsKey(source))
{
_cache[source] = new();
_cache[source] = [];
}

return _cache[source];
Expand Down
4 changes: 2 additions & 2 deletions src/PSCompression/ZipEntryMoveCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ internal sealed class ZipEntryMoveCache
internal ZipEntryMoveCache()
{
_cache = new(StringComparer.InvariantCultureIgnoreCase);
_mappings = new();
_mappings = [];
}

private Dictionary<string, EntryWithPath> WithSource(ZipEntryBase entry)
{
if (!_cache.ContainsKey(entry.Source))
{
_cache[entry.Source] = new();
_cache[entry.Source] = [];
}

return _cache[entry.Source];
Expand Down
7 changes: 7 additions & 0 deletions src/PSCompression/ZipEntryType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace PSCompression;

public enum ZipEntryType
{
Directory = 0,
Archive = 1
}
9 changes: 7 additions & 2 deletions tests/EncodingTransformation.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand Down
7 changes: 7 additions & 0 deletions tests/ZipEntryCmdlets.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,13 @@ Describe 'ZipEntry Cmdlets' {
Should -BeOfType ([string])
}

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 |
Should -BeOfType ([string])
}

It 'Can read bytes from zip file entries' {
$zip | Get-ZipEntry -Type Archive |
Get-ZipEntryContent -AsByteStream |
Expand Down

0 comments on commit df0c22d

Please sign in to comment.