Skip to content

Commit ea640e1

Browse files
committed
Fix broken test that depends on warning output
1 parent f6900aa commit ea640e1

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

Tests/Engine/InvokeScriptAnalyzer.tests.ps1

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,17 @@ Describe "Test CustomizedRulePath" {
223223
Context "When used incorrectly" {
224224
It "file cannot be found" {
225225
$wrongRule = Invoke-ScriptAnalyzer $directory\TestScript.ps1 -CustomizedRulePath "This is a wrong rule" 3>&1 | Select-Object -First 1
226-
$wrongRule | Should Match "Cannot find rule extension 'This is a wrong rule'."
226+
227+
if ($testingLibraryUsage)
228+
{
229+
# Special case for library usage testing: warning output written
230+
# with PSHost.UI.WriteWarningLine does not get redirected correctly
231+
# so we can't use this approach for checking the warning message.
232+
# Instead, reach into the test IOutputWriter implementation to find it.
233+
$wrongRule = $testOutputWriter.MostRecentWarningMessage
234+
}
235+
236+
$wrongRule | Should Match "Cannot find rule extension 'This is a wrong rule'."
227237
}
228238
}
229239
}

Tests/Engine/LibraryUsage.tests.ps1

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ function Invoke-ScriptAnalyzer {
3333
$scriptAnalyzer = New-Object "Microsoft.Windows.PowerShell.ScriptAnalyzer.ScriptAnalyzer"
3434
$scriptAnalyzer.Initialize(
3535
$runspace,
36-
$outputWriter,
36+
$testOutputWriter,
3737
$CustomizedRulePath,
3838
$IncludeRule,
3939
$ExcludeRule,
@@ -54,27 +54,32 @@ public class PesterTestOutputWriter : IOutputWriter
5454
{
5555
private PSHost psHost;
5656
57+
public string MostRecentWarningMessage { get; private set; }
58+
5759
public static PesterTestOutputWriter Create(PSHost psHost)
5860
{
59-
PesterTestOutputWriter outputWriter = new PesterTestOutputWriter();
60-
outputWriter.psHost = psHost;
61-
return outputWriter;
61+
PesterTestOutputWriter testOutputWriter = new PesterTestOutputWriter();
62+
testOutputWriter.psHost = psHost;
63+
return testOutputWriter;
6264
}
6365
64-
// NOTE: We don't implement any Write methods to prevent console spew
65-
6666
public void WriteError(ErrorRecord error)
6767
{
68+
// We don't write errors to avoid misleading
69+
// error messages in test output
6870
}
6971
7072
public void WriteWarning(string message)
7173
{
72-
// Some tests look for warning messages, so write those out
7374
psHost.UI.WriteWarningLine(message);
75+
76+
this.MostRecentWarningMessage = message;
7477
}
7578
7679
public void WriteVerbose(string message)
7780
{
81+
// We don't write verbose output to avoid lots
82+
// of unnecessary messages in test output
7883
}
7984
8085
public void WriteDebug(string message)
@@ -92,9 +97,9 @@ public class PesterTestOutputWriter : IOutputWriter
9297
}
9398
"@ -ReferencedAssemblies "Microsoft.Windows.PowerShell.ScriptAnalyzer" -ErrorAction SilentlyContinue
9499

95-
if ($outputWriter -eq $null)
100+
if ($testOutputWriter -eq $null)
96101
{
97-
$outputWriter = [PesterTestOutputWriter]::Create($Host);
102+
$testOutputWriter = [PesterTestOutputWriter]::Create($Host);
98103
}
99104

100105
# Create a fresh runspace to pass into the ScriptAnalyzer class

0 commit comments

Comments
 (0)