Make the output of $PSVersionTable in alphabetical order (#3530)

This commit is contained in:
Ilya 2017-04-13 09:04:53 +04:00 committed by Dongbo Wang
parent f0c0176aa2
commit acec58c049
2 changed files with 50 additions and 3 deletions

View file

@ -21,7 +21,7 @@ namespace System.Management.Automation
internal const string PSVersionName = "PSVersion";
internal const string SerializationVersionName = "SerializationVersion";
internal const string WSManStackVersionName = "WSManStackVersion";
private static Hashtable s_psVersionTable = null;
private static PSVersionHashTable s_psVersionTable = null;
/// <summary>
/// A constant to track current PowerShell Version.
@ -54,7 +54,7 @@ namespace System.Management.Automation
// Static Constructor.
static PSVersionInfo()
{
s_psVersionTable = new Hashtable(StringComparer.OrdinalIgnoreCase);
s_psVersionTable = new PSVersionHashTable(StringComparer.OrdinalIgnoreCase);
s_psVersionTable[PSVersionInfo.PSVersionName] = s_psV6Version;
s_psVersionTable["PSEdition"] = PSEditionValue;
@ -71,7 +71,7 @@ namespace System.Management.Automation
#endif
}
internal static Hashtable GetPSVersionTable()
internal static PSVersionHashTable GetPSVersionTable()
{
return s_psVersionTable;
}
@ -315,6 +315,43 @@ namespace System.Management.Automation
#endregion
}
/// <summary>
/// Represents an implementation of '$PSVersionTable' variable.
/// The implementation contains ordered 'Keys' and 'GetEnumerator' to get user-friendly output.
/// </summary>
public sealed class PSVersionHashTable : Hashtable, IEnumerable
{
internal PSVersionHashTable(IEqualityComparer equalityComparer) : base(equalityComparer)
{
}
/// <summary>
/// Returns ordered collection with Keys of 'PSVersionHashTable'
/// </summary>
public override ICollection Keys
{
get
{
Array arr = new string[base.Keys.Count];
base.Keys.CopyTo(arr, 0);
Array.Sort(arr, StringComparer.OrdinalIgnoreCase);
return arr;
}
}
/// <summary>
/// Returns an enumerator for 'PSVersionHashTable'.
/// The enumeration is ordered (based on ordered version of 'Keys').
/// </summary>
IEnumerator IEnumerable.GetEnumerator()
{
foreach (string key in Keys)
{
yield return new System.Collections.DictionaryEntry(key, this[key]);
}
}
}
/// <summary>
/// An implementation of semantic versioning (http://semver.org)
/// that can be converted to/from <see cref="System.Version"/>.

View file

@ -115,4 +115,14 @@ Describe "Validate special variables" -Tags "CI" {
It "Verify `$PSVersionTable.PSEdition" {
$PSVersionTable["PSEdition"] | Should Be "Core"
}
It "Verify `$PSVersionTable is ordered" {
$keys1 = $PSVersionTable.Keys
$keys1sorted = $keys1 | Sort-Object
$keys2 = ($PSVersionTable | Format-Table -HideTableHeaders -Property Name | Out-String) -split [System.Environment]::NewLine | Where-Object {$_}
$keys2sorted = $keys2 | Sort-Object
Compare-Object -ReferenceObject $keys1 -DifferenceObject $keys1sorted -SyncWindow 0 | Should Be $null
Compare-Object -ReferenceObject $keys2 -DifferenceObject $keys2sorted -SyncWindow 0 | Should Be $null
}
}