forked from Azure/bicep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BuildParamsCommandTests.cs
161 lines (128 loc) · 7.65 KB
/
BuildParamsCommandTests.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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Text.Json;
using System.Threading.Tasks;
using Bicep.Core.Configuration;
using Bicep.Core.Samples;
using Bicep.Core.UnitTests;
using Bicep.Core.UnitTests.Assertions;
using Bicep.Core.UnitTests.Features;
using Bicep.Core.UnitTests.Utils;
using FluentAssertions;
using FluentAssertions.Execution;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Bicep.Cli.IntegrationTests
{
[TestClass]
public class BuildParamsCommandTests : TestBase
{
[NotNull]
public TestContext? TestContext { get; set; }
[TestMethod]
public async Task Build_Params_With_Incorrect_Bicep_File_Extension_ShouldFail_WithExpectedErrorMessage()
{
var bicepparamsPath = FileHelper.SaveResultFile(TestContext, "input.bicepparam", "using './main.bicep'");
var bicepPath = FileHelper.SaveResultFile(TestContext, "main.wrongExt", "", Path.GetDirectoryName(bicepparamsPath));
var outputFilePath = FileHelper.GetResultFilePath(TestContext, "output.json");
File.Exists(outputFilePath).Should().BeFalse();
var(output, error, result) = await Bicep("build-params", bicepparamsPath,"--bicep-file", bicepPath, "--outfile", outputFilePath);
result.Should().Be(1);
output.Should().BeEmpty();
error.Should().Contain($"{bicepPath} is not a bicep file");
}
[TestMethod]
public async Task Build_Params_With_CLI_Input_And_Using_Declaration_Bicep_File_Reference_Mismatch_ShouldFail_WithExpectedErrorMessage()
{
var bicepparamsPath = FileHelper.SaveResultFile(TestContext, "input.bicepparam", "using './main.bicep'");
var bicepPath = FileHelper.SaveResultFile(TestContext, "main.bicep", "", Path.GetDirectoryName(bicepparamsPath));
var otherBicepPath = FileHelper.SaveResultFile(TestContext, "otherMain.bicep", "", Path.GetDirectoryName(bicepparamsPath));
var settings = new InvocationSettings(new(TestContext), BicepTestConstants.ClientFactory, BicepTestConstants.TemplateSpecRepositoryFactory);
var outputFilePath = FileHelper.GetResultFilePath(TestContext, "output.json");
File.Exists(outputFilePath).Should().BeFalse();
var(output, error, result) = await Bicep(settings, "build-params", bicepparamsPath,"--bicep-file", otherBicepPath, "--outfile", outputFilePath);
result.Should().Be(1);
output.Should().BeEmpty();
error.Should().Contain($"Bicep file {otherBicepPath} provided with --bicep-file option doesn't match the Bicep file {bicepPath} referenced by the using declaration in the parameters file");
}
[TestMethod]
public async Task Build_Params_Bicep_File_Reference_Mismatch_And_Other_Diagnostics_ShouldFail_WithAllExpectedErrorMessages()
{
var bicepparamsPath = FileHelper.SaveResultFile(TestContext, "input.bicepparam",
@"
using './main.bicep'
param foo = 'bar'
");
var bicepPath = FileHelper.SaveResultFile(TestContext, "main.bicep", "param foo object", Path.GetDirectoryName(bicepparamsPath));
var otherBicepPath = FileHelper.SaveResultFile(TestContext, "otherMain.bicep", "", Path.GetDirectoryName(bicepparamsPath));
var settings = new InvocationSettings(new(TestContext), BicepTestConstants.ClientFactory, BicepTestConstants.TemplateSpecRepositoryFactory);
var outputFilePath = FileHelper.GetResultFilePath(TestContext, "output.json");
var(output, error, result) = await Bicep(settings, "build-params", bicepparamsPath,"--bicep-file", otherBicepPath, "--outfile", outputFilePath);
var diagnostics = await GetAllParamDiagnostics(bicepparamsPath);
result.Should().Be(1);
output.Should().BeEmpty();
error.Should().Contain($"Bicep file {otherBicepPath} provided with --bicep-file option doesn't match the Bicep file {bicepPath} referenced by the using declaration in the parameters file");
error.Should().ContainAll(diagnostics);
File.Exists(outputFilePath).Should().BeFalse();
}
[DataTestMethod]
[BaselineData_Bicepparam.TestData(Filter = BaselineData_Bicepparam.TestDataFilterType.ValidOnly)]
[TestCategory(BaselineHelper.BaselineTestCategory)]
public async Task Build_Valid_Params_File_Should_Succeed(BaselineData_Bicepparam baselineData)
{
var data = baselineData.GetData(TestContext);
var features = new FeatureProviderOverrides(TestContext);
var settings = new InvocationSettings(features, BicepTestConstants.ClientFactory, BicepTestConstants.TemplateSpecRepositoryFactory);
var (output, error, result) = await Bicep(settings, "build-params", data.Parameters.OutputFilePath, "--bicep-file", data.Bicep.OutputFilePath);
using (new AssertionScope())
{
result.Should().Be(0);
output.Should().BeEmpty();
AssertNoErrors(error);
}
data.Compiled!.ShouldHaveExpectedJsonValue();
}
[DataTestMethod]
[BaselineData_Bicepparam.TestData(Filter = BaselineData_Bicepparam.TestDataFilterType.ValidOnly)]
[TestCategory(BaselineHelper.BaselineTestCategory)]
public async Task Build_Valid_Params_File_ToStdOut_Should_Succeed(BaselineData_Bicepparam baselineData)
{
var data = baselineData.GetData(TestContext);
var settings = new InvocationSettings(new (), BicepTestConstants.ClientFactory, BicepTestConstants.TemplateSpecRepositoryFactory);
var (output, error, result) = await Bicep(settings, "build-params", data.Parameters.OutputFilePath, "--bicep-file", data.Bicep.OutputFilePath, "--stdout");
using (new AssertionScope())
{
result.Should().Be(0);
output.Should().NotBeEmpty();
AssertNoErrors(error);
}
var parametersStdout = JsonSerializer.Deserialize<ParametersStdout>(output)!;
data.Compiled!.WriteToOutputFolder(parametersStdout.parametersJson);
data.Compiled.ShouldHaveExpectedJsonValue();
}
[DataTestMethod]
[BaselineData_Bicepparam.TestData(Filter = BaselineData_Bicepparam.TestDataFilterType.InvalidOnly)]
[TestCategory(BaselineHelper.BaselineTestCategory)]
public async Task Build_Invalid_Single_Params_File_ShouldFail_WithExpectedErrorMessage(BaselineData_Bicepparam baselineData)
{
var data = baselineData.GetData(TestContext);
var settings = new InvocationSettings(new(TestContext), BicepTestConstants.ClientFactory, BicepTestConstants.TemplateSpecRepositoryFactory);
var diagnostics = await GetAllParamDiagnostics(data.Parameters.OutputFilePath);
var (output, error, result) = await Bicep(settings, "build-params", data.Parameters.OutputFilePath, "--bicep-file", data.Bicep.OutputFilePath);
using (new AssertionScope())
{
result.Should().Be(1);
output.Should().BeEmpty();
error.Should().ContainAll(diagnostics);
}
}
[TestInitialize]
public void testInit()
{
System.Environment.SetEnvironmentVariable("stringEnvVariableName", "test");
System.Environment.SetEnvironmentVariable("intEnvVariableName", "100");
System.Environment.SetEnvironmentVariable("boolEnvironmentVariable", "true");
}
}
}