Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes372downloads
Get-Variable.Tests.ps1183 linesDownload Raw Back to Microsoft.PowerShell.Utility
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3 4Describe "Get-Variable DRT Unit Tests" -Tags "CI" {5	It "Get-Variable of not existing variable Name should throw ItemNotFoundException"{6		{ Get-Variable -ErrorAction Stop -Name nonexistingVariableName } |7			Should -Throw -ErrorId "VariableNotFound,Microsoft.PowerShell.Commands.GetVariableCommand"8	}9 10	It "Get-Variable of existing variable Name with include and bogus exclude should work"{11		Set-Variable newVar testing12		$var1=Get-Variable -Name newVar -Include newVar -Exclude bogus13		$var1.Name | Should -BeExactly "newVar"14		$var1.Value | Should -BeExactly "testing"15	}16 17	It "Get-Variable of existing variable Name with Description and Option should work"{18		Set-Variable newVar testing -Option ReadOnly -Description "testing description"19		$var1=Get-Variable -Name newVar20		$var1.Name | Should -BeExactly "newVar"21		$var1.Value | Should -BeExactly "testing"22		$var1.Options | Should -BeExactly "ReadOnly"23		$var1.Description | Should -BeExactly "testing description"24	}25 26	It "Get-Variable of existing variable Globbing Name should work"{27		Set-Variable abcaVar testing28		Set-Variable bcdaVar "another test"29		Set-Variable aVarfoo wow30		$var1=Get-Variable -Name *aVar* -Scope local31		$var1.Count | Should -Be 332		$var1[0].Name | Should -BeExactly "abcaVar"33		$var1[0].Value | Should -BeExactly "testing"34		$var1[1].Name | Should -BeExactly "aVarfoo"35		$var1[1].Value | Should -BeExactly "wow"36		$var1[2].Name | Should -BeExactly "bcdaVar"37		$var1[2].Value | Should -BeExactly "another test"38	}39 40	It "Get-Variable of existing private variable Name should throw ItemNotFoundException"{41		Set-Variable newVar testing -Option Private42		{Get-Variable -Name newVar -ErrorAction Stop} |43			Should -Throw -ErrorId "VariableNotFound,Microsoft.PowerShell.Commands.GetVariableCommand"44	}45}46 47Describe "Get-Variable" -Tags "CI" {48    It "Should be able to call with no parameters without error" {49		{ Get-Variable } | Should -Not -Throw50    }51 52    It "Should return environment variables when called with no parameters" {53		(Get-Variable).Name -contains "$" | Should -BeTrue54		(Get-Variable).Name -contains "?" | Should -BeTrue55		(Get-Variable).Name -contains "HOST" | Should -BeTrue56		(Get-Variable).Name -contains "PWD" | Should -BeTrue57		(Get-Variable).Name -contains "PID" | Should -BeTrue58		(Get-Variable).Name -contains "^" | Should -BeTrue59    }60 61    It "Should return the value of an object" {62		New-Variable -Name tempVar -Value 163		(Get-Variable tempVar).Value | Should -Be (1)64    }65 66    It "Should be able to call using the Name switch" {67		New-Variable -Name var1 -Value 468 69		{ Get-Variable -Name var1 } | Should -Not -Throw70 71		(Get-Variable -Name var1).Value | Should -Be 472 73		Remove-Variable var174    }75 76    It "Should be able to use wildcard characters in the Name field" {77		New-Variable -Name var1 -Value 478		New-Variable -Name var2 -Value "test"79 80		(Get-Variable -Name var*).Value[0] | Should -Be 481		(Get-Variable -Name var*).Value[1] | Should -BeExactly "test"82 83		Remove-Variable var184		Remove-Variable var285    }86 87    It "Should return only the value if the value switch is used" {88		New-Variable -Name var1 -Value 489 90		Get-Variable -Name var1 -ValueOnly | Should -Be 491 92		Remove-Variable var193    }94 95    It "Should pipe string to the name field without the Name field being specified"{96		New-Variable -Name var1 -Value 397 98		("var1" | Get-Variable ).Value | Should -Be 399 100		Remove-Variable var1101    }102 103    It "Should be able to include a set of variables to get" {104		New-Variable -Name var1 -Value 4105		New-Variable -Name var2 -Value 2106 107		$actual = Get-Variable -Include var1, var2108 109		$actual[0].Name | Should -Be var1110		$actual[1].Name | Should -Be var2111 112		$actual[0].Value | Should -Be 4113		$actual[1].Value | Should -Be 2114 115		Remove-Variable var1116		Remove-Variable var2117    }118 119    It "Should be able to exclude a set of variables to get" {120		New-Variable -Name var1 -Value 4121		New-Variable -Name var2 -Value 2122		New-Variable -Name var3 -Value "test"123 124		$actual = Get-Variable -Exclude var1, var2125 126		$actual.Name -contains "var3" | Should -BeTrue127    }128 129    Context "Scope Tests" {130	# This will violate the DRY principle.  Tread softly.131	It "Should be able to get a global scope variable using the global switch" {132	    New-Variable globalVar -Value 1 -Scope global -Force133 134	    (Get-Variable -Name globalVar -Scope global).Value | Should -Be 1135	}136 137	It "Should not be able to clear a global scope variable using the local switch" {138	    New-Variable globalVar -Value 1 -Scope global -Force139 140	    { Get-Variable -Name globalVar -Scope local -ErrorAction Stop } | Should -Throw -ErrorId "VariableNotFound,Microsoft.PowerShell.Commands.GetVariableCommand"141	}142 143	It "Should be able to get a global variable when there's one in the script scope" {144	    New-Variable globalVar -Value 1 -Scope global -Force145	    { New-Variable globalVar -Value 2 -Scope script -Force}146 147	    (Get-Variable -Name globalVar).Value | Should -Be 1148	}149 150	It "Should be able to get an item locally using the local switch" {151	    {152		New-Variable localVar -Value 1 -Scope local -Force153 154		Get-Variable -Name localVar -Scope local155	    } | Should -Not -Throw156	}157 158	It "Should be able to get a variable created in the global scope when there's one in local scope" {159	    New-Variable localVar -Value 1 -Scope local -Force160 161	    New-Variable localVar -Value 2 -Scope global -Force162 163	    (Get-Variable -Name localVar -Scope global).Value | Should -Be 2164	}165 166	It "Should be able to get a script variable created using the script switch" {167	    {168		New-Variable scriptVar -Value 1 -Scope script -Force169 170		Get-Variable -Name scriptVar -Scope script171	    } | Should -Not -Throw172	}173 174	It "Should be able to clear a global script variable that was created using the script scope switch" {175	    {176		New-Variable scriptVar -Value 1 -Scope script -Force177 178		Get-Variable -Name scriptVar -Scope script179	    } | Should -Not -Throw180	}181    }182}183