8000 Move PSVersionInfo.PSVersionName to first place by iSazonov · Pull Request #3562 · PowerShell/PowerShell · GitHub
[go: up one dir, main page]

Skip to content

Move PSVersionInfo.PSVersionName to first place #3562

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
May 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 61 additions & 21 deletions src/System.Management.Automation/engine/PSVersionInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ internal class PSVersionInfo
internal const string PSVersionTableName = "PSVersionTable";
internal const string PSRemotingProtocolVersionName = "PSRemotingProtocolVersion";
internal const string PSVersionName = "PSVersion";
internal const string PSEditionName = "PSEdition";
internal const string PSBuildVersionName = "BuildVersion";
internal const string PSGitCommitIdName = "GitCommitId";
internal const string PSCompatibleVersionsName = "PSCompatibleVersions";
internal const string PSCLRVersionName = "CLRVersion";
internal const string PSPlatformName = "Platform";
internal const string PSOSName = "OS";
internal const string SerializationVersionName = "SerializationVersion";
internal const string WSManStackVersionName = "WSManStackVersion";
private static PSVersionHashTable s_psVersionTable = null;
Expand Down Expand Up @@ -57,20 +64,20 @@ static PSVersionInfo()
s_psVersionTable = new PSVersionHashTable(StringComparer.OrdinalIgnoreCase);

s_psVersionTable[PSVersionInfo.PSVersionName] = s_psV6Version;
s_psVersionTable["PSEdition"] = PSEditionValue;
s_psVersionTable["BuildVersion"] = GetBuildVersion();
s_psVersionTable["GitCommitId"] = GetCommitInfo();
s_psVersionTable["PSCompatibleVersions"] = new Version[] { s_psV1Version, s_psV2Version, s_psV3Version, s_psV4Version, s_psV5Version, s_psV51Version, s_psV6Version };
s_psVersionTable[PSVersionInfo.PSEditionName] = PSEditionValue;
s_psVersionTable[PSBuildVersionName] = GetBuildVersion();
s_psVersionTable[PSGitCommitIdName] = GetCommitInfo();
s_psVersionTable[PSCompatibleVersionsName] = new Version[] { s_psV1Version, s_psV2Version, s_psV3Version, s_psV4Version, s_psV5Version, s_psV51Version, s_psV6Version };
s_psVersionTable[PSVersionInfo.SerializationVersionName] = new Version(InternalSerializer.DefaultVersion);
s_psVersionTable[PSVersionInfo.PSRemotingProtocolVersionName] = RemotingConstants.ProtocolVersion;
s_psVersionTable[PSVersionInfo.WSManStackVersionName] = GetWSManStackVersion();
s_psVersionTable["Platform"] = Environment.OSVersion.Platform.ToString();
s_psVersionTable[PSPlatformName] = Environment.OSVersion.Platform.ToString();
#if CORECLR
s_psVersionTable["OS"] = Runtime.InteropServices.RuntimeInformation.OSDescription.ToString();
s_psVersionTable["CLRVersion"] = null;
            s_psVersionTable[PSCLRVersionName] = null;
s_psVersionTable[PSOSName] = Runtime.InteropServices.RuntimeInformation.OSDescription.ToString();
#else
s_psVersionTable["CLRVersion"] = Environment.Version;
s_psVersionTable["OS"] = Environment.OSVersion.ToString();
s_psVersionTable[PSCLRVersionName] = Environment.Version;
s_psVersionTable[PSOSName] = Environment.OSVersion.ToString();
#endif
}

Expand Down Expand Up @@ -160,47 +167,47 @@ internal static string GitCommitId
{
get
{
return (string)GetPSVersionTable()["GitCommitId"];
return (string)GetPSVersionTable()[PSGitCommitIdName];
}
}

internal static Version CLRVersion
{
get
{
return (Version)GetPSVersionTable()["CLRVersion"];
return (Version)GetPSVersionTable()[PSCLRVersionName];
}
}

