Skip to content

Commit

Permalink
add: Output type to functions
Browse files Browse the repository at this point in the history
  • Loading branch information
DrIOSX committed Jun 9, 2024
1 parent 5a0475c commit b9de063
Show file tree
Hide file tree
Showing 17 changed files with 100 additions and 41 deletions.
1 change: 1 addition & 0 deletions source/Private/Assert-ModuleAvailability.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
function Assert-ModuleAvailability {
[OutputType([void]) ]
param(
[string]$ModuleName,
[string]$RequiredVersion,
Expand Down
1 change: 1 addition & 0 deletions source/Private/Connect-M365Suite.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
function Connect-M365Suite {
[OutputType([void])]
[CmdletBinding()]
param (
[Parameter(Mandatory=$false)]
Expand Down
1 change: 1 addition & 0 deletions source/Private/Disconnect-M365Suite.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
function Disconnect-M365Suite {
[OutputType([void])]
param (
[Parameter(Mandatory)]
[string[]]$RequiredConnections
Expand Down
8 changes: 6 additions & 2 deletions source/Private/Format-MissingAction.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
function Format-MissingAction {
param ([array]$missingActions)
[CmdletBinding()]
[OutputType([hashtable])]
param (
[array]$missingActions
)

$actionGroups = @{
"Admin" = @()
Expand All @@ -22,4 +26,4 @@ function Format-MissingAction {
}

return $formattedResults
}
}
19 changes: 19 additions & 0 deletions source/Private/Format-RequiredModuleList.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function Format-RequiredModuleList {
[CmdletBinding()]
[OutputType([string])]
param (
[Parameter(Mandatory = $true)]
[System.Object[]]$RequiredModules
)

$requiredModulesFormatted = ""
foreach ($module in $RequiredModules) {
if ($module.SubModules -and $module.SubModules.Count -gt 0) {
$subModulesFormatted = $module.SubModules -join ', '
$requiredModulesFormatted += "$($module.ModuleName) (SubModules: $subModulesFormatted), "
} else {
$requiredModulesFormatted += "$($module.ModuleName), "
}
}
return $requiredModulesFormatted.TrimEnd(", ")
}
4 changes: 3 additions & 1 deletion source/Private/Get-MostCommonWord.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
function Get-MostCommonWord {
[CmdletBinding()]
[OutputType([string])]
param (
[Parameter(Mandatory = $true)]
[string[]]$InputStrings
Expand All @@ -19,4 +21,4 @@ function Get-MostCommonWord {
} else {
return $null
}
}
}
4 changes: 3 additions & 1 deletion source/Private/Get-TestDefinitionsObject.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
function Get-TestDefinitionsObject {
[CmdletBinding()]
[OutputType([object[]])]
param (
[Parameter(Mandatory = $true)]
[object[]]$TestDefinitions,
Expand Down Expand Up @@ -60,4 +62,4 @@ function Get-TestDefinitionsObject {

Write-Verbose "Filtered test definitions count: $($TestDefinitions.Count)"
return $TestDefinitions
}
}
1 change: 1 addition & 0 deletions source/Private/Initialize-CISAuditResult.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
function Initialize-CISAuditResult {
[CmdletBinding()]
[OutputType([CISAuditResult])]
param (
[Parameter(Mandatory = $true)]
[string]$Rec,
Expand Down
1 change: 1 addition & 0 deletions source/Private/Invoke-TestFunction.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
function Invoke-TestFunction {
[OutputType([CISAuditResult[]])]
param (
[Parameter(Mandatory = $true)]
[PSObject]$FunctionFile,
Expand Down
1 change: 1 addition & 0 deletions source/Private/Measure-AuditResult.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
function Measure-AuditResult {
[OutputType([void])]
param (
[Parameter(Mandatory = $true)]
[System.Collections.ArrayList]$AllAuditResults,
Expand Down
1 change: 1 addition & 0 deletions source/Private/Merge-CISExcelAndCsvData.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
function Merge-CISExcelAndCsvData {
[CmdletBinding(DefaultParameterSetName = 'CsvInput')]
[OutputType([PSCustomObject[]])]
param (
[Parameter(Mandatory = $true)]
[string]$ExcelPath,
Expand Down
2 changes: 2 additions & 0 deletions source/Private/New-MergedObject.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
function New-MergedObject {
[CmdletBinding()]
[OutputType([PSCustomObject])]
param (
[Parameter(Mandatory = $true)]
[psobject]$ExcelItem,
Expand Down
1 change: 1 addition & 0 deletions source/Private/Update-CISExcelWorksheet.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
function Update-CISExcelWorksheet {
[OutputType([void])]
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
Expand Down
51 changes: 26 additions & 25 deletions source/Private/Update-WorksheetCell.ps1
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
function Update-WorksheetCell {
param (
$Worksheet,
$Data,
$StartingRowIndex
)
function Update-WorksheetCell {
[OutputType([void])]
param (
$Worksheet,
$Data,
$StartingRowIndex
)

# Check and set headers
$firstItem = $Data[0]
$colIndex = 1
foreach ($property in $firstItem.PSObject.Properties) {
if ($StartingRowIndex -eq 2 -and $Worksheet.Cells[1, $colIndex].Value -eq $null) {
$Worksheet.Cells[1, $colIndex].Value = $property.Name
}
$colIndex++
}
# Check and set headers
$firstItem = $Data[0]
$colIndex = 1
foreach ($property in $firstItem.PSObject.Properties) {
if ($StartingRowIndex -eq 2 -and $Worksheet.Cells[1, $colIndex].Value -eq $null) {
$Worksheet.Cells[1, $colIndex].Value = $property.Name
}
$colIndex++
}

# Iterate over each row in the data and update cells
$rowIndex = $StartingRowIndex
foreach ($item in $Data) {
$colIndex = 1
foreach ($property in $item.PSObject.Properties) {
$Worksheet.Cells[$rowIndex, $colIndex].Value = $property.Value
$colIndex++
}
$rowIndex++
}
# Iterate over each row in the data and update cells
$rowIndex = $StartingRowIndex
foreach ($item in $Data) {
$colIndex = 1
foreach ($property in $item.PSObject.Properties) {
$Worksheet.Cells[$rowIndex, $colIndex].Value = $property.Value
$colIndex++
}
$rowIndex++
}
}
17 changes: 5 additions & 12 deletions source/Public/Invoke-M365SecurityAudit.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -183,18 +183,11 @@ function Invoke-M365SecurityAudit {
}
# Ensure required modules are installed
$requiredModules = Get-RequiredModule -AuditFunction
$requiredModulesFormatted = ""
foreach ($module in $requiredModules) {
if ($module.SubModules -and $module.SubModules.Count -gt 0) {
$subModulesFormatted = $module.SubModules -join ', '
$requiredModulesFormatted += "$($module.ModuleName) (SubModules: $subModulesFormatted), "
}
else {
$requiredModulesFormatted += "$($module.ModuleName), "
}
}
$requiredModulesFormatted = $requiredModulesFormatted.TrimEnd(", ")

# Format the required modules list
$requiredModulesFormatted = Format-RequiredModuleList -RequiredModules $requiredModules

# Check and install required modules if necessary
if (!($NoModuleCheck) -and $PSCmdlet.ShouldProcess("Check for required modules: $requiredModulesFormatted", "Check")) {
foreach ($module in $requiredModules) {
Assert-ModuleAvailability -ModuleName $module.ModuleName -RequiredVersion $module.RequiredVersion -SubModules $module.SubModules
Expand Down Expand Up @@ -254,7 +247,7 @@ function Invoke-M365SecurityAudit {
}



Write-Information "A total of $($totalTests) tests were selected to run..." -InformationAction Continue
# Import the test functions
$testFiles | ForEach-Object {
$currentTestIndex++
Expand Down
1 change: 1 addition & 0 deletions source/Public/Sync-CISExcelAndCsvData.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ If the SkipUpdate switch is used, the function returns an array of custom object
https://criticalsolutionsnetwork.github.io/M365FoundationsCISReport/#Sync-CISExcelAndCsvData
#>
function Sync-CISExcelAndCsvData {
[OutputType([void], [PSCustomObject[]])]
[CmdletBinding(DefaultParameterSetName = 'CsvInput')]
param (
[Parameter(Mandatory = $true)]
Expand Down
27 changes: 27 additions & 0 deletions tests/Unit/Private/Format-RequiredModuleList.tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
$ProjectPath = "$PSScriptRoot\..\..\.." | Convert-Path
$ProjectName = ((Get-ChildItem -Path $ProjectPath\*\*.psd1).Where{
($_.Directory.Name -match 'source|src' -or $_.Directory.Name -eq $_.BaseName) -and
$(try { Test-ModuleManifest $_.FullName -ErrorAction Stop } catch { $false } )
}).BaseName


Import-Module $ProjectName

InModuleScope $ProjectName {
Describe Get-PrivateFunction {
Context 'Default' {
BeforeEach {
$return = Get-PrivateFunction -PrivateData 'string'
}

It 'Returns a single object' {
($return | Measure-Object).Count | Should -Be 1
}

It 'Returns a string based on the parameter PrivateData' {
$return | Should -Be 'string'
}
}
}
}

0 comments on commit b9de063

Please sign in to comment.