Skip to content

Commit

Permalink
fix: Enabled prefix for test folders when testing defaults/max/waf-al…
Browse files Browse the repository at this point in the history
…igned & added improved error output for invalid templates (#736)

## Description

### Prefix
Enabled prefix for test folders when testing defaults/max/waf-aligned.
For example `/linux.defaults/main.test.ps1`.

It accepts either e.g. `/defaults/` or `/<anything>.defaults/`,
enforcing a `.` in between whatever prefix and the actual test folder
name.

Validation via Regex101:

![image](https://github.com/Azure/bicep-registry-modules/assets/5365358/e1899acd-ef04-413a-8a16-4f93e698ecb1)

| Pipeline (to prove no existing functionality breaks) |
| - |

[![avm.res.key-vault.vault](https://github.com/AlexanderSehr/bicep-registry-modules/actions/workflows/avm.res.key-vault.vault.yml/badge.svg?branch=users%2Falsehr%2F735_alternateFolderNames&event=workflow_dispatch)](https://github.com/AlexanderSehr/bicep-registry-modules/actions/workflows/avm.res.key-vault.vault.yml)

### Added Error Output

Added additional error output ([example
run](https://github.com/AlexanderSehr/bicep-registry-modules/actions/runs/7279337656/job/19835447963#step:4:155))
to increase verbosity in case a template cannot be built (until now the
error showed up somewhere where the logic tied to access the empty
output - leading to incorrect conflusions).


![image](https://github.com/Azure/bicep-registry-modules/assets/5365358/29fe2f99-9586-48bf-bd2f-e6ca7a17c9cf)

---------

Co-authored-by: Erika Gressi <[email protected]>
  • Loading branch information
AlexanderSehr and eriqua authored Dec 21, 2023
1 parent cabf6e7 commit 5e92f22
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
12 changes: 9 additions & 3 deletions avm/utilities/pipelines/sharedScripts/Set-ModuleReadMe.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1158,11 +1158,17 @@ function Set-UsageExamplesSection {
# Prepare data (using thread-safe multithreading) to consume later
$buildTestFileMap = [System.Collections.Concurrent.ConcurrentDictionary[string, object]]::new()
$testFilePaths | ForEach-Object -Parallel {
$dict = $using:buildTestFileMap

$folderName = Split-Path (Split-Path -Path $_) -Leaf
$buildTemplate = (bicep build $_ --stdout 2>$null) | ConvertFrom-Json -AsHashtable
$builtTemplate = (bicep build $_ --stdout 2>$null) | Out-String

$dict = $using:buildTestFileMap
$null = $dict.TryAdd($folderName, $buildTemplate)
if ([String]::IsNullOrEmpty($builtTemplate)) {
throw "Failed to build template [$_]. Try running the command ``bicep build $_ --stdout`` locally for troubleshooting. Make sure you have the latest Bicep CLI installed."
}
$templateHashTable = ConvertFrom-Json $builtTemplate -AsHashtable

$null = $dict.TryAdd($folderName, $templateHashTable)
}

# Process data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,12 @@ foreach ($moduleFolderPath in $moduleFolderPaths) {
$builtTestFileMap = [System.Collections.Concurrent.ConcurrentDictionary[string, object]]::new()
$pathsToBuild | ForEach-Object -Parallel {
$dict = $using:builtTestFileMap
$builtTemplate = (bicep build $_ --stdout 2>$null) | ConvertFrom-Json -AsHashtable
$null = $dict.TryAdd($_, $builtTemplate)
$builtTemplate = (bicep build $_ --stdout 2>$null) | Out-String
if ([String]::IsNullOrEmpty($builtTemplate)) {
throw "Failed to build template [$_]. Try running the command ``bicep build $_ --stdout`` locally for troubleshooting. Make sure you have the latest Bicep CLI installed."
}
$templateHashTable = ConvertFrom-Json $builtTemplate -AsHashtable
$null = $dict.TryAdd($_, $templateHashTable)
}

Describe 'File/folder tests' -Tag 'Modules' {
Expand Down Expand Up @@ -1182,7 +1186,7 @@ Describe 'Test file tests' -Tag 'TestTemplate' {
($testFileContent -match "^param serviceShort string = '(.*)$") | Should -Not -BeNullOrEmpty -Because 'the module test deployment file should contain a parameter [serviceShort] using the syntax [param serviceShort string = ''*''].'
}

It '[<moduleFolderName>] [<testName>] Bicep test deployment files in a [defaults] folder should have a parameter [serviceShort] with a value ending with [min]' -TestCases ($deploymentTestFileTestCases | Where-Object { $_.testFilePath -match '.*[\\|\/]defaults[\\|\/].*' }) {
It '[<moduleFolderName>] [<testName>] Bicep test deployment files in a [defaults] folder should have a parameter [serviceShort] with a value ending with [min]' -TestCases ($deploymentTestFileTestCases | Where-Object { $_.testFilePath -match '.*[\\|\/](.+\.)?defaults[\\|\/].*' }) {

param(
[object[]] $testFileContent
Expand All @@ -1195,7 +1199,7 @@ Describe 'Test file tests' -Tag 'TestTemplate' {
}
}

It '[<moduleFolderName>] [<testName>] Bicep test deployment files in a [max] folder should have a [serviceShort] parameter with a value ending with [max]' -TestCases ($deploymentTestFileTestCases | Where-Object { $_.testFilePath -match '.*[\\|\/]max[\\|\/].*' }) {
It '[<moduleFolderName>] [<testName>] Bicep test deployment files in a [max] folder should have a [serviceShort] parameter with a value ending with [max]' -TestCases ($deploymentTestFileTestCases | Where-Object { $_.testFilePath -match '.*[\\|\/](.+\.)?max[\\|\/].*' }) {

param(
[object[]] $testFileContent
Expand All @@ -1208,7 +1212,7 @@ Describe 'Test file tests' -Tag 'TestTemplate' {
}
}

It '[<moduleFolderName>] [<testName>] Bicep test deployment files in a [waf-aligned] folder should have a [serviceShort] parameter with a value ending with [waf]' -TestCases ($deploymentTestFileTestCases | Where-Object { $_.testFilePath -match '.*[\\|\/]waf\-aligned[\\|\/].*' }) {
It '[<moduleFolderName>] [<testName>] Bicep test deployment files in a [waf-aligned] folder should have a [serviceShort] parameter with a value ending with [waf]' -TestCases ($deploymentTestFileTestCases | Where-Object { $_.testFilePath -match '.*[\\|\/](.+\.)?waf\-aligned[\\|\/].*' }) {

param(
[object[]] $testFileContent
Expand Down

0 comments on commit 5e92f22

Please sign in to comment.