Skip to content

Commit 64b52cc

Browse files
committed
tests: fix tests on Linux
1 parent 998bf2a commit 64b52cc

9 files changed

Lines changed: 55 additions & 39 deletions

ScriptLogger/Helpers/Write-ScriptLoggerLog.ps1

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,33 @@ function Write-ScriptLoggerLog
6060
# Check the logging level: The requested level needs to be equals or higher than the configured level
6161
if ($levelMap[$Level] -ge $levelMap[$logger.Level])
6262
{
63+
# Use the call stack to get the caller info. Skip the first two
64+
# entries (this function and the Write-* wrapper function).
65+
$callerContext = Get-PSCallStack | Select-Object -First 1 -Skip 2
66+
if (-not [System.String]::IsNullOrEmpty($callerContext.ScriptName))
67+
{
68+
$callerInfo = [System.IO.Path]::GetFileName($callerContext.ScriptName)
69+
if ($callerContext.ScriptLineNumber -gt 0)
70+
{
71+
$callerInfo += ':' + $callerContext.ScriptLineNumber
72+
}
73+
}
74+
else
75+
{
76+
$callerInfo = [System.String] $callerContext.Command
77+
}
78+
6379
if ($logger.LogFile -and $PSCmdlet.ShouldProcess('LogFile', 'Write Log'))
6480
{
6581
try
6682
{
6783
# Output to log file
68-
$line = $logger.Format -f (Get-Date), $env:ComputerName, $Env:Username, $Level, $Message
84+
$line = $logger.Format -f (Get-Date), $env:ComputerName, $Env:Username, $Level, $Message, $callerInfo
6985
$line | Out-File -FilePath $logger.Path -Encoding $logger.Encoding -Append -ErrorAction Stop
7086
}
7187
catch
7288
{
73-
Write-Warning "ScriptLogger '$Name' module error during write log file: $_"
89+
Microsoft.PowerShell.Utility\Write-Warning "ScriptLogger '$Name' module error during write log file: $_"
7490
}
7591
}
7692

@@ -81,11 +97,11 @@ function Write-ScriptLoggerLog
8197
try
8298
{
8399
# Output to event log
84-
Write-EventLog -LogName 'Windows PowerShell' -Source 'PowerShell' -EventId 0 -Category 0 -EntryType $entryType -Message $Message -ErrorAction Stop
100+
Write-EventLog -LogName 'Windows PowerShell' -Source 'PowerShell' -EventId 0 -Category 0 -EntryType $entryType -Message "[$callerInfo] $Message" -ErrorAction Stop
85101
}
86102
catch
87103
{
88-
Write-Warning "ScriptLogger '$Name' module error during write event log: $_"
104+
Microsoft.PowerShell.Utility\Write-Warning "ScriptLogger '$Name' module error during write event log: $_"
89105
}
90106
}
91107

@@ -104,6 +120,6 @@ function Write-ScriptLoggerLog
104120
}
105121
else
106122
{
107-
Write-Warning "ScriptLogger '$Name' not found. No logs written."
123+
Microsoft.PowerShell.Utility\Write-Warning "ScriptLogger '$Name' not found. No logs written."
108124
}
109125
}

