From d71760a6eef09d6c01d8c472815a511a191fc5fd Mon Sep 17 00:00:00 2001 From: Klaudia Algiz Date: Fri, 9 Mar 2018 17:33:55 -0800 Subject: [PATCH 01/10] Terminate the loop in GetMainModule if main module is null. --- .../commands/management/Process.cs | 5 ++++- src/System.Management.Automation/utils/PsUtils.cs | 1 + 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/Process.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/Process.cs index 80b01fcaa3b..0ac97156103 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/Process.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/Process.cs @@ -650,7 +650,10 @@ protected override void ProcessRecord() //if fileversion of each process is to be displayed try { - WriteObject(PsUtils.GetMainModule(process).FileVersionInfo, true); + ProcessModule mainModule = PsUtils.GetMainModule(process); + if (mainModule != null) { + WriteObject(mainModule.FileVersionInfo, true); + } } catch (InvalidOperationException exception) { diff --git a/src/System.Management.Automation/utils/PsUtils.cs b/src/System.Management.Automation/utils/PsUtils.cs index b6ede8c993f..751752ee594 100644 --- a/src/System.Management.Automation/utils/PsUtils.cs +++ b/src/System.Management.Automation/utils/PsUtils.cs @@ -66,6 +66,7 @@ internal static ProcessModule GetMainModule(Process targetProcess) try { mainModule = targetProcess.MainModule; + break; } catch (System.ComponentModel.Win32Exception e) { From b457c01f3fef3cac9b7a22d456f5872d700fc1e0 Mon Sep 17 00:00:00 2001 From: Klaudia Algiz Date: Mon, 12 Mar 2018 11:59:44 -0700 Subject: [PATCH 02/10] Code cleanup --- src/System.Management.Automation/utils/PsUtils.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/System.Management.Automation/utils/PsUtils.cs b/src/System.Management.Automation/utils/PsUtils.cs index 751752ee594..7897d11620a 100644 --- a/src/System.Management.Automation/utils/PsUtils.cs +++ b/src/System.Management.Automation/utils/PsUtils.cs @@ -65,8 +65,7 @@ internal static ProcessModule GetMainModule(Process targetProcess) { try { - mainModule = targetProcess.MainModule; - break; + return targetProcess.MainModule; } catch (System.ComponentModel.Win32Exception e) { From 5a250bbfada2715a074b7ae8d97b977b68869819 Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Mon, 12 Mar 2018 12:15:51 -0700 Subject: [PATCH 03/10] [Feature] Further clean up the code --- src/System.Management.Automation/utils/PsUtils.cs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/System.Management.Automation/utils/PsUtils.cs b/src/System.Management.Automation/utils/PsUtils.cs index 7897d11620a..6729b1b2db2 100644 --- a/src/System.Management.Automation/utils/PsUtils.cs +++ b/src/System.Management.Automation/utils/PsUtils.cs @@ -59,9 +59,8 @@ internal static class PsUtils internal static ProcessModule GetMainModule(Process targetProcess) { int caughtCount = 0; - ProcessModule mainModule = null; - while (mainModule == null) + while (true) { try { @@ -81,8 +80,6 @@ internal static ProcessModule GetMainModule(Process targetProcess) throw; } } - - return mainModule; } // Cache of the current process' parentId From 59901673f6e9f40cfe494035eda3e15a2754f239 Mon Sep 17 00:00:00 2001 From: kalgiz Date: Tue, 20 Mar 2018 15:41:44 -0700 Subject: [PATCH 04/10] [Feature] Run tests with FileVersionInfo also on non-windows platforms. Add test for process which main module can be null. --- .../Microsoft.PowerShell.Management/Get-Process.Tests.ps1 | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Get-Process.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Get-Process.Tests.ps1 index 2852b79ecf9..c4af1374fed 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Get-Process.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Get-Process.Tests.ps1 @@ -13,10 +13,15 @@ Describe "Get-Process for admin" -Tags @('CI', 'RequireAdminOnWindows') { } } - It "Should support -FileVersionInfo" -Skip:(!$IsWindows) { + It "Should support -FileVersionInfo" { $pwshVersion = Get-Process -Id $pid -FileVersionInfo $PSVersionTable.PSVersion | Should -Match $pwshVersion.FileVersion } + + It "Should run correctly with parameter -FileVersionInfo also when process' main module is null." { + # Main module for idle process can be null on non-Windows platforms + { $pwshVersion = Get-Process -Id 0 -FileVersionInfo } | Should -Not -Throw + } } Describe "Get-Process" -Tags "CI" { From 16fd8380b7da52eb88c50a8b9bdab1803628c20e Mon Sep 17 00:00:00 2001 From: kalgiz Date: Wed, 21 Mar 2018 12:05:29 -0700 Subject: [PATCH 05/10] [Feature] Tests correction for Get-process -FileversionInfo command. --- .../Get-Process.Tests.ps1 | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Get-Process.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Get-Process.Tests.ps1 index c4af1374fed..9024287d2d7 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Get-Process.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Get-Process.Tests.ps1 @@ -15,12 +15,20 @@ Describe "Get-Process for admin" -Tags @('CI', 'RequireAdminOnWindows') { It "Should support -FileVersionInfo" { $pwshVersion = Get-Process -Id $pid -FileVersionInfo - $PSVersionTable.PSVersion | Should -Match $pwshVersion.FileVersion + if ($IsWindows) { + $pwshVersion.FileVersion | Should -BeExactly $PSVersionTable.PSVersion + } else { + $pwshVersion.FileVersion | Should -BeNullOrEmpty + } } - It "Should run correctly with parameter -FileVersionInfo also when process' main module is null." { + It "Should run correctly on non Windows platform with parameter -FileVersionInfo also when process' main module is null." -Skip:$IsWindows { # Main module for idle process can be null on non-Windows platforms - { $pwshVersion = Get-Process -Id 0 -FileVersionInfo } | Should -Not -Throw + Wait-UntilTrue { $pwshVersion = Get-Process -Id 0 -FileVersionInfo; return $true } -timeout 10000 + } + + It "Run with parameter -FileVersionInfo for idle process should throw on Windows." -Skip:(!$IsWindows) { + { $pwshVersion = Get-Process -Id 0 -FileVersionInfo -ErrorAction Stop } | Should -Trow -ErrorId "CouldNotEnumerateFileVer,Microsoft.PowerShell.Commands.GetProcessCommand" } } From 94836997145082393153aa9d4ae0d4abaccae0d0 Mon Sep 17 00:00:00 2001 From: kalgiz Date: Wed, 21 Mar 2018 12:37:11 -0700 Subject: [PATCH 06/10] [Feature] Get-process -FileVersionInfo parameter tests correction. --- .../Microsoft.PowerShell.Management/Get-Process.Tests.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Get-Process.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Get-Process.Tests.ps1 index 9024287d2d7..a28230d38df 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Get-Process.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Get-Process.Tests.ps1 @@ -22,9 +22,9 @@ Describe "Get-Process for admin" -Tags @('CI', 'RequireAdminOnWindows') { } } - It "Should run correctly on non Windows platform with parameter -FileVersionInfo also when process' main module is null." -Skip:$IsWindows { + It "Run with parameter -FileVersionInfo should not hang on non Windows platform also when process' main module is null." -Skip:$IsWindows { # Main module for idle process can be null on non-Windows platforms - Wait-UntilTrue { $pwshVersion = Get-Process -Id 0 -FileVersionInfo; return $true } -timeout 10000 + { $pwshVersion = Get-Process -Id 0 -FileVersionInfo -ErrorAction Stop } | Should -Not -Throw } It "Run with parameter -FileVersionInfo for idle process should throw on Windows." -Skip:(!$IsWindows) { From 33cacc1689cf3def57ff74711bcdebb43b6c90b6 Mon Sep 17 00:00:00 2001 From: kalgiz Date: Wed, 21 Mar 2018 14:21:44 -0700 Subject: [PATCH 07/10] [Feature] Get-Process test fix. --- .../Microsoft.PowerShell.Management/Get-Process.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Get-Process.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Get-Process.Tests.ps1 index a28230d38df..3f906d4cdca 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Get-Process.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Get-Process.Tests.ps1 @@ -28,7 +28,7 @@ Describe "Get-Process for admin" -Tags @('CI', 'RequireAdminOnWindows') { } It "Run with parameter -FileVersionInfo for idle process should throw on Windows." -Skip:(!$IsWindows) { - { $pwshVersion = Get-Process -Id 0 -FileVersionInfo -ErrorAction Stop } | Should -Trow -ErrorId "CouldNotEnumerateFileVer,Microsoft.PowerShell.Commands.GetProcessCommand" + { $pwshVersion = Get-Process -Id 0 -FileVersionInfo -ErrorAction Stop } | Should -Throw -ErrorId "CouldNotEnumerateFileVer,Microsoft.PowerShell.Commands.GetProcessCommand" } } From fd351d8779df92e7b1950e2f3dab6a9f59701b85 Mon Sep 17 00:00:00 2001 From: Klaudia Algiz Date: Fri, 23 Mar 2018 12:22:12 -0700 Subject: [PATCH 08/10] [Feature] Get-Process test correction. --- .../Microsoft.PowerShell.Management/Get-Process.Tests.ps1 | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Get-Process.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Get-Process.Tests.ps1 index 3f906d4cdca..7ca532f6da7 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Get-Process.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Get-Process.Tests.ps1 @@ -16,7 +16,8 @@ Describe "Get-Process for admin" -Tags @('CI', 'RequireAdminOnWindows') { It "Should support -FileVersionInfo" { $pwshVersion = Get-Process -Id $pid -FileVersionInfo if ($IsWindows) { - $pwshVersion.FileVersion | Should -BeExactly $PSVersionTable.PSVersion + $pwshVersion.FileVersion | Should -MatchExactly $PSVersionTable.PSVersion + $pwshVersion.ProductVersion.Replace("-dirty","") | Should -BeExactly $PSVersionTable.GitCommitId.Replace("v","") } else { $pwshVersion.FileVersion | Should -BeNullOrEmpty } From 9182513d2cba4bf8913c193df92665c7ae165a4e Mon Sep 17 00:00:00 2001 From: kalgiz Date: Fri, 23 Mar 2018 14:27:52 -0700 Subject: [PATCH 09/10] [Feature] Tests correction. --- .../Microsoft.PowerShell.Management/Get-Process.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Get-Process.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Get-Process.Tests.ps1 index 7ca532f6da7..cddd4971bb1 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Get-Process.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Get-Process.Tests.ps1 @@ -16,7 +16,7 @@ Describe "Get-Process for admin" -Tags @('CI', 'RequireAdminOnWindows') { It "Should support -FileVersionInfo" { $pwshVersion = Get-Process -Id $pid -FileVersionInfo if ($IsWindows) { - $pwshVersion.FileVersion | Should -MatchExactly $PSVersionTable.PSVersion + $PSVersionTable.PSVersion | Should -MatchExactly $pwshVersion.FileVersion $pwshVersion.ProductVersion.Replace("-dirty","") | Should -BeExactly $PSVersionTable.GitCommitId.Replace("v","") } else { $pwshVersion.FileVersion | Should -BeNullOrEmpty From f07dd26deed6c6ec5f87140620023aaab3b48b84 Mon Sep 17 00:00:00 2001 From: Klaudia Algiz Date: Fri, 23 Mar 2018 16:20:48 -0700 Subject: [PATCH 10/10] [Feature] Tests correction. --- .../Microsoft.PowerShell.Management/Get-Process.Tests.ps1 | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Get-Process.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Get-Process.Tests.ps1 index 7ca532f6da7..65e98744baa 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Get-Process.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Get-Process.Tests.ps1 @@ -17,7 +17,9 @@ Describe "Get-Process for admin" -Tags @('CI', 'RequireAdminOnWindows') { $pwshVersion = Get-Process -Id $pid -FileVersionInfo if ($IsWindows) { $pwshVersion.FileVersion | Should -MatchExactly $PSVersionTable.PSVersion - $pwshVersion.ProductVersion.Replace("-dirty","") | Should -BeExactly $PSVersionTable.GitCommitId.Replace("v","") + $gitCommitId = $PSVersionTable.GitCommitId + if ($gitCommitId.StartsWith("v")) { $gitCommitId = $gitCommitId.Substring(1) } + $pwshVersion.ProductVersion.Replace("-dirty","") | Should -BeExactly $gitCommitId } else { $pwshVersion.FileVersion | Should -BeNullOrEmpty }