8000 Move 'PSVersion' and 'PSEdition' to first and second places in $PSVer… · PowerShell/PowerShell@42cb8ba · GitHub
[go: up one dir, main page]

Skip to content

Commit 42cb8ba

Browse files
iSazonovdaxian-dbw
authored andcommitted
Move 'PSVersion' and 'PSEdition' to first and second places in $PSVersionTable (#3562)
1 parent 563806c commit 42cb8ba

File tree

3 files changed

+147
-41
lines changed
< 10000 span class="prc-TooltipV2-Tooltip-cYMVY" data-direction="se" aria-hidden="true" id=":Riplab:">Expand file tree

3 files changed

+147
-41
lines changed

src/System.Management.Automation/engine/PSVersionInfo.cs

Lines changed: 61 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ internal class PSVersionInfo
1919
internal const string PSVersionTableName = "PSVersionTable";
2020
internal const string PSRemotingProtocolVersionName = "PSRemotingProtocolVersion";
2121
internal const string PSVersionName = "PSVersion";
22+
internal const string PSEditionName = "PSEdition";
23+
internal const string PSBuildVersionName = "BuildVersion";
24+
internal const string PSGitCommitIdName = "GitCommitId";
25+
internal const string PSCompatibleVersionsName = "PSCompatibleVersions";
26+
internal const string PSCLRVersionName = "CLRVersion";
27+
internal const string PSPlatformName = "Platform";
28+
internal const string PSOSName = "OS";
2229
internal const string SerializationVersionName = "SerializationVersion";
2330
internal const string WSManStackVersionName = "WSManStackVersion";
2431
private static PSVersionHashTable s_psVersionTable = null;
@@ -57,20 +64,20 @@ static PSVersionInfo()
5764
s_psVersionTable = new PSVersionHashTable(StringComparer.OrdinalIgnoreCase);
5865

5966
s_psVersionTable[PSVersionInfo.PSVersionName] = s_psV6Version;
60-
s_psVersionTable["PSEdition"] = PSEditionValue;
61-
s_psVersionTable["BuildVersion"] = GetBuildVersion();
62-
s_psVersionTable["GitCommitId"] = GetCommitInfo();
63-
s_psVersionTable["PSCompatibleVersions"] = new Version[] { s_psV1Version, s_psV2Version, s_psV3Version, s_psV4Version, s_psV5Version, s_psV51Version, s_psV6Version };
67+
s_psVersionTable[PSVersionInfo.PSEditionName] = PSEditionValue;
68+
s_psVersionTable[PSBuildVersionName] = GetBuildVersion();
69+
s_psVersionTable[PSGitCommitIdName] = GetCommitInfo();
70+
s_psVersionTable[PSCompatibleVersionsName] = new Version[] { s_psV1Version, s_psV2Version, s_psV3Version, s_psV4Version, s_psV5Version, s_psV51Version, s_psV6Version };
6471
s_psVersionTable[PSVersionInfo.SerializationVersionName] = new Version(InternalSerializer.DefaultVersion);
6572
s_psVersionTable[PSVersionInfo.PSRemotingProtocolVersionName] = RemotingConstants.ProtocolVersion;
6673
s_psVersionTable[PSVersionInfo.WSManStackVersionName] = GetWSManStackVersion();
67-
s_psVersionTable["Platform"] = Environment.OSVersion.Platform.ToString();
74+
s_psVersionTable[PSPlatformName] = Environment.OSVersion.Platform.ToString();
6875
#if CORECLR
69-
s_psVersionTable["OS"] = Runtime.InteropServices.RuntimeInformation.OSDescription.ToString();
70-
s_psVersionTable["CLRVersion"] = null;
76+
            s_psVersionTable[PSCLRVersionName] = null;
77+
s_psVersionTable[PSOSName] = Runtime.InteropServices.RuntimeInformation.OSDescription.ToString();
7178
#else
72-
s_psVersionTable["CLRVersion"] = Environment.Version;
73-
s_psVersionTable["OS"] = Environment.OSVersion.ToString();
79+
s_psVersionTable[PSCLRVersionName] = Environment.Version;
80+
s_psVersionTable[PSOSName] = Environment.OSVersion.ToString();
7481
#endif
7582
}
7683

@@ -160,47 +167,47 @@ internal static string GitCommitId
160167
{
161168
get
162169
{
163-
return (string)GetPSVersionTable()["GitCommitId"];
170+
return (string)GetPSVersionTable()[PSGitCommitIdName];
164171
}
165172
}
166173

167174
internal static Version CLRVersion
168175
{
169176
get
170177
{
171-
return (Version)GetPSVersionTable()["CLRVersion"];
178+
return (Version)GetPSVersionTable()[PSCLRVersionName];
172179
}
173180
}
174181

175182
internal static Version BuildVersion
176183
{
177184
get
178185
{
179-
return (Version)GetPSVersionTable()["BuildVersion"];
186+
return (Version)GetPSVersionTable()[PSBuildVersionName];
180187
}
181188
}
182189

