"Fossies" - the Fresh Open Source Software Archive

Member "PowerShell-7.2.5/test/powershell/Modules/Microsoft.PowerShell.Management/Alias.Tests.ps1" (21 Jun 2022, 4242 Bytes) of package /linux/misc/PowerShell-7.2.5.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Microsoft PowerShell source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 # Copyright (c) Microsoft Corporation.
    2 # Licensed under the MIT License.
    3 Describe "Basic Alias Provider Tests" -Tags "CI" {
    4     BeforeAll {
    5         $testAliasName = "TestAlias"
    6         $testAliasValue = "Get-Date"
    7     }
    8 
    9     BeforeEach {
   10         New-Item -Path "Alias:\" -Name $testAliasName -Value $testAliasValue > $null
   11     }
   12 
   13     AfterEach {
   14         Remove-Item -Path "Alias:\${testAliasName}" -Force -ErrorAction SilentlyContinue
   15     }
   16 
   17     It "Test number of alias not Zero" {
   18         $aliases = @(Get-ChildItem "Alias:\")
   19         $aliases.Count | Should -Not -Be 0
   20     }
   21 
   22     It "Test alias dir" {
   23         $dirAlias = Get-Item "Alias:\dir"
   24         $dirAlias.CommandType | Should -BeExactly "Alias"
   25         $dirAlias.Name | Should -BeExactly "dir"
   26         $dirAlias.Definition | Should -BeExactly "Get-ChildItem"
   27     }
   28 
   29     It "Test creating new alias" {
   30         try {
   31             $newAlias = New-Item -Path "Alias:\" -Name "NewTestAlias" -Value $testAliasValue
   32             $newAlias.CommandType | Should -BeExactly "Alias"
   33             $newAlias.Name | Should -BeExactly "NewTestAlias"
   34             $newAlias.Definition | Should -BeExactly $testAliasValue
   35         }
   36         finally {
   37             Remove-Item -Path "Alias:\NewTestAlias" -Force -ErrorAction SilentlyContinue
   38         }
   39     }
   40 
   41     It "Test Get-Item on alias provider" {
   42         $alias = Get-Item -Path "Alias:\${testAliasName}"
   43         $alias.CommandType | Should -BeExactly "Alias"
   44         $alias.Name | Should -BeExactly $testAliasName
   45         $alias.Definition | Should -BeExactly $testAliasValue
   46     }
   47 
   48     It "Test Test-Path on alias provider" {
   49         $aliasExists = Test-Path "Alias:\testAlias"
   50         $aliasExists | Should -BeTrue
   51     }
   52 
   53     It "Test executing the new alias" {
   54         $result = Invoke-Expression $testAliasName
   55         $result | Should -BeOfType DateTime
   56     }
   57 }
   58 
   59 Describe "Extended Alias Provider Tests" -Tags "Feature" {
   60     Context "Valdiation of Set-Item parameters for the Alias Provider" {
   61         BeforeAll {
   62             $testAliasName = "TestAlias"
   63             $testAliasName2 = "TestAlias2"
   64             $testAliasValue = "Get-Date"
   65         }
   66 
   67         BeforeEach {
   68             New-Item -Path Alias:\ -Name $testAliasName -Value $testAliasValue > $null
   69             New-Item -Path Alias:\ -Name $testAliasName2 -Value $testAliasValue > $null
   70         }
   71 
   72         AfterEach {
   73             Remove-Item -Path "Alias:\${testAliasName}" -Force -ErrorAction SilentlyContinue
   74             Remove-Item -Path "Alias:\${testAliasName2}" -Force -ErrorAction SilentlyContinue
   75         }
   76 
   77         It "Verifying Whatif" {
   78             $before = (Get-Item -Path "Alias:\${testAliasName}").Definition
   79             Set-Item -Path "Alias:\${testAliasName}" -Value "Get-Location" -WhatIf
   80             $after = (Get-Item -Path "Alias:\${testAliasName}").Definition
   81             $after | Should -BeExactly $before # Definition should not have changed
   82         }
   83 
   84         It "Verifying Confirm can be bypassed" {
   85             Set-Item -Path "Alias:\${testAliasName}" -Value "Get-Location" -Confirm:$false
   86             $result = Get-Item -Path "Alias:\${testAliasName}"
   87             $result.Definition | Should -BeExactly "Get-Location"
   88         }
   89 
   90         It "Verifying Force" {
   91             Set-Item -Path "Alias:\${testAliasName}" -Value "Get-Location" -Force
   92             $result =  Get-Item -Path "Alias:\${testAliasName}"
   93             $result.Definition | Should -BeExactly "Get-Location"
   94         }
   95 
   96         It "Verifying Include" {
   97             Set-Item -Path "Alias:\*" -Value "Get-Location" -Include "TestAlias*"
   98             $alias1 = Get-Item -Path "Alias:\${testAliasName}"
   99             $alias2 = Get-Item -Path "Alias:\${testAliasName2}"
  100             $alias1.Definition | Should -BeExactly "Get-Location"
  101             $alias2.Definition | Should -BeExactly "Get-Location"
  102         }
  103 
  104         It "Verifying Exclude" {
  105             Set-Item -Path "Alias:\TestAlias*" -Value "Get-Location" -Exclude "*2"
  106             $alias1 = Get-Item -Path "Alias:\${testAliasName}"
  107             $alias2 = Get-Item -Path "Alias:\${testAliasName2}"
  108             $alias1.Definition | Should -BeExactly "Get-Location"
  109             $alias2.Definition | Should -BeExactly "Get-Date"
  110         }
  111     }
  112 }