8000 Make the output of $PSVersionTable in alphabetical order (#3530) · PowerShell/PowerShell@acec58c · GitHub
[go: up one dir, main page]

Skip to content

Commit acec58c

Browse files
iSazonovdaxian-dbw
authored andcommitted
Make the output of $PSVersionTable in alphabetical order (#3530)
1 parent f0c0176 commit acec58c

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

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

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ internal class PSVersionInfo
2121
internal const string PSVersionName = "PSVersion";
2222
internal const string SerializationVersionName = "SerializationVersion";
2323
internal const string WSManStackVersionName = "WSManStackVersion";
24-
private static Hashtable s_psVersionTable = null;
24+
private static PSVersionHashTable s_psVersionTable = null;
2525

2626
/// <summary>
2727
/// A constant to track current PowerShell Version.
@@ -54,7 +54,7 @@ internal class PSVersionInfo
5454
// Static Constructor.
5555
static PSVersionInfo()
5656
{
57-
s_psVersionTable = new Hashtable(StringComparer.OrdinalIgnoreCase);
57+
s_psVersionTable = new PSVersionHashTable(StringComparer.OrdinalIgnoreCase);
5858

5959
s_psVersionTable[PSVersionInfo.PSVersionName] = s_psV6Version;
6060
s_psVersionTable["PSEdition"] = PSEditionValue;
@@ -71,7 +71,7 @@ static PSVersionInfo()
7171
#endif
7272
}
7373

74-
internal static Hashtable GetPSVersionTable()
74+
internal static PSVersionHashTable GetPSVersionTable()
7575
{
7676
return s_psVersionTable;
7777
}
@@ -315,6 +315,43 @@ internal static SemanticVersion PSV6Version
315315
#endregion
316316
}
317317

318+
/// <summary>
319+
/// Represents an implementation of '$PSVersionTable' variable.
320+
/// The implementation contains ordered 'Keys' and 'GetEnumerator' to get user-friendly output.
321+
/// </summary>
322+
public sealed class PSVersionHashTable : Hashtable, IEnumerable
323+
{
324+
internal PSVersionHashTable(IEqualityComparer equalityComparer) : base(equalityComparer)
325+
{
326+
}
327+
328+
/// <summary>
329+
/// Returns ordered collection with Keys of 'PSVersionHashTable'
330+
/// </summary>
331+
public override ICollection Keys
332+
{
333+
get
334+
{
335+
Array arr = new string[base.Keys.Count];
336+
base.Keys.CopyTo(arr, 0);
337+
Array.Sort(arr, StringComparer.OrdinalIgnoreCase);
338+
return arr;
339+
}
340+
}
341+
342+
/// <summary>
343+
/// Returns an enumerator for 'PSVersionHashTable'.
344+
/// The enumeration is ordered (based on ordered version of 'Keys').
345+
/// </summary>
346+
IEnumerator IEnumerable.GetEnumerator()
347+
{
348+
foreach (string key in Keys)
349+
{
350+
yield return new System.Collections.DictionaryEntry(key, this[key]);
351+
}
352+
}
353+
}
354+
318355
/// <summary>
319356
/// An implementation of semantic versioning (http://semver.org)
320357
/// that can be converted to/from <see cref="System.Version"/>.

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,14 @@ Describe "Validate special variables" -Tags "CI" {
115115
It "Verify `$PSVersionTable.PSEdition" {
116116
$PSVersionTable["PSEdition"] | Should Be "Core"
117117
}
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+
}
118128
}

0 commit comments

Comments
 (0)
0