Skip to content

Commit

Permalink
Add -Name, -NoUserOverrides and -ListAvailable parameters to Get-Cult…
Browse files Browse the repository at this point in the history
…ure cmdlet (PowerShell#7702)

Add new parameters in Get-Culture cmdlet:
-Name - to allow retrieving a specific culture
-NoUserOverrides - ignore user changes for current culture
-ListAvalable - to allow retrieving all cultures supported on the platform
  • Loading branch information
iSazonov committed Oct 18, 2018
1 parent dd907c5 commit 94c69b6
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 15 deletions.
@@ -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
{
/// <summary>
/// Returns the thread's current culture.
/// Returns:
/// - the thread's current culture
/// - culture by name
/// - list of all supported cultures.
/// </summary>
[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";

/// <summary>
/// Gets or sets culture names for which CultureInfo values are returned.
/// Empty string matches Invariant culture.
/// </summary>
[Parameter(ParameterSetName = NameParameterSet, Position = 0, ValueFromPipeline = true)]
[ValidateSet(typeof(ValidateCultureNamesGenerator))]
[ValidateNotNull]
public string[] Name { get; set; }

/// <summary>
/// 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.
/// </summary>
[Parameter(ParameterSetName = CurrentCultureParameterSet)]
[Parameter(ParameterSetName = NameParameterSet)]
public SwitchParameter NoUserOverrides { get; set; }

/// <summary>
/// Gets or sets a switch to list all available cultures.
/// </summary>
[Parameter(ParameterSetName = ListAvailableParameterSet)]
public SwitchParameter ListAvailable { get; set; }

/// <summary>
/// Output the current Culture info object.
/// Output:
/// - the thread's current culture
/// - culture by name
/// - list of all supported cultures.
/// </summary>
protected override void BeginProcessing()
protected override void ProcessRecord()
{
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;
}
}
}

/// <summary>
/// Get list of valid culture names for ValidateSet attribute.
/// </summary>
public class ValidateCultureNamesGenerator : IValidateSetValuesGenerator
{
string[] IValidateSetValuesGenerator.GetValidValues()
{
WriteObject(Host.CurrentCulture);
var cultures = CultureInfo.GetCultures(CultureTypes.AllCultures);
var result = new List<string>(cultures.Length);
foreach (var cultureInfo in cultures)
{
result.Add(cultureInfo.Name);
}

return result.ToArray();
}
}
}
@@ -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 (Get-Culture).Name variable be equivalent to `$PSCulture" {

(Get-Culture).Name | Should -BeExactly $PsCulture
}

It "Should have $ culture variable be equivalent to (Get-Culture).Name" {
It "Should return the specified culture with '-Name' parameter" {

(Get-Culture).Name | Should -Be $PsCulture
$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)
}
}

0 comments on commit 94c69b6

Please sign in to comment.