/
PSVersionTable.Tests.ps1
177 lines (148 loc) · 7.5 KB
/
PSVersionTable.Tests.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
Describe "PSVersionTable" -Tags "CI" {
BeforeAll {
$sma = Get-Item (Join-Path $PSHOME "System.Management.Automation.dll")
$formattedVersion = $sma.VersionInfo.ProductVersion
$mainVersionPattern = "(\d+\.\d+\.\d+)(-.+)?"
$fullVersionPattern = "^(\d+\.\d+\.\d+)(-.+)?-(\d+)-g(.+)$"
$expectedPSVersion = ($formattedVersion -split " ")[0]
$expectedVersionPattern = "^$mainVersionPattern$"
if ($formattedVersion.Contains(" Commits: "))
{
$rawGitCommitId = $formattedVersion.Replace(" Commits: ", "-").Replace(" SHA: ", "-g")
$expectedGitCommitIdPattern = $fullVersionPattern
$unexpectectGitCommitIdPattern = "qwerty"
} else {
$rawGitCommitId = ($formattedVersion -split " SHA: ")[0]
$expectedGitCommitIdPattern = "^$mainVersionPattern$"
$unexpectectGitCommitIdPattern = $fullVersionPattern
}
$powerShellVersions = "1.0", "2.0", "3.0", "4.0", "5.0", "5.1", "6.0", "6.1", "6.2", "7.0", "7.1", "7.2"
$powerShellCompatibleVersions = $PSVersionTable.PSCompatibleVersions |
ForEach-Object {$_.ToString(2).SubString(0,3)}
}
It "Should have version table entries" {
$PSVersionTable.Count | Should -Be 9
}
It "Should have the right version table entries" {
$PSVersionTable.ContainsKey("PSVersion") | Should -BeTrue
$PSVersionTable.ContainsKey("PSEdition") | Should -BeTrue
$PSVersionTable.ContainsKey("WSManStackVersion") | Should -BeTrue
$PSVersionTable.ContainsKey("SerializationVersion") | Should -BeTrue
$PSVersionTable.ContainsKey("PSCompatibleVersions") | Should -BeTrue
$PSVersionTable.ContainsKey("PSRemotingProtocolVersion") | Should -BeTrue
$PSVersionTable.ContainsKey("GitCommitId") | Should -BeTrue
$PSVersionTable.ContainsKey("Platform") | Should -BeTrue
$PSVersionTable.ContainsKey("OS") | Should -BeTrue
}
It "PSVersion property" {
$PSVersionTable.PSVersion | Should -BeOfType System.Management.Automation.SemanticVersion
$PSVersionTable.PSVersion | Should -BeExactly $expectedPSVersion
$PSVersionTable.PSVersion | Should -Match $expectedVersionPattern
$PSVersionTable.PSVersion.Major | Should -Be 7
}
It "GitCommitId property" {
$PSVersionTable.GitCommitId | Should -BeOfType System.String
$PSVersionTable.GitCommitId | Should -Match $expectedGitCommitIdPattern
$PSVersionTable.GitCommitId | Should -Not -Match $unexpectectGitCommitIdPattern
$PSVersionTable.GitCommitId | Should -BeExactly $rawGitCommitId
}
It "Should have the correct platform info" {
$platform = [String][System.Environment]::OSVersion.Platform
[String]$PSVersionTable["Platform"] | Should -Be $platform
}
It "Should have the correct OS info" {
if ($IsCoreCLR)
{
$OSDescription = [String][System.Runtime.InteropServices.RuntimeInformation]::OSDescription
[String]$PSVersionTable["OS"] | Should -Be $OSDescription
}
else
{
$OSDescription = [String][System.Environment]::OSVersion
[String]$PSVersionTable["OS"] | Should -Be $OSDescription
}
}
It "Verify `$PSVersionTable.PSEdition" {
if ($IsCoreCLR) {
$edition = "Core"
}
else
{
$edition = "Desktop"
}
$PSVersionTable["PSEdition"] | Should -Be $edition
}
It "Verify `$PSVersionTable is ordered and 'PSVersion' is on first place" {
$PSVersionName = "PSVersion"
$keys1 = ($PSVersionTable | Format-Table -HideTableHeaders -Property Name | Out-String) -split [System.Environment]::NewLine | Where-Object {$_} | ForEach-Object {$_.Trim()}
$keys1[0] | Should -Be "PSVersion"
$keys1[1] | Should -Be "PSEdition"
$keys1last = $keys1[2..($keys1.length-1)]
$keys1sortedlast = $keys1last | Sort-Object
Compare-Object -ReferenceObject $keys1last -DifferenceObject $keys1sortedlast -SyncWindow 0 | Should -Be $null
}
It "Verify `$PSVersionTable can be formatted correctly when it has non-string key" {
try {
$key = Get-Item $PSScriptRoot
$PSVersionTable.Add($key, "TEST")
{ $PSVersionTable | Format-Table } | Should -Not -Throw
} finally {
$PSVersionTable.Remove($key)
}
}
It "Verify `$PSVersionTable can be formatted correctly when 'PSVersion' is removed" {
try {
$VersionValue = $PSVersionTable["PSVersion"]
$PSVersionTable.Remove("PSVersion")
$keys1 = ($PSVersionTable | Format-Table -HideTableHeaders -Property Name | Out-String) -split [System.Environment]::NewLine | Where-Object {$_} | ForEach-Object {$_.Trim()}
$keys1[0] | Should -Be "PSEdition"
$keys1.Length | Should -Be $PSVersionTable.Count
$keys1last = $keys1[1..($keys1.length-1)]
$keys1sortedlast = $keys1last | Sort-Object
Compare-Object -ReferenceObject $keys1last -DifferenceObject $keys1sortedlast -SyncWindow 0 | Should -Be $null
} finally {
$PSVersionTable.Add("PSVersion", $VersionValue)
}
}
It "Verify `$PSVersionTable can be formatted correctly when 'PSEdition' is removed" {
try {
$EditionValue = $PSVersionTable["PSEdition"]
$PSVersionTable.Remove("PSEdition")
$keys1 = ($PSVersionTable | Format-Table -HideTableHeaders -Property Name | Out-String) -split [System.Environment]::NewLine | Where-Object {$_} | ForEach-Object {$_.Trim()}
$keys1[0] | Should -Be "PSVersion"
$keys1.Length | Should -Be $PSVersionTable.Count
$keys1last = $keys1[1..($keys1.length-1)]
$keys1sortedlast = $keys1last | Sort-Object
Compare-Object -ReferenceObject $keys1last -DifferenceObject $keys1sortedlast -SyncWindow 0 | Should -Be $null
} finally {
$PSVersionTable.Add("PSEdition", $EditionValue)
}
}
It "Verify `$PSVersionTable can be formatted correctly when both 'PSEdition' and 'PSVersion' are removed" {
try {
$VersionValue = $PSVersionTable["PSVersion"]
$EditionValue = $PSVersionTable["PSEdition"]
$PSVersionTable.Remove("PSVersion")
$PSVersionTable.Remove("PSEdition")
$keys1 = ($PSVersionTable | Format-Table -HideTableHeaders -Property Name | Out-String) -split [System.Environment]::NewLine | Where-Object {$_} | ForEach-Object {$_.Trim()}
$keys1.Length | Should -Be $PSVersionTable.Count
$keys1sortedlast = $keys1 | Sort-Object
Compare-Object -ReferenceObject $keys1 -DifferenceObject $keys1sortedlast -SyncWindow 0 | Should -Be $null
} finally {
$PSVersionTable.Add("PSVersion", $VersionValue)
$PSVersionTable.Add("PSEdition", $EditionValue)
}
}
It "Verify PSCompatibleVersions has an entry for all known versions of PowerShell" {
foreach ($version in $powerShellVersions) {
$version | Should -BeIn $powerShellCompatibleVersions
}
}
It "Verify PSCompatibleVersions has no unknown PowerShell entries" {
foreach ($version in $powerShellCompatibleVersions) {
$version | Should -BeIn $powerShellVersions
}
}
}