183190
internal static Version[] PSCompatibleVersions
184191
{
185192
get
186193
{
187-
return (Version[])GetPSVersionTable()["PSCompatibleVersions"];
194+
return (Version[])GetPSVersionTable()[PSCompatibleVersionsName];
188195
}
189196
}
190197

191198
internal static string PSEdition
192199
{
193200
get
194201
{
195-
return (string)GetPSVersionTable()["PSEdition"];
202+
return (string)GetPSVersionTable()[PSVersionInfo.PSEditionName];
196203
}
197204
}
198205

199206
internal static Version SerializationVersion
200207
{
201208
get
202209
{
203-
return (Version)GetPSVersionTable()["SerializationVersion"];
210+
return (Version)GetPSVersionTable()[SerializationVersionName];
204211
}
205212
}
206213

@@ -324,21 +331,54 @@ internal static SemanticVersion PSV6Version
324331
/// </summary>
325332
public sealed class PSVersionHashTable : Hashtable, IEnumerable
326333
{
334+
private static readonly PSVersionTableComparer s_keysComparer = new PSVersionTableComparer();
327335
internal PSVersionHashTable(IEqualityComparer equalityComparer) : base(equalityComparer)
328336
{
329337
}
330338

331339
/// <summary>
332340
/// Returns ordered collection with Keys of 'PSVersionHashTable'
341+
/// We want see special order:
342+
/// 1. PSVersionName
343+
/// 2. PSEditionName
344+
/// 3. Remaining properties in alphabetical order
333345
/// </summary>
334346
public override ICollection Keys
335347
{
336348
get
337349
{
338-
Array arr = new string[base.Keys.Count];
339-
base.Keys.CopyTo(arr, 0);
340-
Array.Sort(arr, StringComparer.OrdinalIgnoreCase);
341-
return arr;
350+
ArrayList keyList = new ArrayList(base.Keys);
351+
keyList.Sort(s_keysComparer);
352+
return keyList;
353+
}
354+
}
355+
356+
private class PSVersionTableComparer : IComparer
357+
{
358+
public int Compare(object x, object y)
359+
{
360+
string xString = (string)LanguagePrimitives.ConvertTo(x, typeof(string), CultureInfo.CurrentCulture);
361+
string yString = (string)LanguagePrimitives.ConvertTo(y, typeof(string), CultureInfo.CurrentCulture);
362+
if (PSVersionInfo.PSVersionName.Equals(xString, StringComparison.OrdinalIgnoreCase))
363+
{
364+
return -1;
365+
}
366+
else if (PSVersionInfo.PSVersionName.Equals(yString, StringComparison.OrdinalIgnoreCase))
367+
{
368+
return 1;
369+
}
370+
else if (PSVersionInfo.PSEditionName.Equals(xString, StringComparison.OrdinalIgnoreCase))
371+
{
372+
return -1;
373+
}
374+
else if (PSVersionInfo.PSEditionName.Equals(yString, StringComparison.OrdinalIgnoreCase))
375+
{
376+
return 1;
377+
}
378+
else
379+
{
380+
return String.Compare(xString, yString, StringComparison.OrdinalIgnoreCase);
381+
}
342382
}
343383
}
344384

@@ -348,9 +388,9 @@ public override ICollection Keys
348388
/// </summary>
349389
IEnumerator IEnumerable.GetEnumerator()
350390
{
351-
foreach (string key in Keys)
391+
foreach (object key in Keys)
352392
{
353-
yield return new System.Collections.DictionaryEntry(key, this[key]);
393+
yield return new DictionaryEntry(key, this[key]);
354394
}
355395
}
356396
}

test/powershell/Host/PSVersionTable.Tests.ps1

Lines changed: 86 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@ Describe "PSVersionTable" -Tags "CI" {
2121
$PSVersionTable.GitCommitId | Should not match "powershell.version"
2222
}
2323

