Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes372downloads
Set-Location.Tests.ps1266 linesDownload Raw Back to Microsoft.PowerShell.Management
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3Describe "Set-Location" -Tags "CI" {4 5    BeforeAll {6        $startDirectory = Get-Location7 8        if ($IsWindows)9        {10            $target = "C:\"11        }12        else13        {14            $target = "/"15        }16    }17 18    AfterAll {19        Set-Location $startDirectory20    }21 22    It "Should be able to be called without error" {23        { Set-Location $target }    | Should -Not -Throw24    }25 26    It "Should be able to be called on different providers" {27        { Set-Location alias: } | Should -Not -Throw28        { Set-Location env: }   | Should -Not -Throw29    }30 31    It "Should have the correct current location when using the set-location cmdlet" {32        Set-Location $startDirectory33 34        $(Get-Location).Path | Should -BeExactly $startDirectory.Path35    }36 37    It "Should be able to use the Path parameter" {38        { Set-Location -Path $target } | Should -Not -Throw39    }40 41    It "Should generate a pathinfo object when using the Passthru switch" {42        $result = Set-Location $target -PassThru43        $result | Should -BeOfType System.Management.Automation.PathInfo44    }45 46    # https://github.com/PowerShell/PowerShell/issues/575247    It "Should accept path containing wildcard characters" -Pending {48        $null = New-Item -ItemType Directory -Path "$TestDrive\aa"49        $null = New-Item -ItemType Directory -Path "$TestDrive\ba"50        $testPath = New-Item -ItemType Directory -Path "$TestDrive\[ab]a"51 52        Set-Location $TestDrive53        Set-Location -Path "[ab]a"54        $(Get-Location).Path | Should -BeExactly $testPath.FullName55    }56 57    It "Should not use filesystem root folder if not in filesystem provider" -Skip:(!$IsWindows) {58        # find filesystem root folder that doesn't exist in HKCU:59        $foundFolder = $false60        foreach ($folder in Get-ChildItem "${env:SystemDrive}\" -Directory) {61            if (-Not (Test-Path "HKCU:\$($folder.Name)")) {62                $testFolder = $folder.Name63                $foundFolder = $true64                break65            }66        }67        $foundFolder | Should -BeTrue68        Set-Location HKCU:\69        { Set-Location ([System.IO.Path]::DirectorySeparatorChar + $testFolder) -ErrorAction Stop } |70            Should -Throw -ErrorId "PathNotFound,Microsoft.PowerShell.Commands.SetLocationCommand"71    }72 73    It "Should use actual casing of folder on case-insensitive filesystem" -Skip:($IsLinux) {74        $testPath = New-Item -ItemType Directory -Path testdrive:/teST75        Set-Location $testPath.FullName.ToUpper()76        $(Get-Location).Path | Should -BeExactly $testPath.FullName77    }78 79    It "Should use actual casing of folder on case-sensitive filesystem: <dir>" -Skip:(!$IsLinux) {80        $dir = "teST"81        $testPathLower = New-Item -ItemType Directory -Path (Join-Path $TestDrive $dir.ToLower())82        $testPathUpper = New-Item -ItemType Directory -Path (Join-Path $TestDrive $dir.ToUpper())83        Set-Location $testPathLower.FullName84        $(Get-Location).Path | Should -BeExactly $testPathLower.FullName85        Set-Location $testPathUpper.FullName86        $(Get-Location).Path | Should -BeExactly $testPathUpper.FullName87        { Set-Location (Join-Path $TestDrive $dir) -ErrorAction Stop } | Should -Throw -ErrorId "PathNotFound,Microsoft.PowerShell.Commands.SetLocationCommand"88    }89 90    Context 'Set-Location with no arguments' {91 92        It 'Should go to $env:HOME when Set-Location run with no arguments from FileSystem provider' {93            Set-Location 'TestDrive:\'94            Set-Location95            (Get-Location).Path | Should -BeExactly (Get-PSProvider FileSystem).Home96        }97 98        It 'Should go to $env:HOME when Set-Location run with no arguments from Env: provider' {99            Set-Location 'Env:'100            Set-Location101            (Get-Location).Path | Should -BeExactly (Get-PSProvider FileSystem).Home102        }103    }104 105    It "Should set location to new drive's current working directory when path is the colon-terminated name of a different drive" {106        try107        {108            $oldLocation = Get-Location109            Set-Location 'TestDrive:\'110            New-Item -Path 'TestDrive:\' -Name 'Directory1' -ItemType Directory111            New-PSDrive -Name 'Z' -PSProvider FileSystem -Root 'TestDrive:\Directory1'112            New-Item -Path 'Z:\' -Name 'Directory2' -ItemType Directory113 114            Set-Location 'TestDrive:\Directory1'115            $pathToTest1 = (Get-Location).Path116            Set-Location 'Z:\Directory2'117            $pathToTest2 = (Get-Location).Path118 119            Set-Location 'TestDrive:'120            (Get-Location).Path | Should -BeExactly $pathToTest1121            Set-Location 'Z:'122            (Get-Location).Path | Should -BeExactly $pathToTest2123        }124        finally125        {126            Set-Location $oldLocation127            Remove-PSDrive -Name 'Z'128        }129    }130 131    Context 'Set-Location with last location history' {132 133        It 'Should go to last location when specifying minus as a path' {134            $initialLocation = Get-Location135            Set-Location ([System.IO.Path]::GetTempPath())136            Set-Location -137            (Get-Location).Path | Should -Be ($initialLocation).Path138        }139 140        It 'Should go to last location back, forth and back again when specifying minus, plus and minus as a path' {141            $initialLocation = (Get-Location).Path142            Set-Location ([System.IO.Path]::GetTempPath())143            $tempPath = (Get-Location).Path144            Set-Location -145            (Get-Location).Path | Should -Be $initialLocation146            Set-Location +147            (Get-Location).Path | Should -Be $tempPath148            Set-Location -149            (Get-Location).Path | Should -Be $initialLocation150        }151 152        It 'Should go back to previous locations when specifying minus twice' {153            $initialLocation = (Get-Location).Path154            Set-Location ([System.IO.Path]::GetTempPath())155            $firstLocationChange = (Get-Location).Path156            Set-Location ([System.Environment]::GetFolderPath("user"))157            Set-Location -158            (Get-Location).Path | Should -Be $firstLocationChange159            Set-Location -160            (Get-Location).Path | Should -Be $initialLocation161        }162 163        It 'Location History is limited' {164            $initialLocation = (Get-Location).Path165            $maximumLocationHistory = 20166            foreach ($i in 1..$maximumLocationHistory) {167                Set-Location ([System.IO.Path]::GetTempPath())168            }169            $tempPath = (Get-Location).Path170            # Go back up to the maximum171            foreach ($i in 1..$maximumLocationHistory) {172                Set-Location -173            }174            (Get-Location).Path | Should -Be $initialLocation175            { Set-Location - } | Should -Throw -ErrorId 'System.InvalidOperationException,Microsoft.PowerShell.Commands.SetLocationCommand'176            # Go forwards up to the maximum177            foreach ($i in 1..($maximumLocationHistory)) {178                Set-Location +179            }180            (Get-Location).Path | Should -Be $tempPath181            { Set-Location + } | Should -Throw -ErrorId 'System.InvalidOperationException,Microsoft.PowerShell.Commands.SetLocationCommand'182        }183    }184 185    It 'Should nativate to literal path "<path>"' -TestCases @(186        @{ path = "-" },187        @{ path = "+" }188    ) {189        param($path)190 191        Set-Location $TestDrive192        $literalPath = Join-Path $TestDrive $path193        New-Item -ItemType Directory -Path $literalPath194        Set-Location -LiteralPath $path195        (Get-Location).Path | Should -BeExactly $literalPath196    }197 198    Context 'Test the LocationChangedAction event handler' {199 200        AfterEach {201            $ExecutionContext.InvokeCommand.LocationChangedAction = $null202        }203 204        It 'The LocationChangedAction should fire when changing location' {205            $initialPath = $PWD206            $oldPath = $null207            $newPath = $null208            $eventSessionState = $null209            $eventRunspace = $null210            $ExecutionContext.InvokeCommand.LocationChangedAction = {211                (Get-Variable eventRunspace).Value = $this212                (Get-Variable eventSessionState).Value = $_.SessionState213                (Get-Variable oldPath).Value = $_.oldPath214                (Get-Variable newPath).Value = $_.newPath215            }216            Set-Location ..217            $newPath.Path | Should -Be $PWD.Path218            $oldPath.Path | Should -Be $initialPath.Path219            $eventSessionState | Should -Be $ExecutionContext.SessionState220            $eventRunspace | Should -Be ([runspace]::DefaultRunspace)221        }222 223        It 'Errors in the LocationChangedAction should be catchable but not fail the cd' {224            $location = $PWD225            Set-Location ..226            $ExecutionContext.InvokeCommand.LocationChangedAction = { throw "Boom" }227            # Verify that the exception occurred228            { Set-Location $location } | Should -Throw "Boom"229            # But the location should still have changed230            $PWD.Path | Should -Be $location.Path231        }232    }233 234    Context 'Parsing of Set-Location to cd' {235        It 'Should go to Filesystem home on cd~ run' {236            Set-Location 'TestDrive:\'237            cd~238            (Get-Location).Path | Should -BeExactly (Get-PSProvider FileSystem).Home239        }240        It 'Should go to the parent folder on cd.. run' {241            Set-Location 'TestDrive:\'242            $ParentDir = (Get-Location).path243            New-Item -Path 'TestDrive:\' -Name 'Directory1' -ItemType Directory -ErrorAction Ignore244            Set-Location 'TestDrive:\Directory1'245            cd..246            (Get-Location).Path | Should -BeExactly $ParentDir247        }248        It 'Should go to root of current drive on cd\ run (Windows)' -Skip:(!$IsWindows){249            #root is / on linux and Mac, so it's not happy with this check.250            Set-Location 'TestDrive:\'251            $DriveRoot = (Get-Location).path252            New-Item -Path 'TestDrive:\Directory1' -Name 'Directory2' -ItemType Directory253            Set-Location 'TestDrive:\Directory1\Directory2'254            cd\255            (Get-Location).Path | Should -BeExactly $DriveRoot256        }257        It 'Should go to root of current drive on cd\ run (Linux/Mac)' -Skip:($IsWindows){258            Set-Location 'TestDrive:\'259            New-Item -Path 'TestDrive:\Directory1' -Name 'Directory2' -ItemType Directory -ErrorAction Ignore260            Set-Location 'TestDrive:\Directory1\Directory2'261            cd\262            (Get-Location).Path | Should -BeExactly "/"263        }264    }265}266