Skip to content

Commit

Permalink
Fix psobject to string array (pester#1924)
Browse files Browse the repository at this point in the history
* Tests

* Fix assignment from Resolve-Path

* Don't skip convert from T to T
  • Loading branch information
nohwnd authored Apr 29, 2021
1 parent 0f073a0 commit b793235
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
32 changes: 30 additions & 2 deletions src/csharp/Pester/DictionaryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,46 @@ public static T[] GetArrayOrNull<T>(this IDictionary dictionary, string key) whe
{
if (!dictionary.Contains(key))
return null;

var value = dictionary[key];

if (value.GetType() == typeof(T[]))
{
return (T[])value;
}

if (value.GetType() == typeof(object[]))
if (typeof(T) == typeof(string))
if (value is PathInfo o)
return new[] { (T)Convert.ChangeType(o.ToString(), typeof(string)) };

if (typeof(T) == typeof(string))
if (dictionary[key] is PSObject o)
return new[] { (T)Convert.ChangeType(o.ToString(), typeof(string)) };

if (value is object[] v)
{
try
{
return ((object[])value).Cast<T>().ToArray();
var arr = new T[v.Length];

var i = 0;
foreach (var j in v)
{
if (j is T)
{
arr[i] = (T) j;
}

if (j is PSObject || j is PathInfo)
{
if (typeof(T) == typeof(string))
{
arr[i] = (T)Convert.ChangeType(j.ToString(), typeof(string));
}
}
i++;
}
return arr;
}
catch { }
}
Expand Down
7 changes: 7 additions & 0 deletions src/csharp/Pester/StringArrayOption.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Linq;
using System.Management.Automation;

// those types implement Pester configuration in a way that allows it to show information about each item
// in the powershell console without making it difficult to use. there are two tricks being used:
Expand Down Expand Up @@ -61,5 +62,11 @@ public static implicit operator StringArrayOption(string value)
var array = new[] { value };
return new StringArrayOption(string.Empty, array, array);
}

public static implicit operator StringArrayOption (PathInfo pathInfo)
{
var array = new[] { pathInfo?.ToString() };
return new StringArrayOption(string.Empty, array, array);
}
}
}
32 changes: 32 additions & 0 deletions tst/Pester.RSpec.Configuration.ts.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,38 @@ i -PassThru:$PassThru {
Verify-Same $path[0] -Actual $config.Run.Path.Value[0]
Verify-Same $path[1] -Actual $config.Run.Path.Value[1]
}

t "StringArrayOption can be assigned an System.Management.Automation.PathInfo" {
$config = [PesterConfiguration]::Default
$path = Join-Path (Split-Path $PWD) (Split-Path $PWD -Leaf) | Resolve-Path
$config.Run.Path = $path

Verify-Equal $path[0].ToString() -Actual $config.Run.Path.Value[0]
}

t "StringArrayOption can be assigned an System.Management.Automation.PathInfo in object array" {
$config = [PesterConfiguration]::Default
$path = (Join-Path (Split-Path $PWD) (Split-Path $PWD -Leaf) | Resolve-Path), (Join-Path (Split-Path $PWD ) (Split-Path $PWD -Leaf) | Resolve-Path)
$config.Run.Path = $path

Verify-Equal $path[0].ToString() -Actual $config.Run.Path.Value[0]
Verify-Equal $path[1].ToString() -Actual $config.Run.Path.Value[1]
}

t "StringArrayOption can be assigned an PSCustomObject from hashtable" {
$path = Join-Path (Split-Path $PWD) (Split-Path $PWD -Leaf) | Resolve-Path
$config = [PesterConfiguration]@{ Run = @{ Path = $path } }

Verify-Equal $path[0].ToString() -Actual $config.Run.Path.Value[0]
}

t "StringArrayOption can be assigned PSCustomObjects in object array" {
$path = (Join-Path (Split-Path $PWD) (Split-Path $PWD -Leaf)), (Join-Path (Split-Path $PWD) (Split-Path $PWD -Leaf)) | Resolve-Path
$config = [PesterConfiguration]@{ Run = @{ Path = $path } }

Verify-Equal $path[0].ToString() -Actual $config.Run.Path.Value[0]
Verify-Equal $path[1].ToString() -Actual $config.Run.Path.Value[1]
}
}

b "Cloning" {
Expand Down

0 comments on commit b793235

Please sign in to comment.