diff --git a/Test/include/parameter.test.helper.ps1 b/Test/include/parameter.test.helper.ps1 index c27cd99..4837c40 100644 --- a/Test/include/parameter.test.helper.ps1 +++ b/Test/include/parameter.test.helper.ps1 @@ -4,23 +4,27 @@ # # sample usage: # -# function Get-SomeCommand { -# [CmdletBinding()] -# param() -# -# Write-Verbose "this is a verbose message" -# } -# -# $result = Get-SomeCommand @ VerboseParameters -# Assert-Contains -Expected "this is a verbose message" -Presented $verboseVar +# function Get-DummyFunction{ +# [CmdletBinding()] +# param() -# Verbose parameters will now work.to capture the verbose outptut pipe 4>&1 and capture the output -# [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments','',Scope='function')] -# $VerboseParameters =@{ -# VerboseAction = 'SilentlyContinue' -# VerboseVariable = 'verboseVar' +# Write-Error "Error Message from dummyFunction" +# Write-Verbose "Verbose Message from dummyFunction" +# Write-Warning "Warning Message from dummyFunction" +# Write-Information "Information Message from dummyFunction" +# Write-host "Host Message from dummyFunction" + +# return $true # } +# $result = Get-DummyFunction @ErrorParameters @WarningParameters @InfoParameters + +# Assert-IsTrue -Condition $result +# Assert-Contains -Expected "Error Message from dummyFunction" -Presented $errorVar[0].exception.Message +# Assert-Contains -Expected "Warning Message from dummyFunction" -Presented $warningVar +# Assert-Contains -Expected "Information Message from dummyFunction" -Presented $infoVar +# Assert-Contains -Expected "Host Message from dummyFunction" -Presented $infoVar + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments','',Scope='function')] $WarningParameters = @{ WarningAction = 'SilentlyContinue' diff --git a/Test/include/transcriptHelp.ps1 b/Test/include/transcriptHelp.ps1 index 22a62a6..4d36a40 100644 --- a/Test/include/transcriptHelp.ps1 +++ b/Test/include/transcriptHelp.ps1 @@ -17,8 +17,30 @@ function Start-MyTranscript { } function Stop-MyTranscript { - Stop-Transcript + + $null = Stop-Transcript + $transcriptContent = Get-Content -Path $TEST_TRANSCRIPT_FILE Remove-Item -Path $TEST_TRANSCRIPT_FILE - return $transcriptContent + + $ret = Export-MyTranscript -transcriptContent $transcriptContent + + return $ret +} + +function Export-MyTranscript { + [CmdletBinding()] + param ( + [Parameter(Mandatory, Position = 0)] + [string[]]$transcriptContent + ) + + $i = 0..($transcriptContent.Count - 1) | Where-Object { $transcriptContent[$_] -eq "**********************" } + + $firstLine = $i[1] + 1 + $lastLine = $i[2] - 1 + + $retlist = $transcriptContent[$firstLine..$lastLine] + + return $retlist } diff --git a/Test/public/MyWrite.test.ps1 b/Test/public/MyWrite.test.ps1 new file mode 100644 index 0000000..5794b18 --- /dev/null +++ b/Test/public/MyWrite.test.ps1 @@ -0,0 +1,27 @@ +function Test_WriteMyHost_Singleline { + + Start-MyTranscript + + Invoke-PrivateContext { + Write-MyHost -Message "This is a test transcript." + } + + $result = Stop-MyTranscript + + Assert-AreEqual -Expected "This is a test transcript." -Presented $result + +} + +function Test_WriteMyHost_Multiline { + + Start-MyTranscript + + Invoke-PrivateContext { + Write-MyHost -Message "This is a test transcript 0" + Write-MyHost -Message "This is a test transcript 1" + } + $result = Stop-MyTranscript + + Assert-AreEqual -Expected "This is a test transcript 0" -Presented $result[0] + Assert-AreEqual -Expected "This is a test transcript 1" -Presented $result[1] +} \ No newline at end of file diff --git a/Test/public/dependencies.test.ps1 b/Test/public/dependencies.test.ps1 index 41e08f8..4a50ed0 100644 --- a/Test/public/dependencies.test.ps1 +++ b/Test/public/dependencies.test.ps1 @@ -13,14 +13,15 @@ function Test_ImportDepepency_Already_loaded{ Mock_ImportModule -Name $name -Folder $modulesFolder #Act - $result = Import-Dependency -Name $name -Verbose -Confirm:$false 4>&1 # pipe verbose stream to standard output + Set-IncludeHelperVerbose + Start-MyTranscript + $result = Import-Dependency -Name $name -Verbose -Confirm:$false + $tt = Stop-MyTranscript + Clear-IncludeHelperVerbose #Assert verbose message - Assert-IsNotNull -Object $result - Assert-Contains -Presented $result -Expected "Module [$Name] imported from own module" - # Assert module output - $module = $result | where-object {$_.GetType().Name -eq "PSCustomObject"} - Assert-AreEqual -Expected $name -Presented $module.Name + Assert-AreEqual -Expected $name -Presented $result.Name + Assert-Contains -Presented $tt -Expected "Module [$Name] imported from own module" } function Test_ImportDepepency_SideBySide{ @@ -41,14 +42,17 @@ function Test_ImportDepepency_SideBySide{ Mock_ImportModule -Name $name -Folder $modulesFolder # Act - $result = Import-Dependency -Name $name -Verbose -Confirm:$false 4>&1 # pipe verbose stream to standard output + Set-IncludeHelperVerbose + Start-MyTranscript + $result = Import-Dependency -Name $name -Verbose -Confirm:$false + $tt = Stop-MyTranscript + Clear-IncludeHelperVerbose - #Assert verbose message - Assert-IsNotNull -Object $result - Assert-Contains -Presented $result -Expected "Module [$Name] imported from side by side path" # Assert module output - $module = $result | where-object {$_.GetType().Name -eq "PSCustomObject"} - Assert-AreEqual -Expected $name -Presented $module.Name + Assert-AreEqual -Expected $name -Presented $result.Name + #Assert verbose message + Assert-AreEqual -Presented $tt -Expected "Module [$Name] imported from side by side path" + } function Test_ImportDepepency_Import_From_Module_Manager{ @@ -73,13 +77,16 @@ function Test_ImportDepepency_Import_From_Module_Manager{ #Act - $result = Import-Dependency -Name $name -Verbose -Confirm:$false 4>&1 # pipe verbose stream to standard output + Set-IncludeHelperVerbose + Start-MyTranscript + $output = Import-Dependency -Name $name -Verbose -Confirm:$false + $result = Stop-MyTranscript + Clear-IncludeHelperVerbose #Assert verbose message + Assert-AreEqual -Expected $name -Presented $output.Name Assert-Contains -Presented $result -Expected "Module [$Name] imported from Powershell Module Manager" - # Assert module output - $module = $result | where-object {$_.GetType().Name -eq "PSCustomObject"} - Assert-AreEqual -Expected $name -Presented $module.Name + } function Test_ImportDepepency_Install_From_Gallery{ @@ -106,15 +113,20 @@ function Test_ImportDepepency_Install_From_Gallery{ return $null '@ $expression = $expression -replace "{alias}", "Get-Module -Name $name -ListAvailable" - $expression = $expression -replace "{command}", 'New-Object -TypeName PSCustomObject -Property @{ Name = "$name" }' + $expression = $expression -replace "{command}", 'New-Object -TypeName PSCustomObject -Property @{ Name = "{name}" }' + $expression = $expression -replace "{name}", $name $expression = $expression -replace "{tag}", $MODULE_INVOKATION_TAG_MOCK MockCallExpression -Command "Install-Module -Name $name -AllowPrerelease -Force" -Expression $expression #Act - $result = Import-Dependency -Name $name -Verbose -Confirm:$false 4>&1 # pipe verbose stream to standard output + Set-IncludeHelperVerbose + Start-MyTranscript + $output = Import-Dependency -Name $name -Verbose -Confirm:$false + $result = Stop-MyTranscript + Clear-IncludeHelperVerbose #Assert verbose message - Assert-IsNotNull -Object $result + Assert-AreEqual -Expected $name -Presented $output.Name Assert-Contains -Presented $result -Expected "Module [$Name] installed from PowerShell Gallery" } @@ -142,10 +154,15 @@ function Test_ImportDependency_Clone_From_GitHub{ Mock_ImportModule -Name $name -Folder $modulesFolder - $result = Import-Dependency -Name $name -Verbose -Confirm:$false 4>&1 # pipe verbose stream to standard output + Set-IncludeHelperVerbose + Start-MyTranscript + $output = Import-Dependency -Name $name -Verbose -Confirm:$false + $result = Stop-MyTranscript + Clear-IncludeHelperVerbose #Assert verbose message - Assert-IsNotNull -Object $result + Assert-AreEqual -Expected $name -Presented $output.Name + Assert-Contains -Presented $result -Expected "Module [$Name] cloned from GitHub repository" Assert-Contains -Presented $result -Expected "Module [$Name] imported from GitHub repository" } diff --git a/Test/public/parameter.test.helper.test.ps1 b/Test/public/parameter.test.helper.test.ps1 index be1933f..033f488 100644 --- a/Test/public/parameter.test.helper.test.ps1 +++ b/Test/public/parameter.test.helper.test.ps1 @@ -1,38 +1,57 @@ + function Test_parameterstest{ - $result = Get-DummyFunction @WarningParameters @InfoParameters @ErrorParameters - + Set-IncludeHelperVerbose + Start-MyTranscript + $result = Get-DummyFunction @ErrorParameters @WarningParameters @InfoParameters + $tt = Stop-MyTranscript + Clear-IncludeHelperVerbose + + # Assert result Assert-IsTrue -Condition $result + Assert-Contains -Expected "Error Message from dummyFunction" -Presented $errorVar[0].exception.Message Assert-Contains -Expected "Warning Message from dummyFunction" -Presented $warningVar Assert-Contains -Expected "Information Message from dummyFunction" -Presented $infoVar - Assert-Contains -Expected "Error Message from dummyFunction" -Presented $errorVar + Assert-Contains -Expected "Information Message from dummyFunction" -Presented $infoVar + + # Not displaied + Assert-NotContains -Presented $tt -Expected "Error Message from dummyFunction" + Assert-NotContains -Presented $tt -Expected "Warning Message from dummyFunction" + Assert-NotContains -Presented $tt -Expected "Information Message from dummyFunction" + + # Host message + Assert-Contains -Presented $tt -Expected "Host Message from dummyFunction" } -function Test_parameterstest_Error{ +function Test_parameterstest_Verbose{ - # $ErrorActionPreference = 'SilentlyContinue' - $result = Get-DummyFunction @ErrorParameters @WarningParameters @InfoParameters - + Set-IncludeHelperVerbose + Start-MyTranscript + $result = Get-DummyFunction @ErrorParameters @WarningParameters @InfoParameters -Verbose + $tt = Stop-MyTranscript + Clear-IncludeHelperVerbose + + # Assert result Assert-IsTrue -Condition $result + Assert-Contains -Expected "Error Message from dummyFunction" -Presented $errorVar[0].exception.Message Assert-Contains -Expected "Warning Message from dummyFunction" -Presented $warningVar Assert-Contains -Expected "Information Message from dummyFunction" -Presented $infoVar -} + Assert-Contains -Expected "Information Message from dummyFunction" -Presented $infoVar -function Test_parameterstest_Verbose{ + # Not displaied + Assert-NotContains -Presented $tt -Expected "Error Message from dummyFunction" + Assert-NotContains -Presented $tt -Expected "Warning Message from dummyFunction" + Assert-NotContains -Presented $tt -Expected "Information Message from dummyFunction" - $result = Get-DummyFunction -Verbose 4>&1 @ErrorParameters @WarningParameters @InfoParameters - Assert-Contains -Presented $result -Expected "True" - Assert-Contains -Presented $result -Expected "Verbose Message from dummyFunction" + # Host message + Assert-Contains -Presented $tt -Expected "VERBOSE: Verbose Message from dummyFunction" + Assert-Contains -Presented $tt -Expected "Host Message from dummyFunction" } function Get-DummyFunction{ [CmdletBinding()] param() - Assert-AreEqual -Expected "SilentlyContinue" -Presented $ErrorActionPreference - Assert-AreEqual -Expected "SilentlyContinue" -Presented $WarningPreference - Assert-AreEqual -Expected "SilentlyContinue" -Presented $InformationPreference - Write-Error "Error Message from dummyFunction" Write-Verbose "Verbose Message from dummyFunction" Write-Warning "Warning Message from dummyFunction" diff --git a/Test/public/transcriptHelp.test.ps1 b/Test/public/transcriptHelp.test.ps1 index 813c6c7..6898dfe 100644 --- a/Test/public/transcriptHelp.test.ps1 +++ b/Test/public/transcriptHelp.test.ps1 @@ -17,4 +17,26 @@ function Test_transcript{ Assert-Contains -Expected "This is a test transcript." -Presented $result Assert-Contains -Expected "WARNING: This is a warning message." -Presented $result # Assert-Contains -Expected " | This is an error message." -Presented $result +} + +function Test_ExportTranscriptLines{ + + # Act + Start-MyTranscript + + Write-Host "Line 1" + Write-Host "Line 2" + Write-Host "Line 3" + + $result = Stop-MyTranscript + + # Assert + $expectedLines = @( + "Line 1", + "Line 2", + "Line 3" + ) + for($i=0; $i -lt $expectedLines.Count; $i++){ + Assert-AreEqual -Expected $expectedLines[$i] -Presented $result[$i] + } } \ No newline at end of file diff --git a/helper/invokeCommand.helper.ps1 b/helper/invokeCommand.helper.ps1 index bfe0526..34b95ed 100644 --- a/helper/invokeCommand.helper.ps1 +++ b/helper/invokeCommand.helper.ps1 @@ -16,16 +16,24 @@ function Set-MyInvokeCommandAlias{ [Parameter(Mandatory,Position=1)][string]$Command ) - # throw if MODULE_INVOKATION_TAG is not set or is "MyModuleModule" - if (-not $MODULE_INVOKATION_TAG) { - throw "MODULE_INVOKATION_TAG is not set. Please set it to a unique value before calling Set-MyInvokeCommandAlias." - } - if ($PSCmdlet.ShouldProcess("InvokeCommandAliasList", ("Add Command Alias [{0}] = [{1}]" -f $Alias, $Command))) { InvokeHelper\Set-InvokeCommandAlias -Alias $Alias -Command $Command -Tag $MODULE_INVOKATION_TAG } } +function Invoke-MyCommand{ + [CmdletBinding()] + param( + [Parameter(Mandatory,ValueFromPipeline,Position=0)][string]$Command, + [Parameter(Position=1)][hashtable]$Parameters + ) + + Write-MyDebug "[invoke] $Command" $Parameters + + return InvokeHelper\Invoke-MyCommand -Command $Command -Parameters $Parameters +} + + function Reset-MyInvokeCommandAlias{ [CmdletBinding(SupportsShouldProcess)] param() diff --git a/include/MyWrite.ps1 b/include/MyWrite.ps1 index e5443bc..66a0155 100644 --- a/include/MyWrite.ps1 +++ b/include/MyWrite.ps1 @@ -1,46 +1,201 @@ +# Include MyWrite.ps1 +# Provides Write-MyError, Write-MyWarning, Write-MyVerbose, Write-MyHost, Write-MyDebug +# and Test-MyVerbose, Test-MyDebug functions for consistent logging and debugging output. +# Use env variables ModuleHelper_VERBOSE and ModuleHelper_DEBUG to control verbosity and debug output. +# Example: $env:ModuleHelper_DEBUG="all" or $env:ModuleHelper_DEBUG="Sync-Project" + +$ModuleRootPath = Get-ModuleRootPath -ModuleRootPath $ModuleRootPath +$MODULE_NAME = (Get-ChildItem -Path $ModuleRootPath -Filter *.psd1 | Select-Object -First 1).BaseName $ERROR_COLOR = "Red" $WARNING_COLOR = "Yellow" +$VERBOSE_COLOR = "DarkYellow" $OUTPUT_COLOR = "DarkCyan" +$DEBUG_COLOR = "DarkGray" -function Write-MyError{ +function Write-MyError { + [CmdletBinding()] + [Alias("Write-Error")] param( - [Parameter(Mandatory,ValueFromPipeline)][string]$Message + [Parameter(Mandatory, ValueFromPipeline)][string]$Message ) # Write-Host "Error: $message" -ForegroundColor $ERROR_COLOR Write-ToConsole "Error: $message" -Color $ERROR_COLOR } -function Write-MyWarning{ +function Write-MyWarning { + [CmdletBinding()] + [Alias("Write-Warning")] param( - [Parameter(Mandatory,ValueFromPipeline)][string]$Message + [Parameter(Mandatory, ValueFromPipeline)][string]$Message ) # Write-Host "Error: $message" -ForegroundColor $WARNING_COLOR Write-ToConsole $message -Color $WARNING_COLOR } -function Write-MyVerbose{ +function Write-MyVerbose { + [CmdletBinding()] + [Alias("Write-Verbose")] param( [Parameter(ValueFromPipeline)][string]$Message ) - Write-Verbose -Message $message + + if (Test-MyVerbose) { + Write-ToConsole $message -Color $VERBOSE_COLOR + } } -function Write-MyHost{ +function Write-MyHost { + [CmdletBinding()] + [Alias("Write-Host")] param( [Parameter(ValueFromPipeline)][string]$Message, + #NoNewLine [Parameter()][switch]$NoNewLine ) # Write-Host $message -ForegroundColor $OUTPUT_COLOR Write-ToConsole $message -Color $OUTPUT_COLOR -NoNewLine:$NoNewLine } -function Write-ToConsole{ +function Write-MyDebug { + [CmdletBinding()] + [Alias("Write-Debug")] + param( + [Parameter(Position = 0)][string]$section, + [Parameter(Position = 1, ValueFromPipeline)][string]$Message, + [Parameter(Position = 2)][object]$Object + ) + + process{ + + if (Test-MyDebug -section $section) { + + if ($Object) { + $objString = $Object | Get-ObjetString + $message = $message + " - " + $objString + } + $timestamp = Get-Date -Format 'HH:mm:ss.fff' + "[$timestamp][D][$section] $message" | Write-ToConsole -Color $DEBUG_COLOR + } + } +} + +function Write-ToConsole { param( [Parameter(ValueFromPipeline)][string]$Color, - [Parameter(ValueFromPipeline, Position=0)][string]$Message, + [Parameter(ValueFromPipeline, Position = 0)][string]$Message, [Parameter()][switch]$NoNewLine ) - Write-Host $message -ForegroundColor $Color -NoNewLine:$NoNewLine -} \ No newline at end of file + if([string]::IsNullOrWhiteSpace($Color)){ + Microsoft.PowerShell.Utility\Write-Host $message -NoNewLine:$NoNewLine + } else { + Microsoft.PowerShell.Utility\Write-Host $message -ForegroundColor:$Color -NoNewLine:$NoNewLine + } + +} + + +function Test-MyVerbose { + param( + [Parameter(Position = 0)][string]$section + ) + + $moduleDebugVarName = $MODULE_NAME + "_VERBOSE" + $flag = [System.Environment]::GetEnvironmentVariable($moduleDebugVarName) + + if ([string]::IsNullOrWhiteSpace( $flag )) { + return $false + } + + $trace = ($flag -like '*all*') -or ( $section -like "*$flag*") + return $trace +} + +function Set-ModuleNameVerbose{ + param( + [Parameter(Position = 0)][string]$section + ) + + if( [string]::IsNullOrWhiteSpace( $section )) { + $flag = "all" + } else { + $flag = $section + } + + $moduleDebugVarName = $MODULE_NAME + "_VERBOSE" + [System.Environment]::SetEnvironmentVariable($moduleDebugVarName, $flag) +} +Rename-Item -path Function:Set-ModuleNameVerbose -NewName "Set-$($MODULE_NAME)Verbose" +Export-ModuleMember -Function "Set-$($MODULE_NAME)Verbose" + +function Clear-ModuleNameVerbose{ + param() + + $moduleDebugVarName = $MODULE_NAME + "_VERBOSE" + [System.Environment]::SetEnvironmentVariable($moduleDebugVarName, $null) +} +Rename-Item -path Function:Clear-ModuleNameVerbose -NewName "Clear-$($MODULE_NAME)Verbose" +Export-ModuleMember -Function "Clear-$($MODULE_NAME)Verbose" + +function Test-MyDebug { + param( + [Parameter(Position = 0)][string]$section + ) + + $moduleDebugVarName = $MODULE_NAME + "_DEBUG" + $flag = [System.Environment]::GetEnvironmentVariable($moduleDebugVarName) + + # Enable debug + if ([string]::IsNullOrWhiteSpace( $flag )) { + return $false + } + + $trace = ($flag -like '*all*') -or ( $section -like "*$flag*") + return $trace +} + +function Set-ModuleNameDebug{ + param( + [Parameter(Position = 0)][string]$section + ) + + if( [string]::IsNullOrWhiteSpace( $section )) { + $flag = "all" + } else { + $flag = $section + } + + $moduleDebugVarName = $MODULE_NAME + "_DEBUG" + [System.Environment]::SetEnvironmentVariable($moduleDebugVarName, $flag) +} +Rename-Item -path Function:Set-ModuleNameDebug -NewName "Set-$($MODULE_NAME)Debug" +Export-ModuleMember -Function "Set-$($MODULE_NAME)Debug" + +function Clear-ModuleNameDebug{ + param() + + $moduleDebugVarName = $MODULE_NAME + "_DEBUG" + [System.Environment]::SetEnvironmentVariable($moduleDebugVarName, $null) +} +Rename-Item -path Function:Clear-ModuleNameDebug -NewName "Clear-$($MODULE_NAME)Debug" +Export-ModuleMember -Function "Clear-$($MODULE_NAME)Debug" + +function Get-ObjetString { + param( + [Parameter(ValueFromPipeline, Position = 0)][object]$Object + ) + + process{ + + if ($null -eq $Object) { + return "null" + } + + if ($Object -is [string]) { + return $Object + } + + return $Object | ConvertTo-Json -Depth 10 -ErrorAction SilentlyContinue + } +} diff --git a/include/dependencies.ps1 b/include/dependencies.ps1 index eb8edbe..cea9df4 100644 --- a/include/dependencies.ps1 +++ b/include/dependencies.ps1 @@ -103,7 +103,7 @@ function Import-Dependency{ if ($moduleAvailable) { # this does not return any module if ($PSCmdlet.ShouldProcess("Installing module $name", "Do you want to install [$Name] from PowerShell Gallery?")) { - Invoke-MyCommand -Command 'InstallModule' -Parameters @{ name = $Name } + $null = Invoke-MyCommand -Command 'InstallModule' -Parameters @{ name = $Name } #Check if the module is available now $module = Invoke-MyCommand -Command 'GetModuleListAvailable' -Parameters @{ name = $Name } if($module){ @@ -131,7 +131,7 @@ function Import-Dependency{ $local = Invoke-MyCommand -Command 'GetMyModuleRootPath' $modulePath = $local | split-path -Parent | Join-Path -ChildPath $Name - if ($PSCmdlet.ShouldProcess("Cloning module $name", "Do you want to cline [$url] to [$modulePath]?")) { + if ($PSCmdlet.ShouldProcess("Cloning module $name", "Do you want to clone [$url] to [$modulePath]?")) { $null = Invoke-MyCommand -Command 'CloneRepo' -Parameters @{ url = $url; folder = $modulePath } } else { "Module [$Name] not cloned. Skipped clone from GitHub repository" | Write-Warning