mirror of
https://github.com/Tyrrrz/CliFx.git
synced 2025-10-25 15:19:17 +00:00
Fail when there are no commands defined
This commit is contained in:
@@ -23,7 +23,7 @@ namespace CliFx.Tests
|
|||||||
}
|
}
|
||||||
|
|
||||||
[Command("faulty-command")]
|
[Command("faulty-command")]
|
||||||
private class FaultyCommand : ICommand
|
private class TestFaultyCommand : ICommand
|
||||||
{
|
{
|
||||||
public Task ExecuteAsync(CommandContext context) => Task.FromException(new CommandErrorException(-1337));
|
public Task ExecuteAsync(CommandContext context) => Task.FromException(new CommandErrorException(-1337));
|
||||||
}
|
}
|
||||||
@@ -86,6 +86,53 @@ namespace CliFx.Tests
|
|||||||
|
|
||||||
// Default command is not defined
|
// Default command is not defined
|
||||||
|
|
||||||
|
yield return new TestCaseData(
|
||||||
|
new[] {typeof(TestNamedCommand)},
|
||||||
|
new string[0]
|
||||||
|
);
|
||||||
|
|
||||||
|
yield return new TestCaseData(
|
||||||
|
new[] {typeof(TestNamedCommand)},
|
||||||
|
new[] {"--version"}
|
||||||
|
);
|
||||||
|
|
||||||
|
yield return new TestCaseData(
|
||||||
|
new[] {typeof(TestNamedCommand)},
|
||||||
|
new[] {"--help"}
|
||||||
|
);
|
||||||
|
|
||||||
|
yield return new TestCaseData(
|
||||||
|
new[] {typeof(TestNamedCommand)},
|
||||||
|
new[] {"-h"}
|
||||||
|
);
|
||||||
|
|
||||||
|
yield return new TestCaseData(
|
||||||
|
new[] {typeof(TestNamedCommand)},
|
||||||
|
new[] {"-?"}
|
||||||
|
);
|
||||||
|
|
||||||
|
// Specified a faulty command
|
||||||
|
|
||||||
|
yield return new TestCaseData(
|
||||||
|
new[] {typeof(TestFaultyCommand)},
|
||||||
|
new[] {"faulty-command", "--help"}
|
||||||
|
);
|
||||||
|
|
||||||
|
yield return new TestCaseData(
|
||||||
|
new[] {typeof(TestFaultyCommand)},
|
||||||
|
new[] {"faulty-command", "-h"}
|
||||||
|
);
|
||||||
|
|
||||||
|
yield return new TestCaseData(
|
||||||
|
new[] {typeof(TestFaultyCommand)},
|
||||||
|
new[] {"faulty-command", "-?"}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static IEnumerable<TestCaseData> GetTestCases_RunAsync_Negative()
|
||||||
|
{
|
||||||
|
// No commands defined
|
||||||
|
|
||||||
yield return new TestCaseData(
|
yield return new TestCaseData(
|
||||||
new Type[0],
|
new Type[0],
|
||||||
new string[0]
|
new string[0]
|
||||||
@@ -111,33 +158,6 @@ namespace CliFx.Tests
|
|||||||
new[] {"-?"}
|
new[] {"-?"}
|
||||||
);
|
);
|
||||||
|
|
||||||
// Specified a faulty command
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(FaultyCommand)},
|
|
||||||
new[] {"--version"}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(FaultyCommand)},
|
|
||||||
new[] {"--help"}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(FaultyCommand)},
|
|
||||||
new[] {"-h"}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(FaultyCommand)},
|
|
||||||
new[] {"-?"}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static IEnumerable<TestCaseData> GetTestCases_RunAsync_Negative()
|
|
||||||
{
|
|
||||||
// Specified command is not defined
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
yield return new TestCaseData(
|
||||||
new Type[0],
|
new Type[0],
|
||||||
new[] {"command"}
|
new[] {"command"}
|
||||||
@@ -145,23 +165,35 @@ namespace CliFx.Tests
|
|||||||
|
|
||||||
yield return new TestCaseData(
|
yield return new TestCaseData(
|
||||||
new Type[0],
|
new Type[0],
|
||||||
|
new[] {"faulty-command"}
|
||||||
|
);
|
||||||
|
|
||||||
|
// Specified command is not defined
|
||||||
|
|
||||||
|
yield return new TestCaseData(
|
||||||
|
new[] {typeof(TestDefaultCommand)},
|
||||||
|
new[] {"command"}
|
||||||
|
);
|
||||||
|
|
||||||
|
yield return new TestCaseData(
|
||||||
|
new[] {typeof(TestDefaultCommand)},
|
||||||
new[] {"command", "--help"}
|
new[] {"command", "--help"}
|
||||||
);
|
);
|
||||||
|
|
||||||
yield return new TestCaseData(
|
yield return new TestCaseData(
|
||||||
new Type[0],
|
new[] {typeof(TestDefaultCommand)},
|
||||||
new[] {"command", "-h"}
|
new[] {"command", "-h"}
|
||||||
);
|
);
|
||||||
|
|
||||||
yield return new TestCaseData(
|
yield return new TestCaseData(
|
||||||
new Type[0],
|
new[] {typeof(TestDefaultCommand)},
|
||||||
new[] {"command", "-?"}
|
new[] {"command", "-?"}
|
||||||
);
|
);
|
||||||
|
|
||||||
// Specified a faulty command
|
// Specified a faulty command
|
||||||
|
|
||||||
yield return new TestCaseData(
|
yield return new TestCaseData(
|
||||||
new[] {typeof(FaultyCommand)},
|
new[] {typeof(TestFaultyCommand)},
|
||||||
new[] {"faulty-command"}
|
new[] {"faulty-command"}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -55,13 +55,26 @@ namespace CliFx
|
|||||||
var availableCommandSchemas = _commandSchemaResolver.GetCommandSchemas(_commandTypes);
|
var availableCommandSchemas = _commandSchemaResolver.GetCommandSchemas(_commandTypes);
|
||||||
var matchingCommandSchema = availableCommandSchemas.FindByNameOrNull(commandInput.CommandName);
|
var matchingCommandSchema = availableCommandSchemas.FindByNameOrNull(commandInput.CommandName);
|
||||||
|
|
||||||
|
// Fail if there are no commands defined
|
||||||
|
if (!availableCommandSchemas.Any())
|
||||||
|
{
|
||||||
|
stdErr.WriteLine("There are no commands defined in this application.");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
// Fail if specified a command which is not defined
|
// Fail if specified a command which is not defined
|
||||||
if (commandInput.IsCommandSpecified() && matchingCommandSchema == null)
|
if (commandInput.IsCommandSpecified() && matchingCommandSchema == null)
|
||||||
{
|
{
|
||||||
stdErr.WriteLine($"Specified command [{commandInput.CommandName}] doesn't exist.");
|
stdErr.WriteLine($"Specified command [{commandInput.CommandName}] is not defined.");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Use a stub if command was not specified but there is no default command defined
|
||||||
|
if (matchingCommandSchema == null)
|
||||||
|
{
|
||||||
|
matchingCommandSchema = _commandSchemaResolver.GetCommandSchema(typeof(StubDefaultCommand));
|
||||||
|
}
|
||||||
|
|
||||||
// Show version if it was requested without specifying a command
|
// Show version if it was requested without specifying a command
|
||||||
if (commandInput.IsVersionRequested() && !commandInput.IsCommandSpecified())
|
if (commandInput.IsVersionRequested() && !commandInput.IsCommandSpecified())
|
||||||
{
|
{
|
||||||
@@ -70,12 +83,6 @@ namespace CliFx
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use a stub if command was not specified but there is no default command defined
|
|
||||||
if (matchingCommandSchema == null)
|
|
||||||
{
|
|
||||||
matchingCommandSchema = _commandSchemaResolver.GetCommandSchema(typeof(StubDefaultCommand));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Show help if it was requested
|
// Show help if it was requested
|
||||||
if (commandInput.IsHelpRequested())
|
if (commandInput.IsHelpRequested())
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user