Skip to content

Commit

Permalink
Add test for correct AWS tools module imports
Browse files Browse the repository at this point in the history
  • Loading branch information
FC\abm committed Mar 11, 2020
1 parent b6adad8 commit c5f5992
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions tests/0-ModuleManifest.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,68 @@ Describe "$ModuleName Module - Testing Manifest File (.psd1)" {
$_ | Should Not Match '\s'
}
}
It "Should have matching dependencies in RequiredModules and PrivateData.PSData['ExternalModuleDependencies']" {

Compare-Object -ReferenceObject $ModuleInformation.RequiredModules -DifferenceObject $ModuleInformation.PrivateData.PSData['ExternalModuleDependencies'] | SHould -BeNullOrEmpty
}
}

Context 'Required dependencies are listed' {
Write-Host -ForegroundColor Green ' Scanning module files...'

# All verbs
$verbs = Get-Verb | Select-Object -ExpandProperty Verb

# AWS service info
$services = Get-AWSService |
Where-Object {
# Confusion between AS and ASA prefixes
('AWS.Tools.AWSSupport') -inotcontains $_.ModuleName
}

# Service cmdlet noun prefixes
$nounPrefixes = $services |
Select-Object -ExpandProperty CmdletNounPrefix |
Sort-Object -Property @{Expression = {$_.Length}; Descending = $true}

# Build regex to search for AWS cmldet calls within the module
$regex = New-Object System.Text.RegularExpressions.Regex -ArgumentList "($($verbs -join '|'))-(?<prefix>$($nounPrefixes -join '|'))[A-Za-z\d]+"

# Gather module content
$moduleScripts = Get-ChildItem -Path ([IO.Path]::Combine($PSScriptRoot, '..', 'aws-toolbox')) -Filter *.ps1 -Recurse

# Detect all usages of AWS cmdlets by unique noun prefix
$detectedNounPrefixes = $moduleScripts |
ForEach-Object {
$_ | Get-Content |
ForEach-Object {
$mc = $regex.Matches($_)

if ($mc.Success)
{
$mc |
Foreach-Object {
$_.Groups['prefix'].Value
}
}
}
} |
Sort-Object -Unique

# Check module for each prefix is listed as a dependency
$detectedNounPrefixes |
Foreach-Object {

$prefix = $_
$dependency = $services |
Where-Object {
$_.CmdletNounPrefix -eq $prefix
}

It "Should import $($dependency.ModuleName)" {

$ModuleInformation.RequiredModules.Name | Should -Contain $dependency.ModuleName
}
}
}
}

0 comments on commit c5f5992

Please sign in to comment.