Skip to content

Commit c62b8b2

Browse files
authored
Make Invoke-Formatter cleanup its state in the same way as Invoke-ScriptAnalyzer does to avoid knock on effect on Invoke-ScriptAnalyzer when Invoke-Formatter is being called (#1121)
1 parent d0aa363 commit c62b8b2

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

Engine/Commands/InvokeFormatterCommand.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,18 @@ protected override void ProcessRecord()
113113
this.WriteObject(formattedScriptDefinition);
114114
}
115115

116+
protected override void EndProcessing()
117+
{
118+
ScriptAnalyzer.Instance.CleanUp();
119+
base.EndProcessing();
120+
}
121+
122+
protected override void StopProcessing()
123+
{
124+
ScriptAnalyzer.Instance.CleanUp();
125+
base.StopProcessing();
126+
}
127+
116128
private void ValidateInputSettings()
117129
{
118130
// todo implement this

Tests/Engine/InvokeFormatter.tests.ps1

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ $testRootDirectory = Split-Path -Parent $directory
33
Import-Module (Join-Path $testRootDirectory "PSScriptAnalyzerTestHelper.psm1")
44

55
Describe "Invoke-Formatter Cmdlet" {
6+
Context "Cmdlet cleans up and has no knock on effect" {
7+
It "Invoke-Formatter has knock on effect on Invoke-ScriptAnalyzer" {
8+
Invoke-Formatter 'foo'
9+
Invoke-ScriptAnalyzer -ScriptDefinition 'gci' | Should -Not -BeNullOrEmpty
10+
}
11+
}
12+
613
Context "When positional parameters are given" {
714
It "Should use the positional parameters" {
815
$def = @"
@@ -30,12 +37,12 @@ function foo {
3037
}
3138

3239
It "Should not expand unary operators when being used as a single negative argument" {
33-
$script = '$foo.bar(-$a)'
40+
$script = '$foo.bar(-$a)'
3441
Invoke-Formatter '$foo.bar(-$a)' -Settings CodeFormatting | Should -Be $script
3542
}
3643

3744
It "Should expand unary operators when not being used as a single negative argument" {
38-
Invoke-Formatter '$foo.bar(-$a+$b+$c)' -Settings CodeFormatting | Should -Be '$foo.bar(-$a + $b + $c)'
45+
Invoke-Formatter '$foo.bar(-$a+$b+$c)' -Settings CodeFormatting | Should -Be '$foo.bar(-$a + $b + $c)'
3946
}
4047
}
4148

0 commit comments

Comments
 (0)