Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes372downloads
PSVersionTable.Tests.ps1191 linesDownload Raw Back to Host
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3Describe "PSVersionTable" -Tags "CI" {4 5    BeforeAll {6        Set-StrictMode -Version 37        $sma = Get-Item (Join-Path $PSHOME "System.Management.Automation.dll")8        $formattedVersion = $sma.VersionInfo.ProductVersion9 10        $mainVersionPattern = "(\d+\.\d+\.\d+)(-.+)?"11        $fullVersionPattern = "^(\d+\.\d+\.\d+)(-.+)?-(\d+)-g(.+)$"12 13        $expectedPSVersion = ($formattedVersion -split " ")[0]14        $expectedVersionPattern = "^$mainVersionPattern$"15 16        if ($formattedVersion.Contains(" Commits: "))17        {18            $rawGitCommitId = $formattedVersion.Replace(" Commits: ", "-").Replace(" SHA: ", "-g")19            $expectedGitCommitIdPattern = $fullVersionPattern20            $unexpectectGitCommitIdPattern = "qwerty"21        } else {22            $rawGitCommitId = ($formattedVersion -split " SHA: ")[0]23            $expectedGitCommitIdPattern = "^$mainVersionPattern$"24            $unexpectectGitCommitIdPattern = $fullVersionPattern25        }26    }27 28    It "Should have version table entries" {29       $PSVersionTable.Count | Should -Be 930    }31 32    It "Should have the right version table entries" {33       $PSVersionTable.ContainsKey("PSVersion")                 | Should -BeTrue34       $PSVersionTable.ContainsKey("PSEdition")                 | Should -BeTrue35       $PSVersionTable.ContainsKey("WSManStackVersion")         | Should -BeTrue36       $PSVersionTable.ContainsKey("SerializationVersion")      | Should -BeTrue37       $PSVersionTable.ContainsKey("PSCompatibleVersions")      | Should -BeTrue38       $PSVersionTable.ContainsKey("PSRemotingProtocolVersion") | Should -BeTrue39       $PSVersionTable.ContainsKey("GitCommitId")               | Should -BeTrue40       $PSVersionTable.ContainsKey("Platform")                  | Should -BeTrue41       $PSVersionTable.ContainsKey("OS")                        | Should -BeTrue42 43    }44 45    It "PSVersion property" {46       $PSVersionTable.PSVersion | Should -BeOfType System.Management.Automation.SemanticVersion47       $PSVersionTable.PSVersion | Should -BeExactly $expectedPSVersion48       $PSVersionTable.PSVersion | Should -Match $expectedVersionPattern49       $PSVersionTable.PSVersion.Major | Should -Be 750    }51 52    It "GitCommitId property" {53       $PSVersionTable.GitCommitId | Should -BeOfType System.String54       $PSVersionTable.GitCommitId | Should -Match $expectedGitCommitIdPattern55       $PSVersionTable.GitCommitId | Should -Not -Match $unexpectectGitCommitIdPattern56       $PSVersionTable.GitCommitId | Should -BeExactly $rawGitCommitId57    }58 59    It "Should have the correct platform info" {60       $platform = [String][System.Environment]::OSVersion.Platform61       [String]$PSVersionTable["Platform"] | Should -Be $platform62    }63 64    It "Should have the correct OS info" {65       if ($IsCoreCLR)66       {67           $OSDescription = [String][System.Runtime.InteropServices.RuntimeInformation]::OSDescription68           [String]$PSVersionTable["OS"] | Should -Be $OSDescription69       }70       else71       {72           $OSDescription = [String][System.Environment]::OSVersion73           [String]$PSVersionTable["OS"] | Should -Be $OSDescription74       }75    }76 77    It "Verify `$PSVersionTable.PSEdition" {78        if ($IsCoreCLR) {79            $edition = "Core"80        }81        else82        {83            $edition = "Desktop"84        }85        $PSVersionTable["PSEdition"] | Should -Be $edition86    }87 88    It "Verify `$PSVersionTable is ordered and 'PSVersion' is on first place" {89        $PSVersionName = "PSVersion"90        $keys1 = ($PSVersionTable | Format-Table -HideTableHeaders -Property Name | Out-String) -split [System.Environment]::NewLine | Where-Object {$_} | ForEach-Object {$_.Trim()}91 92        $keys1[0] | Should -Be "PSVersion"93        $keys1[1] | Should -Be "PSEdition"94 95        $keys1last = $keys1[2..($keys1.length-1)]96        $keys1sortedlast = $keys1last | Sort-Object97 98        Compare-Object -ReferenceObject $keys1last -DifferenceObject $keys1sortedlast -SyncWindow 0 | Should -Be $null99    }100 101    It "Verify `$PSVersionTable can be formatted correctly when it has non-string key" {102        try {103            $key = Get-Item $PSScriptRoot104            $PSVersionTable.Add($key, "TEST")105            { $PSVersionTable | Format-Table } | Should -Not -Throw106        } finally {107            $PSVersionTable.Remove($key)108        }109    }110 111    It "Verify `$PSVersionTable can be formatted correctly when 'PSVersion' is removed" {112        try {113            $VersionValue = $PSVersionTable["PSVersion"]114            $PSVersionTable.Remove("PSVersion")115 116            $keys1 = ($PSVersionTable | Format-Table -HideTableHeaders -Property Name | Out-String) -split [System.Environment]::NewLine | Where-Object {$_} | ForEach-Object {$_.Trim()}117            $keys1[0] | Should -Be "PSEdition"118            $keys1.Length | Should -Be $PSVersionTable.Count119 120            $keys1last = $keys1[1..($keys1.length-1)]121            $keys1sortedlast = $keys1last | Sort-Object122            Compare-Object -ReferenceObject $keys1last -DifferenceObject $keys1sortedlast -SyncWindow 0 | Should -Be $null123        } finally {124            $PSVersionTable.Add("PSVersion", $VersionValue)125        }126    }127 128    It "Verify `$PSVersionTable can be formatted correctly when 'PSEdition' is removed" {129        try {130            $EditionValue = $PSVersionTable["PSEdition"]131            $PSVersionTable.Remove("PSEdition")132 133            $keys1 = ($PSVersionTable | Format-Table -HideTableHeaders -Property Name | Out-String) -split [System.Environment]::NewLine | Where-Object {$_} | ForEach-Object {$_.Trim()}134            $keys1[0] | Should -Be "PSVersion"135            $keys1.Length | Should -Be $PSVersionTable.Count136 137            $keys1last = $keys1[1..($keys1.length-1)]138            $keys1sortedlast = $keys1last | Sort-Object139            Compare-Object -ReferenceObject $keys1last -DifferenceObject $keys1sortedlast -SyncWindow 0 | Should -Be $null140        } finally {141            $PSVersionTable.Add("PSEdition", $EditionValue)142        }143    }144 145    It "Verify `$PSVersionTable can be formatted correctly when both 'PSEdition' and 'PSVersion' are removed" {146        try {147            $VersionValue = $PSVersionTable["PSVersion"]148            $EditionValue = $PSVersionTable["PSEdition"]149            $PSVersionTable.Remove("PSVersion")150            $PSVersionTable.Remove("PSEdition")151 152            $keys1 = ($PSVersionTable | Format-Table -HideTableHeaders -Property Name | Out-String) -split [System.Environment]::NewLine | Where-Object {$_} | ForEach-Object {$_.Trim()}153            $keys1.Length | Should -Be $PSVersionTable.Count154 155            $keys1sortedlast = $keys1 | Sort-Object156            Compare-Object -ReferenceObject $keys1 -DifferenceObject $keys1sortedlast -SyncWindow 0 | Should -Be $null157        } finally {158            $PSVersionTable.Add("PSVersion", $VersionValue)159            $PSVersionTable.Add("PSEdition", $EditionValue)160        }161    }162 163    Context "PSCompatibleVersions property" {164        It "Is of type System.Version[]" {165            Should -ActualValue $PSVersionTable.PSCompatibleVersions -BeOfType System.Version[]166        }167 168        It "Is sorted in ascending order" {169            $array = $PSVersionTable.PSCompatibleVersions170            [array]::Sort($array)171 172            $PSVersionTable.PSCompatibleVersions | Should -Be $array173        }174 175        It "Has no unexpected items present" {176            $expectedItems = @(177                [version]::new(1, 0)178                [version]::new(2, 0)179                [version]::new(3, 0)180                [version]::new(4, 0)181                [version]::new(5, 0)182                [version]::new(5, 1)183                [version]::new(6, 0)184                [version]::new(7, 0)185            )186 187            Compare-Object $expectedItems $PSVersionTable.PSCompatibleVersions | Should -Be $null188        }189    }190}191