Skip to content

Commit

Permalink
Fix the ui language override helper method for input language "en" (#…
Browse files Browse the repository at this point in the history
…9392)

* Fix the ui language override helper method

---------

Co-authored-by: Farhad Alizada <falizada@microsoft.com>
  • Loading branch information
f-alizada and Farhad Alizada committed Nov 6, 2023
1 parent 7a6b2dd commit 50086fe
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 20 deletions.
5 changes: 1 addition & 4 deletions src/Framework/EncodingUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -272,10 +272,7 @@ internal static Encoding BatchFileEncoding(string contents, string encodingSpeci
CultureInfo? externalLanguageSetting = GetExternalOverriddenUILanguage();
if (externalLanguageSetting != null)
{
if (
!externalLanguageSetting.TwoLetterISOLanguageName.Equals("en", StringComparison.InvariantCultureIgnoreCase) &&
CurrentPlatformIsWindowsAndOfficiallySupportsUTF8Encoding()
)
if (CurrentPlatformIsWindowsAndOfficiallySupportsUTF8Encoding())
{
// Setting both encodings causes a change in the CHCP, making it so we don't need to P-Invoke CHCP ourselves.
Console.OutputEncoding = Encoding.UTF8;
Expand Down
16 changes: 0 additions & 16 deletions src/Tasks.UnitTests/Exec_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -895,22 +895,6 @@ public void ConsoleToMSBuild()
Assert.Equal(2, exec.ConsoleOutput.Length);
}

/// <summary>
/// Test the CanEncode method with and without ANSI characters to determine if they can be encoded
/// in the current system encoding.
/// </summary>
[WindowsOnlyFact]
public void CanEncodeTest()
{
var defaultEncoding = EncodingUtilities.CurrentSystemOemEncoding;

string nonAnsiCharacters = "\u521B\u5EFA";
string pathWithAnsiCharacters = @"c:\windows\system32\cmd.exe";

Assert.False(EncodingUtilities.CanEncodeString(defaultEncoding.CodePage, nonAnsiCharacters));
Assert.True(EncodingUtilities.CanEncodeString(defaultEncoding.CodePage, pathWithAnsiCharacters));
}

[Fact]
public void EndToEndMultilineExec()
{
Expand Down
57 changes: 57 additions & 0 deletions src/Utilities.UnitTests/EncodingUtilities_Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Threading;
using Microsoft.Build.Framework;
using Microsoft.Build.Shared;
using Shouldly;
using Xunit;

#nullable disable

namespace Microsoft.Build.UnitTests
{
public sealed class EncodingUtilities_Tests
{
/// <summary>
/// Test the CanEncode method with and without ANSI characters to determine if they can be encoded
/// in the current system encoding.
/// </summary>
[WindowsOnlyFact]
public void CanEncodeTest()
{
var defaultEncoding = EncodingUtilities.CurrentSystemOemEncoding;

string nonAnsiCharacters = "\u521B\u5EFA";
string pathWithAnsiCharacters = @"c:\windows\system32\cmd.exe";

EncodingUtilities.CanEncodeString(defaultEncoding.CodePage, nonAnsiCharacters).ShouldBeFalse();
EncodingUtilities.CanEncodeString(defaultEncoding.CodePage, pathWithAnsiCharacters).ShouldBeTrue();
}

/// <summary>
/// Test for bug where the MSBuild does not respect "en" CultureInfo
/// </summary>
[Theory]
[InlineData("en", "en")]
[InlineData("jp", "jp")]
[InlineData("fr", "fr")]
public void GetExternalOverriddenUILanguageIfSupportableWithEncoding_RespectsOverriddenLanguage(string inputLanguage, string expectedLanguage)
{
if (!EncodingUtilities.CurrentPlatformIsWindowsAndOfficiallySupportsUTF8Encoding())
{
return; // Do not run test to replicate the behaviour of the invoking method
}
const string DOTNET_CLI_UI_LANGUAGE = nameof(DOTNET_CLI_UI_LANGUAGE);
using TestEnvironment testEnvironment = TestEnvironment.Create();

// Override the ui language by setting environment variable
testEnvironment.SetEnvironmentVariable(DOTNET_CLI_UI_LANGUAGE, inputLanguage);

EncodingUtilities.GetExternalOverriddenUILanguageIfSupportableWithEncoding().ShouldBeEquivalentTo(new CultureInfo(expectedLanguage));
}
}
}

0 comments on commit 50086fe

Please sign in to comment.