diff --git a/CHANGELOG.md b/CHANGELOG.md index 6471d7a919..aafb1987d5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/src/PSRule/Pipeline/PipelineReciever.cs b/src/PSRule/Pipeline/PipelineReciever.cs index 771d86311d..5f65970388 100644 --- a/src/PSRule/Pipeline/PipelineReciever.cs +++ b/src/PSRule/Pipeline/PipelineReciever.cs @@ -22,12 +22,26 @@ public static IEnumerable PassThru(PSObject targetObject) public static IEnumerable 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(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(json, new PSObjectArrayJsonConverter()); if (value == null) { @@ -58,7 +72,8 @@ public static IEnumerable ConvertFromJson(PSObject sourceObject, Visit public static IEnumerable 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 }; } @@ -69,7 +84,18 @@ public static IEnumerable 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(); diff --git a/tests/PSRule.Tests/PSRule.Common.Tests.ps1 b/tests/PSRule.Tests/PSRule.Common.Tests.ps1 index 6a25e1e390..b45a239754 100644 --- a/tests/PSRule.Tests/PSRule.Common.Tests.ps1 +++ b/tests/PSRule.Tests/PSRule.Common.Tests.ps1 @@ -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; @@ -240,7 +240,16 @@ 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; @@ -248,6 +257,15 @@ Describe 'Invoke-PSRule' -Tag 'Invoke-PSRule','Common' { $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' {