Skip to content
Draft
Show file tree
Hide file tree
Changes from 2 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
7 changes: 7 additions & 0 deletions src/Cli/func/Actions/LocalActions/CreateFunctionAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check should happen much sooner and for all init and new. (Maybe in the GlobalSettings init method).

Scenarios to validate:

  • func init
  • func init --java
  • func init --worker-runtime java
  • func new
  • func new --java
  • func new --language 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");
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
}
else if (string.IsNullOrWhiteSpace(Language))
{
Expand Down
62 changes: 62 additions & 0 deletions test/Cli/Func.UnitTests/ActionsTests/CreateFunctionActionTests.cs
Original file line number Diff line number Diff line change
@@ -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<ITemplatesManager>();
var secretsManager = new Mock<ISecretsManager>();
var contextHelpManager = new Mock<IContextHelpManager>();
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);
}
}
}
}
}
Loading