From ca092858ee57c5bf325b640fb84dba58359954d5 Mon Sep 17 00:00:00 2001 From: satvu Date: Wed, 6 Aug 2025 11:44:22 -0700 Subject: [PATCH 1/4] initial commit --- .../LocalActions/CreateFunctionAction.cs | 7 +++ .../ActionsTests/CreateFunctionActionTests.cs | 62 +++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 test/Cli/Func.UnitTests/ActionsTests/CreateFunctionActionTests.cs diff --git a/src/Cli/func/Actions/LocalActions/CreateFunctionAction.cs b/src/Cli/func/Actions/LocalActions/CreateFunctionAction.cs index a3af8b424..26b8e7d9f 100644 --- a/src/Cli/func/Actions/LocalActions/CreateFunctionAction.cs +++ b/src/Cli/func/Actions/LocalActions/CreateFunctionAction.cs @@ -309,6 +309,13 @@ public async Task UpdateLanguageAndRuntime() throw new CliException("Selected language doesn't match worker set in local.settings.json." + $"Selected worker is: {_workerRuntime} and selected language is: {workerRuntimeSelected}"); } + + if (workerRuntimeSelected == WorkerRuntime.Java) + { + ColoredConsole + .WriteLine($"This action is not supported when using the core tools directly. Please use Maven to test {workerRuntimeSelected} apps locally as specified by" + + $"https://learn.microsoft.com/en-us/azure/azure-functions/functions-develop-local?pivots=programming-language-java"); + } } else if (string.IsNullOrWhiteSpace(Language)) { diff --git a/test/Cli/Func.UnitTests/ActionsTests/CreateFunctionActionTests.cs b/test/Cli/Func.UnitTests/ActionsTests/CreateFunctionActionTests.cs new file mode 100644 index 000000000..15d08dde7 --- /dev/null +++ b/test/Cli/Func.UnitTests/ActionsTests/CreateFunctionActionTests.cs @@ -0,0 +1,62 @@ +// 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.Actions.LocalActions; +using Azure.Functions.Cli.Helpers; +using Azure.Functions.Cli.Interfaces; +using Moq; +using Xunit; + +namespace Azure.Functions.Cli.UnitTests.ActionsTests +{ + public class CreateFunctionActionTests + { + [Fact] + public async Task UpdateLanguageAndRuntime_ThrowsCliException_ForJavaWorkerRuntime() + { + // Arrange + var templatesManager = new Mock(); + var secretsManager = new Mock(); + var contextHelpManager = new Mock(); + var action = new CreateFunctionAction(templatesManager.Object, secretsManager.Object, contextHelpManager.Object) + { + Language = "java" + }; + + // Set the current worker runtime to Java, mocking the behavior where func init has already been run + GlobalCoreToolsSettings.CurrentWorkerRuntime = WorkerRuntime.Java; + + // Ensure local.settings.json exists in the current directory + var localSettingsPath = Path.Combine(Environment.CurrentDirectory, "local.settings.json"); + File.WriteAllText(localSettingsPath, "{}"); // Write a minimal valid JSON + + // Simulate worker runtime is Java + typeof(CreateFunctionAction) + .GetField("_workerRuntime", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance) + .SetValue(action, WorkerRuntime.Java); + + var stringWriter = new StringWriter(); + var originalOut = Console.Out; + Console.SetOut(stringWriter); + + try + { + // make sure no error is thrown + await action.UpdateLanguageAndRuntime(); + + // Assert + var output = stringWriter.ToString(); + Assert.Contains("Please use Maven to test", output); + Assert.Contains("Java", output); + } + finally + { + Console.SetOut(originalOut); + if (File.Exists(localSettingsPath)) + { + File.Delete(localSettingsPath); + } + } + } + } +} From 03d9f9dcdebf100eb41cad467e5b331a8de39858 Mon Sep 17 00:00:00 2001 From: satvu Date: Wed, 6 Aug 2025 11:45:37 -0700 Subject: [PATCH 2/4] update message --- src/Cli/func/Actions/LocalActions/CreateFunctionAction.cs | 2 +- .../Func.UnitTests/ActionsTests/CreateFunctionActionTests.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Cli/func/Actions/LocalActions/CreateFunctionAction.cs b/src/Cli/func/Actions/LocalActions/CreateFunctionAction.cs index 26b8e7d9f..ebb61d9cd 100644 --- a/src/Cli/func/Actions/LocalActions/CreateFunctionAction.cs +++ b/src/Cli/func/Actions/LocalActions/CreateFunctionAction.cs @@ -313,7 +313,7 @@ public async Task UpdateLanguageAndRuntime() if (workerRuntimeSelected == WorkerRuntime.Java) { ColoredConsole - .WriteLine($"This action is not supported when using the core tools directly. Please use Maven to test {workerRuntimeSelected} apps locally as specified by" + + .WriteLine($"This action is not supported when using the core tools directly. Please use one of the supported environments to test {workerRuntimeSelected} apps locally as specified by" + $"https://learn.microsoft.com/en-us/azure/azure-functions/functions-develop-local?pivots=programming-language-java"); } } diff --git a/test/Cli/Func.UnitTests/ActionsTests/CreateFunctionActionTests.cs b/test/Cli/Func.UnitTests/ActionsTests/CreateFunctionActionTests.cs index 15d08dde7..0aa2b4cbd 100644 --- a/test/Cli/Func.UnitTests/ActionsTests/CreateFunctionActionTests.cs +++ b/test/Cli/Func.UnitTests/ActionsTests/CreateFunctionActionTests.cs @@ -46,7 +46,7 @@ public async Task UpdateLanguageAndRuntime_ThrowsCliException_ForJavaWorkerRunti // Assert var output = stringWriter.ToString(); - Assert.Contains("Please use Maven to test", output); + Assert.Contains("This action is not supported", output); Assert.Contains("Java", output); } finally From 82454b6feb22d5f45067df6d7f91f943f6da578a Mon Sep 17 00:00:00 2001 From: satvu Date: Wed, 6 Aug 2025 13:59:29 -0700 Subject: [PATCH 3/4] move error message location --- .../LocalActions/CreateFunctionAction.cs | 7 --- .../func/Helpers/GlobalCoreToolsSettings.cs | 9 ++- .../ActionsTests/CreateFunctionActionTests.cs | 62 ------------------- .../GlobalCoreToolsSettingsTests.cs | 43 +++++++++++++ 4 files changed, 51 insertions(+), 70 deletions(-) delete mode 100644 test/Cli/Func.UnitTests/ActionsTests/CreateFunctionActionTests.cs create mode 100644 test/Cli/Func.UnitTests/GlobalCoreToolsSettingsTests.cs diff --git a/src/Cli/func/Actions/LocalActions/CreateFunctionAction.cs b/src/Cli/func/Actions/LocalActions/CreateFunctionAction.cs index ebb61d9cd..a3af8b424 100644 --- a/src/Cli/func/Actions/LocalActions/CreateFunctionAction.cs +++ b/src/Cli/func/Actions/LocalActions/CreateFunctionAction.cs @@ -309,13 +309,6 @@ public async Task UpdateLanguageAndRuntime() throw new CliException("Selected language doesn't match worker set in local.settings.json." + $"Selected worker is: {_workerRuntime} and selected language is: {workerRuntimeSelected}"); } - - if (workerRuntimeSelected == WorkerRuntime.Java) - { - ColoredConsole - .WriteLine($"This action is not supported when using the core tools directly. Please use one of the supported environments to test {workerRuntimeSelected} apps locally as specified by" + - $"https://learn.microsoft.com/en-us/azure/azure-functions/functions-develop-local?pivots=programming-language-java"); - } } else if (string.IsNullOrWhiteSpace(Language)) { diff --git a/src/Cli/func/Helpers/GlobalCoreToolsSettings.cs b/src/Cli/func/Helpers/GlobalCoreToolsSettings.cs index 288b8d406..850b4941c 100644 --- a/src/Cli/func/Helpers/GlobalCoreToolsSettings.cs +++ b/src/Cli/func/Helpers/GlobalCoreToolsSettings.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// 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; @@ -99,6 +99,13 @@ public static void Init(ISecretsManager secretsManager, string[] args) { _currentWorkerRuntime = WorkerRuntime.None; } + + if (_currentWorkerRuntime == WorkerRuntime.Java) + { + ColoredConsole + .WriteLine(WarningColor($"This action is not supported when using the core tools directly. Please use one of the supported environments to test {_currentWorkerRuntime} apps locally as specified by" + + $"https://learn.microsoft.com/en-us/azure/azure-functions/functions-develop-local?pivots=programming-language-java")); + } } // Test helper method to set _currentWorkerRuntime for testing purpose diff --git a/test/Cli/Func.UnitTests/ActionsTests/CreateFunctionActionTests.cs b/test/Cli/Func.UnitTests/ActionsTests/CreateFunctionActionTests.cs deleted file mode 100644 index 0aa2b4cbd..000000000 --- a/test/Cli/Func.UnitTests/ActionsTests/CreateFunctionActionTests.cs +++ /dev/null @@ -1,62 +0,0 @@ -// 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.Actions.LocalActions; -using Azure.Functions.Cli.Helpers; -using Azure.Functions.Cli.Interfaces; -using Moq; -using Xunit; - -namespace Azure.Functions.Cli.UnitTests.ActionsTests -{ - public class CreateFunctionActionTests - { - [Fact] - public async Task UpdateLanguageAndRuntime_ThrowsCliException_ForJavaWorkerRuntime() - { - // Arrange - var templatesManager = new Mock(); - var secretsManager = new Mock(); - var contextHelpManager = new Mock(); - var action = new CreateFunctionAction(templatesManager.Object, secretsManager.Object, contextHelpManager.Object) - { - Language = "java" - }; - - // Set the current worker runtime to Java, mocking the behavior where func init has already been run - GlobalCoreToolsSettings.CurrentWorkerRuntime = WorkerRuntime.Java; - - // Ensure local.settings.json exists in the current directory - var localSettingsPath = Path.Combine(Environment.CurrentDirectory, "local.settings.json"); - File.WriteAllText(localSettingsPath, "{}"); // Write a minimal valid JSON - - // Simulate worker runtime is Java - typeof(CreateFunctionAction) - .GetField("_workerRuntime", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance) - .SetValue(action, WorkerRuntime.Java); - - var stringWriter = new StringWriter(); - var originalOut = Console.Out; - Console.SetOut(stringWriter); - - try - { - // make sure no error is thrown - await action.UpdateLanguageAndRuntime(); - - // Assert - var output = stringWriter.ToString(); - Assert.Contains("This action is not supported", output); - Assert.Contains("Java", output); - } - finally - { - Console.SetOut(originalOut); - if (File.Exists(localSettingsPath)) - { - File.Delete(localSettingsPath); - } - } - } - } -} diff --git a/test/Cli/Func.UnitTests/GlobalCoreToolsSettingsTests.cs b/test/Cli/Func.UnitTests/GlobalCoreToolsSettingsTests.cs new file mode 100644 index 000000000..e3fc54e61 --- /dev/null +++ b/test/Cli/Func.UnitTests/GlobalCoreToolsSettingsTests.cs @@ -0,0 +1,43 @@ +// 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.Helpers; +using Azure.Functions.Cli.Interfaces; +using Moq; +using Xunit; + +namespace Azure.Functions.Cli.UnitTests +{ + public class GlobalCoreToolsSettingsTests + { + [Fact] + public void Init_LogsWarning_ForJavaWorkerRuntime() + { + // Arrange + var secretsManager = new Mock(); + var args = new[] { "--java" }; + + var stringWriter = new StringWriter(); + var originalOut = Console.Out; + Console.SetOut(stringWriter); + + try + { + // Act + GlobalCoreToolsSettings.Init(secretsManager.Object, args); + + // Assert + var output = stringWriter.ToString(); + Assert.Contains("This action is not supported when using the core tools directly", output); + Assert.Contains("java", output, StringComparison.OrdinalIgnoreCase); + } + finally + { + Console.SetOut(originalOut); + + // Reset static state for other tests + GlobalCoreToolsSettings.SetWorkerRuntime(WorkerRuntime.None); + } + } + } +} From 37ea2e78026e14f60d2279bc5264e55ae5763c7d Mon Sep 17 00:00:00 2001 From: satvu Date: Wed, 6 Aug 2025 15:00:24 -0700 Subject: [PATCH 4/4] updates --- src/Cli/func/Helpers/GlobalCoreToolsSettings.cs | 2 +- test/Cli/Func.UnitTests/GlobalCoreToolsSettingsTests.cs | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/Cli/func/Helpers/GlobalCoreToolsSettings.cs b/src/Cli/func/Helpers/GlobalCoreToolsSettings.cs index 850b4941c..c39decbca 100644 --- a/src/Cli/func/Helpers/GlobalCoreToolsSettings.cs +++ b/src/Cli/func/Helpers/GlobalCoreToolsSettings.cs @@ -100,7 +100,7 @@ public static void Init(ISecretsManager secretsManager, string[] args) _currentWorkerRuntime = WorkerRuntime.None; } - if (_currentWorkerRuntime == WorkerRuntime.Java) + if (_currentWorkerRuntime == WorkerRuntime.Java || args.Any(arg => string.Equals(arg, WorkerRuntime.Java.ToString(), StringComparison.OrdinalIgnoreCase))) { ColoredConsole .WriteLine(WarningColor($"This action is not supported when using the core tools directly. Please use one of the supported environments to test {_currentWorkerRuntime} apps locally as specified by" + diff --git a/test/Cli/Func.UnitTests/GlobalCoreToolsSettingsTests.cs b/test/Cli/Func.UnitTests/GlobalCoreToolsSettingsTests.cs index e3fc54e61..c2c6f342b 100644 --- a/test/Cli/Func.UnitTests/GlobalCoreToolsSettingsTests.cs +++ b/test/Cli/Func.UnitTests/GlobalCoreToolsSettingsTests.cs @@ -10,12 +10,15 @@ namespace Azure.Functions.Cli.UnitTests { public class GlobalCoreToolsSettingsTests { - [Fact] - public void Init_LogsWarning_ForJavaWorkerRuntime() + [Theory] + [InlineData("init", "--java")] + [InlineData("init", "--worker-runtime", "java")] + [InlineData("new", "--java")] + [InlineData("new", "--language", "java")] + public void Init_LogsWarning_ForJavaWorkerRuntime(params string[] args) { // Arrange var secretsManager = new Mock(); - var args = new[] { "--java" }; var stringWriter = new StringWriter(); var originalOut = Console.Out;