diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetCultureCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetCultureCommand.cs index 2cf61e92fb..e1fa418898 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetCultureCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetCultureCommand.cs @@ -1,23 +1,124 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. +using System; +using System.Collections.Generic; +using System.Globalization; using System.Management.Automation; namespace Microsoft.PowerShell.Commands { /// - /// Returns the thread's current culture. + /// Returns: + /// - the thread's current culture + /// - culture by name + /// - list of all supported cultures. /// - [Cmdlet(VerbsCommon.Get, "Culture", HelpUri = "https://go.microsoft.com/fwlink/?LinkID=113312")] + [Cmdlet(VerbsCommon.Get, "Culture", DefaultParameterSetName = CurrentCultureParameterSet, HelpUri = "https://go.microsoft.com/fwlink/?LinkID=113312")] [OutputType(typeof(System.Globalization.CultureInfo))] public sealed class GetCultureCommand : PSCmdlet { + private const string CurrentCultureParameterSet = "CurrentCulture"; + private const string NameParameterSet = "Name"; + private const string ListAvailableParameterSet = "ListAvailable"; + /// - /// Output the current Culture info object. + /// Gets or sets culture names for which CultureInfo values are returned. + /// Empty string matches Invariant culture. /// - protected override void BeginProcessing() + [Parameter(ParameterSetName = NameParameterSet, Position = 0, ValueFromPipeline = true)] + [ValidateSet(typeof(ValidateCultureNamesGenerator))] + [ValidateNotNull] + public string[] Name { get; set; } + + /// + /// Gets or sets a switch to return current culture with user overrides (by default). + /// With the switch on, we return current culture without user overrides. + /// + [Parameter(ParameterSetName = CurrentCultureParameterSet)] + [Parameter(ParameterSetName = NameParameterSet)] + public SwitchParameter NoUserOverrides { get; set; } + + /// + /// Gets or sets a switch to list all available cultures. + /// + [Parameter(ParameterSetName = ListAvailableParameterSet)] + public SwitchParameter ListAvailable { get; set; } + + /// + /// Output: + /// - the thread's current culture + /// - culture by name + /// - list of all supported cultures. + /// + protected override void ProcessRecord() { - WriteObject(Host.CurrentCulture); + CultureInfo ci; + + switch (ParameterSetName) + { + case CurrentCultureParameterSet: + if (NoUserOverrides) + { + ci = CultureInfo.GetCultureInfo(Host.CurrentCulture.Name); + } + else + { + ci = Host.CurrentCulture; + } + + WriteObject(ci); + + break; + case NameParameterSet: + try + { + foreach (var cultureName in Name) + { + if (!NoUserOverrides && string.Equals(cultureName, Host.CurrentCulture.Name, StringComparison.CurrentCultureIgnoreCase)) + { + ci = Host.CurrentCulture; + } + else + { + ci = CultureInfo.GetCultureInfo(cultureName); + } + + WriteObject(ci); + } + } + catch (CultureNotFoundException exc) + { + WriteError(new ErrorRecord(exc, "ItemNotFoundException", ErrorCategory.ObjectNotFound, Name)); + } + + break; + case ListAvailableParameterSet: + foreach (var cultureInfo in CultureInfo.GetCultures(CultureTypes.AllCultures)) + { + WriteObject(cultureInfo); + } + + break; + } + } + } + + /// + /// Get list of valid culture names for ValidateSet attribute. + /// + public class ValidateCultureNamesGenerator : IValidateSetValuesGenerator + { + string[] IValidateSetValuesGenerator.GetValidValues() + { + var cultures = CultureInfo.GetCultures(CultureTypes.AllCultures); + var result = new List(cultures.Length); + foreach (var cultureInfo in cultures) + { + result.Add(cultureInfo.Name); + } + + return result.ToArray(); } } } diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Culture.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Culture.Tests.ps1 index 5226f5ac4a..cba0bd5976 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Culture.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Culture.Tests.ps1 @@ -1,25 +1,72 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. -Describe "Get-Culture DRT Unit Tests" -Tags "CI" { - It "Should works proper with get-culture" { - $results = get-Culture - $results -is "System.Globalization.CultureInfo" | Should -BeTrue - $results[0].Name | Should -Be $PSCulture - } -} Describe "Get-Culture" -Tags "CI" { It "Should return a type of CultureInfo for Get-Culture cmdlet" { - Get-Culture | Should -BeOfType CultureInfo + $culture = Get-Culture + $culture | Should -BeOfType [CultureInfo] + ($culture).EnglishName | Should -BeExactly $host.CurrentCulture.EnglishName + Get-Culture -NoUserOverrides | Should -BeOfType [CultureInfo] } - It "Should have $ culture variable be equivalent to (Get-Culture).Name" { - - (Get-Culture).Name | Should -Be $PsCulture + It "Should have (Get-Culture).Name variable be equivalent to `$PSCulture" { + (Get-Culture).Name | Should -BeExactly $PsCulture } + It "Should return the specified culture with '-Name' parameter" { + + $ci = Get-Culture -Name ru-RU + $ci | Should -BeOfType [CultureInfo] + $ci.Name | Should -BeExactly "ru-RU" + + $ci = Get-Culture -Name ru-RU -NoUserOverrides + $ci | Should -BeOfType [CultureInfo] + $ci.Name | Should -BeExactly "ru-RU" + } + + It "Should return specified cultures with '-Name' parameter" { + + $ciArray = Get-Culture "", "ru-RU" + $ciArray | Should -HaveCount 2 + $ciArray[0] | Should -BeOfType [CultureInfo] + $ciArray[0].EnglishName | Should -BeExactly "Invariant Language (Invariant Country)" + + $ciArray[1] | Should -BeOfType [CultureInfo] + $ciArray[1].Name | Should -BeExactly "ru-RU" + $ciArray[1].EnglishName | Should -BeExactly "Russian (Russia)" + } + + It "Should accept values from a pipeline for '-Name' parameter" { + + $ciArray = "", "ru-RU" | Get-Culture + $ciArray | Should -HaveCount 2 + $ciArray[0] | Should -BeOfType [CultureInfo] + $ciArray[0].EnglishName | Should -BeExactly "Invariant Language (Invariant Country)" + $ciArray[1] | Should -BeOfType [CultureInfo] + $ciArray[1].Name | Should -BeExactly "ru-RU" + $ciArray[1].EnglishName | Should -BeExactly "Russian (Russia)" + } + + It "Should return the culture array with '-ListAvailable' parameter" { + + $ciArray = Get-Culture -ListAvailable + $ciArray.Count | Should -BeGreaterThan 0 + $ciArray[0] | Should -BeOfType [CultureInfo] + } + + It "Should write an error on unsupported culture name" { + + { Get-Culture -Name "abcdefghijkl" -ErrorAction Stop } | Should -PassThru -Throw -ErrorId "ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetCultureCommand" + } +} + +Describe "`$PSCulture" -Tags "CI" { + + It "Check `$PSCulture value" { + $PSCulture | Should -BeExactly $([System.Globalization.CultureInfo]::CurrentCulture.Name) + } }