Skip to content

Commit

Permalink
Add support for input deserialization from FileInfo objects #95 (#96)
Browse files Browse the repository at this point in the history
* Support input deserialization from FileInfo objects #95
  • Loading branch information
BernieWhite authored Feb 25, 2019
1 parent ba42d0a commit 1d55968
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

## Unreleased

- Added support for input deserialization from FileInfo objects [#95](https://github.com/BernieWhite/PSRule/issues/95)

## v0.3.0-B190231 (pre-release)

- Added support for pipelining with `Exists`, `Within`, `Match` and `TypeOf` keywords [#90](https://github.com/BernieWhite/PSRule/issues/90)
Expand Down
34 changes: 30 additions & 4 deletions src/PSRule/Pipeline/PipelineReciever.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,26 @@ public static IEnumerable<PSObject> PassThru(PSObject targetObject)

public static IEnumerable<PSObject> ConvertFromJson(PSObject sourceObject, VisitTargetObject next)
{
if (!(sourceObject.BaseObject is string))
// Only attempt to deserialize if the input is a string or a file
if (!(sourceObject.BaseObject is string || sourceObject.BaseObject is FileInfo))
{
return new PSObject[] { sourceObject };
}

var value = JsonConvert.DeserializeObject<PSObject[]>(sourceObject.BaseObject.ToString(), new PSObjectArrayJsonConverter());
var json = string.Empty;

if (sourceObject.BaseObject is string)
{
json = sourceObject.BaseObject.ToString();
}
else
{
var fileInfo = sourceObject.BaseObject as FileInfo;
var reader = new StreamReader(fileInfo.FullName);
json = reader.ReadToEnd();
}

var value = JsonConvert.DeserializeObject<PSObject[]>(json, new PSObjectArrayJsonConverter());

if (value == null)
{
Expand Down Expand Up @@ -58,7 +72,8 @@ public static IEnumerable<PSObject> ConvertFromJson(PSObject sourceObject, Visit

public static IEnumerable<PSObject> ConvertFromYaml(PSObject sourceObject, VisitTargetObject next)
{
if (!(sourceObject.BaseObject is string))
// Only attempt to deserialize if the input is a string or a file
if (!(sourceObject.BaseObject is string || sourceObject.BaseObject is FileInfo))
{
return new PSObject[] { sourceObject };
}
Expand All @@ -69,7 +84,18 @@ public static IEnumerable<PSObject> ConvertFromYaml(PSObject sourceObject, Visit
.WithNodeTypeResolver(new PSObjectYamlTypeResolver())
.Build();

var reader = new StringReader(sourceObject.BaseObject.ToString());
TextReader reader = null;

if (sourceObject.BaseObject is string)
{
reader = new StringReader(sourceObject.BaseObject.ToString());
}
else
{
var fileInfo = sourceObject.BaseObject as FileInfo;
reader = new StreamReader(fileInfo.FullName);
}

var parser = new Parser(reader);

parser.Expect<StreamStart>();
Expand Down
22 changes: 20 additions & 2 deletions tests/PSRule.Tests/PSRule.Common.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ Describe 'Invoke-PSRule' -Tag 'Invoke-PSRule','Common' {
}

Context 'Using -Format' {
It 'Processes Yaml' {
It 'Yaml String' {
$yaml = Get-Content -Path (Join-Path -Path $here -ChildPath 'ObjectFromFile.yaml') -Raw;
$result = @(Invoke-PSRule -Path (Join-Path -Path $here -ChildPath 'FromFile.Rule.ps1') -Name 'WithFormat' -InputObject $yaml -Format Yaml);
$result | Should -Not -BeNullOrEmpty;
Expand All @@ -240,14 +240,32 @@ Describe 'Invoke-PSRule' -Tag 'Invoke-PSRule','Common' {
$result.TargetName | Should -BeIn 'TestObject1', 'TestObject2'
}

It 'Processes Json' {
It 'Yaml FileInfo' {
$file = Get-ChildItem -Path (Join-Path -Path $here -ChildPath 'ObjectFromFile.yaml') -File;
$result = @(Invoke-PSRule -Path (Join-Path -Path $here -ChildPath 'FromFile.Rule.ps1') -Name 'WithFormat' -InputObject $file -Format Yaml);
$result | Should -Not -BeNullOrEmpty;
$result.Length | Should -Be 2;
$result | Should -BeOfType PSRule.Rules.RuleRecord;
$result.TargetName | Should -BeIn 'TestObject1', 'TestObject2'
}

It 'Json String' {
$json = Get-Content -Path (Join-Path -Path $here -ChildPath 'ObjectFromFile.json') -Raw;
$result = @(Invoke-PSRule -Path (Join-Path -Path $here -ChildPath 'FromFile.Rule.ps1') -Name 'WithFormat' -InputObject $json -Format Json);
$result | Should -Not -BeNullOrEmpty;
$result.Length | Should -Be 2;
$result | Should -BeOfType PSRule.Rules.RuleRecord;
$result.TargetName | Should -BeIn 'TestObject1', 'TestObject2'
}

It 'Json FileInfo' {
$file = Get-ChildItem -Path (Join-Path -Path $here -ChildPath 'ObjectFromFile.json') -File;
$result = @(Invoke-PSRule -Path (Join-Path -Path $here -ChildPath 'FromFile.Rule.ps1') -Name 'WithFormat' -InputObject $file -Format Json);
$result | Should -Not -BeNullOrEmpty;
$result.Length | Should -Be 2;
$result | Should -BeOfType PSRule.Rules.RuleRecord;
$result.TargetName | Should -BeIn 'TestObject1', 'TestObject2'
}
}

Context 'Using -ObjectPath' {
Expand Down

0 comments on commit 1d55968

Please sign in to comment.