internal static Version BuildVersion
{
get
{
return (Version)GetPSVersionTable()["BuildVersion"];
return (Version)GetPSVersionTable()[PSBuildVersionName];
}
}

internal static Version[] PSCompatibleVersions
{
get
{
return (Version[])GetPSVersionTable()["PSCompatibleVersions"];
return (Version[])GetPSVersionTable()[PSCompatibleVersionsName];
}
}

internal static string PSEdition
{
get
{
return (string)GetPSVersionTable()["PSEdition"];
return (string)GetPSVersionTable()[PSVersionInfo.PSEditionName];
}
}

internal static Version SerializationVersion
{
get
{
return (Version)GetPSVersionTable()["SerializationVersion"];
return (Version)GetPSVersionTable()[SerializationVersionName];
}
}

Expand Down Expand Up @@ -324,21 +331,54 @@ internal static SemanticVersion PSV6Version
/// </summary>
public sealed class PSVersionHashTable : Hashtable, IEnumerable
{
private static readonly PSVersionTableComparer s_keysComparer = new PSVersionTableComparer();
internal PSVersionHashTable(IEqualityComparer equalityComparer) : base(equalityComparer)
{
}

/// <summary>
/// Returns ordered collection with Keys of 'PSVersionHashTable'
/// We want see special order:
/// 1. PSVersionName
/// 2. PSEditionName
/// 3. Remaining properties in alphabetical order
/// </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;
ArrayList keyList = new ArrayList(base.Keys);
keyList.Sort(s_keysComparer);
return keyList;
}
}

private class PSVersionTableComparer : IComparer
{
public int Compare(object x, object y)
{
string xString = (string)LanguagePrimitives.ConvertTo(x, typeof(string), CultureInfo.CurrentCulture);
string yString = (string)LanguagePrimitives.ConvertTo(y, typeof(string), CultureInfo.CurrentCulture);
if (PSVersionInfo.PSVersionName.Equals(xString, StringComparison.OrdinalIgnoreCase))
{
return -1;
}
else if (PSVersionInfo.PSVersionName.Equals(yString, StringComparison.OrdinalIgnoreCase))
{
return 1;
}
else if (PSVersionInfo.PSEditionName.Equals(xString, StringComparison.OrdinalIgnoreCase))
{
return -1;
}
else if (PSVersionInfo.PSEditionName.Equals(yString, StringComparison.OrdinalIgnoreCase))
{
return 1;
}
else
{
return String.Compare(xString, yString, StringComparison.OrdinalIgnoreCase);
}
}
}

Expand All @@ -348,9 +388,9 @@ public override ICollection Keys
/// </summary>
IEnumerator IEnumerable.GetEnumerator()
{
foreach (string key in Keys)
foreach (object key in Keys)
{
yield return new System.Collections.DictionaryEntry(key, this[key]);
yield return new DictionaryEntry(key, this[key]);
}
}
}
Expand Down
90 changes: 86 additions & 4 deletions test/powershell/Host/PSVersionTable.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ Describe "PSVersionTable" -Tags "CI" {
$PSVersionTable.GitCommitId | Should not match "powershell.version"
}

It "Should have the correct edition" -Skip:(!$IsCoreCLR) {
$PSVersionTable["PSEdition"] | Should Be "Core"
}

It "Should have the correct platform info" {
$platform = [String][System.Environment]::OSVersion.Platform
[String]$PSVersionTable["Platform"] | Should Be $platform
Expand All @@ -42,4 +38,90 @@ Describe "PSVersionTable" -Tags "CI" {
[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)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,19 +110,3 @@ Describe "Validate basic negative test cases for Variable provider cmdlets" -Tag
catch { $_.FullyQualifiedErrorId | Should be "NotSupported,Microsoft.PowerShell.Commands.GetItemPropertyValueCommand" }
}
}

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
}
}
0