diff --git a/.github/copilot-pull-request-description-instructions.md b/.github/copilot-pull-request-description-instructions.md index b871512..ecbc496 100644 --- a/.github/copilot-pull-request-description-instructions.md +++ b/.github/copilot-pull-request-description-instructions.md @@ -35,7 +35,7 @@ References: - https://seesparkbox.com/foundry/semantic_commit_messages - http://karma-runner.github.io/1.0/dev/git-commit-msg.html -## Pull Reques description +## Pull Request description -- Add a summery of the intention of the PR. Use the title and the messages of the commits to create a summary. -- Add a list with all the commit messages in the PR. +- Add a summary of the intention of the PR. Use the title and the messages of the commits to create a summary. +- Add a bullet list with all the commit messages in the PR commits. diff --git a/Test/include/run_BeforeAfter.ps1 b/Test/include/run_BeforeAfter.ps1 index 180271a..2ddb3d1 100644 --- a/Test/include/run_BeforeAfter.ps1 +++ b/Test/include/run_BeforeAfter.ps1 @@ -16,9 +16,11 @@ # Write-Verbose "Run_AfterAll" # } -# function Run_BeforeEach{ -# Write-Verbose "Run_BeforeEach" -# } +function Run_BeforeEach{ + # Write-Verbose "Run_BeforeEach" + Disable-IncludeHelperDebug + +} # function Run_AfterEach{ # Write-Verbose "Run_AfterEach" diff --git a/Test/public/MyWrite.test.ps1 b/Test/public/MyWrite.test.ps1 index 0f8f2c0..1125535 100644 --- a/Test/public/MyWrite.test.ps1 +++ b/Test/public/MyWrite.test.ps1 @@ -22,6 +22,248 @@ function Test_WriteMyHost_Multiline { } $result = Stop-MyTranscript + Assert-Count -Expected 2 -Presented $result Assert-AreEqual -Expected "This is a test transcript 0" -Presented $result[0] Assert-AreEqual -Expected "This is a test transcript 1" -Presented $result[1] +} + +function Test_EnableMyDebug_Set{ + # Arrange + Disable-IncludeHelperDebug + # Act + Enable-IncludeHelperDebug + # Assert + Assert-DebugEnv "all" "" + + # Arrange + Disable-IncludeHelperDebug + # Act + Enable-IncludeHelperDebug -Sections "section0","section1" + # Assert + Assert-DebugEnv "section0 section1" "" + + # Arrange + Disable-IncludeHelperDebug + # Act + Enable-IncludeHelperDebug -Sections "-section0-tofilter" + # Assert + Assert-DebugEnv "-section0-tofilter" "" + + # Arrange + Disable-IncludeHelperDebug + # Act + Enable-IncludeHelperDebug -Sections "-section0-tofilter","-section1-tofilter" + # Assert + Assert-DebugEnv "-section0-tofilter -section1-tofilter" "" + + # Arrange + Disable-IncludeHelperDebug + $logfilename = "testlog.log" + # Act + Enable-IncludeHelperDebug -LoggingFilePath $logfilename + # Assert + Assert-DebugEnv "" "" + + # Arrange + Disable-IncludeHelperDebug + # Act + New-TestingFile -Name $logfilename + Enable-IncludeHelperDebug -LoggingFilePath $logfilename + # Assert + Assert-DebugEnv "all" $logfilename + +} + +function Test_EnableMyDebug_All{ + + Enable-IncludeHelperDebug + + $result = [System.Environment]::GetEnvironmentVariable("IncludeHelper_DEBUG") + Assert-AreEqual -Expected "all" -Presented $result + + $text0 = "Debug message 0" + $text1 = "Debug message 1" + + Start-MyTranscript + + Invoke-PrivateContext { + param($Arguments) + + Write-MyDebug -Message $Arguments[0] + Write-MyDebug -Message $Arguments[1] + + } -Arguments $text0,$text1 + + $result = Stop-MyTranscript + + Assert-Count -Expected 2 -Presented $result + Assert-DbgMsg $result[0] "none" $text0 + Assert-DbgMsg $result[1] "none" $text1 + +} + + + +function Test_EnableMyDebug_All_Sections{ + + Enable-IncludeHelperDebug + + $text0 = "Debug message 0" + $text1 = "Debug message 1" + + Start-MyTranscript + + Invoke-PrivateContext { + param($Arguments) + + Write-MyDebug -Message $Arguments[0] -Section "section0" + Write-MyDebug -Message $Arguments[1] -Section "section1" + + } -Arguments $text0,$text1 + + $result = Stop-MyTranscript + + Assert-Count -Expected 2 -Presented $result + Assert-DbgMsg $result[0] "section0" $text0 + Assert-DbgMsg $result[1] "section1" $text1 +} + +function Test_EnableMyDebug_Sections{ + + Enable-IncludeHelperDebug -Sections "section0","section2" + + $result = [System.Environment]::GetEnvironmentVariable("IncludeHelper_DEBUG") + Assert-AreEqual -Expected "section0 section2" -Presented $result + + $text0 = "Debug message 0" + $text1 = "Debug message 1" + $text2 = "Debug message 2" + + Start-MyTranscript + + Invoke-PrivateContext { + param($Arguments) + + Write-MyDebug -Message $Arguments[0] -Section "section0" + Write-MyDebug -Message $Arguments[1] -Section "section1" + Write-MyDebug -Message $Arguments[2] -Section "section2" + + } -Arguments $text0,$text1,$text2 + + $result = Stop-MyTranscript + + Assert-Count -Expected 2 -Presented $result + Assert-DbgMsg $result[0] "section0" $text0 + Assert-DbgMsg $result[1] "section2" $text2 +} + +function Test_EnableMyDebug_All_Filter{ + + Enable-IncludeHelperDebug -Sections "-section1-tofilter" + + $result = [System.Environment]::GetEnvironmentVariable("IncludeHelper_DEBUG") + Assert-AreEqual -Expected "-section1-tofilter" -Presented $result + + $text0 = "Debug message 0" + $text1 = "Debug message 1" + $text2 = "Debug message 2" + + Start-MyTranscript + + Invoke-PrivateContext { + param($Arguments) + + Write-MyDebug -Message $Arguments[0] -Section "section0" + Write-MyDebug -Message $Arguments[1] -Section "section1-tofilter" + Write-MyDebug -Message $Arguments[2] -Section "section2" + + } -Arguments $text0,$text1,$text2 + + $result = Stop-MyTranscript + + Assert-Count -Expected 2 -Presented $result + Assert-DbgMsg $result[0] "section0" $text0 + Assert-DbgMsg $result[1] "section2" $text2 +} + +function Test_EnableMyDebug_All_Filter_morethanone{ + + Enable-IncludeHelperDebug -Sections "-section1-tofilter -section2ToFilter" + + $result = [System.Environment]::GetEnvironmentVariable("IncludeHelper_DEBUG") + Assert-AreEqual -Expected "-section1-tofilter -section2ToFilter" -Presented $result + + $text0 = "Debug message 0" + $text1 = "Debug message 1" + $text2 = "Debug message 2" + $text3 = "Debug message 3" + + Start-MyTranscript + + Invoke-PrivateContext { + param($Arguments) + + Write-MyDebug -Message $Arguments[0] -Section "section0" + Write-MyDebug -Message $Arguments[1] -Section "section1-tofilter" + Write-MyDebug -Message $Arguments[2] -Section "section2ToFilter" + Write-MyDebug -Message $Arguments[3] -Section "section3" + + } -Arguments $text0,$text1,$text2 ,$text3 + + $result = Stop-MyTranscript + + Assert-Count -Expected 2 -Presented $result + Assert-DbgMsg $result[0] "section0" $text0 + Assert-DbgMsg $result[1] "section3" $text3 +} + +function Test_EnableMyDebug_All_LoggingFilePath{ + + $logfilename = "testlog.log" + touch $logfilename + + Enable-IncludeHelperDebug -LoggingFilePath $logfilename + + $text0 = "Debug message 0" + $text1 = "Debug message 1" + $text2 = "Debug message 2" + $text3 = "Debug message 3" + + Start-MyTranscript + + Invoke-PrivateContext { + param($Arguments) + + Write-MyDebug -Message $Arguments[0] -Section "section0" + Write-MyDebug -Message $Arguments[1] -Section "section1-tofilter" + Write-MyDebug -Message $Arguments[2] -Section "section2ToFilter" + Write-MyDebug -Message $Arguments[3] -Section "section3" + + } -Arguments $text0,$text1,$text2 ,$text3 + + $result = Stop-MyTranscript + + Assert-Count -Expected 4 -Presented $result + + $result = Get-Content $logfilename + Assert-Count -Expected 4 -Presented $result + Assert-DbgMsg $result[0] "section0" $text0 + Assert-DbgMsg $result[1] "section1-tofilter" $text1 + Assert-DbgMsg $result[2] "section2ToFilter" $text2 + Assert-DbgMsg $result[3] "section3" $text3 +} + +function Assert-DbgMsg($Presented,$Section,$Message){ + + Assert-IsTrue -Condition ($Presented -match "^\[\d{2}:\d{2}:\d{2}\.\d{3}\]\[D\]\[$Section\] $Message$") +} + +function Assert-DebugEnv($SectionString,$LoggingFile){ + $result = [System.Environment]::GetEnvironmentVariable("IncludeHelper_DEBUG") + $result = $result ?? "" + Assert-AreEqual -Expected $SectionString -Presented $result + + $result = [System.Environment]::GetEnvironmentVariable("IncludeHelper_DEBUG_LOGGING_FILEPATH") + $result = $result ?? "" + Assert-AreEqual -Expected $LoggingFile -Presented $result } \ No newline at end of file diff --git a/include/MyWrite.ps1 b/include/MyWrite.ps1 index 5226eab..442dc46 100644 --- a/include/MyWrite.ps1 +++ b/include/MyWrite.ps1 @@ -61,7 +61,7 @@ function Write-MyDebug { [CmdletBinding()] [Alias("Write-Debug")] param( - [Parameter(Position = 0)][string]$section, + [Parameter(Position = 0)][string]$section = "none", [Parameter(Position = 1, ValueFromPipeline)][string]$Message, [Parameter(Position = 2)][object]$Object ) @@ -103,14 +103,13 @@ function Write-MyDebugLogging { # Check if file exists # This should always exist as logging checks for parent path to be enabled # It may happen if since enable to execution the parent folder aka loggingFilePath is deleted. - if(-not (Test-Path -Path $loggingFilePath) ){ + if(-not (Test-Path -Path $loggingFilePath -PathType Leaf) ){ Write-Warning "Debug logging file path not accesible : '$loggingFilePath'" return $false } # Write to log file - $logFilePath = Join-Path -Path $loggingFilePath -ChildPath "$($MODULE_NAME)_debug.log" - Add-Content -Path $logFilePath -Value $LogMessage + Add-Content -Path $loggingFilePath -Value $LogMessage } } @@ -178,31 +177,50 @@ function Test-MyDebug { [Parameter()][switch]$Logging ) - # Get the module debug environment variable + function testSection($section,$flags){ + if($flags.Count -eq 0){ + return $false + } + $flags = $flags.ToLower() + $section = $section.ToLower() + + return ($flags.Contains("all")) -or ( $flags.Contains($section)) + } + $moduleDebugVarName = $MODULE_NAME + "_DEBUG" - $flag = [System.Environment]::GetEnvironmentVariable($moduleDebugVarName) + $flagsString = [System.Environment]::GetEnvironmentVariable($moduleDebugVarName) - # check if debugging is enabled - if ([string]::IsNullOrWhiteSpace( $flag )) { + # No configuration means no debug + if([string]::IsNullOrWhiteSpace( $flagsString )) { return $false } - $flag = $flag.ToLower() - $section = $section.ToLower() + # Get flags from flagdsString + $flags = getFlagsFromSectionsString $flagsString + + # Add all if allow is empty. + # This mean stat flagsString only contains filters. + $flags.allow = $flags.allow.Count -eq 0 ? @("all") : $flags.allow + + # Get the module debug environment variable + $isAllow = testSection -Section:$section -Flags:$flags.allow + $isFiltered = testSection -Section:$section -Flags:$flags.filter + + $trace = $isAllow -and -not $isFiltered - $trace = ($flag -like '*all*') -or ( $section -like "*$flag*") return $trace } + function Enable-ModuleNameDebug{ param( - [Parameter(Position = 0)][string]$section, + [Parameter(Position = 0)][string[]]$Sections, [Parameter()][string]$LoggingFilePath ) # Check if logging file path is provided if( -Not ( [string]::IsNullOrWhiteSpace( $LoggingFilePath )) ) { - if(Test-Path -Path $LoggingFilePath){ + if(Test-Path -Path $LoggingFilePath -PathType Leaf){ $moduleDEbugLoggingVarName = $MODULE_NAME + "_DEBUG_LOGGING_FILEPATH" [System.Environment]::SetEnvironmentVariable($moduleDEbugLoggingVarName, $LoggingFilePath) } else { @@ -211,20 +229,41 @@ function Enable-ModuleNameDebug{ } } + $flagsString = $sections -join " " + # Check section value - if( [string]::IsNullOrWhiteSpace( $section )) { - $flag = "all" - } else { - $flag = $section + if( [string]::IsNullOrWhiteSpace( $flagsString )) { + $flagsString = "all" } $moduleDebugVarName = $MODULE_NAME + "_DEBUG" - [System.Environment]::SetEnvironmentVariable($moduleDebugVarName, $flag) + [System.Environment]::SetEnvironmentVariable($moduleDebugVarName, $flagsString) } Copy-Item -path Function:Enable-ModuleNameDebug -Destination Function:"Enable-$($MODULE_NAME)Debug" Export-ModuleMember -Function "Enable-$($MODULE_NAME)Debug" +function getFlagsFromSectionsString($sectionsString){ + $flags = @{ + allow = $null + filter = $null + } + + if([string]::IsNullOrWhiteSpace($sectionsString) ){ + $flags.allow = @("all") + return $flags + } + + $list = $sectionsString.Split(" ", [StringSplitOptions]::RemoveEmptyEntries) + + $split = @($list).Where({ $_ -like '-*' }, 'Split') + + $flags.filter = $split[0] | ForEach-Object { $_ -replace '^-', '' } # -> API, Auth + $flags.allow = $split[1] # -> Sync, Cache + + return $flags +} + function Disable-ModuleNameDebug { param()