diff --git a/.azuredevops/pipelineTemplates/jobs.getModuleTestFiles.yml b/.azuredevops/pipelineTemplates/jobs.getModuleTestFiles.yml index 74b38d5e97..6c8dc4e3b0 100644 --- a/.azuredevops/pipelineTemplates/jobs.getModuleTestFiles.yml +++ b/.azuredevops/pipelineTemplates/jobs.getModuleTestFiles.yml @@ -25,18 +25,16 @@ jobs: targetType: inline pwsh: true script: | - # Load used functions - . (Join-Path '$(System.DefaultWorkingDirectory)' 'utilities' 'pipelines' 'sharedScripts' 'Get-ModuleTestFileList.ps1') + # Get the list of parameter file paths + $moduleFolderPath = Join-Path '$(System.DefaultWorkingDirectory)' '${{ parameters.modulePath }}' + $testFilePaths = (Get-ChildItem -Path $moduleFolderPath -Recurse -Filter 'main.test.bicep').FullName | Sort-Object - $functionInput = @{ - ModulePath = Join-Path '$(System.DefaultWorkingDirectory)' '${{ parameters.modulePath }}' + $deploymentTestPaths = $testFilePaths | ForEach-Object { + $_.Replace($moduleFolderPath, '').Trim('\').Trim('/') } - Write-Verbose "Invoke task with" -Verbose - Write-Verbose ($functionInput | ConvertTo-Json | Out-String) -Verbose - - # Set agent up - $deploymentTestPaths = Get-ModuleTestFileList @functionInput -Verbose + Write-Verbose 'Found module test files' -Verbose + $deploymentTestPaths | ForEach-Object { Write-Verbose "- [$_]" -Verbose } $testTable = @{} foreach ($deploymentTestPath in $deploymentTestPaths) { diff --git a/.github/actions/templates/getModuleTestFiles/action.yml b/.github/actions/templates/getModuleTestFiles/action.yml index 1cce2fb82e..704f13bad5 100644 --- a/.github/actions/templates/getModuleTestFiles/action.yml +++ b/.github/actions/templates/getModuleTestFiles/action.yml @@ -20,18 +20,17 @@ runs: run: | # Grouping task logs Write-Output '::group::Get parameter files' - # Load used functions - . (Join-Path $env:GITHUB_WORKSPACE 'utilities' 'pipelines' 'sharedScripts' 'Get-ModuleTestFileList.ps1') - $functionInput = @{ - ModulePath = Join-Path $env:GITHUB_WORKSPACE '${{ inputs.modulePath }}' - } + # Get the list of parameter file paths + $moduleFolderPath = Join-Path $env:GITHUB_WORKSPACE '${{ inputs.modulePath }}' + $testFilePaths = (Get-ChildItem -Path $moduleFolderPath -Recurse -Filter 'main.test.bicep').FullName | Sort-Object - Write-Verbose "Invoke task with" -Verbose - Write-Verbose ($functionInput | ConvertTo-Json | Out-String) -Verbose + $testFilePaths = $testFilePaths | ForEach-Object { + $_.Replace($moduleFolderPath, '').Trim('\').Trim('/') + } - # Get the list of parameter file paths - $testFilePaths = Get-ModuleTestFileList @functionInput -Verbose + Write-Verbose 'Found module test files' -Verbose + $testFilePaths | ForEach-Object { Write-Verbose "- [$_]" -Verbose } # Output values to be accessed by next jobs $compressedOutput = $testFilePaths | ConvertTo-Json -Compress diff --git a/utilities/pipelines/sharedScripts/Get-ModuleTestFileList.ps1 b/utilities/pipelines/sharedScripts/Get-ModuleTestFileList.ps1 deleted file mode 100644 index 7fb5b13699..0000000000 --- a/utilities/pipelines/sharedScripts/Get-ModuleTestFileList.ps1 +++ /dev/null @@ -1,59 +0,0 @@ -<# -.SYNOPSIS -Get the relative file paths of all test files in the given module. - -.DESCRIPTION -Get the relative file paths of all test files (*.json / main.test.bicep) in the given module. -The relative path is returned instead of the full one to make paths easier to read in the pipeline. - -.PARAMETER ModulePath -Mandatory. The module path to search in. - -.PARAMETER SearchFolder -Optional. The folder to search for files in - -.PARAMETER TestFilePattern -Optional. The pattern of test files to search for. For example '*.json' - -.EXAMPLE -Get-ModuleTestFileList -ModulePath 'C:\ResourceModules\modules\compute\virtual-machine' - -Returns the relative file paths of all test files of the virtual-machine module in the default test folder ('tests'). - -.EXAMPLE -Get-ModuleTestFileList -ModulePath 'C:\ResourceModules\modules\compute\virtual-machine' -SearchFolder 'parameters' - -Returns the relative file paths of all test files of the virtual-machine module in folder 'parameters'. -#> -function Get-ModuleTestFileList { - - [CmdletBinding()] - param ( - [Parameter(Mandatory)] - [string] $ModulePath, - - [Parameter(Mandatory = $false)] - [string] $SearchFolder = 'tests', - - [Parameter(Mandatory = $false)] - [string[]] $TestFilePattern = @('*.json', 'main.test.bicep') - ) - - $deploymentTests = @() - if (Test-Path (Join-Path $ModulePath $SearchFolder)) { - $deploymentTests += (Get-ChildItem -Path (Join-Path $ModulePath $SearchFolder) -Recurse -Include $TestFilePattern -File).FullName - } - - if (-not $deploymentTests) { - throw "No deployment test files found for module [$ModulePath]" - } - - $deploymentTests = $deploymentTests | ForEach-Object { - $_.Replace($ModulePath, '').Trim('\').Trim('/') - } - - Write-Verbose 'Found parameter files' - $deploymentTests | ForEach-Object { Write-Verbose "- $_" } - - return $deploymentTests -} diff --git a/utilities/pipelines/sharedScripts/Set-ModuleReadMe.ps1 b/utilities/pipelines/sharedScripts/Set-ModuleReadMe.ps1 index 404fdf259c..8ba3a514d2 100644 --- a/utilities/pipelines/sharedScripts/Set-ModuleReadMe.ps1 +++ b/utilities/pipelines/sharedScripts/Set-ModuleReadMe.ps1 @@ -1172,7 +1172,7 @@ function Set-UsageExamplesSection { $moduleNameCamelCase = $First.Tolower() + (Get-Culture).TextInfo.ToTitleCase($Rest) -Replace '-' } - $testFilePaths = Get-ModuleTestFileList -ModulePath $moduleRoot | ForEach-Object { Join-Path $moduleRoot $_ } + $testFilePaths = (Get-ChildItem -Path $ModuleRoot -Recurse -Filter 'main.test.bicep').FullName | Sort-Object $RequiredParametersList = $TemplateFileContent.parameters.Keys | Where-Object { Get-IsParameterRequired -TemplateFileContent $TemplateFileContent -Parameter $TemplateFileContent.parameters[$_] @@ -1613,7 +1613,6 @@ function Set-ModuleReadMe { # Load external functions . (Join-Path $PSScriptRoot 'Get-NestedResourceList.ps1') - . (Join-Path $PSScriptRoot 'Get-ModuleTestFileList.ps1') . (Join-Path $PSScriptRoot 'helper' 'Merge-FileWithNewContent.ps1') . (Join-Path $PSScriptRoot 'helper' 'Get-IsParameterRequired.ps1') . (Join-Path $PSScriptRoot 'helper' 'Get-SpecsAlignedResourceName.ps1') diff --git a/utilities/pipelines/staticValidation/helper/helper.psm1 b/utilities/pipelines/staticValidation/helper/helper.psm1 index ab80fbbac0..c50c1e2f0e 100644 --- a/utilities/pipelines/staticValidation/helper/helper.psm1 +++ b/utilities/pipelines/staticValidation/helper/helper.psm1 @@ -5,7 +5,6 @@ $repoRootPath = (Get-Item $PSScriptRoot).Parent.Parent.Parent.Parent.FullName . (Join-Path $repoRootPath 'utilities' 'pipelines' 'sharedScripts' 'Get-NestedResourceList.ps1') . (Join-Path $repoRootPath 'utilities' 'pipelines' 'sharedScripts' 'Get-ScopeOfTemplateFile.ps1') -. (Join-Path $repoRootPath 'utilities' 'pipelines' 'sharedScripts' 'Get-ModuleTestFileList.ps1') . (Join-Path $repoRootPath 'utilities' 'pipelines' 'sharedScripts' 'helper' 'ConvertTo-OrderedHashtable.ps1') . (Join-Path $repoRootPath 'utilities' 'pipelines' 'sharedScripts' 'helper' 'Get-IsParameterRequired.ps1') . (Join-Path $repoRootPath 'utilities' 'tools' 'Get-CrossReferencedModuleList.ps1') diff --git a/utilities/pipelines/staticValidation/module.tests.ps1 b/utilities/pipelines/staticValidation/module.tests.ps1 index f608f5f24b..c973dd9929 100644 --- a/utilities/pipelines/staticValidation/module.tests.ps1 +++ b/utilities/pipelines/staticValidation/module.tests.ps1 @@ -3,7 +3,7 @@ param ( [Parameter(Mandatory = $false)] [array] $moduleFolderPaths = ((Get-ChildItem $repoRootPath -Recurse -Directory -Force).FullName | Where-Object { - (Get-ChildItem $_ -File -Depth 0 -Include @('main.json', 'main.bicep') -Force).Count -gt 0 + (Get-ChildItem $_ -File -Depth 0 -Include @('main.bicep') -Force).Count -gt 0 }), [Parameter(Mandatory = $false)] @@ -26,9 +26,6 @@ $script:MGdeployment = 'https://schema.management.azure.com/schemas/2019-08-01/m $script:Tenantdeployment = 'https://schema.management.azure.com/schemas/2019-08-01/tenantDeploymentTemplate.json#' $script:moduleFolderPaths = $moduleFolderPaths -# For runtime purposes, we cache the compiled template in a hashtable that uses a formatted relative module path as a key -$script:convertedTemplates = @{} - # Shared exception messages $script:bicepTemplateCompilationFailedException = "Unable to compile the main.bicep template's content. This can happen if there is an error in the template. Please check if you can run the command ``bicep build {0} --stdout | ConvertFrom-Json -AsHashtable``." # -f $templateFilePath $script:jsonTemplateLoadFailedException = "Unable to load the main.json template's content. This can happen if there is an error in the template. Please check if you can run the command `Get-Content {0} -Raw | ConvertFrom-Json -AsHashtable`." # -f $templateFilePath @@ -37,6 +34,24 @@ $script:templateNotFoundException = 'No template file found in folder [{0}]' # - # Import any helper function used in this test script Import-Module (Join-Path $PSScriptRoot 'helper' 'helper.psm1') -Force +# Building all required files for tests to optimize performance (using thread-safe multithreading) to consume later +# Collecting paths +$pathsToBuild = [System.Collections.ArrayList]@() +$pathsToBuild += $moduleFolderPaths | ForEach-Object { Join-Path $_ 'main.bicep' } +foreach ($moduleFolderPath in $moduleFolderPaths) { + if ($testFilePaths = ((Get-ChildItem -Path $moduleFolderPath -Recurse -Filter 'main.test.bicep').FullName | Sort-Object)) { + $pathsToBuild += $testFilePaths + } +} + +# building paths +$builtTestFileMap = [System.Collections.Concurrent.ConcurrentDictionary[string, object]]::new() +$pathsToBuild | ForEach-Object -Parallel { + $dict = $using:builtTestFileMap + $builtTemplate = bicep build $_ --stdout | ConvertFrom-Json -AsHashtable + $null = $dict.TryAdd($_, $builtTemplate) +} + $script:crossReferencedModuleList = Get-CrossReferencedModuleList Describe 'File/folder tests' -Tag 'Modules' { @@ -52,13 +67,13 @@ Describe 'File/folder tests' -Tag 'Modules' { } } - It '[] Module should contain a [` main.json ` / ` main.bicep `] file.' -TestCases $moduleFolderTestCases { + It '[] Module should contain a [` main.json ` & ` main.bicep `] file.' -TestCases $moduleFolderTestCases { param( [string] $moduleFolderPath ) $hasARM = Test-Path (Join-Path -Path $moduleFolderPath 'main.json') $hasBicep = Test-Path (Join-Path -Path $moduleFolderPath 'main.bicep') - ($hasARM -or $hasBicep) | Should -Be $true + ($hasARM -and $hasBicep) | Should -Be $true } It '[] Module should contain a [` README.md `] file.' -TestCases $moduleFolderTestCases { @@ -111,11 +126,10 @@ Describe 'File/folder tests' -Tag 'Modules' { It '[] Folder should contain one or more test files.' -TestCases $folderTestCases { param( - [string] $moduleFolderName, [string] $moduleFolderPath ) - $moduleTestFilePaths = Get-ModuleTestFileList -ModulePath $moduleFolderPath | ForEach-Object { Join-Path $moduleFolderPath $_ } + $moduleTestFilePaths = (Get-ChildItem -Path $moduleFolderPath -Recurse -Filter 'main.test.bicep').FullName | Sort-Object $moduleTestFilePaths.Count | Should -BeGreaterThan 0 } @@ -123,7 +137,7 @@ Describe 'File/folder tests' -Tag 'Modules' { foreach ($moduleFolderPath in $moduleFolderPaths) { $testFolderPath = Join-Path $moduleFolderPath '.test' if (Test-Path $testFolderPath) { - foreach ($testFilePath in (Get-ModuleTestFileList -ModulePath $moduleFolderPath | ForEach-Object { Join-Path $moduleFolderPath $_ })) { + foreach ($testFilePath in ((Get-ChildItem -Path $moduleFolderPath -Recurse -Filter 'main.test.bicep').FullName | Sort-Object)) { $testFolderFilesTestCases += @{ moduleFolderName = $moduleFolderPath.Replace('\', '/').Split('/modules/')[1] testFilePath = $testFilePath @@ -131,19 +145,6 @@ Describe 'File/folder tests' -Tag 'Modules' { } } } - - It '[] JSON test files in the `.test` folder should be valid json.' -TestCases $testFolderFilesTestCases { - - param( - [string] $moduleFolderName, - [string] $testFilePath - ) - if ((Split-Path $testFilePath -Extension) -eq '.json') { - { (Get-Content $testFilePath) | ConvertFrom-Json } | Should -Not -Throw - } else { - Set-ItResult -Skipped -Because 'the module has no JSON test files.' - } - } } } @@ -311,41 +312,13 @@ Describe 'Module tests' -Tag 'Module' { foreach ($moduleFolderPath in $moduleFolderPaths) { - # For runtime purposes, we cache the compiled template in a hashtable that uses a formatted relative module path as a key - $moduleFolderPathKey = $moduleFolderPath.Replace('\', '/').Split('/modules/')[1].Trim('/').Replace('/', '-') - if (-not ($convertedTemplates.Keys -contains $moduleFolderPathKey)) { - if (Test-Path (Join-Path $moduleFolderPath 'main.bicep')) { - $templateFilePath = Join-Path $moduleFolderPath 'main.bicep' - $templateContent = bicep build $templateFilePath --stdout | ConvertFrom-Json -AsHashtable - - if (-not $templateContent) { - throw ($bicepTemplateCompilationFailedException -f $templateFilePath) - } - } elseIf (Test-Path (Join-Path $moduleFolderPath 'main.json')) { - $templateFilePath = Join-Path $moduleFolderPath 'main.json' - $templateContent = Get-Content $templateFilePath -Raw | ConvertFrom-Json -AsHashtable - - if (-not $templateContent) { - throw ($jsonTemplateLoadFailedException -f $templateFilePath) - } - } else { - throw ($templateNotFoundException -f $moduleFolderPath) - } - $convertedTemplates[$moduleFolderPathKey] = @{ - templateFilePath = $templateFilePath - templateContent = $templateContent - } - } else { - $templateContent = $convertedTemplates[$moduleFolderPathKey].templateContent - $templateFilePath = $convertedTemplates[$moduleFolderPathKey].templateFilePath - } - $resourceTypeIdentifier = $moduleFolderPath.Replace('\', '/').Split('/modules/')[1] + $templateFilePath = Join-Path $moduleFolderPath 'main.bicep' $readmeFileTestCases += @{ moduleFolderName = $resourceTypeIdentifier moduleFolderPath = $moduleFolderPath - templateContent = $templateContent + templateContent = $builtTestFileMap[$templateFilePath] templateFilePath = $templateFilePath readMeFilePath = Join-Path -Path $moduleFolderPath 'README.md' readMeContent = Get-Content (Join-Path -Path $moduleFolderPath 'README.md') @@ -459,34 +432,8 @@ Describe 'Module tests' -Tag 'Module' { $deploymentFolderTestCases = [System.Collections.ArrayList] @() foreach ($moduleFolderPath in $moduleFolderPaths) { - # For runtime purposes, we cache the compiled template in a hashtable that uses a formatted relative module path as a key - $moduleFolderPathKey = $moduleFolderPath.Replace('\', '/').Split('/modules/')[1].Trim('/').Replace('/', '-') - if (-not ($convertedTemplates.Keys -contains $moduleFolderPathKey)) { - if (Test-Path (Join-Path $moduleFolderPath 'main.bicep')) { - $templateFilePath = Join-Path $moduleFolderPath 'main.bicep' - $templateContent = bicep build $templateFilePath --stdout | ConvertFrom-Json -AsHashtable - - if (-not $templateContent) { - throw ($bicepTemplateCompilationFailedException -f $templateFilePath) - } - } elseIf (Test-Path (Join-Path $moduleFolderPath 'main.json')) { - $templateFilePath = Join-Path $moduleFolderPath 'main.json' - $templateContent = Get-Content $templateFilePath -Raw | ConvertFrom-Json -AsHashtable - - if (-not $templateContent) { - throw ($jsonTemplateLoadFailedException -f $templateFilePath) - } - } else { - throw ($templateNotFoundException -f $moduleFolderPath) - } - $convertedTemplates[$moduleFolderPathKey] = @{ - templateFilePath = $templateFilePath - templateContent = $templateContent - } - } else { - $templateContent = $convertedTemplates[$moduleFolderPathKey].templateContent - $templateFilePath = $convertedTemplates[$moduleFolderPathKey].templateFilePath - } + $templateFilePath = Join-Path $moduleFolderPath 'main.bicep' + $templateContent = $builtTestFileMap[$templateFilePath] # Parameter file test cases $testFileTestCases = @() @@ -497,24 +444,13 @@ Describe 'Module tests' -Tag 'Module' { if (Test-Path (Join-Path $moduleFolderPath '.test')) { # Can be removed after full migration to bicep test files - $moduleTestFilePaths = Get-ModuleTestFileList -ModulePath $moduleFolderPath | ForEach-Object { Join-Path $moduleFolderPath $_ } + $moduleTestFilePaths = (Get-ChildItem -Path $moduleFolderPath -Recurse -Filter 'main.test.bicep').FullName | Sort-Object foreach ($moduleTestFilePath in $moduleTestFilePaths) { - if ((Split-Path $moduleTestFilePath -Extension) -eq '.json') { - - $rawContentHashtable = (Get-Content $moduleTestFilePath) | ConvertFrom-Json -AsHashtable - # Skipping any file that is not actually a ARM-JSON parameter file - $isParameterFile = $rawContentHashtable.'$schema' -like '*deploymentParameters*' - if (-not $isParameterFile) { - continue - } + $deploymentFileContent = bicep build $moduleTestFilePath --stdout | ConvertFrom-Json -AsHashtable + $deploymentTestFile_AllParameterNames = $deploymentFileContent.resources[-1].properties.parameters.Keys | Sort-Object # The last resource should be the test - $deploymentTestFile_AllParameterNames = $rawContentHashtable.parameters.Keys | Sort-Object - } else { - $deploymentFileContent = bicep build $moduleTestFilePath --stdout | ConvertFrom-Json -AsHashtable - $deploymentTestFile_AllParameterNames = $deploymentFileContent.resources[-1].properties.parameters.Keys | Sort-Object # The last resource should be the test - } $testFileTestCases += @{ testFile_Path = $moduleTestFilePath testFile_Name = Split-Path $moduleTestFilePath -Leaf @@ -900,35 +836,6 @@ Describe 'Module tests' -Tag 'Module' { $incorrectOutputs | Should -BeNullOrEmpty } - # PARAMETER Tests - It '[] All parameters in parameters files exist in template file (`main.json`).' -TestCases $deploymentFolderTestCases { - param ( - [hashtable[]] $testFileTestCases - ) - - foreach ($parameterFileTestCase in $testFileTestCases) { - $testFile_AllParameterNames = $parameterFileTestCase.testFile_AllParameterNames - $templateFile_AllParameterNames = $parameterFileTestCase.templateFile_AllParameterNames - - $nonExistentParameters = $testFile_AllParameterNames | Where-Object { $templateFile_AllParameterNames -notcontains $_ } - $nonExistentParameters.Count | Should -Be 0 -Because ('no parameter in the parameter file should not exist in the template file. Found excess items: [{0}].' -f ($nonExistentParameters -join ', ')) - } - } - - It '[] All required parameters in template file (`main.json`) should exist in parameters files.' -TestCases $deploymentFolderTestCases { - param ( - [hashtable[]] $testFileTestCases - ) - - foreach ($parameterFileTestCase in $testFileTestCases) { - $TemplateFile_RequiredParametersNames = $parameterFileTestCase.TemplateFile_RequiredParametersNames - $testFile_AllParameterNames = $parameterFileTestCase.testFile_AllParameterNames - - $missingParameters = $templateFile_RequiredParametersNames | Where-Object { $testFile_AllParameterNames -notcontains $_ } - $missingParameters.Count | Should -Be 0 -Because ('no required parameters in the template file should be missing in the parameter file. Found missing items: [{0}].' -f ($missingParameters -join ', ')) - } - } - It '[] All non-required parameters in template file should not have description that start with "Required.".' -TestCases $deploymentFolderTestCases { param ( [hashtable[]] $testFileTestCases, @@ -956,33 +863,8 @@ Describe 'Module tests' -Tag 'Module' { foreach ($moduleFolderPath in $moduleFolderPaths) { $moduleFolderName = $moduleFolderPath.Replace('\', '/').Split('/modules/')[1] - - # For runtime purposes, we cache the compiled template in a hashtable that uses a formatted relative module path as a key - $moduleFolderPathKey = $moduleFolderPath.Replace('\', '/').Split('/modules/')[1].Trim('/').Replace('/', '-') - if (-not ($convertedTemplates.Keys -contains $moduleFolderPathKey)) { - if (Test-Path (Join-Path $moduleFolderPath 'main.bicep')) { - $templateFilePath = Join-Path $moduleFolderPath 'main.bicep' - $templateContent = bicep build $templateFilePath --stdout | ConvertFrom-Json -AsHashtable - - if (-not $templateContent) { - throw ($bicepTemplateCompilationFailedException -f $templateFilePath) - } - } elseIf (Test-Path (Join-Path $moduleFolderPath 'main.json')) { - $templateFilePath = Join-Path $moduleFolderPath 'main.json' - $templateContent = Get-Content $templateFilePath -Raw | ConvertFrom-Json -AsHashtable - - if (-not $templateContent) { - throw ($jsonTemplateLoadFailedException -f $templateFilePath) - } - } else { - throw ($templateNotFoundException -f $moduleFolderPath) - } - $convertedTemplates[$moduleFolderPathKey] = @{ - templateContent = $templateContent - } - } else { - $templateContent = $convertedTemplates[$moduleFolderPathKey].templateContent - } + $templateFilePath = Join-Path $moduleFolderPath 'main.bicep' + $templateContent = $builtTestFileMap[$templateFilePath] $metadataFileTestCases += @{ moduleFolderName = $moduleFolderName @@ -1023,22 +905,11 @@ Describe 'Test file tests' -Tag 'TestTemplate' { foreach ($moduleFolderPath in $moduleFolderPaths) { if (Test-Path (Join-Path $moduleFolderPath '.test')) { - $testFilePaths = Get-ModuleTestFileList -ModulePath $moduleFolderPath | ForEach-Object { Join-Path $moduleFolderPath $_ } + $testFilePaths = (Get-ChildItem -Path $moduleFolderPath -Recurse -Filter 'main.test.bicep').FullName | Sort-Object foreach ($testFilePath in $testFilePaths) { - $testFileContent = Get-Content $testFilePath - - if ((Split-Path $testFilePath -Extension) -eq '.json') { - # Skip any classic parameter files - $contentHashtable = $testFileContent | ConvertFrom-Json -Depth 99 - $isParameterFile = $contentHashtable.'$schema' -like '*deploymentParameters*' - if ($isParameterFile) { - continue - } - } - $deploymentTestFileTestCases += @{ testFilePath = $testFilePath - testFileContent = $testFileContent + testFileContent = Get-Content $testFilePath moduleFolderName = $moduleFolderPath.Replace('\', '/').Split('/modules/')[1] } } @@ -1076,31 +947,6 @@ Describe 'Test file tests' -Tag 'TestTemplate' { $hasExpectedParam | Should -Be $true } - - It '[] JSON test deployment name should contain [`-test-`].' -TestCases ($deploymentTestFileTestCases | Where-Object { (Split-Path $_.testFilePath -Extension) -eq '.json' }) { - - param( - [object[]] $testFileContent - ) - - # Handle case of deployment test file (instead of ARM-JSON parameter file) - $rawContentHashtable = $testFileContent | ConvertFrom-Json -Depth 99 - - # Uses deployment test file (instead of parameter file). Need to extract parameters. - $testResource = $rawContentHashtable.resources | Where-Object { $_.name -like '*-test-*' } - - $testResource | Should -Not -BeNullOrEmpty -Because 'the handle ''-test-'' should be part of the module test invocation''s resource name to allow identification.' - } - - It '[] JSON test deployment should have parameter [`serviceShort`].' -TestCases ($deploymentTestFileTestCases | Where-Object { (Split-Path $_.testFilePath -Extension) -eq '.json' }) { - - param( - [object[]] $testFileContent - ) - - $rawContentHashtable = $testFileContent | ConvertFrom-Json -Depth 99 -AsHashtable - $rawContentHashtable.parameters.keys | Should -Contain 'serviceShort' - } } Context 'Token usage' { @@ -1110,7 +956,7 @@ Describe 'Test file tests' -Tag 'TestTemplate' { foreach ($moduleFolderPath in $moduleFolderPaths) { if (Test-Path (Join-Path $moduleFolderPath '.test')) { - $testFilePaths = Get-ModuleTestFileList -ModulePath $moduleFolderPath | ForEach-Object { Join-Path $moduleFolderPath $_ } + $testFilePaths = (Get-ChildItem -Path $moduleFolderPath -Recurse -Filter 'main.test.bicep').FullName | Sort-Object foreach ($testFilePath in $testFilePaths) { foreach ($token in $enforcedTokenList.Keys) { $parameterFileTokenTestCases += @{ @@ -1160,35 +1006,8 @@ Describe 'API version tests' -Tag 'ApiCheck' { foreach ($moduleFolderPath in $moduleFolderPaths) { $moduleFolderName = $moduleFolderPath.Replace('\', '/').Split('/modules/')[1] - - # For runtime purposes, we cache the compiled template in a hashtable that uses a formatted relative module path as a key - $moduleFolderPathKey = $moduleFolderPath.Replace('\', '/').Split('/modules/')[1].Trim('/').Replace('/', '-') - if (-not ($convertedTemplates.Keys -contains $moduleFolderPathKey)) { - if (Test-Path (Join-Path $moduleFolderPath 'main.bicep')) { - $templateFilePath = Join-Path $moduleFolderPath 'main.bicep' - $templateContent = bicep build $templateFilePath --stdout | ConvertFrom-Json -AsHashtable - - if (-not $templateContent) { - throw ($bicepTemplateCompilationFailedException -f $templateFilePath) - } - } elseIf (Test-Path (Join-Path $moduleFolderPath 'main.json')) { - $templateFilePath = Join-Path $moduleFolderPath 'main.json' - $templateContent = Get-Content $templateFilePath -Raw | ConvertFrom-Json -AsHashtable - - if (-not $templateContent) { - throw ($jsonTemplateLoadFailedException -f $templateFilePath) - } - } else { - throw ($templateNotFoundException -f $moduleFolderPath) - } - $convertedTemplates[$moduleFolderPathKey] = @{ - templateFilePath = $templateFilePath - templateContent = $templateContent - } - } else { - $templateContent = $convertedTemplates[$moduleFolderPathKey].templateContent - $templateFilePath = $convertedTemplates[$moduleFolderPathKey].templateFilePath - } + $templateFilePath = Join-Path $moduleFolderPath 'main.bicep' + $templateContent = $builtTestFileMap[$templateFilePath] $nestedResources = Get-NestedResourceList -TemplateFileContent $templateContent | Where-Object { $_.type -notin @('Microsoft.Resources/deployments') -and $_