Skip to content

Commit 65e3385

Browse files
authored
Merge pull request #12 from tinuwalther/mwa/test
Implementing Parameter WhatIf in functions
2 parents 3b15414 + 8139146 commit 65e3385

File tree

6 files changed

+37
-19
lines changed

6 files changed

+37
-19
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22

33
Version | Description | Date | Author
44
-|-|-|-
5-
2.0.1 | Update to Version 2.0.0 and later | 2023-04-26 | Martin Walther
5+
2.0.1 | Initial upload | 2023-04-26 | Martin Walther
6+
2.0.2 | Implementing Parameter WhatIf in functions | 2023-04-29 | Martin Walther

CI/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.json

CI/Build-Module.ps1

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ Write-Host "[BUILD] [START] Launching Build Process" -ForegroundColor Green
1010
#region prepare folders
1111
$Current = (Split-Path -Path $MyInvocation.MyCommand.Path)
1212
$Root = ((Get-Item $Current).Parent).FullName
13-
$BackupPath = Join-Path -Path $Root -ChildPath "Backup"
14-
$TestsPath = Join-Path -Path $Root -ChildPath "Tests"
15-
$CISourcePath = Join-Path -Path $Root -ChildPath "CI"
16-
$CodeSourcePath = Join-Path -Path $Root -ChildPath "Code"
17-
$TestsScript = Join-Path -Path $TestsPath -ChildPath "Functions.Tests.ps1"
18-
$TestsFailures = Join-Path -Path $TestsPath -ChildPath "Failed.Tests.json"
19-
$Settings = Join-Path -Path $CISourcePath -ChildPath "Module-Settings.json"
13+
$BackupPath = Join-Path -Path $Root -ChildPath 'Backup'
14+
$TestsPath = Join-Path -Path $Root -ChildPath 'Tests'
15+
$CISourcePath = Join-Path -Path $Root -ChildPath 'CI'
16+
$CodeSourcePath = Join-Path -Path $Root -ChildPath 'Code'
17+
$PrivatePath = Join-Path -Path $CodeSourcePath -ChildPath 'Private'
18+
$PublicPath = Join-Path -Path $CodeSourcePath -ChildPath 'Public'
19+
$TestsScript = Join-Path -Path $TestsPath -ChildPath 'Functions.Tests.ps1'
20+
$TestsFailures = Join-Path -Path $TestsPath -ChildPath 'Failed.Tests.json'
21+
$Settings = Join-Path -Path $CISourcePath -ChildPath 'Module-Settings.json'
2022
#endregion
2123

2224
#region Module-Settings
@@ -57,13 +59,18 @@ $ModuleName = $ModuleName.ToLower() -replace '\-', '.' # Lower-case
5759
ModulePrefix = $ModulePrefix
5860
LastChange = $LastChange
5961
} | ConvertTo-Json | Out-File -FilePath $Settings -Encoding utf8
62+
#endregion
63+
64+
#region PRE-functions
65+
Copy-Item -Path $CodeSourcePath -Destination $BackupPath -Filter '*-PRE*.ps1' -Recurse -Force -Confirm:$false
6066

