8000 Fix an issue with settings not being applied in CommandContext by s2quake · Pull Request #50 · s2quake/commands · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix an issue with settings not being applied in CommandContext #50

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

Merged
merged 2 commits into from
Jan 10, 2025
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
38 changes: 20 additions & 18 deletions src/JSSoft.Commands/CommandContextBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -291,19 +291,19 @@ string GetVersionNames()
}
}

protected virtual void OnHelpExecute(string[] args)
protected virtual void OnHelpExecute(string[] args, CommandSettings settings)
{
var items = args.Where(item => Settings.IsHelpArg(item) is false).ToArray();
var items = args.Where(item => settings.IsHelpArg(item) is false).ToArray();
var helpCommand = HelpCommand;
var invoker = new InternalCommandInvoker(helpCommand);
var invoker = new InternalCommandInvoker(helpCommand, settings);
invoker.Invoke(items);
}

protected virtual void OnVersionExecute(string[] args)
protected virtual void OnVersionExecute(string[] args, CommandSettings settings)
{
var items = args.Where(item => Settings.IsVersionArg(item) is false).ToArray();
var items = args.Where(item => settings.IsVersionArg(item) is false).ToArray();
var versionCommand = VersionCommand;
var invoker = new InternalCommandInvoker(versionCommand);
var invoker = new InternalCommandInvoker(versionCommand, settings);
invoker.Invoke(items);
}

Expand Down Expand Up @@ -411,21 +411,22 @@ where command.Parent is null
private void ExecuteInternal(string[] args)
{
var argList = new List<string>(args);
var settings = Settings;
if (CommandUtility.IsEmptyArgs(args) is true)
{
OnEmptyExecute();
}
else if (Settings.ContainsHelpOption(args) is true)
else if (settings.ContainsHelpOption(args) is true)
{
OnHelpExecute(args);
OnHelpExecute(args, settings);
}
else if (Settings.ContainsVersionOption(args) is true)
else if (settings.ContainsVersionOption(args) is true)
{
OnVersionExecute(args);
OnVersionExecute(args, settings);
}
else if (GetCommand(_commandNode, argList) is { } command)
{
var invoker = new InternalCommandInvoker(command);
var invoker = new InternalCommandInvoker(command, settings);
invoker.Invoke([.. argList]);
}
else
Expand All @@ -438,21 +439,22 @@ private async Task ExecuteInternalAsync(
string[] args, CancellationToken cancellationToken, IProgress<ProgressInfo> progress)
{
var argList = new List<string>(args);
var settings = Settings;
if (CommandUtility.IsEmptyArgs(args) is true)
{
OnEmptyExecute();
}
else if (Settings.ContainsHelpOption(args) is true)
else if (settings.ContainsHelpOption(args) is true)
{
OnHelpExecute(args);
OnHelpExecute(args, settings);
}
else if (Settings.ContainsVersionOption(args) is true)
else if (settings.ContainsVersionOption(args) is true)
{
OnVersionExecute(args);
OnVersionExecute(args, settings);
}
else if (GetCommand(_commandNode, argList) is { } command)
{
var invoker = new InternalCommandInvoker(command);
var invoker = new InternalCommandInvoker(command, settings);
await invoker.InvokeAsync([.. argList], cancellationToken, progress);
}
else
Expand All @@ -461,8 +463,8 @@ private async Task ExecuteInternalAsync(
}
}

private sealed class InternalCommandInvoker(ICommand command)
: CommandInvoker(command.Name, command)
private sealed class InternalCommandInvoker(ICommand command, CommandSettings settings)
: CommandInvoker(command.Name, command, settings)
{
protected override void OnVerify(string[] args)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class CommandAsICustomCommandDescriptor
public void Test1()
{
var runCommand = new RunCommand();
var commandContext = new TestCommandContext(runCommand);
var commandContext = new TestCommandContext([runCommand]);

commandContext.Execute("run --string1 value1 --string2 value2");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public void ExecutableNotImplemented_ThrowTest()
{
var customCommand = new CustomCommandWithoutExecutable();
Assert.Throws<CommandDefinitionException>(
() => new TestCommandContext(customCommand));
() => new TestCommandContext([customCommand]));
}

private sealed class CustomCommandWithoutExecutable : ICommand
Expand Down Expand Up @@ -52,7 +52,7 @@ private sealed class CustomCommandWithoutExecutable : ICommand
public void ExecutableImplemented_Test()
{
var customCommand = new CustomCommandWithExecutable();
var commandContext = new TestCommandContext(customCommand);
var commandContext = new TestCommandContext([customCommand]);
commandContext.Execute("test");

Assert.Equal(int.MaxValue, customCommand.Value);
Expand Down Expand Up @@ -99,7 +99,7 @@ public void Execute()
public async Task AsyncExecutableImplemented_TestAsync()
{
var customCommand = new CustomCommandWithAsyncExecutable();
var commandContext = new TestCommandContext(customCommand);
var commandContext = new TestCommandContext([customCommand]);
await commandContext.ExecuteAsync("test");

Assert.Equal(int.MaxValue, customCommand.Value);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// <copyright file="ServiceProviderTest.cs" company="JSSoft">
// Copyright (c) 2024 Jeesu Choi. All Rights Reserved.
// Licensed under the MIT License. See LICENSE.md in the project root for license information.
// </copyright>

using System.ComponentModel;
using System.Globalization;

namespace JSSoft.Commands.Tests.CommandContextTests;

public class ServiceProviderTest
{
[Fact]
public void Execute_Test()
{
var testCommand = new TestCommand();
var commands = new ICommand[]
{
testCommand,
};
var settings = new CommandSettings
{
ServiceProvider = new ServiceProvider(),
};
var commandContext = new TestCommandContext(settings, commands);

commandContext.Execute("test --data 10");

Assert.Equal(11, testCommand.Data.Value);
}

private sealed class TestCommand : CommandBase
{
[CommandProperty]
public Data Data { get; set; }

protected override void OnExecute()
{
// do nothing
}
}

[TypeConverter(typeof(DataTypeConverter))]
private struct Data
{
public int Value { get; set; }
}

private sealed class DataTypeConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType)
{
return sourceType == typeof(string);
}

public override object? ConvertFrom(
ITypeDescriptorContext? context, CultureInfo? culture, object value)
{
if (value is string text)
{
var i = int.Parse(text);
if (context is not null && context.GetService(typeof(int)) is int sum)
{
i += sum;
}

return new Data() { Value = i };
}

return base.ConvertFrom(context, culture, value);
}
}

private sealed class ServiceProvider : IServiceProvider
{
public object? GetService(Type serviceType)
{
if (serviceType == typeof(int))
{
return 1;
}

return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@

namespace JSSoft.Commands.Tests.CommandContextTests;

public class TestCommandContext(params ICommand[] commands)
: CommandContextBase(commands)
public class TestCommandContext(CommandSettings settings, ICommand[] commands)
: CommandContextBase(commands, settings)
{
public TestCommandContext(ICommand[] commands)
: this(CommandSettings.Default, commands)
{
}
}
Loading
0