diff --git a/Test/public/MyWrite.test.ps1 b/Test/public/MyWrite.test.ps1 index 1125535..aafb61d 100644 --- a/Test/public/MyWrite.test.ps1 +++ b/Test/public/MyWrite.test.ps1 @@ -72,15 +72,30 @@ function Test_EnableMyDebug_Set{ # Assert Assert-DebugEnv "all" $logfilename + #arrange + Disable-IncludeHelperDebug + Enable-IncludeHelperDebug -Sections "section0","section1" + # Act + Enable-IncludeHelperDebug -AddSections "-section2","section3" + # Assert + Assert-DebugEnv "section0 section1 -section2 section3" "" +} + +function Test_EnableMyDebug_GetDebug{ + + touch kk.txt + Enable-IncludeHelperDebug -Sections "section0","section1" -LoggingFilePath "kk.txt" + + $result = Get-IncludeHelperDebug + + Assert-AreEqual -Expected "section0 section1" -Presented $result.Sections + Assert-AreEqual -Expected "kk.txt" -Presented $result.LoggingFilePath } 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" @@ -132,9 +147,6 @@ 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" @@ -161,9 +173,6 @@ 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" @@ -190,9 +199,6 @@ 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" diff --git a/include/MyWrite.ps1 b/include/MyWrite.ps1 index 442dc46..ca3269f 100644 --- a/include/MyWrite.ps1 +++ b/include/MyWrite.ps1 @@ -211,18 +211,17 @@ function Test-MyDebug { return $trace } - function Enable-ModuleNameDebug{ param( [Parameter(Position = 0)][string[]]$Sections, + [Parameter()][string[]]$AddSections, [Parameter()][string]$LoggingFilePath ) # Check if logging file path is provided if( -Not ( [string]::IsNullOrWhiteSpace( $LoggingFilePath )) ) { if(Test-Path -Path $LoggingFilePath -PathType Leaf){ - $moduleDEbugLoggingVarName = $MODULE_NAME + "_DEBUG_LOGGING_FILEPATH" - [System.Environment]::SetEnvironmentVariable($moduleDEbugLoggingVarName, $LoggingFilePath) + set-LogFile $LoggingFilePath } else { Write-Error "Logging file path '$LoggingFilePath' does not exist. Debug logging will not be enabled." return @@ -230,14 +229,22 @@ function Enable-ModuleNameDebug{ } $flagsString = $sections -join " " + $addedFlagsString = $AddSections -join " " - # Check section value - if( [string]::IsNullOrWhiteSpace( $flagsString )) { - $flagsString = "all" + # if no section get value from env and is still mepty set to all + if([string]::IsNullOrWhiteSpace( $flagsString )) { + $flagsString = get-Sections + if( [string]::IsNullOrWhiteSpace( $flagsString )) { + $flagsString = "all" + } + } + + # Add added to flagsString if provided + if(-Not [string]::IsNullOrWhiteSpace( $addedFlagsString )) { + $flagsString += " " + $addedFlagsString } - $moduleDebugVarName = $MODULE_NAME + "_DEBUG" - [System.Environment]::SetEnvironmentVariable($moduleDebugVarName, $flagsString) + set-Sections $flagsString } Copy-Item -path Function:Enable-ModuleNameDebug -Destination Function:"Enable-$($MODULE_NAME)Debug" @@ -276,6 +283,18 @@ function Disable-ModuleNameDebug { Copy-Item -path Function:Disable-ModuleNameDebug -Destination Function:"Disable-$($MODULE_NAME)Debug" Export-ModuleMember -Function "Disable-$($MODULE_NAME)Debug" +function Get-ModuleNameDebug { + [cmdletbinding()] + param() + + return @{ + Sections = get-Sections + LoggingFilePath = get-LogFile + } +} +Copy-Item -path Function:Get-ModuleNameDebug -Destination Function:"Get-$($MODULE_NAME)Debug" +Export-ModuleMember -Function "Get-$($MODULE_NAME)Debug" + function Get-ObjetString { param( [Parameter(ValueFromPipeline, Position = 0)][object]$Object @@ -294,3 +313,27 @@ function Get-ObjetString { return $Object | ConvertTo-Json -Depth 10 -ErrorAction SilentlyContinue } } + +function get-Sections(){ + $moduleDebugVarName = $MODULE_NAME + "_DEBUG" + $sections = [System.Environment]::GetEnvironmentVariable($moduleDebugVarName) + + return $sections +} + +function set-Sections($sections){ + $moduleDebugVarName = $MODULE_NAME + "_DEBUG" + [System.Environment]::SetEnvironmentVariable($moduleDebugVarName, $sections) +} + +function get-LogFile(){ + $moduleDEbugLoggingVarName = $MODULE_NAME + "_DEBUG_LOGGING_FILEPATH" + $logfile = [System.Environment]::GetEnvironmentVariable($moduleDEbugLoggingVarName) + + return $logfile +} + +function set-LogFile($logFilePath){ + $moduleDEbugLoggingVarName = $MODULE_NAME + "_DEBUG_LOGGING_FILEPATH" + [System.Environment]::SetEnvironmentVariable($moduleDEbugLoggingVarName, $logFilePath) +}