61-
Get-ChildItem -Path (Join-Path $CodeSourcePath -ChildPath 'Private') -Filter '*-*.ps1' | ForEach-Object {
67+
# Rename private and public PRE-functions
68+
Get-ChildItem -Path $PrivatePath -Filter '*-*.ps1' | ForEach-Object {
6269
$newname = $($_.Name -replace '-PRE',"-$($ModulePrefix)")
6370
(Get-Content -Path $_.FullName) -replace '-PRE',"-$($ModulePrefix)" | Set-Content -Path $_.FullName
6471
Rename-Item -Path $_.FullName -NewName $newname #-PassThru
6572
}
66-
Get-ChildItem -Path (Join-Path $CodeSourcePath -ChildPath 'Public') -Filter '*-*.ps1' | ForEach-Object {
73+
Get-ChildItem -Path $PublicPath -Filter '*-*.ps1' | ForEach-Object {
6774
$newname = $($_.Name -replace '-PRE',"-$($ModulePrefix)")
6875
(Get-Content -Path $_.FullName) -replace '-PRE',"-$($ModulePrefix)" | Set-Content -Path $_.FullName
6976
Rename-Item -Path $_.FullName -NewName $newname #-PassThru
@@ -83,20 +90,17 @@ Write-Host "[BUILD] [TEST] Running Function-Tests" -ForegroundColor Green
8390
$TestsResult = Invoke-Pester -Script $TestsScript -Output Normal -PassThru
8491
if($TestsResult.FailedCount -eq 0){
8592

86-
$ModuleFolderRootPath = Join-Path -Path $Root -ChildPath $ModuleName
87-
$ModuleFolderPath = Join-Path -Path $ModuleFolderRootPath -ChildPath $ModuleVersion
88-
#$ModuleFolderPath = $Root
93+
$ModuleFolderPath = Join-Path -Path $Root -ChildPath $ModuleName
94+
8995
if(-not(Test-Path -Path $ModuleFolderPath)){
9096
$null = New-Item -Path $ModuleFolderPath -ItemType Directory -Force
9197
}
92-
#endregion
9398

9499
#region Update the Module-File
95-
# Remove existent PSM1-File
100+
# Move existent PSM1-File to the backup-folder
96101
$ExportPath = Join-Path -Path $ModuleFolderPath -ChildPath "$($ModuleName).psm1"
97102
if(Test-Path $ExportPath){
98103
Write-Host "[BUILD] [PSM1 ] PSM1 file detected. Deleting..." -ForegroundColor Green
99-
#Remove-Item -Path $ExportPath -Force
100104
Move-Item -Path $ExportPath -Destination $BackupPath -Force -Confirm:$false
101105
}
102106

@@ -170,4 +174,11 @@ else{
170174
#region Module.Tests.ps1
171175
Write-Host "`n"
172176
Invoke-Pester -Script (Join-Path -Path $TestsPath -ChildPath "Module.Tests.ps1") -Output Detailed
173-
#endregion
177+
#endregion
178+
179+
#region manually
180+
Write-Host "`nIf you have dependencies to other modules, please fill in to the module manifest (psd1) as RequiredModules. See at " -ForegroundColor Cyan
181+
Write-Host "https://learn.microsoft.com/en-us/powershell/scripting/developer/module/how-to-write-a-powershell-module-manifest?view=powershell-7.3" -ForegroundColor Cyan
182+
Write-Host "`nModule Manifest:"
183+
Import-LocalizedData -BaseDirectory $ModuleFolderPath -FileName "$($ModuleName).psd1"
184+
#endregion

Code/Private/Write-PRELog.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function Write-PRELog{
2828
2929
#>
3030

31-
[CmdletBinding()]
31+
[CmdletBinding(SupportsShouldProcess=$True)]
3232
param(
3333
[Parameter(Mandatory=$false)]
3434
[string] $LogFile,

Code/Public/Get-PRETemplate.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function Get-PRETemplate{
2020
2121
#>
2222

23-
[CmdletBinding()]
23+
[CmdletBinding(SupportsShouldProcess=$True)]
2424
param(
2525
[Parameter(Mandatory = $true)]
2626
[String]$Name

Tests/Module.Tests.ps1

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ Describe 'Module Tests' -Tags 'FunctionalQuality' {
5151
foreach($item in $Data.FunctionsToExport){
5252
#$FunctionNameToTest = 'Get-PRETemplate' -replace 'PRE', $ModulePrefix
5353
$FunctionNameToTest = $item
54+
It "$($FunctionNameToTest) -WhatIf should not throw" -TestCases @{ FunctionNameToTest = $FunctionNameToTest; ModuleNameToTest = $ModuleNameToTest } {
55+
Mock -ModuleName $ModuleNameToTest $FunctionNameToTest { return @{'Name' = 'Angus Young'} }
56+
$ActualValue = '$FunctionNameToTest -Name "Angus Young" -WhatIf'
57+
{ $ActualValue } | should -Not -Throw
58+
}
5459
It "$($FunctionNameToTest) should not throw" -TestCases @{ FunctionNameToTest = $FunctionNameToTest; ModuleNameToTest = $ModuleNameToTest } {
5560
Mock -ModuleName $ModuleNameToTest $FunctionNameToTest { return @{'Name' = 'Angus Young'} }
5661
$ActualValue = '$FunctionNameToTest -Name "Angus Young"'

0 commit comments

Comments
 (0)