Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes372downloads
Get-Process.Tests.ps1175 linesDownload Raw Back to Microsoft.PowerShell.Management
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3Describe "Get-Process for admin" -Tags @('CI', 'RequireAdminOnWindows') {4    It "Should support -Module" -Pending:$IsMacOS {5        $modules = Get-Process -Id $PID -Module6        $modules.GetType() | Should -BeExactly "System.Object[]"7        foreach ($module in $modules) {8            $module.GetType() | Should -BeExactly "System.Diagnostics.ProcessModule"9        }10    }11 12    It "Should support -FileVersionInfo" {13        $pwshVersion = Get-Process -Id $PID -FileVersionInfo14        if ($IsWindows) {15            $pwshVersion.FileVersion | Should -Match $PSVersionTable.PSVersion.ToString().Split("-")[0]16            $pwshVersion.FileMajorPart | Should -BeExactly $PSVersionTable.PSVersion.Major17            $pwshVersion.FileMinorPart | Should -BeExactly $PSVersionTable.PSVersion.Minor18            $pwshVersion.FileBuildPart | Should -BeExactly $PSVersionTable.PSVersion.Patch19            $gitCommitId = $PSVersionTable.GitCommitId20            if ($gitCommitId.StartsWith("v")) { $gitCommitId = $gitCommitId.Substring(1) }21            $productVersion = $pwshVersion.ProductVersion.Replace(" Commits: ", "-").Replace(" SHA: ", "-g")22            $productVersion | Should -Match $gitCommitId23        } else {24            $pwshVersion.FileVersion | Should -BeNullOrEmpty25        }26    }27 28    It "Run with parameter -FileVersionInfo should not stop responding on non Windows platform also when process' main module is null." -Skip:$IsWindows {29        # Main module for idle process can be null on non-Windows platforms30        { $pwshVersion = Get-Process -Id 0 -FileVersionInfo -ErrorAction Stop } | Should -Not -Throw31    }32 33    It "Run with parameter -FileVersionInfo for idle process should throw on Windows." -Skip:(!$IsWindows) {34        { $pwshVersion = Get-Process -Id 0 -FileVersionInfo -ErrorAction Stop } | Should -Throw -ErrorId "CouldNotEnumerateFileVer,Microsoft.PowerShell.Commands.GetProcessCommand"35    }36}37 38Describe "Get-Process" -Tags "CI" {39    # These tests are no good, please replace!40    BeforeAll {41        $ps = Get-Process42        $idleProcessPid = 043    }44    It "Should return a type of Object[] for Get-Process cmdlet" -Pending:$IsMacOS {45        , $ps | Should -BeOfType System.Object[]46    }47 48    It "Should have not empty Name flags set for Get-Process object" -Pending:$IsMacOS {49        $ps | ForEach-Object { $_.Name | Should -Not -BeNullOrEmpty }50    }51 52    It "Should throw an error for non existing process id." {53        $randomId = 12345678954        { Get-Process -Id $randomId -ErrorAction Stop } | Should -Throw -ErrorId "NoProcessFoundForGivenId,Microsoft.PowerShell.Commands.GetProcessCommand"55    }56 57    It "Should throw an exception when process id is null." {58        { Get-Process -Id $null } | Should -Throw -ErrorId "ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.GetProcessCommand"59    }60 61    It "Should throw an exception when -InputObject parameter is null." {62        { Get-Process -InputObject $null } | Should -Throw -ErrorId "ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.GetProcessCommand"63    }64 65    It "Should not fail to get process name even if it is unavailable." {66        { (Get-Process -Id $idleProcessPid).Name } | Should -Not -Throw67    }68 69    It "Test for process property = Name" {70        (Get-Process -Id $PID).Name | Should -BeExactly "pwsh"71    }72 73    It "Test for process property = Id" {74        (Get-Process -Id $PID).Id | Should -BeExactly $PID75    }76 77    It "Should support -IncludeUserName" {78        (Get-Process -Id $PID -IncludeUserName).UserName | Should -Match $env:USERNAME79    }80 81    It "Should fail to run Get-Process with -Module without admin" -Skip:(!$IsWindows) {82        { Get-Process -Module -ErrorAction Stop } | Should -Throw -ErrorId "CouldNotEnumerateModules,Microsoft.PowerShell.Commands.GetProcessCommand"83    }84 85    It "Should not fail to stop Get-Process with -Module when piped to Select-Object" {86        Get-Process -Module -Id $PID -ErrorVariable errs | Select-Object -First 187        $errs | Should -HaveCount 088    }89 90    It "Should fail to run Get-Process with -FileVersionInfo without admin" -Skip:(!$IsWindows) {91        { Get-Process -FileVersionInfo -ErrorAction Stop } | Should -Throw -ErrorId "CouldNotEnumerateFileVer,Microsoft.PowerShell.Commands.GetProcessCommand"92    }93 94    It "Should return CommandLine property" -Skip:($IsMacOS) {95        if ($IsWindows) {96            # Windows will convert the bound parameters and quote them if it97            # contains whitespace. Any inner double quotes are escaped with \".98            $expected = "`"$PSHOME\pwsh.exe`" -NoProfile -NoLogo -NonInteractive -Command `"(Get-Process -Id \`"`$pid\`").CommandLine`""99 100            $actual = & {101                $PSNativeCommandArgumentPassing = 'Windows'102                & $PSHOME/pwsh -NoProfile -NoLogo -NonInteractive -Command '(Get-Process -Id "$pid").CommandLine'103            }104        } else {105            # Linux passes arguments as they are bound. As there is no actual106            # command line string, pwsh just joins each array with a space107            # without attempting to use some sort of quoting rule.108            $expected = "$PSHOME/pwsh -NoProfile -NoLogo -NonInteractive -Command (Get-Process -Id `"`$pid`").CommandLine"109 110            $actual = & "$PSHOME/pwsh" -NoProfile -NoLogo -NonInteractive -Command '(Get-Process -Id "$pid").CommandLine'111        }112 113        $actual | Should -Be $expected114    }115}116 117Describe "Get-Process Formatting" -Tags "Feature" {118    BeforeAll {119        $skip = $false120        if ($IsWindows) {121            # on Windows skip this test until issue #11016 is resolved122            $skip = $true123        }124    }125 126    It "Should not have Handle in table format header" -Skip:$skip {127        $types = "System.Diagnostics.Process", "System.Diagnostics.Process#IncludeUserName"128 129        foreach ($type in $types) {130            $formatData = Get-FormatData -TypeName $type -PowerShellVersion $PSVersionTable.PSVersion131            $tableControls = $formatData.FormatViewDefinition | Where-Object { $_.Control -is "System.Management.Automation.TableControl" }132            foreach ($tableControl in $tableControls) {133                $tableControl.Control.Headers.Label -match "Handle*" | Should -BeNullOrEmpty134                # verify that rows without headers isn't the handlecount (as PowerShell will create a header that matches the property name)135                $tableControl.Control.Rows.Columns.DisplayEntry.Value -eq "HandleCount" | Should -BeNullOrEmpty136            }137        }138    }139}140 141Describe "Process Parent property" -Tags "CI" {142    It "Has Parent process property" {143        $powershellexe = (Get-Process -Id $PID).mainmodule.filename144        & $powershellexe -noprofile -command '(Get-Process -Id $PID).Parent' | Should -Not -BeNullOrEmpty145    }146 147    It "Has valid parent process ID property" -Pending {148        # Bug. See https://github.com/PowerShell/PowerShell/issues/12908149        $powershellexe = (Get-Process -Id $PID).mainmodule.filename150        & $powershellexe -noprofile -command '(Get-Process -Id $PID).Parent.Id' | Should -Be $PID151    }152 153    It "Can find parent with spaces and parenthesis in the name on non-Windows" -Skip:($IsWindows) {154        # Bug. See https://github.com/PowerShell/PowerShell/issues/12908155        $commandName = 't ( e ( s ) t )'156 157        $script = @'158#!/bin/sh159 160while true; do sleep 1; done161'@162 163        # Can't use testdrive: as unelevated user doesn't have perms in test in CI164        Set-Content -Path /tmp/$commandName -Value $script165        chmod +x (Resolve-Path -Path /tmp/$commandName -Relative)166        try {167            $p = Start-Process -FilePath /tmp/$commandName -PassThru168            $p.Parent.Id | Should -Be $pid169        } finally {170            $p | Stop-Process171        }172    }173}174 175