forked from microsoft/PSRule
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInputFormatDeserializerTests.cs
121 lines (103 loc) · 6.46 KB
/
InputFormatDeserializerTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.IO;
using System.Linq;
using System.Management.Automation;
using PSRule.Pipeline;
using Xunit;
namespace PSRule
{
public sealed class InputFormatDeserializerTests
{
[Fact]
public void DeserializeObjectsYaml()
{
var actual = PipelineReceiverActions.ConvertFromYaml(GetYamlContent(), PipelineReceiverActions.PassThru).ToArray();
Assert.Equal(2, actual.Length);
Assert.Equal("TestObject1", actual[0].Value.PropertyValue<string>("targetName"));
Assert.Equal("Test", actual[0].Value.PropertyValue("spec").PropertyValue("properties").PropertyValue<string>("kind"));
Assert.Equal(2, actual[1].Value.PropertyValue("spec").PropertyValue("properties").PropertyValue<int>("value2"));
Assert.Equal(2, actual[1].Value.PropertyValue("spec").PropertyValue("properties").PropertyValue<PSObject[]>("array").Length);
Assert.Equal("TestObject1", PipelineHookActions.BindTargetName(null, false, false, actual[0].Value, out string path));
Assert.Null(path);
// Array item
actual = PipelineReceiverActions.ConvertFromYaml(GetYamlContent("3"), PipelineReceiverActions.PassThru).ToArray();
Assert.Equal(2, actual.Length);
Assert.Equal("item1", actual[0].Value.PropertyValue<string>("name"));
Assert.Equal("value1", actual[0].Value.PropertyValue<string>("value"));
Assert.Equal("item2", actual[1].Value.PropertyValue<string>("name"));
Assert.Equal("value2", actual[1].Value.PropertyValue<string>("value"));
}
[Fact]
public void DeserializeObjectsJson()
{
var actual = PipelineReceiverActions.ConvertFromJson(GetJsonContent(), PipelineReceiverActions.PassThru).ToArray();
Assert.Equal(2, actual.Length);
Assert.Equal("TestObject1", actual[0].Value.PropertyValue<string>("targetName"));
Assert.Equal("Test", actual[0].Value.PropertyValue("spec").PropertyValue("properties").PropertyValue<string>("kind"));
Assert.Equal(2, actual[1].Value.PropertyValue("spec").PropertyValue("properties").PropertyValue<int>("value2"));
Assert.Equal(3, actual[1].Value.PropertyValue("spec").PropertyValue("properties").PropertyValue<PSObject[]>("array").Length);
Assert.Equal("TestObject1", PipelineHookActions.BindTargetName(null, false, false, actual[0].Value, out string path));
Assert.Null(path);
actual[0].Value.TryTargetInfo(out var info1);
actual[1].Value.TryTargetInfo(out var info2);
Assert.Equal("some-file.json", info1.Source[0].File);
Assert.NotNull(info2.Source[0]);
// Single item
actual = PipelineReceiverActions.ConvertFromJson(GetJsonContent("Single"), PipelineReceiverActions.PassThru).ToArray();
Assert.Single(actual);
Assert.Equal("TestObject1", actual[0].Value.PropertyValue<string>("targetName"));
Assert.Equal("Test", actual[0].Value.PropertyValue("spec").PropertyValue("properties").PropertyValue<string>("kind"));
// Malformed item
Assert.Throws<PipelineSerializationException>(() => PipelineReceiverActions.ConvertFromJson(new TargetObject(new PSObject("{")), PipelineReceiverActions.PassThru).ToArray());
Assert.Throws<PipelineSerializationException>(() => PipelineReceiverActions.ConvertFromJson(new TargetObject(new PSObject("{ \"key\": ")), PipelineReceiverActions.PassThru).ToArray());
}
[Fact]
public void DeserializeObjectsMarkdown()
{
var actual = PipelineReceiverActions.ConvertFromMarkdown(GetMarkdownContent(), PipelineReceiverActions.PassThru).ToArray();
Assert.Single(actual);
Assert.Equal("TestObject1", actual[0].Value.PropertyValue<string>("targetName"));
Assert.Equal("Test", actual[0].Value.PropertyValue("spec").PropertyValue("properties").PropertyValue<string>("kind"));
Assert.Equal(1, actual[0].Value.PropertyValue("spec").PropertyValue("properties").PropertyValue<int>("value1"));
Assert.Equal(2, actual[0].Value.PropertyValue("spec").PropertyValue("properties").PropertyValue<PSObject[]>("array").Length);
Assert.Equal("TestObject1", PipelineHookActions.BindTargetName(null, false, false, actual[0].Value, out string path));
Assert.Null(path);
}
[Fact]
public void DeserializeObjectsPowerShellData()
{
var actual = PipelineReceiverActions.ConvertFromPowerShellData(GetDataContent(), PipelineReceiverActions.PassThru).ToArray();
Assert.Single(actual);
Assert.Equal("TestObject1", actual[0].Value.PropertyValue<string>("targetName"));
Assert.Equal("Test", actual[0].Value.PropertyValue("spec").PropertyValue("properties").PropertyValue<string>("kind"));
Assert.Equal(1, actual[0].Value.PropertyValue("spec").PropertyValue("properties").PropertyValue<int>("value1"));
Assert.Equal(2, actual[0].Value.PropertyValue("spec").PropertyValue("properties").PropertyValue<Array>("array").Length);
Assert.Equal("TestObject1", PipelineHookActions.BindTargetName(null, false, false, actual[0].Value, out string path));
Assert.Null(path);
}
#region Helper methods
private static TargetObject GetYamlContent(string suffix = "")
{
var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"ObjectFromFile{suffix}.yaml");
return new TargetObject(new PSObject(File.ReadAllText(path)));
}
private static TargetObject GetJsonContent(string suffix = "")
{
var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"ObjectFromFile{suffix}.json");
return new TargetObject(new PSObject(File.ReadAllText(path)));
}
private static TargetObject GetMarkdownContent()
{
var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ObjectFromFile.md");
return new TargetObject(new PSObject(File.ReadAllText(path)));
}
private static TargetObject GetDataContent()
{
var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ObjectFromFile.psd1");
return new TargetObject(new PSObject(File.ReadAllText(path)));
}
#endregion Helper methods
}
}