Basic unit tests for Generator project (#324)

* Add unit test project for Generator solution
This commit is contained in:
Caroline Quigg 2023-04-14 12:31:05 -07:00 коммит произвёл GitHub
Родитель a1595f9626
Коммит 4f4a940696
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
9 изменённых файлов: 209 добавлений и 5 удалений

Просмотреть файл

@ -1,14 +1,17 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.3.32708.82
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Generators", "./Generators/Generators.csproj", "{C779329A-EF48-47D7-8EC1-5D3CA3CC2E58}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Generators", "Generators\Generators.csproj", "{C779329A-EF48-47D7-8EC1-5D3CA3CC2E58}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Bicep.Cli", "./bicep/src/Bicep.Cli/Bicep.Cli.csproj", "{0E331097-42A8-4B2A-A66A-189035BA2AD0}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Bicep.Cli", "bicep\src\Bicep.Cli\Bicep.Cli.csproj", "{0E331097-42A8-4B2A-A66A-189035BA2AD0}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Bicep.Decompiler", "./bicep/src/Bicep.Decompiler/Bicep.Decompiler.csproj", "{2B418AB3-40D2-4F64-9795-93B4299118E8}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Bicep.Decompiler", "bicep\src\Bicep.Decompiler\Bicep.Decompiler.csproj", "{2B418AB3-40D2-4F64-9795-93B4299118E8}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Bicep.Core", "./bicep/src/Bicep.Core/Bicep.Core.csproj", "{A6B26AAD-D21F-48A5-9FB3-6FD3B00DC8F1}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Bicep.Core", "bicep\src\Bicep.Core\Bicep.Core.csproj", "{A6B26AAD-D21F-48A5-9FB3-6FD3B00DC8F1}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Generators.Tests", "Generators.Tests\Generators.Tests.csproj", "{D1D299F2-190D-491D-BD7C-63E2014EE835}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -32,11 +35,17 @@ Global
{A6B26AAD-D21F-48A5-9FB3-6FD3B00DC8F1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A6B26AAD-D21F-48A5-9FB3-6FD3B00DC8F1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A6B26AAD-D21F-48A5-9FB3-6FD3B00DC8F1}.Release|Any CPU.Build.0 = Release|Any CPU
{D1D299F2-190D-491D-BD7C-63E2014EE835}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D1D299F2-190D-491D-BD7C-63E2014EE835}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D1D299F2-190D-491D-BD7C-63E2014EE835}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D1D299F2-190D-491D-BD7C-63E2014EE835}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {64B8D145-6451-43EA-B0D6-B46165A4B25D}
SolutionGuid = {627494FB-1ABB-4A34-8CF8-A89171AB0B77}
SolutionGuid = {64B8D145-6451-43EA-B0D6-B46165A4B25D}
SolutionGuid = {60CB3592-4A99-4694-9479-23C6577F392A}
EndGlobalSection
EndGlobal

Просмотреть файл

@ -0,0 +1,53 @@
using Generators.LanguageProviders;
namespace Generators.Tests
{
[TestClass]
public class AzureDeploymentImporterTests
{
[TestMethod]
public void Should_Throw_If_Invalid_File_Format()
{
// arrange
var inputFilePath = "in.txt";
var outputFilePath = "outFolder";
// act
Action action = () =>
{
AzureDeploymentImporter.Import(inputFilePath, outputFilePath);
};
// assert
Assert.ThrowsException<FileFormatException>(action);
}
[TestMethod]
public void Should_Succeed_With_Bicep_File()
{
// arrange
var inputFilePath = "./DeploymentFiles/resourceGroup.bicep";
var outputFilePath = "outFolder";
// act
var result = AzureDeploymentImporter.Import(inputFilePath, outputFilePath);
// assert
Assert.IsNotNull(result);
}
[TestMethod]
public void Should_Succeed_With_Json_File()
{
// arrange
var inputFilePath = "./DeploymentFiles/resourceGroup.json";
var outputFilePath = "outFolder";
// act
var result = AzureDeploymentImporter.Import(inputFilePath, outputFilePath);
// assert
Assert.IsNotNull(result);
}
}
}

Просмотреть файл

@ -0,0 +1,13 @@
targetScope = 'subscription'
param name string = 'rg${take(uniqueString(subscription().id), 5)}'
param location string = deployment().location
// https://docs.microsoft.com/en-us/azure/templates/microsoft.resources/resourcegroups?tabs=bicep
resource resourceGroup 'Microsoft.Resources/resourceGroups@2022-09-01' = {
name: name
location: location
}
output name string = resourceGroup.name
output id string = resourceGroup.id

Просмотреть файл

@ -0,0 +1,39 @@
{
"$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"metadata": {
"_generator": {
"name": "bicep",
"version": "0.13.39.55032",
"templateHash": "6896821326683896901"
}
},
"parameters": {
"name": {
"type": "string",
"defaultValue": "[format('rg{0}', take(uniqueString(subscription().id), 5))]"
},
"location": {
"type": "string",
"defaultValue": "[deployment().location]"
}
},
"resources": [
{
"type": "Microsoft.Resources/resourceGroups",
"apiVersion": "2022-09-01",
"name": "[parameters('name')]",
"location": "[parameters('location')]"
}
],
"outputs": {
"name": {
"type": "string",
"value": "[parameters('name')]"
},
"id": {
"type": "string",
"value": "[subscriptionResourceId('Microsoft.Resources/resourceGroups', parameters('name'))]"
}
}
}

Просмотреть файл

@ -0,0 +1,36 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.10" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.10" />
<PackageReference Include="coverlet.collector" Version="3.1.2" />
<PackageReference Include="NSubstitute" Version="5.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Generators\Generators.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="DeploymentFiles\resourceGroup.bicep">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="DeploymentFiles\resourceGroup.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Templates\powershell\template.ps1">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

Просмотреть файл

@ -0,0 +1,22 @@
using Generators.ResourceTypes;
namespace Generators.Tests
{
[TestClass]
public class ResourceTypeTests
{
[TestMethod]
public void Should_Throw_If_Unknown_ResourceType()
{
// arrange
var resourceType = "Unknown";
// act and assert
Action action = () =>
{
ResourceType.Create(resourceType);
};
Assert.ThrowsException<UnknownResourceTypeException>(action);
}
}
}

Просмотреть файл

@ -0,0 +1,30 @@
using Generators.LanguageProviders;
namespace Generators.Tests
{
[TestClass]
public class TestGeneratorTests
{
[TestMethod]
public void Should_Throw_If_Unknown_TestType()
{
// arrange
var resourceType = "Microsoft.Insights/actionGroups";
var resourceName = "Name";
var extraProperties = new Dictionary<string, string>();
var metadata = new TestMetadata(resourceType, resourceName, extraProperties);
var definition = new TestDefinition(metadata, (TestType)Int32.MaxValue);
var generator = new TestGenerator(new PowershellLanguageProvider());
var templateFile = "./templates/powershell/template.ps1";
// act and assert
Action action = () =>
{
generator.Generate(new List<TestDefinition> { definition }, templateFile);
};
Assert.ThrowsException<Exception>(action);
}
}
}

Просмотреть файл

@ -0,0 +1 @@
global using Microsoft.VisualStudio.TestTools.UnitTesting;

Просмотреть файл

@ -0,0 +1 @@