Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes372downloads
Set-PSBreakpoint.Tests.ps1225 linesDownload Raw Back to Microsoft.PowerShell.Utility
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3$ps = Join-Path -Path $PSHOME -ChildPath "pwsh"4 5Describe "Set-PSBreakpoint DRT Unit Tests" -Tags "CI" {6    #Set up7    $scriptFileName = Join-Path $TestDrive -ChildPath breakpointTestScript.ps18    $scriptFileNameBug = Join-Path -Path $TestDrive -ChildPath SetPSBreakpointTests.ExposeBug154112.ps19 10    $contents = @"11function Hello12{13    `$greeting = 'Hello, world!'14    write-host `$greeting15}16 17function Goodbye18{19    `$message = 'Good bye, cruel world!'20    write-host `$message21}22 23Hello24Goodbye25 26# The following 2 statements produce null tokens (needed to verify 105473)27#28`$table = @{}29 30return31"@32 33    $contentsBug = @"34set-psbreakpoint -variable foo35set-psbreakpoint -command foo36"@37 38    $contents > $scriptFileName39    $contentsBug > $scriptFileNameBug40 41    It "Should be able to set psbreakpoints for -Line" {42        $brk = Set-PSBreakpoint -Line 13 -Script $scriptFileName43        $brk.Line | Should -Be 1344        Remove-PSBreakpoint -Id $brk.Id45    }46 47    It "Should be able to set psbreakpoints for -Line and -column" {48        $brk = Set-PSBreakpoint -Line 13 -Column 1 -Script $scriptFileName49        $brk.Line | Should -Be 1350        $brk.Column | Should -Be 151        Remove-PSBreakpoint -Id $brk.Id52    }53 54    It "Should be able to set psbreakpoints for -Line and -action" {55        $brk = Set-PSBreakpoint -Line 13 -Action {{ break; }} -Script $scriptFileName56        $brk.Line | Should -Be 1357        $brk.Action | Should -Match "break"58        Remove-PSBreakpoint -Id $brk.Id59    }60 61    It "Should be able to set psbreakpoints for -Line, -column and -action" {62        $brk = Set-PSBreakpoint -Line 13 -Column 1 -Action {{ break; }} -Script $scriptFileName63        $brk.Line | Should -Be 1364        $brk.Column | Should -Be 165        $brk.Action | Should -Match "break"66        Remove-PSBreakpoint -Id $brk.Id67    }68 69    It "-script and -line can take multiple items" {70        $brk = Set-PSBreakpoint -Line 11,12,13 -Column 1 -Script $scriptFileName,$scriptFileName71        $brk.Line | Should -BeIn 11,12,1372        $brk.Column | Should -BeIn 173        Remove-PSBreakpoint -Id $brk.Id74    }75 76    It "-script and -line are positional" {77        $brk = Set-PSBreakpoint $scriptFileName 1378        $brk.Line | Should -Be 1379        Remove-PSBreakpoint -Id $brk.Id80    }81 82    It "-script, -line and -column are positional" {83        $brk = Set-PSBreakpoint $scriptFileName 13 184        $brk.Line | Should -Be 1385        $brk.Column | Should -Be 186        Remove-PSBreakpoint -Id $brk.Id87    }88 89    It "Should throw Exception when missing mandatory parameter -line" -Pending {90         $output = & $ps -noninteractive -command "sbp -column 1 -script $scriptFileName"91         [system.string]::Join(" ", $output) | Should -Match "MissingMandatoryParameter,Microsoft.PowerShell.Commands.SetPSBreakpointCommand"92    }93 94    It "Should throw Exception when missing mandatory parameter" -Pending {95         $output = & $ps -noprofile -noninteractive -command "sbp -line 1"96         [system.string]::Join(" ", $output) | Should -Match "MissingMandatoryParameter,Microsoft.PowerShell.Commands.SetPSBreakpointCommand"97    }98 99    It "Should be able to set psbreakpoints for -command" {100        $brk = Set-PSBreakpoint -Command "write-host"101        $brk.Command | Should -BeExactly "write-host"102        Remove-PSBreakpoint -Id $brk.Id103    }104 105    It "Should be able to set psbreakpoints for -command, -script" {106        $brk = Set-PSBreakpoint -Command "write-host" -Script $scriptFileName107        $brk.Command | Should -BeExactly "write-host"108        Remove-PSBreakpoint -Id $brk.Id109    }110 111    It "Should be able to set psbreakpoints for -command, -action and -script" {112        $brk = Set-PSBreakpoint -Command "write-host" -Action {{ break; }} -Script $scriptFileName113        $brk.Action | Should -Match "break"114        Remove-PSBreakpoint -Id $brk.Id115    }116 117    It "-Command can take multiple items" {118        $brk = Set-PSBreakpoint -Command write-host,Hello119        $brk.Command | Should -Be write-host,Hello120        Remove-PSBreakpoint -Id $brk.Id121    }122 123    It "-Script is positional" {124        $brk = Set-PSBreakpoint -Command "Hello" $scriptFileName125        $brk.Command | Should -BeExactly "Hello"126        Remove-PSBreakpoint -Id $brk.Id127 128        $brk = Set-PSBreakpoint $scriptFileName -Command "Hello"129        $brk.Command | Should -BeExactly "Hello"130        Remove-PSBreakpoint -Id $brk.Id131    }132 133    It "Should be able to set breakpoints on functions" {134        $brk = Set-PSBreakpoint -Command Hello,Goodbye -Script $scriptFileName135        $brk.Command | Should -Be Hello,Goodbye136        Remove-PSBreakpoint -Id $brk.Id137    }138 139    It "Should be throw Exception when Column number less than 1" {140        { Set-PSBreakpoint -Line 1 -Column -1 -Script $scriptFileName } | Should -Throw -ErrorId "ParameterArgumentValidationError,Microsoft.PowerShell.Commands.SetPSBreakpointCommand"141    }142 143    It "Should be throw Exception when Line number less than 1" {144        $ErrorActionPreference = "Stop"145        { Set-PSBreakpoint -Line -1 -Script $scriptFileName } | Should -Throw -ErrorId "SetPSBreakpoint:LineLessThanOne,Microsoft.PowerShell.Commands.SetPSBreakpointCommand"146        $ErrorActionPreference = "SilentlyContinue"147    }148 149    It "Remove implicit script from 'set-psbreakpoint -script'" {150        & $ps -noprofile $scriptFileNameBug151 152        $breakpoint = Get-PSBreakpoint -Script $scriptFileNameBug153        $breakpoint | Should -BeNullOrEmpty154    }155 156    It "Fail to set psbreakpoints when script is a file of wrong type" {157 158        if ($IsWindows) {159            $tempFile = [System.IO.Path]::GetTempFileName()160        }161        else {162            $tempFile = (Join-Path $env:HOME $([System.IO.Path]::GetRandomFileName()))163        }164 165        $ErrorActionPreference = "Stop"166        {167            Set-PSBreakpoint -Script $tempFile -Line 1168        } | Should -Throw169        $ErrorActionPreference = "SilentlyContinue"170        Remove-Item $tempFile -Force171    }172 173    It "Fail to set psbreakpoints when script file does not exist" {174        $ErrorActionPreference = "Stop"175        ${script.ps1} = 10176        {177            Set-PSBreakpoint -Script variable:\script.ps1 -Line 1178        } | Should -Throw179        $ErrorActionPreference = "SilentlyContinue"180    }181 182    # clean up183    Remove-Item -Path $scriptFileName -Force184    Remove-Item -Path $scriptFileNameBug -Force185}186 187Describe "Set-PSBreakpoint" -Tags "CI" {188    # Set up test script189    $testScript = Join-Path -Path $PSScriptRoot -ChildPath psbreakpointtestscript.ps1190 191    "`$var = 1 " > $testScript192 193    It "Should be able to set a psbreakpoint on a line" {194        $lineNumber = 1195        $brk = Set-PSBreakpoint -Line $lineNumber -Script $testScript196        $brk.Line | Should -Be $lineNumber197        Remove-PSBreakpoint -Id $brk.Id198    }199 200    It "Should throw when a string is entered for a line number" {201        {202            $lineNumber = "one"203            Set-PSBreakpoint -Line $lineNumber -Script $testScript204 205        } | Should -Throw206    }207 208    It "Should be able to set a psbreakpoint on a Command" {209        $command = "theCommand"210        $brk = Set-PSBreakpoint -Command $command -Script $testScript211        $brk.Command | Should -Be $command212        Remove-PSBreakpoint -Id $brk.Id213    }214 215    It "Should be able to set a psbreakpoint on a variable" {216        $var = "theVariable"217        $brk = Set-PSBreakpoint -Command $var -Script $testScript218        $brk.Command | Should -Be $var219        Remove-PSBreakpoint -Id $brk.Id220    }221 222    # clean up after ourselves223    Remove-Item -Path $testScript224}225