Tests/Unit/Start-ScriptLogger.Tests.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
$modulePath = Resolve-Path -Path "$PSScriptRoot\..\..\.." | Select-Object -ExpandProperty Path $moduleName = Resolve-Path -Path "$PSScriptRoot\..\.." | Get-Item | Select-Object -ExpandProperty BaseName Remove-Module -Name $moduleName -Force -ErrorAction SilentlyContinue Import-Module -Name "$modulePath\$moduleName" -Force Describe 'Start-ScriptLogger' { $defaultEnabled = $true $defaultPath = "$PSScriptRoot\Start-ScriptLogger.Tests.ps1.log" $defaultFormat = '{0:yyyy-MM-dd} {0:HH:mm:ss} {1} {2} {3,-11} {4}' $defaultLevel = 'Verbose' $defaultEncoding = 'UTF8' $defaultLogFile = $true $defaultEventLog = $true $defaultConsole = $true It 'should return default values without any specification' { # Act $scriptLogger = Start-ScriptLogger -PassThru # Assert $scriptLogger | Should -Not -BeNullOrEmpty $scriptLogger.Enabled | Should -Be $defaultEnabled $scriptLogger.Path | Should -Be $defaultPath $scriptLogger.Format | Should -Be $defaultFormat $scriptLogger.Level | Should -Be $defaultLevel $scriptLogger.Encoding | Should -Be $defaultEncoding $scriptLogger.LogFile | Should -Be $defaultLogFile $scriptLogger.EventLog | Should -Be $defaultEventLog $scriptLogger.ConsoleOutput | Should -Be $defaultConsole } It 'should return a valid value for the parameter path' { # Arrange $expectedPath = 'TestDrive:\test.log' # Act $scriptLogger = Start-ScriptLogger -Path $expectedPath -PassThru # Assert $scriptLogger | Should -Not -BeNullOrEmpty $scriptLogger.Enabled | Should -Be $defaultEnabled $scriptLogger.Path | Should -Be $expectedPath $scriptLogger.Format | Should -Be $defaultFormat $scriptLogger.Level | Should -Be $defaultLevel $scriptLogger.Encoding | Should -Be $defaultEncoding $scriptLogger.LogFile | Should -Be $defaultLogFile $scriptLogger.EventLog | Should -Be $defaultEventLog $scriptLogger.ConsoleOutput | Should -Be $defaultConsole } It 'should return a valid value for the parameter format' { $expectedFormat = '{4} {3} {2} {1} {0}' # Act $scriptLogger = Start-ScriptLogger -Format $expectedFormat -PassThru # Assert $scriptLogger | Should -Not -BeNullOrEmpty $scriptLogger.Enabled | Should -Be $defaultEnabled $scriptLogger.Path | Should -Be $defaultPath $scriptLogger.Format | Should -Be $expectedFormat $scriptLogger.Level | Should -Be $defaultLevel $scriptLogger.Encoding | Should -Be $defaultEncoding $scriptLogger.LogFile | Should -Be $defaultLogFile $scriptLogger.EventLog | Should -Be $defaultEventLog $scriptLogger.ConsoleOutput | Should -Be $defaultConsole } It 'should return a valid value for the parameter log level' { $expectedLevel = 'Error' # Act $scriptLogger = Start-ScriptLogger -Level $expectedLevel -PassThru # Assert $scriptLogger | Should -Not -BeNullOrEmpty $scriptLogger.Enabled | Should -Be $defaultEnabled $scriptLogger.Path | Should -Be $defaultPath $scriptLogger.Format | Should -Be $defaultFormat $scriptLogger.Level | Should -Be $expectedLevel $scriptLogger.Encoding | Should -Be $defaultEncoding $scriptLogger.LogFile | Should -Be $defaultLogFile $scriptLogger.EventLog | Should -Be $defaultEventLog $scriptLogger.ConsoleOutput | Should -Be $defaultConsole } It 'should return a valid value for the parameter encoding' { $expectedEncoding = 'UTF8' # Act $scriptLogger = Start-ScriptLogger -Encoding $expectedEncoding -PassThru # Assert $scriptLogger | Should -Not -BeNullOrEmpty $scriptLogger.Enabled | Should -Be $defaultEnabled $scriptLogger.Path | Should -Be $defaultPath $scriptLogger.Format | Should -Be $defaultFormat $scriptLogger.Level | Should -Be $defaultLevel $scriptLogger.Encoding | Should -Be $expectedEncoding $scriptLogger.LogFile | Should -Be $defaultLogFile $scriptLogger.EventLog | Should -Be $defaultEventLog $scriptLogger.ConsoleOutput | Should -Be $defaultConsole } It 'should return a valid value for the parameter no log file' { # Act $scriptLogger = Start-ScriptLogger -NoLogFile -PassThru # Assert $scriptLogger | Should -Not -BeNullOrEmpty $scriptLogger.Enabled | Should -Be $defaultEnabled $scriptLogger.Path | Should -Be $defaultPath $scriptLogger.Format | Should -Be $defaultFormat $scriptLogger.Level | Should -Be $defaultLevel $scriptLogger.Encoding | Should -Be $defaultEncoding $scriptLogger.LogFile | Should -Be $false $scriptLogger.EventLog | Should -Be $defaultEventLog $scriptLogger.ConsoleOutput | Should -Be $defaultConsole } It 'should return a valid value for the parameter no event log' { # Act $scriptLogger = Start-ScriptLogger -NoEventLog -PassThru # Assert $scriptLogger | Should -Not -BeNullOrEmpty $scriptLogger.Enabled | Should -Be $defaultEnabled $scriptLogger.Path | Should -Be $defaultPath $scriptLogger.Format | Should -Be $defaultFormat $scriptLogger.Level | Should -Be $defaultLevel $scriptLogger.Encoding | Should -Be $defaultEncoding $scriptLogger.LogFile | Should -Be $defaultLogFile $scriptLogger.EventLog | Should -Be $false $scriptLogger.ConsoleOutput | Should -Be $defaultConsole } It 'should return a valid value for the parameter no console output' { # Act $scriptLogger = Start-ScriptLogger -NoConsoleOutput -PassThru # Assert $scriptLogger | Should -Not -BeNullOrEmpty $scriptLogger.Enabled | Should -Be $defaultEnabled $scriptLogger.Path | Should -Be $defaultPath $scriptLogger.Format | Should -Be $defaultFormat $scriptLogger.Level | Should -Be $defaultLevel $scriptLogger.Encoding | Should -Be $defaultEncoding $scriptLogger.LogFile | Should -Be $defaultLogFile $scriptLogger.EventLog | Should -Be $defaultEventLog $scriptLogger.ConsoleOutput | Should -Be $false } AfterEach { Get-ScriptLogger | Remove-Item -Force Stop-ScriptLogger } } |