-
Notifications
You must be signed in to change notification settings - Fork 467
Fix Console Encoding for Proper Display of Non-ASCII Characters for out of proc #4495
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| // Copyright (c) .NET Foundation. All rights reserved. | ||
| // Licensed under the MIT License. See LICENSE in the project root for license information. | ||
|
|
||
| using System.Text; | ||
|
|
||
| namespace Azure.Functions.Cli.Helpers | ||
| { | ||
| internal static class ConsoleHelper | ||
| { | ||
| /// <summary> | ||
| /// Attempts to switch the console to UTF-8. | ||
| /// Falls back silently if the code page isn’t registered or the host refuses. | ||
| /// </summary> | ||
| internal static void ConfigureConsoleOutputEncoding() | ||
| { | ||
| try | ||
| { | ||
| Console.OutputEncoding = Encoding.UTF8; | ||
| } | ||
| catch | ||
| { | ||
| // UTF-8 encoding not available, international characters may not display correctly. | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
158 changes: 158 additions & 0 deletions
158
test/Cli/Func.E2E.Tests/Commands/FuncStart/ConsoleEncodingTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| // Copyright (c) .NET Foundation. All rights reserved. | ||
| // Licensed under the MIT License. See LICENSE in the project root for license information. | ||
|
|
||
| using Azure.Functions.Cli.Common; | ||
| using Azure.Functions.Cli.E2E.Tests.Traits; | ||
| using Azure.Functions.Cli.TestFramework.Assertions; | ||
| using Azure.Functions.Cli.TestFramework.Commands; | ||
| using Azure.Functions.Cli.TestFramework.Helpers; | ||
| using FluentAssertions; | ||
| using Xunit; | ||
| using Xunit.Abstractions; | ||
|
|
||
| namespace Azure.Functions.Cli.E2E.Tests.Commands.FuncStart | ||
| { | ||
| public class ConsoleEncodingTests(ITestOutputHelper log) : BaseE2ETests(log) | ||
| { | ||
| [Fact] | ||
| [Trait(WorkerRuntimeTraits.WorkerRuntime, WorkerRuntimeTraits.Node)] | ||
| public async Task Start_WithNode_WithNonAsciiLogging_DisplaysCorrectly() | ||
| { | ||
| var port = ProcessHelper.GetAvailablePort(); | ||
| var testName = nameof(Start_WithNode_WithNonAsciiLogging_DisplaysCorrectly); | ||
| var japaneseText = "こんにちは"; | ||
|
|
||
| // Initialize Node.js function app | ||
| await FuncInitWithRetryAsync(testName, [".", "--worker-runtime", "node", "-m", "v4"]); | ||
|
|
||
| // Add HTTP trigger | ||
| await FuncNewWithRetryAsync(testName, [".", "--template", "HttpTrigger", "--name", "HttpTrigger", "--language", "node"], workerRuntime: "node"); | ||
|
|
||
| // Modify the function to log non-ASCII text | ||
| string jsFilePath = Path.Combine(WorkingDirectory, "src", "functions", "HttpTrigger.js"); | ||
| string originalContent = File.ReadAllText(jsFilePath); | ||
|
|
||
| // Find the handler function's opening '{' | ||
| var handlerSignature = "handler: async (request, context) => {"; | ||
| int handlerIndex = originalContent.IndexOf(handlerSignature, StringComparison.Ordinal); | ||
| if (handlerIndex >= 0) | ||
| { | ||
| int bodyStart = originalContent.IndexOf('{', handlerIndex); | ||
| if (bodyStart >= 0) | ||
| { | ||
| bodyStart++; // Move past the '{' | ||
| string logStatement = $"\n context.log(\"Test String: {japaneseText}\");"; | ||
| string modifiedContent = originalContent.Insert(bodyStart, logStatement); | ||
| File.WriteAllText(jsFilePath, modifiedContent); | ||
| } | ||
| } | ||
|
|
||
| // Execute the function | ||
| var funcStartCommand = new FuncStartCommand(FuncPath, testName, Log); | ||
| funcStartCommand.ProcessStartedHandler = async (process) => | ||
| { | ||
| await ProcessHelper.ProcessStartedHandlerHelper(port, process, funcStartCommand.FileWriter ?? throw new ArgumentNullException(nameof(funcStartCommand.FileWriter)), "HttpTrigger?name=Test"); | ||
| }; | ||
|
|
||
| var result = funcStartCommand | ||
| .WithWorkingDirectory(WorkingDirectory) | ||
| .WithEnvironmentVariable(Common.Constants.FunctionsWorkerRuntime, "node") | ||
| .Execute(["--port", port.ToString()]); | ||
|
|
||
| // Verify the Japanese text was correctly displayed (not as question marks) | ||
| result.Should().HaveStdOutContaining($"Test String: {japaneseText}"); | ||
| result.Should().NotHaveStdOutContaining("Test String: ?????"); | ||
| } | ||
|
|
||
| [Fact] | ||
| [Trait(WorkerRuntimeTraits.WorkerRuntime, WorkerRuntimeTraits.DotnetIsolated)] | ||
| public async Task Start_WithDotnetIsolated_WithNonAsciiLogging_DisplaysCorrectly() | ||
| { | ||
| var port = ProcessHelper.GetAvailablePort(); | ||
| var testName = nameof(Start_WithDotnetIsolated_WithNonAsciiLogging_DisplaysCorrectly); | ||
| var japaneseText = "こんにちは"; | ||
|
|
||
| // Initialize .NET function app | ||
| await FuncInitWithRetryAsync(testName, [".", "--worker-runtime", "dotnet-isolated"]); | ||
|
|
||
| // Add HTTP trigger | ||
| await FuncNewWithRetryAsync(testName, [".", "--template", "HttpTrigger", "--name", "HttpTrigger"]); | ||
|
|
||
| // Modify the function to log non-ASCII text | ||
| var csFilesPath = Path.Combine(WorkingDirectory, "HttpTrigger.cs"); | ||
| string originalContent = File.ReadAllText(csFilesPath); | ||
|
|
||
| // Find the Run method signature | ||
| var methodSignature = "IActionResult Run("; | ||
| int methodIndex = originalContent.IndexOf(methodSignature, StringComparison.Ordinal); | ||
| if (methodIndex >= 0) | ||
| { | ||
| // Find the first '{' after the method signature | ||
| int bodyStart = originalContent.IndexOf('{', methodIndex); | ||
| if (bodyStart >= 0) | ||
| { | ||
| bodyStart++; // Move past the '{' | ||
| string logStatement = $"\n _logger.LogInformation(\"Test String: {japaneseText}\");"; | ||
| string modifiedContent = originalContent.Insert(bodyStart, logStatement); | ||
| File.WriteAllText(csFilesPath, modifiedContent); | ||
| } | ||
| } | ||
|
|
||
| // Execute the function | ||
| var funcStartCommand = new FuncStartCommand(FuncPath, testName, Log); | ||
| funcStartCommand.ProcessStartedHandler = async (process) => | ||
| { | ||
| await ProcessHelper.ProcessStartedHandlerHelper(port, process, funcStartCommand.FileWriter ?? throw new ArgumentNullException(nameof(funcStartCommand.FileWriter)), "HttpTrigger?name=Test"); | ||
| }; | ||
|
|
||
| var result = funcStartCommand | ||
| .WithWorkingDirectory(WorkingDirectory) | ||
| .Execute(["--port", port.ToString()]); | ||
|
|
||
| // Verify the Japanese text was correctly displayed (not as question marks) | ||
| result.Should().HaveStdOutContaining($"Test String: {japaneseText}"); | ||
| result.Should().NotHaveStdOutContaining("Test String: ?????"); | ||
| } | ||
|
|
||
| [Fact] | ||
| [Trait(WorkerRuntimeTraits.WorkerRuntime, WorkerRuntimeTraits.Powershell)] | ||
| public async Task Start_WithPowerShell_WithNonAsciiLogging_DisplaysCorrectly() | ||
| { | ||
| var port = ProcessHelper.GetAvailablePort(); | ||
| var testName = nameof(Start_WithPowerShell_WithNonAsciiLogging_DisplaysCorrectly); | ||
| var japaneseText = "こんにちは"; | ||
|
|
||
| // Initialize PowerShell function app | ||
| await FuncInitWithRetryAsync(testName, [".", "--worker-runtime", "powershell"]); | ||
|
|
||
| // Add HTTP trigger | ||
| await FuncNewWithRetryAsync(testName, [".", "--template", "HttpTrigger", "--name", "HttpTrigger", "--language", "powershell"], workerRuntime: "powershell"); | ||
|
|
||
| // Modify the function to log non-ASCII text | ||
| string ps1FilePath = Path.Combine(WorkingDirectory, "HttpTrigger", "run.ps1"); | ||
| string originalContent = File.ReadAllText(ps1FilePath); | ||
|
|
||
| // Replace the existing log statement with both original and test log | ||
| string oldLogStatement = "Write-Host \"PowerShell HTTP trigger function processed a request.\""; | ||
| string newLogStatement = $"Write-Host \"PowerShell HTTP trigger function processed a request.\"\nWrite-Host \"Test String: {japaneseText}\""; | ||
| string modifiedContent = originalContent.Replace(oldLogStatement, newLogStatement); | ||
| File.WriteAllText(ps1FilePath, modifiedContent); | ||
|
|
||
| // Execute the function | ||
| var funcStartCommand = new FuncStartCommand(FuncPath, testName, Log); | ||
| funcStartCommand.ProcessStartedHandler = async (process) => | ||
| { | ||
| await ProcessHelper.ProcessStartedHandlerHelper(port, process, funcStartCommand.FileWriter ?? throw new ArgumentNullException(nameof(funcStartCommand.FileWriter)), "HttpTrigger?name=Test"); | ||
| }; | ||
|
|
||
| var result = funcStartCommand | ||
| .WithWorkingDirectory(WorkingDirectory) | ||
| .WithEnvironmentVariable(Common.Constants.FunctionsWorkerRuntime, "powershell") | ||
| .Execute(["--port", port.ToString()]); | ||
|
|
||
| // Verify the Japanese text was correctly displayed (not as question marks) | ||
| result.Should().HaveStdOutContaining($"Test String: {japaneseText}"); | ||
| result.Should().NotHaveStdOutContaining("Test String: ?????"); | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.