ScriptLogger/Tests/Unit/Get-ScriptLogger.Tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Describe 'Get-ScriptLogger' {
2222
It 'should return a valid object after starting' {
2323

2424
# Act
25-
Start-ScriptLogger -Path 'TestDrive:\log.txt'
25+
Start-ScriptLogger -Path (Join-Path -Path 'TestDrive:' -ChildPath 'log.txt')
2626
$scriptLogger = Get-ScriptLogger
2727

2828
# Assert

ScriptLogger/Tests/Unit/Set-ScriptLogger.Tests.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Describe 'Set-ScriptLogger' {
1616
BeforeAll {
1717

1818
$defaultEnabled = $true
19-
$defaultPath = 'TestDrive:\test.log'
19+
$defaultPath = Join-Path -Path 'TestDrive:' -ChildPath 'test.log'
2020
$defaultFormat = '{0:yyyy-MM-dd HH:mm:ss} {1} {2} {3} {4}'
2121
$defaultLevel = 'Information'
2222
$defaultEncoding = 'UTF8'
@@ -33,7 +33,7 @@ Describe 'Set-ScriptLogger' {
3333
It 'should update the logger path' {
3434

3535
# Arrange
36-
$expectedPath = 'TestDrive:\testnew.log'
36+
$expectedPath = Join-Path -Path 'TestDrive:' -ChildPath 'test.log'
3737

3838
# Act
3939
Set-ScriptLogger -Path $expectedPath

ScriptLogger/Tests/Unit/Start-ScriptLogger.Tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ Describe 'Start-ScriptLogger' {
4949
It 'should return a valid value for the parameter path' {
5050

5151
# Arrange
52-
$expectedPath = 'TestDrive:\test.log'
52+
$expectedPath = Join-Path -Path 'TestDrive:' -ChildPath 'test.log'
5353

5454
# Act
5555
$scriptLogger = Start-ScriptLogger -Path $expectedPath -PassThru

ScriptLogger/Tests/Unit/Stop-ScriptLogger.Tests.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ BeforeAll {
1010

1111
Describe 'Stop-ScriptLogger' {
1212

13-
It 'should clean up the looger' {
13+
It 'should clean up the logger' {
1414

1515
# Arrange
16-
Start-ScriptLogger -Path 'TestDrive:\test.log'
16+
Start-ScriptLogger -Path (Join-Path -Path 'TestDrive:' -ChildPath 'test.log')
1717

1818
# Act
1919
Stop-ScriptLogger

ScriptLogger/Tests/Unit/Write-ErrorLog.Tests.ps1

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -117,21 +117,21 @@ Describe 'Write-ErrorLog' {
117117
It 'should write a valid message to the log file' {
118118

119119
# Arrange
120-
Start-ScriptLogger -Path 'TestDrive:\test.log' -NoEventLog -NoConsoleOutput
120+
Start-ScriptLogger -Path (Join-Path -Path 'TestDrive:' -ChildPath 'test.log') -NoEventLog -NoConsoleOutput
121121
$callerLine = 124
122122

123123
# Act
124124
Write-ErrorLog -Message 'My Error'
125125

126126
# Assert
127-
$logFile = Get-Content -Path 'TestDrive:\test.log'
127+
$logFile = Get-Content -Path (Join-Path -Path 'TestDrive:' -ChildPath 'test.log')
128128
$logFile | Should -Be "2000-12-31 01:02:03 $Env:ComputerName $Env:Username Error [Write-ErrorLog.Tests.ps1:$callerLine] My Error"
129129
}
130130

131131
It 'should write a valid error record to the log file' {
132132

133133
# Arrange
134-
Start-ScriptLogger -Path 'TestDrive:\test.log' -NoEventLog -NoConsoleOutput
134+
Start-ScriptLogger -Path (Join-Path -Path 'TestDrive:' -ChildPath 'test.log') -NoEventLog -NoConsoleOutput
135135
$callerLine = 144
136136

137137
# Act
@@ -145,14 +145,14 @@ Describe 'Write-ErrorLog' {
145145
}
146146

147147
# Assert
148-
$logFile = Get-Content -Path 'TestDrive:\test.log'
148+
$logFile = Get-Content -Path (Join-Path -Path 'TestDrive:' -ChildPath 'test.log')
149149
$logFile | Should -BeLike "2000-12-31 01:02:03 $Env:ComputerName $Env:Username Error ``[Write-ErrorLog.Tests.ps1:$callerLine``] Attempted to divide by zero. (RuntimeException: *\Unit\Write-ErrorLog.Tests.ps1:* char:*)"
150150
}
151151

152152
It 'should write a valid message with stack trace to the log' {
153153

154154
# Arrange
155-
Start-ScriptLogger -Path 'TestDrive:\test.log' -NoEventLog -NoConsoleOutput
155+
Start-ScriptLogger -Path (Join-Path -Path 'TestDrive:' -ChildPath 'test.log') -NoEventLog -NoConsoleOutput
156156
$callerLine = 165
157157

158158
# Act
@@ -166,7 +166,7 @@ Describe 'Write-ErrorLog' {
166166
}
167167

168168
# Assert
169-
$logFile = Get-Content -Path 'TestDrive:\test.log'
169+
$logFile = Get-Content -Path (Join-Path -Path 'TestDrive:' -ChildPath 'test.log')
170170
$logFile[0] | Should -BeLike "2000-12-31 01:02:03 $Env:ComputerName $Env:Username Error ``[Write-ErrorLog.Tests.ps1:$callerLine``] Attempted to divide by zero. (RuntimeException: *\Unit\Write-ErrorLog.Tests.ps1:* char:*)"
171171
$logFile[1] | Should -BeLike "at <ScriptBlock>, *\ScriptLogger\Tests\Unit\Write-ErrorLog.Tests.ps1:*"
172172
}
@@ -177,7 +177,7 @@ Describe 'Write-ErrorLog' {
177177
It 'should write a valid message to the event log' {
178178

179179
# Arrange
180-
Start-ScriptLogger -Path 'TestDrive:\test.log' -NoLogFile -NoConsoleOutput
180+
Start-ScriptLogger -Path (Join-Path -Path 'TestDrive:' -ChildPath 'test.log') -NoLogFile -NoConsoleOutput
181181
$filterTimestamp = [System.DateTime]::Now.AddSeconds(-1)
182182
$callerLine = 185
183183

@@ -197,7 +197,7 @@ Describe 'Write-ErrorLog' {
197197
It 'should write a valid error record to the event log' {
198198

199199
# Arrange
200-
Start-ScriptLogger -Path 'TestDrive:\test.log' -NoLogFile -NoConsoleOutput
200+
Start-ScriptLogger -Path (Join-Path -Path 'TestDrive:' -ChildPath 'test.log') -NoLogFile -NoConsoleOutput
201201
$filterTimestamp = [System.DateTime]::Now.AddSeconds(-1)
202202
$callerLine = 211
203203

@@ -237,7 +237,7 @@ Describe 'Write-ErrorLog' {
237237
InModuleScope $moduleName {
238238

239239
# Arrange
240-
Start-ScriptLogger -Path 'TestDrive:\test.log' -NoLogFile -NoEventLog
240+
Start-ScriptLogger -Path (Join-Path -Path 'TestDrive:' -ChildPath 'test.log') -NoLogFile -NoEventLog
241241

242242
# Act
243243
Write-ErrorLog -Message 'My Error'
@@ -252,7 +252,7 @@ Describe 'Write-ErrorLog' {
252252
InModuleScope $moduleName {
253253

254254
# Arrange
255-
Start-ScriptLogger -Path 'TestDrive:\test.log' -NoLogFile -NoEventLog
255+
Start-ScriptLogger -Path (Join-Path -Path 'TestDrive:' -ChildPath 'test.log') -NoLogFile -NoEventLog
256256

257257
# Act
258258
try
@@ -272,7 +272,7 @@ Describe 'Write-ErrorLog' {
272272

273273
AfterEach {
274274

275-
Remove-Item -Path 'TestDrive:\test.log' -ErrorAction 'SilentlyContinue'
275+
Remove-Item -Path (Join-Path -Path 'TestDrive:' -ChildPath 'test.log') -ErrorAction 'SilentlyContinue'
276276
Stop-ScriptLogger
277277
}
278278
}

ScriptLogger/Tests/Unit/Write-InformationLog.Tests.ps1

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,14 @@ Describe 'Write-InformationLog' {
6969
It 'should write a valid message to the log file' {
7070

7171
# Arrange
72-
Start-ScriptLogger -Path 'TestDrive:\test.log' -NoEventLog -NoConsoleOutput
72+
Start-ScriptLogger -Path (Join-Path -Path 'TestDrive:' -ChildPath 'test.log') -NoEventLog -NoConsoleOutput
7373
$callerLine = 76
7474

7575
# Act
7676
Write-InformationLog -Message 'My Information'
7777

7878
# Assert
79-
$logFile = Get-Content -Path 'TestDrive:\test.log'
79+
$logFile = Get-Content -Path (Join-Path -Path 'TestDrive:' -ChildPath 'test.log')
8080
$logFile | Should -Be "2000-12-31 01:02:03 $Env:ComputerName $Env:Username Information [Write-InformationLog.Tests.ps1:$callerLine] My Information"
8181
}
8282
}
@@ -86,7 +86,7 @@ Describe 'Write-InformationLog' {
8686
It 'should write a valid message to the event log' {
8787

8888
# Arrange
89-
Start-ScriptLogger -Path 'TestDrive:\test.log' -NoLogFile -NoConsoleOutput
89+
Start-ScriptLogger -Path (Join-Path -Path 'TestDrive:' -ChildPath 'test.log') -NoLogFile -NoConsoleOutput
9090
$filterTimestamp = [System.DateTime]::Now.AddSeconds(-1)
9191
$callerLine = 94
9292

@@ -120,7 +120,7 @@ Describe 'Write-InformationLog' {
120120
InModuleScope 'ScriptLogger' {
121121

122122
# Arrange
123-
Start-ScriptLogger -Path 'TestDrive:\test.log' -NoLogFile -NoEventLog
123+
Start-ScriptLogger -Path (Join-Path -Path 'TestDrive:' -ChildPath 'test.log') -NoLogFile -NoEventLog
124124

125125
# Act
126126
Write-InformationLog -Message 'My Information'
@@ -133,7 +133,7 @@ Describe 'Write-InformationLog' {
133133

134134
AfterEach {
135135

136-
Remove-Item -Path 'TestDrive:\test.log' -ErrorAction 'SilentlyContinue'
136+
Remove-Item -Path (Join-Path -Path 'TestDrive:' -ChildPath 'test.log') -ErrorAction 'SilentlyContinue'
137137
Stop-ScriptLogger
138138
}
139139
}

ScriptLogger/Tests/Unit/Write-VerboseLog.Tests.ps1

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,14 @@ Describe 'Write-VerboseLog' {
6969
It 'should write a valid message to the log file' {
7070

7171
# Arrange
72-
Start-ScriptLogger -Path 'TestDrive:\test.log' -NoEventLog -NoConsoleOutput
72+
Start-ScriptLogger -Path (Join-Path -Path 'TestDrive:' -ChildPath 'test.log') -NoEventLog -NoConsoleOutput
7373
$callerLine = 76
7474

7575
# Act
7676
Write-VerboseLog -Message 'My Verbose'
7777

7878
# Assert
79-
$logFile = Get-Content -Path 'TestDrive:\test.log'
79+
$logFile = Get-Content -Path (Join-Path -Path 'TestDrive:' -ChildPath 'test.log')
8080
$logFile | Should -Be "2000-12-31 01:02:03 $Env:ComputerName $Env:Username Verbose [Write-VerboseLog.Tests.ps1:$callerLine] My Verbose"
8181
}
8282
}
@@ -86,7 +86,7 @@ Describe 'Write-VerboseLog' {
8686
It 'should write a valid message to the event log' {
8787

8888
# Arrange
89-
Start-ScriptLogger -Path 'TestDrive:\test.log' -NoLogFile -NoConsoleOutput
89+
Start-ScriptLogger -Path (Join-Path -Path 'TestDrive:' -ChildPath 'test.log') -NoLogFile -NoConsoleOutput
9090
$filterTimestamp = [System.DateTime]::Now.AddSeconds(-1)
9191
$callerLine = 94
9292

@@ -111,7 +111,7 @@ Describe 'Write-VerboseLog' {
111111

112112
InModuleScope 'ScriptLogger' {
113113

114-
Mock 'Show-ScriptLoggerVerboseMessage' -ModuleName 'ScriptLogger' -ParameterFilter { $Message -eq 'My Verbose' } -Verifiable
114+
Mock 'Write-Verbose' -ModuleName 'ScriptLogger' -ParameterFilter { $Message -eq 'My Verbose' } -Verifiable
115115
}
116116
}
117117

@@ -120,20 +120,20 @@ Describe 'Write-VerboseLog' {
120120
InModuleScope 'ScriptLogger' {
121121

122122
# Arrange
123-
Start-ScriptLogger -Path 'TestDrive:\test.log' -NoLogFile -NoEventLog
123+
Start-ScriptLogger -Path (Join-Path -Path 'TestDrive:' -ChildPath 'test.log') -NoLogFile -NoEventLog
124124

125125
# Act
126126
Write-VerboseLog -Message 'My Verbose'
127127

128128
# Assert
129-
Assert-MockCalled -Scope It -CommandName 'Show-ScriptLoggerVerboseMessage' -Times 1 -Exactly
129+
Assert-MockCalled -Scope It -CommandName 'Write-Verbose' -Times 1 -Exactly
130130
}
131131
}
132132
}
133133

134134
AfterEach {
135135

136-
Remove-Item -Path 'TestDrive:\test.log' -ErrorAction 'SilentlyContinue'
136+
Remove-Item -Path (Join-Path -Path 'TestDrive:' -ChildPath 'test.log') -ErrorAction 'SilentlyContinue'
137137
Stop-ScriptLogger
138138
}
139139
}

ScriptLogger/Tests/Unit/Write-WarningLog.Tests.ps1

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,14 @@ Describe 'Write-WarningLog' {
6969
It 'should write a valid message to the log file' {
7070

7171
# Arrange
72-
Start-ScriptLogger -Path 'TestDrive:\test.log' -NoEventLog -NoConsoleOutput
72+
Start-ScriptLogger -Path (Join-Path -Path 'TestDrive:' -ChildPath 'test.log') -NoEventLog -NoConsoleOutput
7373
$callerLine = 76
7474

7575
# Act
7676
Write-WarningLog -Message 'My Warning'
7777

7878
# Assert
79-
$logFile = Get-Content -Path 'TestDrive:\test.log'
79+
$logFile = Get-Content -Path (Join-Path -Path 'TestDrive:' -ChildPath 'test.log')
8080
$logFile | Should -Be "2000-12-31 01:02:03 $Env:ComputerName $Env:Username Warning [Write-WarningLog.Tests.ps1:$callerLine] My Warning"
8181
}
8282
}
@@ -86,7 +86,7 @@ Describe 'Write-WarningLog' {
8686
It 'should write a valid message to the event log' {
8787

8888
# Arrange
89-
Start-ScriptLogger -Path 'TestDrive:\test.log' -NoLogFile -NoConsoleOutput
89+
Start-ScriptLogger -Path (Join-Path -Path 'TestDrive:' -ChildPath 'test.log') -NoLogFile -NoConsoleOutput
9090
$filterTimestamp = [System.DateTime]::Now.AddSeconds(-1)
9191
$callerLine = 94
9292

@@ -120,7 +120,7 @@ Describe 'Write-WarningLog' {
120120
InModuleScope 'ScriptLogger' {
121121

122122
# Arrange
123-
Start-ScriptLogger -Path 'TestDrive:\test.log' -NoLogFile -NoEventLog
123+
Start-ScriptLogger -Path (Join-Path -Path 'TestDrive:' -ChildPath 'test.log') -NoLogFile -NoEventLog
124124

125125
# Act
126126
Write-WarningLog -Message 'My Warning'
@@ -133,7 +133,7 @@ Describe 'Write-WarningLog' {
133133

134134
AfterEach {
135135

136-
Remove-Item -Path 'TestDrive:\test.log' -ErrorAction 'SilentlyContinue'
136+
Remove-Item -Path (Join-Path -Path 'TestDrive:' -ChildPath 'test.log') -ErrorAction 'SilentlyContinue'
137137
Stop-ScriptLogger
138138
}
139139
}

0 commit comments

Comments
 (0)