From 4f4a9406964e17490ad8cbad6e8686208684cdba Mon Sep 17 00:00:00 2001 From: Caroline Quigg Date: Fri, 14 Apr 2023 12:31:05 -0700 Subject: [PATCH] Basic unit tests for Generator project (#324) * Add unit test project for Generator solution --- BenchPress/BenchPress.sln | 19 +++++-- .../AzureDeploymentImporterTests.cs | 53 +++++++++++++++++++ .../DeploymentFiles/resourceGroup.bicep | 13 +++++ .../DeploymentFiles/resourceGroup.json | 39 ++++++++++++++ .../Generators.Tests/Generators.Tests.csproj | 36 +++++++++++++ .../Generators.Tests/ResourceTypeTests.cs | 22 ++++++++ .../Generators.Tests/TestGeneratorTests.cs | 30 +++++++++++ BenchPress/Generators.Tests/Usings.cs | 1 + .../templates/powershell/template.ps1 | 1 + 9 files changed, 209 insertions(+), 5 deletions(-) create mode 100644 BenchPress/Generators.Tests/AzureDeploymentImporterTests.cs create mode 100644 BenchPress/Generators.Tests/DeploymentFiles/resourceGroup.bicep create mode 100644 BenchPress/Generators.Tests/DeploymentFiles/resourceGroup.json create mode 100644 BenchPress/Generators.Tests/Generators.Tests.csproj create mode 100644 BenchPress/Generators.Tests/ResourceTypeTests.cs create mode 100644 BenchPress/Generators.Tests/TestGeneratorTests.cs create mode 100644 BenchPress/Generators.Tests/Usings.cs create mode 100644 BenchPress/Generators.Tests/templates/powershell/template.ps1 diff --git a/BenchPress/BenchPress.sln b/BenchPress/BenchPress.sln index 1d35786..9f0f9da 100644 --- a/BenchPress/BenchPress.sln +++ b/BenchPress/BenchPress.sln @@ -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 diff --git a/BenchPress/Generators.Tests/AzureDeploymentImporterTests.cs b/BenchPress/Generators.Tests/AzureDeploymentImporterTests.cs new file mode 100644 index 0000000..3a26e28 --- /dev/null +++ b/BenchPress/Generators.Tests/AzureDeploymentImporterTests.cs @@ -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(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); + } + } +} diff --git a/BenchPress/Generators.Tests/DeploymentFiles/resourceGroup.bicep b/BenchPress/Generators.Tests/DeploymentFiles/resourceGroup.bicep new file mode 100644 index 0000000..6f96233 --- /dev/null +++ b/BenchPress/Generators.Tests/DeploymentFiles/resourceGroup.bicep @@ -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 diff --git a/BenchPress/Generators.Tests/DeploymentFiles/resourceGroup.json b/BenchPress/Generators.Tests/DeploymentFiles/resourceGroup.json new file mode 100644 index 0000000..c8e2107 --- /dev/null +++ b/BenchPress/Generators.Tests/DeploymentFiles/resourceGroup.json @@ -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'))]" + } + } +} diff --git a/BenchPress/Generators.Tests/Generators.Tests.csproj b/BenchPress/Generators.Tests/Generators.Tests.csproj new file mode 100644 index 0000000..1060bb5 --- /dev/null +++ b/BenchPress/Generators.Tests/Generators.Tests.csproj @@ -0,0 +1,36 @@ + + + + net6.0 + enable + enable + + false + true + + + + + + + + + + + + + + + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + + diff --git a/BenchPress/Generators.Tests/ResourceTypeTests.cs b/BenchPress/Generators.Tests/ResourceTypeTests.cs new file mode 100644 index 0000000..b44c77b --- /dev/null +++ b/BenchPress/Generators.Tests/ResourceTypeTests.cs @@ -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(action); + } + } +} diff --git a/BenchPress/Generators.Tests/TestGeneratorTests.cs b/BenchPress/Generators.Tests/TestGeneratorTests.cs new file mode 100644 index 0000000..8ca9be1 --- /dev/null +++ b/BenchPress/Generators.Tests/TestGeneratorTests.cs @@ -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(); + + 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 { definition }, templateFile); + }; + Assert.ThrowsException(action); + } + } +} diff --git a/BenchPress/Generators.Tests/Usings.cs b/BenchPress/Generators.Tests/Usings.cs new file mode 100644 index 0000000..974ab12 --- /dev/null +++ b/BenchPress/Generators.Tests/Usings.cs @@ -0,0 +1 @@ +global using Microsoft.VisualStudio.TestTools.UnitTesting; diff --git a/BenchPress/Generators.Tests/templates/powershell/template.ps1 b/BenchPress/Generators.Tests/templates/powershell/template.ps1 new file mode 100644 index 0000000..e02abfc --- /dev/null +++ b/BenchPress/Generators.Tests/templates/powershell/template.ps1 @@ -0,0 +1 @@ +