Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 18 additions & 12 deletions Test/public/MyWrite.test.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,30 @@
# Assert
Assert-DebugEnv "all" $logfilename

#arrange

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
Disable-IncludeHelperDebug
Enable-IncludeHelperDebug -Sections "section0","section1"
# Act
Enable-IncludeHelperDebug -AddSections "-section2","section3"
# Assert
Assert-DebugEnv "section0 section1 -section2 section3" ""
}

function Test_EnableMyDebug_GetDebug{

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
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"

Expand Down Expand Up @@ -132,9 +147,6 @@

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"
Expand All @@ -161,9 +173,6 @@

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"
Expand All @@ -190,9 +199,6 @@

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"
Expand Down
59 changes: 51 additions & 8 deletions include/MyWrite.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -211,33 +211,40 @@
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
}
}

$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"
}
}

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
# 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"
Expand Down Expand Up @@ -276,6 +283,18 @@
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 @{

Check notice

Code scanning / PSScriptAnalyzer

The cmdlet 'Get-ModuleNameDebug' returns an object of type 'System.Collections.Hashtable' but this type is not declared in the OutputType attribute. Note

The cmdlet 'Get-ModuleNameDebug' returns an object of type 'System.Collections.Hashtable' but this type is not declared in the OutputType attribute.
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
Expand All @@ -294,3 +313,27 @@
return $Object | ConvertTo-Json -Depth 10 -ErrorAction SilentlyContinue
}
}

function get-Sections(){

Check warning

Code scanning / PSScriptAnalyzer

The cmdlet 'get-Sections' uses a plural noun. A singular noun should be used instead. Warning

The cmdlet 'get-Sections' uses a plural noun. A singular noun should be used instead.
$moduleDebugVarName = $MODULE_NAME + "_DEBUG"
$sections = [System.Environment]::GetEnvironmentVariable($moduleDebugVarName)

return $sections
}

function set-Sections($sections){

Check warning

Code scanning / PSScriptAnalyzer

Function 'set-Sections' has verb that could change system state. Therefore, the function has to support 'ShouldProcess'. Warning

Function 'set-Sections' has verb that could change system state. Therefore, the function has to support 'ShouldProcess'.

Check warning

Code scanning / PSScriptAnalyzer

The cmdlet 'set-Sections' uses a plural noun. A singular noun should be used instead. Warning

The cmdlet 'set-Sections' uses a plural noun. A singular noun should be used instead.
$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){

Check warning

Code scanning / PSScriptAnalyzer

Function 'set-LogFile' has verb that could change system state. Therefore, the function has to support 'ShouldProcess'. Warning

Function 'set-LogFile' has verb that could change system state. Therefore, the function has to support 'ShouldProcess'.
$moduleDEbugLoggingVarName = $MODULE_NAME + "_DEBUG_LOGGING_FILEPATH"
[System.Environment]::SetEnvironmentVariable($moduleDEbugLoggingVarName, $logFilePath)
}
Loading