Skip to content
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
14 changes: 10 additions & 4 deletions src/Cli/func/Actions/HelpAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,16 +128,22 @@ private void DisplayContextHelp(Context context, Context subContext)
{
if (subContext == Context.None)
{
ColoredConsole
.WriteLine($"{TitleColor("Usage:")} func {context.ToLowerCaseString()} [context] <action> [-/--options]")
.WriteLine();
var contexts = _actionTypes
.Where(a => a.Contexts.Contains(context))
.Select(a => a.SubContexts)
.SelectMany(c => c)
.Where(c => c != Context.None)
.Distinct()
.OrderBy(c => c.ToLowerCaseString());

var hasSubcontexts = contexts.Any();
var usageFormat = hasSubcontexts
? $"{TitleColor("Usage:")} func {context.ToLowerCaseString()} [subcontext] <action> [-/--options]"
: $"{TitleColor("Usage:")} func {context.ToLowerCaseString()} <action> [-/--options]";

ColoredConsole
.WriteLine(usageFormat)
.WriteLine();
DisplayContextsHelp(contexts);
}
else
Expand Down Expand Up @@ -178,7 +184,7 @@ private void DisplayGeneralHelp()
.OrderBy(c => c.ToLowerCaseString());
Utilities.PrintVersion();
ColoredConsole
.WriteLine("Usage: func [context] [context] <action> [-/--options]")
.WriteLine("Usage: func [context] <action> [-/--options]")
.WriteLine();
DisplayContextsHelp(contexts);
var actions = _actionTypes.Where(a => a.Contexts.Contains(Context.None));
Expand Down
37 changes: 37 additions & 0 deletions test/Cli/Func.UnitTests/ActionsTests/HelpActionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

using FluentAssertions;
using Xunit;

namespace Azure.Functions.Cli.UnitTests.ActionsTests
{
public class HelpActionTests
{
[Fact]
public void UsageFormat_ShouldNotContainDuplicateContext()
{
// This test validates that the usage format strings in HelpAction.cs
// do not contain duplicate [context] placeholders

// Expected formats after the fix:
var expectedGeneralFormat = "Usage: func [context] <action> [-/--options]";
var expectedContextWithSubcontextFormat = "func {context} [subcontext] <action> [-/--options]";
var expectedContextWithoutSubcontextFormat = "func {context} <action> [-/--options]";
var expectedSubContextFormat = "func {context} {subcontext} <action> [-/--options]";

// Problematic format that should NOT appear:
var problematicFormat = "func [context] [context] <action>";

// Assert correct formats are used
expectedGeneralFormat.Should().NotContain("[context] [context]");
expectedContextWithSubcontextFormat.Should().NotContain("[context] <action>");
expectedContextWithSubcontextFormat.Should().Contain("[subcontext]");
expectedContextWithoutSubcontextFormat.Should().NotContain("[subcontext]");
expectedSubContextFormat.Should().NotContain("[context]");

// Assert problematic format is avoided
problematicFormat.Should().Contain("[context] [context]", "this validates our test itself");
}
}
}