Skip to content
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

Fix the ui language override helper method for input language "en" #9392

Merged
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
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));
}
}
}