Skip to content
Closed
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
4 changes: 2 additions & 2 deletions src/Cli/func/Actions/LocalActions/CreateFunctionAction.cs
Original file line number Diff line number Diff line change
@@ -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 System.Text.RegularExpressions;
Expand Down Expand Up @@ -110,9 +110,9 @@ public override async Task RunAsync()
return;
}

// Ensure that the _templates are loaded before we proceed
_templates = await _templatesManager.Templates;

// Ensure that the worker runtime and language are set.
Copy link
Member

Choose a reason for hiding this comment

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

The only changes in this PR is a comment, was there meant to be something else?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes Lilian, after reverting only this comment is the addition

Copy link
Member

Choose a reason for hiding this comment

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

Sorry I don't understand how this makes sense, what does a comment have to do with the original issue?

In the issue, you mention:

@liliankasem have checked the issue and debugged, found out that little line order change in the code to check the language and runtime been selected before creating any templates if we run 'func new' without 'func init' will resolve this issue by changing the line order of 116 prior to 114 - https://github.com/Azure/azure-functions-core-tools/blob/main/src/Cli/func/Actions/LocalActions/CreateFunctionAction.cs

This makes sense so I was expecting this PR to swap around these checks

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi @liliankasem ... the fix which was mentioned throwing build error for some test cases, so we are fixing it and will remove the unnecessary comments in the next fix shortly

await UpdateLanguageAndRuntime();

// Depends on UpdateLanguageAndRuntime to set 'Language'
Expand Down
12 changes: 12 additions & 0 deletions src/Cli/func/Actions/LocalActions/InitAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ private static string LanguageSelectionIfRelevant(WorkerRuntime workerRuntime)
|| workerRuntime == Helpers.WorkerRuntime.DotnetIsolated
|| workerRuntime == Helpers.WorkerRuntime.Dotnet)
{
DeleteOrOverwriteHostJsonIfOnlyFile();
if (WorkerRuntimeLanguageHelper.WorkerToSupportedLanguages.TryGetValue(workerRuntime, out IEnumerable<string> languages)
&& languages.Count() != 0)
{
Expand Down Expand Up @@ -643,5 +644,16 @@ private async Task ShowEolMessage()
// ignore. Failure to show the EOL message should not fail the init command.
}
}

public static void DeleteOrOverwriteHostJsonIfOnlyFile()
{
var currentDir = Environment.CurrentDirectory;
var files = Directory.GetFiles(currentDir);
if (files.Length == 1 && string.Equals(Path.GetFileName(files[0]), "host.json", StringComparison.OrdinalIgnoreCase))
{
// Delete the host.json file
File.Delete(files[0]);
}
}
}
}
51 changes: 51 additions & 0 deletions test/Cli/Func.UnitTests/InitActionHostJsonTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// 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 Xunit;

namespace Func.UnitTests.ActionsTests
{
public class InitActionHostJsonTests
{
private readonly string _testDir;

public InitActionHostJsonTests()
{
_testDir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(_testDir);
Directory.SetCurrentDirectory(_testDir);
}

[Fact]
public void DeleteOrOverwriteHostJsonIfOnlyFile_DeletesHostJson_WhenOnlyFilePresent()
{
// Arrange
var hostJsonPath = Path.Combine(_testDir, "host.json");
File.WriteAllText(hostJsonPath, "{ \"test\": true }");

// Act
InitAction.DeleteOrOverwriteHostJsonIfOnlyFile();

// Assert
Assert.False(File.Exists(hostJsonPath));
}

[Fact]
public void DeleteOrOverwriteHostJsonIfOnlyFile_DoesNotDeleteHostJson_WhenOtherFilesPresent()
{
// Arrange
var hostJsonPath = Path.Combine(_testDir, "host.json");
var otherFilePath = Path.Combine(_testDir, "other.txt");
File.WriteAllText(hostJsonPath, "{ \"test\": true }");
File.WriteAllText(otherFilePath, "other");

// Act
InitAction.DeleteOrOverwriteHostJsonIfOnlyFile();

// Assert
Assert.True(File.Exists(hostJsonPath));
Assert.True(File.Exists(otherFilePath));
}
}
}
Loading