24-
It "Should have the correct edition" -Skip:(!$IsCoreCLR) {
25-
$PSVersionTable["PSEdition"] | Should Be "Core"
26-
}
27-
2824
It "Should have the correct platform info" {
2925
$platform = [String][System.Environment]::OSVersion.Platform
3026
[String]$PSVersionTable["Platform"] | Should Be $platform
@@ -42,4 +38,90 @@ Describe "PSVersionTable" -Tags "CI" {
4238
[String]$PSVersionTable["OS"] | Should Be $OSDescription
4339
}
4440
}
41+
42+
It "Verify `$PSVersionTable.PSEdition" {
43+
if ($isCoreCLR) {
44+
$edition = "Core"
45+
}
46+
else
47+
{
48+
$edition = "Desktop"
49+
}
50+
$PSVersionTable["PSEdition"] | Should Be $edition
51+
}
52+
53+
It "Verify `$PSVersionTable is ordered and 'PSVersion' is on first place" {
54+
$PSVersionName = "PSVersion"
55+
$keys1 = ($PSVersionTable | Format-Table -HideTableHeaders -Property Name | Out-String) -split [System.Environment]::NewLine | Where-Object {$_} | ForEach-Object {$_.Trim()}
56+
57+
$keys1[0] | Should Be "PSVersion"
58+
$keys1[1] | Should Be "PSEdition"
59+
60+
$keys1last = $keys1[2..($keys1.length-1)]
61+
$keys1sortedlast = $keys1last | Sort-Object
62+
63+
Compare-Object -ReferenceObject $keys1last -DifferenceObject $keys1sortedlast -SyncWindow 0 | Should Be $null
64+
}
65+
66+
It "Verify `$PSVersionTable can be formatted correctly when it has non-string key" {
67+
try {
68+
$key = Get-Item $PSScriptRoot
69+
$PSVersionTable.Add($key, "TEST")
70+
{ $PSVersionTable | Format-Table } | Should Not Throw
71+
} finally {
72+
$PSVersionTable.Remove($key)
73+
}
74+
}
75+
76+
It "Verify `$PSVersionTable can be formatted correctly when 'PSVersion' is removed" {
77+
try {
78+
$VersionValue = $PSVersionTable["PSVersion"]
79+
$PSVersionTable.Remove("PSVersion")
80+
81+
$keys1 = ($PSVersionTable | Format-Table -HideTableHeaders -Property Name | Out-String) -split [System.Environment]::NewLine | Where-Object {$_} | ForEach-Object {$_.Trim()}
82+
$keys1[0] | Should Be "PSEdition"
83+
$keys1.Length | Should Be $PSVersionTable.Count
84+
85+
$keys1last = $keys1[1..($keys1.length-1)]
86+
$keys1sortedlast = $keys1last | Sort-Object
87+
Compare-Object -ReferenceObject $keys1last -DifferenceObject $keys1sortedlast -SyncWindow 0 | Should Be $null
88+
} finally {
89+
$PSVersionTable.Add("PSVersion", $VersionValue)
90+
}
91+
}
92+
93+
It "Verify `$PSVersionTable can be formatted correctly when 'PSEdition' is removed" {
94+
try {
95+
$EditionValue = $PSVersionTable["PSEdition"]
96+
$PSVersionTable.Remove("PSEdition")
97+
98+
$keys1 = ($PSVersionTable | Format-Table -HideTableHeaders -Property Name | Out-String) -split [System.Environment]::NewLine | Where-Object {$_} | ForEach-Object {$_.Trim()}
99+
$keys1[0] | Should Be "PSVersion"
100+
$keys1.Length | Should Be $PSVersionTable.Count
101+
102+
$keys1last = $keys1[1..($keys1.length-1)]
103+
$keys1sortedlast = $keys1last | Sort-Object
104+
Compare-Object -ReferenceObject $keys1last -DifferenceObject $keys1sortedlast -SyncWindow 0 | Should Be $null
105+
} finally {
106+
$PSVersionTable.Add("PSEdition", $EditionValue)
107+
}
108+
}
109+
110+
It "Verify `$PSVersionTable can be formatted correctly when both 'PSEdition' and 'PSVersion' are removed" {
111+
try {
112+
$VersionValue = $PSVersionTable["PSVersion"]
113+
$EditionValue = $PSVersionTable["PSEdition"]
114+
$PSVersionTable.Remove("PSVersion")
115+
$PSVersionTable.Remove("PSEdition")
116+
117+
$keys1 = ($PSVersionTable | Format-Table -HideTableHeaders -Property Name | Out-String) -split [System.Environment]::NewLine | Where-Object {$_} | ForEach-Object {$_.Trim()}
118+
$keys1.Length | Should Be $PSVersionTable.Count
119+
120+
$keys1sortedlast = $keys1 | Sort-Object
121+
Compare-Object -ReferenceObject $keys1 -DifferenceObject $keys1sortedlast -SyncWindow 0 | Should Be $null
122+
} finally {
123+
$PSVersionTable.Add("PSVersion", $VersionValue)
124+
$PSVersionTable.Add("PSEdition", $EditionValue)
125+
}
126+
}
45127
}

test/powershell/Modules/Microsoft.PowerShell.Management/Variable.Tests.ps1

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -110,19 +110,3 @@ Describe "Validate basic negative test cases for Variable provider cmdlets" -Tag
110110
catch { $_.FullyQualifiedErrorId | Should be "NotSupported,Microsoft.PowerShell.Commands.GetItemPropertyValueCommand" }
111111
}
112112
}
113-
114-
Describe "Validate special variables" -Tags "CI" {
115-
It "Verify `$PSVersionTable.PSEdition" {
116-
$PSVersionTable["PSEdition"] | Should Be "Core"
117-
}
118-
119-
It "Verify `$PSVersionTable is ordered" {
120-
$keys1 = $PSVersionTable.Keys
121-
$keys1sorted = $keys1 | Sort-Object
122-
$keys2 = ($PSVersionTable | Format-Table -HideTableHeaders -Property Name | Out-String) -split [System.Environment]::NewLine | Where-Object {$_}
123-
$keys2sorted = $keys2 | Sort-Object
124-
125-
Compare-Object -ReferenceObject $keys1 -DifferenceObject $keys1sorted -SyncWindow 0 | Should Be $null
126-
Compare-Object -ReferenceObject $keys2 -DifferenceObject $keys2sorted -SyncWindow 0 | Should Be $null
127-
}
128-
}

0 commit comments

Comments
 (0)
0