Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: CI - Added test for UDT nullable & array + support for PesterTags #4098

Merged
merged 12 commits into from
Jan 18, 2025
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,6 @@ Describe 'Module tests' -Tag 'Module' {
$incorrectParameters | Should -BeNullOrEmpty -Because ('parameters in the template file should be camel-cased. Found incorrect items: [{0}].' -f ($incorrectParameters -join ', '))
}


It "[<moduleFolderName>] Each parameters' & UDT's description should start with a one word category starting with a capital letter, followed by a dot, a space and the actual description text ending with a dot." -TestCases $moduleFolderTestCases {

param(
Expand Down Expand Up @@ -1174,6 +1173,83 @@ Describe 'Module tests' -Tag 'Module' {
$outputs | Should -Contain 'systemAssignedMIPrincipalId'
}
}

Context 'UDT-spcific' {
AlexanderSehr marked this conversation as resolved.
Show resolved Hide resolved

It '[<moduleFolderName>] A UDT should not be of type array, but instead the parameter that uses it. AVM-Spec-Ref: BCPNFR18.' -TestCases $moduleFolderTestCases -Tag 'UDT' {

param(
[hashtable] $templateFileContent
)

if (-not $templateFileContent.definitions) {
Set-ItResult -Skipped -Because 'the module template has no user-defined types.'
return
}

$incorrectTypes = [System.Collections.ArrayList]@()
foreach ($type in $templateFileContent.definitions.Keys) {
if ($templateFileContent.definitions.$type.type -eq 'array') {
$incorrectTypes += $type
}
}
# To be re-enabled once more modules are prepared. The code right below can then be removed.
# $incorrectTypes | Should -BeNullOrEmpty -Because ('no user-defined type should be declared as an array, but instead the parameter that uses the type. This makes the template and its parameters easier to understand. Found incorrect items: [{0}].' -f ($incorrectTypes -join ', '))
$warningMessage = ('No user-defined type should be declared as an array, but instead the parameter that uses the type. This makes the template and its parameters easier to understand. Found incorrect items: [{0}].' -f ($incorrectTypes -join ', '))
Write-Warning $warningMessage
Set-ItResult -Skipped -Because $warningMessage
}

It '[<moduleFolderName>] A UDT should not be nullable, but instead the parameter that uses it. AVM-Spec-Ref: BCPNFR18.' -TestCases $moduleFolderTestCases -Tag 'UDT' {

param(
[hashtable] $templateFileContent
)

if (-not $templateFileContent.definitions) {
Set-ItResult -Skipped -Because 'the module template has no user-defined types.'
return
}

$incorrectTypes = [System.Collections.ArrayList]@()
foreach ($type in $templateFileContent.definitions.Keys) {
if ($templateFileContent.definitions.$type.nullable -eq $true) {
$incorrectTypes += $type
}
}

# To be re-enabled once more modules are prepared. The code right below can then be removed.
# $incorrectTypes | Should -BeNullOrEmpty -Because ('no user-defined type should be declared as nullable, but instead the parameter that uses the type. This makes the template and its parameters easier to understand. Found incorrect items: [{0}].' -f ($incorrectTypes -join ', '))
$warningMessage = ('No user-defined type should be declared as nullable, but instead the parameter that uses the type. This makes the template and its parameters easier to understand. Found incorrect items: [{0}].' -f ($incorrectTypes -join ', '))
Write-Warning $warningMessage
Set-ItResult -Skipped -Because $warningMessage
}

It '[<moduleFolderName>] A UDT should always be camel-cased and end with the suffix "Type". AVM-Spec-Ref: BCPNFR19.' -TestCases $moduleFolderTestCases -Tag 'UDT' {

param(
[hashtable] $templateFileContent
)

if (-not $templateFileContent.definitions) {
Set-ItResult -Skipped -Because 'the module template has no user-defined types.'
return
}

$incorrectTypes = [System.Collections.ArrayList]@()
foreach ($typeName in $templateFileContent.definitions.Keys) {
if ($typeName -cnotmatch '^[a-z].*Type$') {
$incorrectTypes += $typeName
}
}

# To be re-enabled once more modules are prepared. The code right below can then be removed.
# $incorrectTypes | Should -BeNullOrEmpty -Because ('every used-defined type should be camel-cased and end with the suffix "Type". Found incorrect items: [{0}].' -f ($incorrectTypes -join ', '))
$warningMessage = ('Every used-defined type should be camel-cased and end with the suffix "Type". Found incorrect items: [{0}].' -f ($incorrectTypes -join ', '))
Write-Warning $warningMessage
Set-ItResult -Skipped -Because $warningMessage
}
}
}
}

Expand Down
17 changes: 16 additions & 1 deletion utilities/tools/Test-ModuleLocally.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ Mandatory. Path to the Bicep/ARM module that is being tested
.PARAMETER ModuleTestFilePath
Optional. Path to the template file/folder that is to be tested with the template file. Defaults to the module's default '.test' folder. Will be used if the DeploymentTest/ValidationTest switches are set.

.PARAMETER PesterTag
AlexanderSehr marked this conversation as resolved.
Show resolved Hide resolved
Optional. A string array that can be specified to run only Pester tests with the specified tag

.PARAMETER PesterTest
Optional. A switch parameter that triggers a Pester test for the module

Expand Down Expand Up @@ -135,6 +138,10 @@ function Test-ModuleLocally {
[Parameter(Mandatory = $false)]
[hashtable] $AdditionalTokens = @{},

[Parameter(Mandatory = $false)]
[Alias('PesterTags')]
[string[]] $PesterTag,

[Parameter(Mandatory = $false)]
[switch] $PesterTest,

Expand Down Expand Up @@ -186,7 +193,7 @@ function Test-ModuleLocally {
}
}

Invoke-Pester -Configuration @{
$configuration = @{
Run = @{
Container = New-PesterContainer -Path $testFiles -Data @{
repoRootPath = $repoRootPath
Expand All @@ -197,6 +204,14 @@ function Test-ModuleLocally {
Verbosity = 'Detailed'
}
}

if (-not [String]::IsNullOrEmpty($PesterTag)) {
$configuration['Filter'] = @{
Tag = $PesterTag
}
}

Invoke-Pester -Configuration $configuration
} catch {
$PSItem.Exception.Message
}
Expand Down
Loading