forked from Azure/bicep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DecompileParamsCommandTests.cs
200 lines (168 loc) · 6.22 KB
/
DecompileParamsCommandTests.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using Bicep.Core.FileSystem;
using Bicep.Core.UnitTests.Assertions;
using Bicep.Core.UnitTests.Utils;
using FluentAssertions;
using FluentAssertions.Execution;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Bicep.Cli.IntegrationTests
{
[TestClass]
public class DecompileParamsCommandTests : TestBase
{
private readonly string[] DecompilationDisclaimer = new[]
{
"WARNING: Decompilation is a best-effort process, as there is no guaranteed mapping from ARM JSON to Bicep Template or Bicep Parameters.",
"You may need to fix warnings and errors in the generated bicep/bicepparam file(s), or decompilation may fail entirely if an accurate conversion is not possible.",
"If you would like to report any issues or inaccurate conversions, please see https://github.com/Azure/bicep/issues."
};
[TestMethod]
public async Task Decompile_ValidParamFile_ShouldSucceed()
{
var paramFile =
@"{
""$schema"": ""https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#"",
""contentVersion"": ""1.0.0.0"",
""parameters"": {
""first"": {
""value"": ""test""
},
""second"": {
""value"": 1
},
""third"" : {
""value"" : [
1,
""foo""
]
},
""fourth"" : {
""value"" : {
""firstKey"" : ""bar"",
""secondKey"" : 1
}
}
}
}";
var expectedOutput =
"""
using '' /*TODO: Provide a path to a bicep template*/
param first = 'test'
param second = 1
param third = [
1
'foo'
]
param fourth = {
firstKey: 'bar'
secondKey: 1
}
""";
var (jsonPath, bicepparamPath) = Setup(TestContext, paramFile);
var (output, error, result) = await Bicep("decompile-params", jsonPath);
using (new AssertionScope())
{
output.Should().BeEmpty();
error.AsLines().Should().Contain(DecompilationDisclaimer);
result.Should().Be(0);
File.ReadAllText(bicepparamPath).Should().BeEquivalentToIgnoringNewlines(expectedOutput);
}
}
[TestMethod]
public async Task Decompile_ValidParamFileWithBicepPath_ShouldSucceed()
{
var paramFile =
"""
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"foo": {
"value": "bar"
}
}
}
""";
var expectedOutput =
"""
using 'dir/main.bicep'
param foo = 'bar'
""";
var (jsonPath, bicepparamPath) = Setup(TestContext, paramFile);
var bicepPath = PathHelper.ResolvePath("./dir/main.bicep", Path.GetDirectoryName(jsonPath));
var (output, error, result) = await Bicep("decompile-params", jsonPath, "--bicep-file", bicepPath);
using (new AssertionScope())
{
output.Should().BeEmpty();
error.AsLines().Should().Contain(DecompilationDisclaimer);
result.Should().Be(0);
File.ReadAllText(bicepparamPath).Should().BeEquivalentToIgnoringNewlines(expectedOutput);
}
}
[TestMethod]
public async Task Decompile_ValidParamFileWithStdOut_ShouldSucceed()
{
var paramFile =
"""
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"foo": {
"value": "bar"
}
}
}
""";
var expectedOutput =
"""
using '' /*TODO: Provide a path to a bicep template*/
param foo = 'bar'
""";
var (jsonPath, bicepparamPath) = Setup(TestContext, paramFile);
var (output, error, result) = await Bicep("decompile-params", jsonPath, "--stdout");
using (new AssertionScope())
{
output.Should().Be(expectedOutput);
error.AsLines().Should().Contain(DecompilationDisclaimer);
result.Should().Be(0);
}
}
[TestMethod]
public async Task Decompile_InvalidParamFile_ShouldFailWithErrors()
{
var paramFile =
@"{
""$schema"": ""https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#"",
""contentVersion"": ""1.0.0.0"",
""parameters"": {
""foo"": { }
}
}";
var (jsonPath, bicepparamPath) = Setup(TestContext, paramFile);
var (output, error, result) = await Bicep("decompile-params", jsonPath);
using (new AssertionScope())
{
output.Should().BeEmpty();
error.AsLines().Should().Contain(DecompilationDisclaimer);
error.AsLines().Should().Contain($"{jsonPath}: Decompilation failed with fatal error \"No value found parameter foo\"");
result.Should().Be(1);
}
}
private static (string jsonPath, string bicepparamPath) Setup(TestContext context, string template, string? inputFile = null, string? outputDir = null)
{
var jsonPath = FileHelper.SaveResultFile(context, inputFile ?? "param.json", template);
string bicepparamPath;
if (outputDir is null)
{
bicepparamPath = PathHelper.GetDefaultDecompileparamOutputPath(jsonPath);
}
else
{
bicepparamPath = FileHelper.GetResultFilePath(context, outputDir);
}
return (jsonPath, bicepparamPath);
}
}
}