mirror of
https://github.com/Tyrrrz/CliFx.git
synced 2025-10-25 15:19:17 +00:00
Use CSharpier
This commit is contained in:
@@ -11,6 +11,7 @@
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Basic.Reference.Assemblies.Net70" Version="1.4.2" />
|
||||
<PackageReference Include="coverlet.collector" Version="6.0.0" PrivateAssets="all" />
|
||||
<PackageReference Include="CSharpier.MsBuild" Version="0.25.0" PrivateAssets="all" />
|
||||
<PackageReference Include="GitHubActionsTestLogger" Version="2.3.2" PrivateAssets="all" />
|
||||
<PackageReference Include="FluentAssertions" Version="6.11.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.0" />
|
||||
|
||||
@@ -13,8 +13,7 @@ public class CommandMustBeAnnotatedAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
@@ -30,8 +29,7 @@ public class CommandMustBeAnnotatedAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public abstract class MyCommand : ICommand
|
||||
{
|
||||
@@ -48,8 +46,7 @@ public class CommandMustBeAnnotatedAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
public abstract class MyCommand : ICommand
|
||||
{
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
@@ -65,8 +62,7 @@ public class CommandMustBeAnnotatedAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
public class Foo
|
||||
{
|
||||
public int Bar { get; init; } = 5;
|
||||
|
||||
@@ -6,15 +6,15 @@ namespace CliFx.Analyzers.Tests;
|
||||
|
||||
public class CommandMustImplementInterfaceAnalyzerSpecs
|
||||
{
|
||||
private static DiagnosticAnalyzer Analyzer { get; } = new CommandMustImplementInterfaceAnalyzer();
|
||||
private static DiagnosticAnalyzer Analyzer { get; } =
|
||||
new CommandMustImplementInterfaceAnalyzer();
|
||||
|
||||
[Fact]
|
||||
public void Analyzer_reports_an_error_if_a_command_does_not_implement_ICommand_interface()
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public class MyCommand
|
||||
{
|
||||
@@ -31,8 +31,7 @@ public class CommandMustImplementInterfaceAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
@@ -49,8 +48,7 @@ public class CommandMustImplementInterfaceAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
public class Foo
|
||||
{
|
||||
public int Bar { get; init; } = 5;
|
||||
|
||||
@@ -12,8 +12,7 @@ public class GeneralSpecs
|
||||
public void All_analyzers_have_unique_diagnostic_IDs()
|
||||
{
|
||||
// Arrange
|
||||
var analyzers = typeof(AnalyzerBase)
|
||||
.Assembly
|
||||
var analyzers = typeof(AnalyzerBase).Assembly
|
||||
.GetTypes()
|
||||
.Where(t => !t.IsAbstract && t.IsAssignableTo(typeof(DiagnosticAnalyzer)))
|
||||
.Select(t => (DiagnosticAnalyzer)Activator.CreateInstance(t)!)
|
||||
|
||||
@@ -13,8 +13,7 @@ public class OptionMustBeInsideCommandAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
public class MyClass
|
||||
{
|
||||
[CommandOption("foo")]
|
||||
@@ -31,8 +30,7 @@ public class OptionMustBeInsideCommandAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
@@ -52,8 +50,7 @@ public class OptionMustBeInsideCommandAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
public abstract class MyCommand
|
||||
{
|
||||
[CommandOption("foo")]
|
||||
@@ -70,8 +67,7 @@ public class OptionMustBeInsideCommandAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
|
||||
@@ -6,15 +6,15 @@ namespace CliFx.Analyzers.Tests;
|
||||
|
||||
public class OptionMustBeRequiredIfPropertyRequiredAnalyzerSpecs
|
||||
{
|
||||
private static DiagnosticAnalyzer Analyzer { get; } = new OptionMustBeRequiredIfPropertyRequiredAnalyzer();
|
||||
private static DiagnosticAnalyzer Analyzer { get; } =
|
||||
new OptionMustBeRequiredIfPropertyRequiredAnalyzer();
|
||||
|
||||
[Fact]
|
||||
public void Analyzer_reports_an_error_if_a_non_required_option_is_bound_to_a_required_property()
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
@@ -34,8 +34,7 @@ public class OptionMustBeRequiredIfPropertyRequiredAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
@@ -55,8 +54,7 @@ public class OptionMustBeRequiredIfPropertyRequiredAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
@@ -76,8 +74,7 @@ public class OptionMustBeRequiredIfPropertyRequiredAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
@@ -97,8 +94,7 @@ public class OptionMustBeRequiredIfPropertyRequiredAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
|
||||
@@ -6,15 +6,15 @@ namespace CliFx.Analyzers.Tests;
|
||||
|
||||
public class OptionMustHaveNameOrShortNameAnalyzerSpecs
|
||||
{
|
||||
private static DiagnosticAnalyzer Analyzer { get; } = new OptionMustHaveNameOrShortNameAnalyzer();
|
||||
private static DiagnosticAnalyzer Analyzer { get; } =
|
||||
new OptionMustHaveNameOrShortNameAnalyzer();
|
||||
|
||||
[Fact]
|
||||
public void Analyzer_reports_an_error_if_an_option_does_not_have_a_name_or_short_name()
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
@@ -34,8 +34,7 @@ public class OptionMustHaveNameOrShortNameAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
@@ -55,8 +54,7 @@ public class OptionMustHaveNameOrShortNameAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
@@ -76,8 +74,7 @@ public class OptionMustHaveNameOrShortNameAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
|
||||
@@ -13,8 +13,7 @@ public class OptionMustHaveUniqueNameAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
@@ -37,8 +36,7 @@ public class OptionMustHaveUniqueNameAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
@@ -61,8 +59,7 @@ public class OptionMustHaveUniqueNameAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
@@ -82,8 +79,7 @@ public class OptionMustHaveUniqueNameAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
|
||||
@@ -6,15 +6,15 @@ namespace CliFx.Analyzers.Tests;
|
||||
|
||||
public class OptionMustHaveUniqueShortNameAnalyzerSpecs
|
||||
{
|
||||
private static DiagnosticAnalyzer Analyzer { get; } = new OptionMustHaveUniqueShortNameAnalyzer();
|
||||
private static DiagnosticAnalyzer Analyzer { get; } =
|
||||
new OptionMustHaveUniqueShortNameAnalyzer();
|
||||
|
||||
[Fact]
|
||||
public void Analyzer_reports_an_error_if_an_option_has_the_same_short_name_as_another_option()
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
@@ -37,8 +37,7 @@ public class OptionMustHaveUniqueShortNameAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
@@ -61,8 +60,7 @@ public class OptionMustHaveUniqueShortNameAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
@@ -85,8 +83,7 @@ public class OptionMustHaveUniqueShortNameAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
@@ -106,8 +103,7 @@ public class OptionMustHaveUniqueShortNameAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
|
||||
@@ -6,15 +6,15 @@ namespace CliFx.Analyzers.Tests;
|
||||
|
||||
public class OptionMustHaveValidConverterAnalyzerSpecs
|
||||
{
|
||||
private static DiagnosticAnalyzer Analyzer { get; } = new OptionMustHaveValidConverterAnalyzer();
|
||||
private static DiagnosticAnalyzer Analyzer { get; } =
|
||||
new OptionMustHaveValidConverterAnalyzer();
|
||||
|
||||
[Fact]
|
||||
public void Analyzer_reports_an_error_if_an_option_has_a_converter_that_does_not_derive_from_BindingConverter()
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
public class MyConverter
|
||||
{
|
||||
public string Convert(string? rawValue) => rawValue;
|
||||
@@ -39,8 +39,7 @@ public class OptionMustHaveValidConverterAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
public class MyConverter : BindingConverter<int>
|
||||
{
|
||||
public override int Convert(string? rawValue) => 42;
|
||||
@@ -65,8 +64,7 @@ public class OptionMustHaveValidConverterAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
public class MyConverter : BindingConverter<string>
|
||||
{
|
||||
public override string Convert(string? rawValue) => rawValue;
|
||||
@@ -91,8 +89,7 @@ public class OptionMustHaveValidConverterAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
public class MyConverter : BindingConverter<int>
|
||||
{
|
||||
public override int Convert(string? rawValue) => 42;
|
||||
@@ -117,8 +114,7 @@ public class OptionMustHaveValidConverterAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
public class MyConverter : BindingConverter<string>
|
||||
{
|
||||
public override string Convert(string? rawValue) => rawValue;
|
||||
@@ -143,8 +139,7 @@ public class OptionMustHaveValidConverterAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
@@ -164,8 +159,7 @@ public class OptionMustHaveValidConverterAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
|
||||
@@ -13,8 +13,7 @@ public class OptionMustHaveValidNameAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
@@ -34,8 +33,7 @@ public class OptionMustHaveValidNameAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
@@ -55,8 +53,7 @@ public class OptionMustHaveValidNameAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
@@ -76,8 +73,7 @@ public class OptionMustHaveValidNameAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
@@ -97,8 +93,7 @@ public class OptionMustHaveValidNameAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
|
||||
@@ -6,15 +6,15 @@ namespace CliFx.Analyzers.Tests;
|
||||
|
||||
public class OptionMustHaveValidShortNameAnalyzerSpecs
|
||||
{
|
||||
private static DiagnosticAnalyzer Analyzer { get; } = new OptionMustHaveValidShortNameAnalyzer();
|
||||
private static DiagnosticAnalyzer Analyzer { get; } =
|
||||
new OptionMustHaveValidShortNameAnalyzer();
|
||||
|
||||
[Fact]
|
||||
public void Analyzer_reports_an_error_if_an_option_has_a_short_name_which_is_not_a_letter_character()
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
@@ -34,8 +34,7 @@ public class OptionMustHaveValidShortNameAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
@@ -55,8 +54,7 @@ public class OptionMustHaveValidShortNameAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
@@ -76,8 +74,7 @@ public class OptionMustHaveValidShortNameAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
|
||||
@@ -6,15 +6,15 @@ namespace CliFx.Analyzers.Tests;
|
||||
|
||||
public class OptionMustHaveValidValidatorsAnalyzerSpecs
|
||||
{
|
||||
private static DiagnosticAnalyzer Analyzer { get; } = new OptionMustHaveValidValidatorsAnalyzer();
|
||||
private static DiagnosticAnalyzer Analyzer { get; } =
|
||||
new OptionMustHaveValidValidatorsAnalyzer();
|
||||
|
||||
[Fact]
|
||||
public void Analyzer_reports_an_error_if_an_option_has_a_validator_that_does_not_derive_from_BindingValidator()
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
public class MyValidator
|
||||
{
|
||||
public void Validate(string value) {}
|
||||
@@ -39,8 +39,7 @@ public class OptionMustHaveValidValidatorsAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
public class MyValidator : BindingValidator<int>
|
||||
{
|
||||
public override BindingValidationError Validate(int value) => Ok();
|
||||
@@ -65,8 +64,7 @@ public class OptionMustHaveValidValidatorsAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
public class MyValidator : BindingValidator<string>
|
||||
{
|
||||
public override BindingValidationError Validate(string value) => Ok();
|
||||
@@ -91,8 +89,7 @@ public class OptionMustHaveValidValidatorsAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
@@ -112,8 +109,7 @@ public class OptionMustHaveValidValidatorsAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
|
||||
@@ -6,15 +6,15 @@ namespace CliFx.Analyzers.Tests;
|
||||
|
||||
public class ParameterMustBeInsideCommandAnalyzerSpecs
|
||||
{
|
||||
private static DiagnosticAnalyzer Analyzer { get; } = new ParameterMustBeInsideCommandAnalyzer();
|
||||
private static DiagnosticAnalyzer Analyzer { get; } =
|
||||
new ParameterMustBeInsideCommandAnalyzer();
|
||||
|
||||
[Fact]
|
||||
public void Analyzer_reports_an_error_if_a_parameter_is_inside_a_class_that_is_not_a_command()
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
public class MyClass
|
||||
{
|
||||
[CommandParameter(0)]
|
||||
@@ -31,8 +31,7 @@ public class ParameterMustBeInsideCommandAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
@@ -52,8 +51,7 @@ public class ParameterMustBeInsideCommandAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
public abstract class MyCommand
|
||||
{
|
||||
[CommandParameter(0)]
|
||||
@@ -70,8 +68,7 @@ public class ParameterMustBeInsideCommandAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
|
||||
@@ -6,15 +6,15 @@ namespace CliFx.Analyzers.Tests;
|
||||
|
||||
public class ParameterMustBeLastIfNonRequiredAnalyzerSpecs
|
||||
{
|
||||
private static DiagnosticAnalyzer Analyzer { get; } = new ParameterMustBeLastIfNonRequiredAnalyzer();
|
||||
private static DiagnosticAnalyzer Analyzer { get; } =
|
||||
new ParameterMustBeLastIfNonRequiredAnalyzer();
|
||||
|
||||
[Fact]
|
||||
public void Analyzer_reports_an_error_if_a_non_required_parameter_is_not_the_last_in_order()
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
@@ -37,8 +37,7 @@ public class ParameterMustBeLastIfNonRequiredAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
@@ -61,8 +60,7 @@ public class ParameterMustBeLastIfNonRequiredAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
@@ -85,8 +83,7 @@ public class ParameterMustBeLastIfNonRequiredAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
|
||||
@@ -6,15 +6,15 @@ namespace CliFx.Analyzers.Tests;
|
||||
|
||||
public class ParameterMustBeLastIfNonScalarAnalyzerSpecs
|
||||
{
|
||||
private static DiagnosticAnalyzer Analyzer { get; } = new ParameterMustBeLastIfNonScalarAnalyzer();
|
||||
private static DiagnosticAnalyzer Analyzer { get; } =
|
||||
new ParameterMustBeLastIfNonScalarAnalyzer();
|
||||
|
||||
[Fact]
|
||||
public void Analyzer_reports_an_error_if_a_non_scalar_parameter_is_not_the_last_in_order()
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
@@ -37,8 +37,7 @@ public class ParameterMustBeLastIfNonScalarAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
@@ -61,8 +60,7 @@ public class ParameterMustBeLastIfNonScalarAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
@@ -85,8 +83,7 @@ public class ParameterMustBeLastIfNonScalarAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
|
||||
@@ -6,15 +6,15 @@ namespace CliFx.Analyzers.Tests;
|
||||
|
||||
public class ParameterMustBeRequiredIfPropertyRequiredAnalyzerSpecs
|
||||
{
|
||||
private static DiagnosticAnalyzer Analyzer { get; } = new ParameterMustBeRequiredIfPropertyRequiredAnalyzer();
|
||||
private static DiagnosticAnalyzer Analyzer { get; } =
|
||||
new ParameterMustBeRequiredIfPropertyRequiredAnalyzer();
|
||||
|
||||
[Fact]
|
||||
public void Analyzer_reports_an_error_if_a_non_required_parameter_is_bound_to_a_required_property()
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
@@ -34,8 +34,7 @@ public class ParameterMustBeRequiredIfPropertyRequiredAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
@@ -55,8 +54,7 @@ public class ParameterMustBeRequiredIfPropertyRequiredAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
@@ -76,8 +74,7 @@ public class ParameterMustBeRequiredIfPropertyRequiredAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
@@ -97,8 +94,7 @@ public class ParameterMustBeRequiredIfPropertyRequiredAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
|
||||
@@ -6,15 +6,15 @@ namespace CliFx.Analyzers.Tests;
|
||||
|
||||
public class ParameterMustBeSingleIfNonRequiredAnalyzerSpecs
|
||||
{
|
||||
private static DiagnosticAnalyzer Analyzer { get; } = new ParameterMustBeSingleIfNonRequiredAnalyzer();
|
||||
private static DiagnosticAnalyzer Analyzer { get; } =
|
||||
new ParameterMustBeSingleIfNonRequiredAnalyzer();
|
||||
|
||||
[Fact]
|
||||
public void Analyzer_reports_an_error_if_more_than_one_non_required_parameters_are_defined()
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
@@ -37,8 +37,7 @@ public class ParameterMustBeSingleIfNonRequiredAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
@@ -61,8 +60,7 @@ public class ParameterMustBeSingleIfNonRequiredAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
@@ -85,8 +83,7 @@ public class ParameterMustBeSingleIfNonRequiredAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
|
||||
@@ -6,15 +6,15 @@ namespace CliFx.Analyzers.Tests;
|
||||
|
||||
public class ParameterMustBeSingleIfNonScalarAnalyzerSpecs
|
||||
{
|
||||
private static DiagnosticAnalyzer Analyzer { get; } = new ParameterMustBeSingleIfNonScalarAnalyzer();
|
||||
private static DiagnosticAnalyzer Analyzer { get; } =
|
||||
new ParameterMustBeSingleIfNonScalarAnalyzer();
|
||||
|
||||
[Fact]
|
||||
public void Analyzer_reports_an_error_if_more_than_one_non_scalar_parameters_are_defined()
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
@@ -37,8 +37,7 @@ public class ParameterMustBeSingleIfNonScalarAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
@@ -61,8 +60,7 @@ public class ParameterMustBeSingleIfNonScalarAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
@@ -85,8 +83,7 @@ public class ParameterMustBeSingleIfNonScalarAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
|
||||
@@ -13,8 +13,7 @@ public class ParameterMustHaveUniqueNameAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
@@ -37,8 +36,7 @@ public class ParameterMustHaveUniqueNameAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
@@ -61,8 +59,7 @@ public class ParameterMustHaveUniqueNameAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
|
||||
@@ -6,15 +6,15 @@ namespace CliFx.Analyzers.Tests;
|
||||
|
||||
public class ParameterMustHaveUniqueOrderAnalyzerSpecs
|
||||
{
|
||||
private static DiagnosticAnalyzer Analyzer { get; } = new ParameterMustHaveUniqueOrderAnalyzer();
|
||||
private static DiagnosticAnalyzer Analyzer { get; } =
|
||||
new ParameterMustHaveUniqueOrderAnalyzer();
|
||||
|
||||
[Fact]
|
||||
public void Analyzer_reports_an_error_if_a_parameter_has_the_same_order_as_another_parameter()
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
@@ -37,8 +37,7 @@ public class ParameterMustHaveUniqueOrderAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
@@ -61,8 +60,7 @@ public class ParameterMustHaveUniqueOrderAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
|
||||
@@ -6,15 +6,15 @@ namespace CliFx.Analyzers.Tests;
|
||||
|
||||
public class ParameterMustHaveValidConverterAnalyzerSpecs
|
||||
{
|
||||
private static DiagnosticAnalyzer Analyzer { get; } = new ParameterMustHaveValidConverterAnalyzer();
|
||||
private static DiagnosticAnalyzer Analyzer { get; } =
|
||||
new ParameterMustHaveValidConverterAnalyzer();
|
||||
|
||||
[Fact]
|
||||
public void Analyzer_reports_an_error_if_a_parameter_has_a_converter_that_does_not_derive_from_BindingConverter()
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
public class MyConverter
|
||||
{
|
||||
public string Convert(string? rawValue) => rawValue;
|
||||
@@ -39,8 +39,7 @@ public class ParameterMustHaveValidConverterAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
public class MyConverter : BindingConverter<int>
|
||||
{
|
||||
public override int Convert(string? rawValue) => 42;
|
||||
@@ -56,7 +55,6 @@ public class ParameterMustHaveValidConverterAnalyzerSpecs
|
||||
}
|
||||
""";
|
||||
|
||||
|
||||
// Act & assert
|
||||
Analyzer.Should().ProduceDiagnostics(code);
|
||||
}
|
||||
@@ -66,8 +64,7 @@ public class ParameterMustHaveValidConverterAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
public class MyConverter : BindingConverter<string>
|
||||
{
|
||||
public override string Convert(string? rawValue) => rawValue;
|
||||
@@ -92,8 +89,7 @@ public class ParameterMustHaveValidConverterAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
public class MyConverter : BindingConverter<int>
|
||||
{
|
||||
public override int Convert(string? rawValue) => 42;
|
||||
@@ -118,8 +114,7 @@ public class ParameterMustHaveValidConverterAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
public class MyConverter : BindingConverter<string>
|
||||
{
|
||||
public override string Convert(string? rawValue) => rawValue;
|
||||
@@ -144,8 +139,7 @@ public class ParameterMustHaveValidConverterAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
@@ -165,8 +159,7 @@ public class ParameterMustHaveValidConverterAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
|
||||
@@ -6,15 +6,15 @@ namespace CliFx.Analyzers.Tests;
|
||||
|
||||
public class ParameterMustHaveValidValidatorsAnalyzerSpecs
|
||||
{
|
||||
private static DiagnosticAnalyzer Analyzer { get; } = new ParameterMustHaveValidValidatorsAnalyzer();
|
||||
private static DiagnosticAnalyzer Analyzer { get; } =
|
||||
new ParameterMustHaveValidValidatorsAnalyzer();
|
||||
|
||||
[Fact]
|
||||
public void Analyzer_reports_an_error_a_parameter_has_a_validator_that_does_not_derive_from_BindingValidator()
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
public class MyValidator
|
||||
{
|
||||
public void Validate(string value) {}
|
||||
@@ -39,8 +39,7 @@ public class ParameterMustHaveValidValidatorsAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
public class MyValidator : BindingValidator<int>
|
||||
{
|
||||
public override BindingValidationError Validate(int value) => Ok();
|
||||
@@ -65,8 +64,7 @@ public class ParameterMustHaveValidValidatorsAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
public class MyValidator : BindingValidator<string>
|
||||
{
|
||||
public override BindingValidationError Validate(string value) => Ok();
|
||||
@@ -91,8 +89,7 @@ public class ParameterMustHaveValidValidatorsAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
@@ -112,8 +109,7 @@ public class ParameterMustHaveValidValidatorsAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
|
||||
@@ -6,15 +6,15 @@ namespace CliFx.Analyzers.Tests;
|
||||
|
||||
public class SystemConsoleShouldBeAvoidedAnalyzerSpecs
|
||||
{
|
||||
private static DiagnosticAnalyzer Analyzer { get; } = new SystemConsoleShouldBeAvoidedAnalyzer();
|
||||
private static DiagnosticAnalyzer Analyzer { get; } =
|
||||
new SystemConsoleShouldBeAvoidedAnalyzer();
|
||||
|
||||
[Fact]
|
||||
public void Analyzer_reports_an_error_if_a_command_calls_a_method_on_SystemConsole()
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
@@ -35,8 +35,7 @@ public class SystemConsoleShouldBeAvoidedAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
@@ -57,8 +56,7 @@ public class SystemConsoleShouldBeAvoidedAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
@@ -79,8 +77,7 @@ public class SystemConsoleShouldBeAvoidedAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
@@ -101,8 +98,7 @@ public class SystemConsoleShouldBeAvoidedAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
@@ -121,8 +117,7 @@ public class SystemConsoleShouldBeAvoidedAnalyzerSpecs
|
||||
{
|
||||
// Arrange
|
||||
// lang=csharp
|
||||
const string code =
|
||||
"""
|
||||
const string code = """
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
|
||||
@@ -18,9 +18,7 @@ internal class AnalyzerAssertions : ReferenceTypeAssertions<DiagnosticAnalyzer,
|
||||
protected override string Identifier { get; } = "analyzer";
|
||||
|
||||
public AnalyzerAssertions(DiagnosticAnalyzer analyzer)
|
||||
: base(analyzer)
|
||||
{
|
||||
}
|
||||
: base(analyzer) { }
|
||||
|
||||
private Compilation Compile(string sourceCode)
|
||||
{
|
||||
@@ -33,8 +31,7 @@ internal class AnalyzerAssertions : ReferenceTypeAssertions<DiagnosticAnalyzer,
|
||||
};
|
||||
|
||||
// Get default CliFx namespaces
|
||||
var defaultCliFxNamespaces = typeof(ICommand)
|
||||
.Assembly
|
||||
var defaultCliFxNamespaces = typeof(ICommand).Assembly
|
||||
.GetTypes()
|
||||
.Where(t => t.IsPublic)
|
||||
.Select(t => t.Namespace)
|
||||
@@ -43,10 +40,10 @@ internal class AnalyzerAssertions : ReferenceTypeAssertions<DiagnosticAnalyzer,
|
||||
|
||||
// Append default imports to the source code
|
||||
var sourceCodeWithUsings =
|
||||
string.Join(Environment.NewLine, defaultSystemNamespaces.Select(n => $"using {n};")) +
|
||||
string.Join(Environment.NewLine, defaultCliFxNamespaces.Select(n => $"using {n};")) +
|
||||
Environment.NewLine +
|
||||
sourceCode;
|
||||
string.Join(Environment.NewLine, defaultSystemNamespaces.Select(n => $"using {n};"))
|
||||
+ string.Join(Environment.NewLine, defaultCliFxNamespaces.Select(n => $"using {n};"))
|
||||
+ Environment.NewLine
|
||||
+ sourceCode;
|
||||
|
||||
// Parse the source code
|
||||
var ast = SyntaxFactory.ParseSyntaxTree(
|
||||
@@ -58,8 +55,9 @@ internal class AnalyzerAssertions : ReferenceTypeAssertions<DiagnosticAnalyzer,
|
||||
var compilation = CSharpCompilation.Create(
|
||||
"CliFxTests_DynamicAssembly_" + Guid.NewGuid(),
|
||||
new[] { ast },
|
||||
Net70.References.All
|
||||
.Append(MetadataReference.CreateFromFile(typeof(ICommand).Assembly.Location)),
|
||||
Net70.References.All.Append(
|
||||
MetadataReference.CreateFromFile(typeof(ICommand).Assembly.Location)
|
||||
),
|
||||
// DLL to avoid having to define the Main() method
|
||||
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)
|
||||
);
|
||||
@@ -103,10 +101,12 @@ internal class AnalyzerAssertions : ReferenceTypeAssertions<DiagnosticAnalyzer,
|
||||
var producedDiagnosticIds = producedDiagnostics.Select(d => d.Id).Distinct().ToArray();
|
||||
|
||||
var isSuccessfulAssertion =
|
||||
expectedDiagnosticIds.Intersect(producedDiagnosticIds).Count() ==
|
||||
expectedDiagnosticIds.Length;
|
||||
expectedDiagnosticIds.Intersect(producedDiagnosticIds).Count()
|
||||
== expectedDiagnosticIds.Length;
|
||||
|
||||
Execute.Assertion.ForCondition(isSuccessfulAssertion).FailWith(() =>
|
||||
Execute.Assertion
|
||||
.ForCondition(isSuccessfulAssertion)
|
||||
.FailWith(() =>
|
||||
{
|
||||
var buffer = new StringBuilder();
|
||||
|
||||
@@ -148,7 +148,9 @@ internal class AnalyzerAssertions : ReferenceTypeAssertions<DiagnosticAnalyzer,
|
||||
var producedDiagnostics = GetProducedDiagnostics(sourceCode);
|
||||
var isSuccessfulAssertion = !producedDiagnostics.Any();
|
||||
|
||||
Execute.Assertion.ForCondition(isSuccessfulAssertion).FailWith(() =>
|
||||
Execute.Assertion
|
||||
.ForCondition(isSuccessfulAssertion)
|
||||
.FailWith(() =>
|
||||
{
|
||||
var buffer = new StringBuilder();
|
||||
|
||||
|
||||
@@ -14,7 +14,8 @@ public abstract class AnalyzerBase : DiagnosticAnalyzer
|
||||
protected AnalyzerBase(
|
||||
string diagnosticTitle,
|
||||
string diagnosticMessage,
|
||||
DiagnosticSeverity diagnosticSeverity = DiagnosticSeverity.Error)
|
||||
DiagnosticSeverity diagnosticSeverity = DiagnosticSeverity.Error
|
||||
)
|
||||
{
|
||||
SupportedDiagnostic = new DiagnosticDescriptor(
|
||||
"CliFx_" + GetType().Name.TrimEnd("Analyzer"),
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="CSharpier.MsBuild" Version="0.25.0" PrivateAssets="all" />
|
||||
<!-- Make sure to target the lowest possible version of the compiler for wider support -->
|
||||
<PackageReference Include="Microsoft.CodeAnalysis" Version="3.0.0" PrivateAssets="all" />
|
||||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.0.0" PrivateAssets="all" />
|
||||
|
||||
@@ -13,14 +13,14 @@ public class CommandMustBeAnnotatedAnalyzer : AnalyzerBase
|
||||
public CommandMustBeAnnotatedAnalyzer()
|
||||
: base(
|
||||
$"Commands must be annotated with `{SymbolNames.CliFxCommandAttribute}`",
|
||||
$"This type must be annotated with `{SymbolNames.CliFxCommandAttribute}` in order to be a valid command.")
|
||||
{
|
||||
}
|
||||
$"This type must be annotated with `{SymbolNames.CliFxCommandAttribute}` in order to be a valid command."
|
||||
) { }
|
||||
|
||||
private void Analyze(
|
||||
SyntaxNodeAnalysisContext context,
|
||||
ClassDeclarationSyntax classDeclaration,
|
||||
ITypeSymbol type)
|
||||
ITypeSymbol type
|
||||
)
|
||||
{
|
||||
// Ignore abstract classes, because they may be used to define
|
||||
// base implementations for commands, in which case the command
|
||||
@@ -28,12 +28,11 @@ public class CommandMustBeAnnotatedAnalyzer : AnalyzerBase
|
||||
if (type.IsAbstract)
|
||||
return;
|
||||
|
||||
var implementsCommandInterface = type
|
||||
.AllInterfaces
|
||||
.Any(i => i.DisplayNameMatches(SymbolNames.CliFxCommandInterface));
|
||||
var implementsCommandInterface = type.AllInterfaces.Any(
|
||||
i => i.DisplayNameMatches(SymbolNames.CliFxCommandInterface)
|
||||
);
|
||||
|
||||
var hasCommandAttribute = type
|
||||
.GetAttributes()
|
||||
var hasCommandAttribute = type.GetAttributes()
|
||||
.Select(a => a.AttributeClass)
|
||||
.Any(c => c.DisplayNameMatches(SymbolNames.CliFxCommandAttribute));
|
||||
|
||||
@@ -41,9 +40,7 @@ public class CommandMustBeAnnotatedAnalyzer : AnalyzerBase
|
||||
// then it's very likely a user error.
|
||||
if (implementsCommandInterface && !hasCommandAttribute)
|
||||
{
|
||||
context.ReportDiagnostic(
|
||||
CreateDiagnostic(classDeclaration.Identifier.GetLocation())
|
||||
);
|
||||
context.ReportDiagnostic(CreateDiagnostic(classDeclaration.Identifier.GetLocation()));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -13,31 +13,28 @@ public class CommandMustImplementInterfaceAnalyzer : AnalyzerBase
|
||||
public CommandMustImplementInterfaceAnalyzer()
|
||||
: base(
|
||||
$"Commands must implement `{SymbolNames.CliFxCommandInterface}` interface",
|
||||
$"This type must implement `{SymbolNames.CliFxCommandInterface}` interface in order to be a valid command.")
|
||||
{
|
||||
}
|
||||
$"This type must implement `{SymbolNames.CliFxCommandInterface}` interface in order to be a valid command."
|
||||
) { }
|
||||
|
||||
private void Analyze(
|
||||
SyntaxNodeAnalysisContext context,
|
||||
ClassDeclarationSyntax classDeclaration,
|
||||
ITypeSymbol type)
|
||||
ITypeSymbol type
|
||||
)
|
||||
{
|
||||
var hasCommandAttribute = type
|
||||
.GetAttributes()
|
||||
var hasCommandAttribute = type.GetAttributes()
|
||||
.Select(a => a.AttributeClass)
|
||||
.Any(c => c.DisplayNameMatches(SymbolNames.CliFxCommandAttribute));
|
||||
|
||||
var implementsCommandInterface = type
|
||||
.AllInterfaces
|
||||
.Any(i => i.DisplayNameMatches(SymbolNames.CliFxCommandInterface));
|
||||
var implementsCommandInterface = type.AllInterfaces.Any(
|
||||
i => i.DisplayNameMatches(SymbolNames.CliFxCommandInterface)
|
||||
);
|
||||
|
||||
// If the attribute is present, but the interface is not implemented,
|
||||
// it's very likely a user error.
|
||||
if (hasCommandAttribute && !implementsCommandInterface)
|
||||
{
|
||||
context.ReportDiagnostic(
|
||||
CreateDiagnostic(classDeclaration.Identifier.GetLocation())
|
||||
);
|
||||
context.ReportDiagnostic(CreateDiagnostic(classDeclaration.Identifier.GetLocation()));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -25,7 +25,8 @@ internal partial class CommandOptionSymbol : ICommandMemberSymbol
|
||||
char? shortName,
|
||||
bool? isRequired,
|
||||
ITypeSymbol? converterType,
|
||||
IReadOnlyList<ITypeSymbol> validatorTypes)
|
||||
IReadOnlyList<ITypeSymbol> validatorTypes
|
||||
)
|
||||
{
|
||||
Property = property;
|
||||
Name = name;
|
||||
@@ -38,9 +39,14 @@ internal partial class CommandOptionSymbol : ICommandMemberSymbol
|
||||
|
||||
internal partial class CommandOptionSymbol
|
||||
{
|
||||
private static AttributeData? TryGetOptionAttribute(IPropertySymbol property) => property
|
||||
private static AttributeData? TryGetOptionAttribute(IPropertySymbol property) =>
|
||||
property
|
||||
.GetAttributes()
|
||||
.FirstOrDefault(a => a.AttributeClass?.DisplayNameMatches(SymbolNames.CliFxCommandOptionAttribute) == true);
|
||||
.FirstOrDefault(
|
||||
a =>
|
||||
a.AttributeClass?.DisplayNameMatches(SymbolNames.CliFxCommandOptionAttribute)
|
||||
== true
|
||||
);
|
||||
|
||||
public static CommandOptionSymbol? TryResolve(IPropertySymbol property)
|
||||
{
|
||||
@@ -48,40 +54,45 @@ internal partial class CommandOptionSymbol
|
||||
if (attribute is null)
|
||||
return null;
|
||||
|
||||
var name = attribute
|
||||
.ConstructorArguments
|
||||
var name =
|
||||
attribute.ConstructorArguments
|
||||
.Where(a => a.Type?.SpecialType == SpecialType.System_String)
|
||||
.Select(a => a.Value)
|
||||
.FirstOrDefault() as string;
|
||||
|
||||
var shortName = attribute
|
||||
.ConstructorArguments
|
||||
var shortName =
|
||||
attribute.ConstructorArguments
|
||||
.Where(a => a.Type?.SpecialType == SpecialType.System_Char)
|
||||
.Select(a => a.Value)
|
||||
.FirstOrDefault() as char?;
|
||||
|
||||
var isRequired = attribute
|
||||
.NamedArguments
|
||||
var isRequired =
|
||||
attribute.NamedArguments
|
||||
.Where(a => a.Key == "IsRequired")
|
||||
.Select(a => a.Value.Value)
|
||||
.FirstOrDefault() as bool?;
|
||||
|
||||
var converter = attribute
|
||||
.NamedArguments
|
||||
var converter = attribute.NamedArguments
|
||||
.Where(a => a.Key == "Converter")
|
||||
.Select(a => a.Value.Value)
|
||||
.Cast<ITypeSymbol?>()
|
||||
.FirstOrDefault();
|
||||
|
||||
var validators = attribute
|
||||
.NamedArguments
|
||||
var validators = attribute.NamedArguments
|
||||
.Where(a => a.Key == "Validators")
|
||||
.SelectMany(a => a.Value.Values)
|
||||
.Select(c => c.Value)
|
||||
.Cast<ITypeSymbol>()
|
||||
.ToArray();
|
||||
|
||||
return new CommandOptionSymbol(property, name, shortName, isRequired, converter, validators);
|
||||
return new CommandOptionSymbol(
|
||||
property,
|
||||
name,
|
||||
shortName,
|
||||
isRequired,
|
||||
converter,
|
||||
validators
|
||||
);
|
||||
}
|
||||
|
||||
public static bool IsOptionProperty(IPropertySymbol property) =>
|
||||
|
||||
@@ -25,7 +25,8 @@ internal partial class CommandParameterSymbol : ICommandMemberSymbol
|
||||
string? name,
|
||||
bool? isRequired,
|
||||
ITypeSymbol? converterType,
|
||||
IReadOnlyList<ITypeSymbol> validatorTypes)
|
||||
IReadOnlyList<ITypeSymbol> validatorTypes
|
||||
)
|
||||
{
|
||||
Property = property;
|
||||
Order = order;
|
||||
@@ -38,9 +39,14 @@ internal partial class CommandParameterSymbol : ICommandMemberSymbol
|
||||
|
||||
internal partial class CommandParameterSymbol
|
||||
{
|
||||
private static AttributeData? TryGetParameterAttribute(IPropertySymbol property) => property
|
||||
private static AttributeData? TryGetParameterAttribute(IPropertySymbol property) =>
|
||||
property
|
||||
.GetAttributes()
|
||||
.FirstOrDefault(a => a.AttributeClass?.DisplayNameMatches(SymbolNames.CliFxCommandParameterAttribute) == true);
|
||||
.FirstOrDefault(
|
||||
a =>
|
||||
a.AttributeClass?.DisplayNameMatches(SymbolNames.CliFxCommandParameterAttribute)
|
||||
== true
|
||||
);
|
||||
|
||||
public static CommandParameterSymbol? TryResolve(IPropertySymbol property)
|
||||
{
|
||||
@@ -48,32 +54,27 @@ internal partial class CommandParameterSymbol
|
||||
if (attribute is null)
|
||||
return null;
|
||||
|
||||
var order = (int)attribute
|
||||
.ConstructorArguments
|
||||
.Select(a => a.Value)
|
||||
.First()!;
|
||||
var order = (int)attribute.ConstructorArguments.Select(a => a.Value).First()!;
|
||||
|
||||
var name = attribute
|
||||
.NamedArguments
|
||||
var name =
|
||||
attribute.NamedArguments
|
||||
.Where(a => a.Key == "Name")
|
||||
.Select(a => a.Value.Value)
|
||||
.FirstOrDefault() as string;
|
||||
|
||||
var isRequired = attribute
|
||||
.NamedArguments
|
||||
var isRequired =
|
||||
attribute.NamedArguments
|
||||
.Where(a => a.Key == "IsRequired")
|
||||
.Select(a => a.Value.Value)
|
||||
.FirstOrDefault() as bool?;
|
||||
|
||||
var converter = attribute
|
||||
.NamedArguments
|
||||
var converter = attribute.NamedArguments
|
||||
.Where(a => a.Key == "Converter")
|
||||
.Select(a => a.Value.Value)
|
||||
.Cast<ITypeSymbol?>()
|
||||
.FirstOrDefault();
|
||||
|
||||
var validators = attribute
|
||||
.NamedArguments
|
||||
var validators = attribute.NamedArguments
|
||||
.Where(a => a.Key == "Validators")
|
||||
.SelectMany(a => a.Value.Values)
|
||||
.Select(c => c.Value)
|
||||
|
||||
@@ -16,6 +16,6 @@ internal interface ICommandMemberSymbol
|
||||
internal static class CommandMemberSymbolExtensions
|
||||
{
|
||||
public static bool IsScalar(this ICommandMemberSymbol member) =>
|
||||
member.Property.Type.SpecialType == SpecialType.System_String ||
|
||||
member.Property.Type.TryGetEnumerableUnderlyingType() is null;
|
||||
member.Property.Type.SpecialType == SpecialType.System_String
|
||||
|| member.Property.Type.TryGetEnumerableUnderlyingType() is null;
|
||||
}
|
||||
@@ -4,7 +4,8 @@ internal static class SymbolNames
|
||||
{
|
||||
public const string CliFxCommandInterface = "CliFx.ICommand";
|
||||
public const string CliFxCommandAttribute = "CliFx.Attributes.CommandAttribute";
|
||||
public const string CliFxCommandParameterAttribute = "CliFx.Attributes.CommandParameterAttribute";
|
||||
public const string CliFxCommandParameterAttribute =
|
||||
"CliFx.Attributes.CommandParameterAttribute";
|
||||
public const string CliFxCommandOptionAttribute = "CliFx.Attributes.CommandOptionAttribute";
|
||||
public const string CliFxConsoleInterface = "CliFx.Infrastructure.IConsole";
|
||||
public const string CliFxBindingConverterClass = "CliFx.Extensibility.BindingConverter<T>";
|
||||
|
||||
@@ -13,14 +13,14 @@ public class OptionMustBeInsideCommandAnalyzer : AnalyzerBase
|
||||
public OptionMustBeInsideCommandAnalyzer()
|
||||
: base(
|
||||
"Options must be defined inside commands",
|
||||
$"This option must be defined inside a class that implements `{SymbolNames.CliFxCommandInterface}`.")
|
||||
{
|
||||
}
|
||||
$"This option must be defined inside a class that implements `{SymbolNames.CliFxCommandInterface}`."
|
||||
) { }
|
||||
|
||||
private void Analyze(
|
||||
SyntaxNodeAnalysisContext context,
|
||||
PropertyDeclarationSyntax propertyDeclaration,
|
||||
IPropertySymbol property)
|
||||
IPropertySymbol property
|
||||
)
|
||||
{
|
||||
if (property.ContainingType is null)
|
||||
return;
|
||||
@@ -31,10 +31,9 @@ public class OptionMustBeInsideCommandAnalyzer : AnalyzerBase
|
||||
if (!CommandOptionSymbol.IsOptionProperty(property))
|
||||
return;
|
||||
|
||||
var isInsideCommand = property
|
||||
.ContainingType
|
||||
.AllInterfaces
|
||||
.Any(i => i.DisplayNameMatches(SymbolNames.CliFxCommandInterface));
|
||||
var isInsideCommand = property.ContainingType.AllInterfaces.Any(
|
||||
i => i.DisplayNameMatches(SymbolNames.CliFxCommandInterface)
|
||||
);
|
||||
|
||||
if (!isInsideCommand)
|
||||
{
|
||||
|
||||
@@ -12,14 +12,14 @@ public class OptionMustBeRequiredIfPropertyRequiredAnalyzer : AnalyzerBase
|
||||
public OptionMustBeRequiredIfPropertyRequiredAnalyzer()
|
||||
: base(
|
||||
"Options bound to required properties cannot be marked as non-required",
|
||||
"This option cannot be marked as non-required because it's bound to a required property.")
|
||||
{
|
||||
}
|
||||
"This option cannot be marked as non-required because it's bound to a required property."
|
||||
) { }
|
||||
|
||||
private void Analyze(
|
||||
SyntaxNodeAnalysisContext context,
|
||||
PropertyDeclarationSyntax propertyDeclaration,
|
||||
IPropertySymbol property)
|
||||
IPropertySymbol property
|
||||
)
|
||||
{
|
||||
if (property.ContainingType is null)
|
||||
return;
|
||||
@@ -34,11 +34,7 @@ public class OptionMustBeRequiredIfPropertyRequiredAnalyzer : AnalyzerBase
|
||||
if (option.IsRequired != false)
|
||||
return;
|
||||
|
||||
context.ReportDiagnostic(
|
||||
CreateDiagnostic(
|
||||
propertyDeclaration.Identifier.GetLocation()
|
||||
)
|
||||
);
|
||||
context.ReportDiagnostic(CreateDiagnostic(propertyDeclaration.Identifier.GetLocation()));
|
||||
}
|
||||
|
||||
public override void Initialize(AnalysisContext context)
|
||||
|
||||
@@ -12,14 +12,14 @@ public class OptionMustHaveNameOrShortNameAnalyzer : AnalyzerBase
|
||||
public OptionMustHaveNameOrShortNameAnalyzer()
|
||||
: base(
|
||||
"Options must have either a name or short name specified",
|
||||
"This option must have either a name or short name specified.")
|
||||
{
|
||||
}
|
||||
"This option must have either a name or short name specified."
|
||||
) { }
|
||||
|
||||
private void Analyze(
|
||||
SyntaxNodeAnalysisContext context,
|
||||
PropertyDeclarationSyntax propertyDeclaration,
|
||||
IPropertySymbol property)
|
||||
IPropertySymbol property
|
||||
)
|
||||
{
|
||||
var option = CommandOptionSymbol.TryResolve(property);
|
||||
if (option is null)
|
||||
|
||||
@@ -14,16 +14,16 @@ public class OptionMustHaveUniqueNameAnalyzer : AnalyzerBase
|
||||
public OptionMustHaveUniqueNameAnalyzer()
|
||||
: base(
|
||||
"Options must have unique names",
|
||||
"This option's name must be unique within the command (comparison IS NOT case sensitive). " +
|
||||
"Specified name: `{0}`. " +
|
||||
"Property bound to another option with the same name: `{1}`.")
|
||||
{
|
||||
}
|
||||
"This option's name must be unique within the command (comparison IS NOT case sensitive). "
|
||||
+ "Specified name: `{0}`. "
|
||||
+ "Property bound to another option with the same name: `{1}`."
|
||||
) { }
|
||||
|
||||
private void Analyze(
|
||||
SyntaxNodeAnalysisContext context,
|
||||
PropertyDeclarationSyntax propertyDeclaration,
|
||||
IPropertySymbol property)
|
||||
IPropertySymbol property
|
||||
)
|
||||
{
|
||||
if (property.ContainingType is null)
|
||||
return;
|
||||
@@ -35,8 +35,7 @@ public class OptionMustHaveUniqueNameAnalyzer : AnalyzerBase
|
||||
if (string.IsNullOrWhiteSpace(option.Name))
|
||||
return;
|
||||
|
||||
var otherProperties = property
|
||||
.ContainingType
|
||||
var otherProperties = property.ContainingType
|
||||
.GetMembers()
|
||||
.OfType<IPropertySymbol>()
|
||||
.Where(m => !m.Equals(property))
|
||||
|
||||
@@ -13,16 +13,16 @@ public class OptionMustHaveUniqueShortNameAnalyzer : AnalyzerBase
|
||||
public OptionMustHaveUniqueShortNameAnalyzer()
|
||||
: base(
|
||||
"Options must have unique short names",
|
||||
"This option's short name must be unique within the command (comparison IS case sensitive). " +
|
||||
"Specified short name: `{0}` " +
|
||||
"Property bound to another option with the same short name: `{1}`.")
|
||||
{
|
||||
}
|
||||
"This option's short name must be unique within the command (comparison IS case sensitive). "
|
||||
+ "Specified short name: `{0}` "
|
||||
+ "Property bound to another option with the same short name: `{1}`."
|
||||
) { }
|
||||
|
||||
private void Analyze(
|
||||
SyntaxNodeAnalysisContext context,
|
||||
PropertyDeclarationSyntax propertyDeclaration,
|
||||
IPropertySymbol property)
|
||||
IPropertySymbol property
|
||||
)
|
||||
{
|
||||
if (property.ContainingType is null)
|
||||
return;
|
||||
@@ -34,8 +34,7 @@ public class OptionMustHaveUniqueShortNameAnalyzer : AnalyzerBase
|
||||
if (option.ShortName is null)
|
||||
return;
|
||||
|
||||
var otherProperties = property
|
||||
.ContainingType
|
||||
var otherProperties = property.ContainingType
|
||||
.GetMembers()
|
||||
.OfType<IPropertySymbol>()
|
||||
.Where(m => !m.Equals(property))
|
||||
|
||||
@@ -13,14 +13,14 @@ public class OptionMustHaveValidConverterAnalyzer : AnalyzerBase
|
||||
public OptionMustHaveValidConverterAnalyzer()
|
||||
: base(
|
||||
$"Option converters must derive from `{SymbolNames.CliFxBindingConverterClass}`",
|
||||
$"Converter specified for this option must derive from a compatible `{SymbolNames.CliFxBindingConverterClass}`.")
|
||||
{
|
||||
}
|
||||
$"Converter specified for this option must derive from a compatible `{SymbolNames.CliFxBindingConverterClass}`."
|
||||
) { }
|
||||
|
||||
private void Analyze(
|
||||
SyntaxNodeAnalysisContext context,
|
||||
PropertyDeclarationSyntax propertyDeclaration,
|
||||
IPropertySymbol property)
|
||||
IPropertySymbol property
|
||||
)
|
||||
{
|
||||
var option = CommandOptionSymbol.TryResolve(property);
|
||||
if (option is null)
|
||||
@@ -29,21 +29,26 @@ public class OptionMustHaveValidConverterAnalyzer : AnalyzerBase
|
||||
if (option.ConverterType is null)
|
||||
return;
|
||||
|
||||
var converterValueType = option
|
||||
.ConverterType
|
||||
var converterValueType = option.ConverterType
|
||||
.GetBaseTypes()
|
||||
.FirstOrDefault(t => t.ConstructedFrom.DisplayNameMatches(SymbolNames.CliFxBindingConverterClass))?
|
||||
.TypeArguments
|
||||
.FirstOrDefault();
|
||||
.FirstOrDefault(
|
||||
t => t.ConstructedFrom.DisplayNameMatches(SymbolNames.CliFxBindingConverterClass)
|
||||
)
|
||||
?.TypeArguments.FirstOrDefault();
|
||||
|
||||
// Value returned by the converter must be assignable to the property type
|
||||
var isCompatible =
|
||||
converterValueType is not null && (option.IsScalar()
|
||||
converterValueType is not null
|
||||
&& (
|
||||
option.IsScalar()
|
||||
// Scalar
|
||||
? context.Compilation.IsAssignable(converterValueType, property.Type)
|
||||
// Non-scalar (assume we can handle all IEnumerable types for simplicity)
|
||||
: property.Type.TryGetEnumerableUnderlyingType() is { } enumerableUnderlyingType &&
|
||||
context.Compilation.IsAssignable(converterValueType, enumerableUnderlyingType)
|
||||
: property.Type.TryGetEnumerableUnderlyingType() is { } enumerableUnderlyingType
|
||||
&& context.Compilation.IsAssignable(
|
||||
converterValueType,
|
||||
enumerableUnderlyingType
|
||||
)
|
||||
);
|
||||
|
||||
if (!isCompatible)
|
||||
|
||||
@@ -12,15 +12,15 @@ public class OptionMustHaveValidNameAnalyzer : AnalyzerBase
|
||||
public OptionMustHaveValidNameAnalyzer()
|
||||
: base(
|
||||
"Options must have valid names",
|
||||
"This option's name must be at least 2 characters long and must start with a letter. " +
|
||||
"Specified name: `{0}`.")
|
||||
{
|
||||
}
|
||||
"This option's name must be at least 2 characters long and must start with a letter. "
|
||||
+ "Specified name: `{0}`."
|
||||
) { }
|
||||
|
||||
private void Analyze(
|
||||
SyntaxNodeAnalysisContext context,
|
||||
PropertyDeclarationSyntax propertyDeclaration,
|
||||
IPropertySymbol property)
|
||||
IPropertySymbol property
|
||||
)
|
||||
{
|
||||
var option = CommandOptionSymbol.TryResolve(property);
|
||||
if (option is null)
|
||||
@@ -32,10 +32,7 @@ public class OptionMustHaveValidNameAnalyzer : AnalyzerBase
|
||||
if (option.Name.Length < 2 || !char.IsLetter(option.Name[0]))
|
||||
{
|
||||
context.ReportDiagnostic(
|
||||
CreateDiagnostic(
|
||||
propertyDeclaration.Identifier.GetLocation(),
|
||||
option.Name
|
||||
)
|
||||
CreateDiagnostic(propertyDeclaration.Identifier.GetLocation(), option.Name)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,15 +12,15 @@ public class OptionMustHaveValidShortNameAnalyzer : AnalyzerBase
|
||||
public OptionMustHaveValidShortNameAnalyzer()
|
||||
: base(
|
||||
"Option short names must be letter characters",
|
||||
"This option's short name must be a single letter character. " +
|
||||
"Specified short name: `{0}`.")
|
||||
{
|
||||
}
|
||||
"This option's short name must be a single letter character. "
|
||||
+ "Specified short name: `{0}`."
|
||||
) { }
|
||||
|
||||
private void Analyze(
|
||||
SyntaxNodeAnalysisContext context,
|
||||
PropertyDeclarationSyntax propertyDeclaration,
|
||||
IPropertySymbol property)
|
||||
IPropertySymbol property
|
||||
)
|
||||
{
|
||||
var option = CommandOptionSymbol.TryResolve(property);
|
||||
if (option is null)
|
||||
@@ -32,10 +32,7 @@ public class OptionMustHaveValidShortNameAnalyzer : AnalyzerBase
|
||||
if (!char.IsLetter(option.ShortName.Value))
|
||||
{
|
||||
context.ReportDiagnostic(
|
||||
CreateDiagnostic(
|
||||
propertyDeclaration.Identifier.GetLocation(),
|
||||
option.ShortName
|
||||
)
|
||||
CreateDiagnostic(propertyDeclaration.Identifier.GetLocation(), option.ShortName)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,14 +13,14 @@ public class OptionMustHaveValidValidatorsAnalyzer : AnalyzerBase
|
||||
public OptionMustHaveValidValidatorsAnalyzer()
|
||||
: base(
|
||||
$"Option validators must derive from `{SymbolNames.CliFxBindingValidatorClass}`",
|
||||
$"Each validator specified for this option must derive from a compatible `{SymbolNames.CliFxBindingValidatorClass}`.")
|
||||
{
|
||||
}
|
||||
$"Each validator specified for this option must derive from a compatible `{SymbolNames.CliFxBindingValidatorClass}`."
|
||||
) { }
|
||||
|
||||
private void Analyze(
|
||||
SyntaxNodeAnalysisContext context,
|
||||
PropertyDeclarationSyntax propertyDeclaration,
|
||||
IPropertySymbol property)
|
||||
IPropertySymbol property
|
||||
)
|
||||
{
|
||||
var option = CommandOptionSymbol.TryResolve(property);
|
||||
if (option is null)
|
||||
@@ -30,14 +30,16 @@ public class OptionMustHaveValidValidatorsAnalyzer : AnalyzerBase
|
||||
{
|
||||
var validatorValueType = validatorType
|
||||
.GetBaseTypes()
|
||||
.FirstOrDefault(t => t.ConstructedFrom.DisplayNameMatches(SymbolNames.CliFxBindingValidatorClass))?
|
||||
.TypeArguments
|
||||
.FirstOrDefault();
|
||||
.FirstOrDefault(
|
||||
t =>
|
||||
t.ConstructedFrom.DisplayNameMatches(SymbolNames.CliFxBindingValidatorClass)
|
||||
)
|
||||
?.TypeArguments.FirstOrDefault();
|
||||
|
||||
// Value passed to the validator must be assignable from the property type
|
||||
var isCompatible =
|
||||
validatorValueType is not null &&
|
||||
context.Compilation.IsAssignable(property.Type, validatorValueType);
|
||||
validatorValueType is not null
|
||||
&& context.Compilation.IsAssignable(property.Type, validatorValueType);
|
||||
|
||||
if (!isCompatible)
|
||||
{
|
||||
|
||||
@@ -13,14 +13,14 @@ public class ParameterMustBeInsideCommandAnalyzer : AnalyzerBase
|
||||
public ParameterMustBeInsideCommandAnalyzer()
|
||||
: base(
|
||||
"Parameters must be defined inside commands",
|
||||
$"This parameter must be defined inside a class that implements `{SymbolNames.CliFxCommandInterface}`.")
|
||||
{
|
||||
}
|
||||
$"This parameter must be defined inside a class that implements `{SymbolNames.CliFxCommandInterface}`."
|
||||
) { }
|
||||
|
||||
private void Analyze(
|
||||
SyntaxNodeAnalysisContext context,
|
||||
PropertyDeclarationSyntax propertyDeclaration,
|
||||
IPropertySymbol property)
|
||||
IPropertySymbol property
|
||||
)
|
||||
{
|
||||
if (property.ContainingType is null)
|
||||
return;
|
||||
@@ -31,10 +31,9 @@ public class ParameterMustBeInsideCommandAnalyzer : AnalyzerBase
|
||||
if (!CommandParameterSymbol.IsParameterProperty(property))
|
||||
return;
|
||||
|
||||
var isInsideCommand = property
|
||||
.ContainingType
|
||||
.AllInterfaces
|
||||
.Any(i => i.DisplayNameMatches(SymbolNames.CliFxCommandInterface));
|
||||
var isInsideCommand = property.ContainingType.AllInterfaces.Any(
|
||||
i => i.DisplayNameMatches(SymbolNames.CliFxCommandInterface)
|
||||
);
|
||||
|
||||
if (!isInsideCommand)
|
||||
{
|
||||
|
||||
@@ -13,15 +13,15 @@ public class ParameterMustBeLastIfNonRequiredAnalyzer : AnalyzerBase
|
||||
public ParameterMustBeLastIfNonRequiredAnalyzer()
|
||||
: base(
|
||||
"Parameters marked as non-required must be the last in order",
|
||||
"This parameter is non-required so it must be the last in order (its order must be highest within the command). " +
|
||||
"Property bound to another non-required parameter: `{0}`.")
|
||||
{
|
||||
}
|
||||
"This parameter is non-required so it must be the last in order (its order must be highest within the command). "
|
||||
+ "Property bound to another non-required parameter: `{0}`."
|
||||
) { }
|
||||
|
||||
private void Analyze(
|
||||
SyntaxNodeAnalysisContext context,
|
||||
PropertyDeclarationSyntax propertyDeclaration,
|
||||
IPropertySymbol property)
|
||||
IPropertySymbol property
|
||||
)
|
||||
{
|
||||
if (property.ContainingType is null)
|
||||
return;
|
||||
@@ -33,8 +33,7 @@ public class ParameterMustBeLastIfNonRequiredAnalyzer : AnalyzerBase
|
||||
if (parameter.IsRequired != false)
|
||||
return;
|
||||
|
||||
var otherProperties = property
|
||||
.ContainingType
|
||||
var otherProperties = property.ContainingType
|
||||
.GetMembers()
|
||||
.OfType<IPropertySymbol>()
|
||||
.Where(m => !m.Equals(property))
|
||||
|
||||
@@ -13,15 +13,15 @@ public class ParameterMustBeLastIfNonScalarAnalyzer : AnalyzerBase
|
||||
public ParameterMustBeLastIfNonScalarAnalyzer()
|
||||
: base(
|
||||
"Parameters of non-scalar types must be the last in order",
|
||||
"This parameter has a non-scalar type so it must be the last in order (its order must be highest within the command). " +
|
||||
"Property bound to another non-scalar parameter: `{0}`.")
|
||||
{
|
||||
}
|
||||
"This parameter has a non-scalar type so it must be the last in order (its order must be highest within the command). "
|
||||
+ "Property bound to another non-scalar parameter: `{0}`."
|
||||
) { }
|
||||
|
||||
private void Analyze(
|
||||
SyntaxNodeAnalysisContext context,
|
||||
PropertyDeclarationSyntax propertyDeclaration,
|
||||
IPropertySymbol property)
|
||||
IPropertySymbol property
|
||||
)
|
||||
{
|
||||
if (property.ContainingType is null)
|
||||
return;
|
||||
@@ -33,8 +33,7 @@ public class ParameterMustBeLastIfNonScalarAnalyzer : AnalyzerBase
|
||||
if (parameter.IsScalar())
|
||||
return;
|
||||
|
||||
var otherProperties = property
|
||||
.ContainingType
|
||||
var otherProperties = property.ContainingType
|
||||
.GetMembers()
|
||||
.OfType<IPropertySymbol>()
|
||||
.Where(m => !m.Equals(property))
|
||||
|
||||
@@ -12,14 +12,14 @@ public class ParameterMustBeRequiredIfPropertyRequiredAnalyzer : AnalyzerBase
|
||||
public ParameterMustBeRequiredIfPropertyRequiredAnalyzer()
|
||||
: base(
|
||||
"Parameters bound to required properties cannot be marked as non-required",
|
||||
"This parameter cannot be marked as non-required because it's bound to a required property.")
|
||||
{
|
||||
}
|
||||
"This parameter cannot be marked as non-required because it's bound to a required property."
|
||||
) { }
|
||||
|
||||
private void Analyze(
|
||||
SyntaxNodeAnalysisContext context,
|
||||
PropertyDeclarationSyntax propertyDeclaration,
|
||||
IPropertySymbol property)
|
||||
IPropertySymbol property
|
||||
)
|
||||
{
|
||||
if (property.ContainingType is null)
|
||||
return;
|
||||
@@ -34,11 +34,7 @@ public class ParameterMustBeRequiredIfPropertyRequiredAnalyzer : AnalyzerBase
|
||||
if (parameter.IsRequired != false)
|
||||
return;
|
||||
|
||||
context.ReportDiagnostic(
|
||||
CreateDiagnostic(
|
||||
propertyDeclaration.Identifier.GetLocation()
|
||||
)
|
||||
);
|
||||
context.ReportDiagnostic(CreateDiagnostic(propertyDeclaration.Identifier.GetLocation()));
|
||||
}
|
||||
|
||||
public override void Initialize(AnalysisContext context)
|
||||
|
||||
@@ -13,15 +13,15 @@ public class ParameterMustBeSingleIfNonRequiredAnalyzer : AnalyzerBase
|
||||
public ParameterMustBeSingleIfNonRequiredAnalyzer()
|
||||
: base(
|
||||
"Parameters marked as non-required are limited to one per command",
|
||||
"This parameter is non-required so it must be the only such parameter in the command. " +
|
||||
"Property bound to another non-required parameter: `{0}`.")
|
||||
{
|
||||
}
|
||||
"This parameter is non-required so it must be the only such parameter in the command. "
|
||||
+ "Property bound to another non-required parameter: `{0}`."
|
||||
) { }
|
||||
|
||||
private void Analyze(
|
||||
SyntaxNodeAnalysisContext context,
|
||||
PropertyDeclarationSyntax propertyDeclaration,
|
||||
IPropertySymbol property)
|
||||
IPropertySymbol property
|
||||
)
|
||||
{
|
||||
if (property.ContainingType is null)
|
||||
return;
|
||||
@@ -33,8 +33,7 @@ public class ParameterMustBeSingleIfNonRequiredAnalyzer : AnalyzerBase
|
||||
if (parameter.IsRequired != false)
|
||||
return;
|
||||
|
||||
var otherProperties = property
|
||||
.ContainingType
|
||||
var otherProperties = property.ContainingType
|
||||
.GetMembers()
|
||||
.OfType<IPropertySymbol>()
|
||||
.Where(m => !m.Equals(property))
|
||||
|
||||
@@ -13,15 +13,15 @@ public class ParameterMustBeSingleIfNonScalarAnalyzer : AnalyzerBase
|
||||
public ParameterMustBeSingleIfNonScalarAnalyzer()
|
||||
: base(
|
||||
"Parameters of non-scalar types are limited to one per command",
|
||||
"This parameter has a non-scalar type so it must be the only such parameter in the command. " +
|
||||
"Property bound to another non-scalar parameter: `{0}`.")
|
||||
{
|
||||
}
|
||||
"This parameter has a non-scalar type so it must be the only such parameter in the command. "
|
||||
+ "Property bound to another non-scalar parameter: `{0}`."
|
||||
) { }
|
||||
|
||||
private void Analyze(
|
||||
SyntaxNodeAnalysisContext context,
|
||||
PropertyDeclarationSyntax propertyDeclaration,
|
||||
IPropertySymbol property)
|
||||
IPropertySymbol property
|
||||
)
|
||||
{
|
||||
if (property.ContainingType is null)
|
||||
return;
|
||||
@@ -33,8 +33,7 @@ public class ParameterMustBeSingleIfNonScalarAnalyzer : AnalyzerBase
|
||||
if (parameter.IsScalar())
|
||||
return;
|
||||
|
||||
var otherProperties = property
|
||||
.ContainingType
|
||||
var otherProperties = property.ContainingType
|
||||
.GetMembers()
|
||||
.OfType<IPropertySymbol>()
|
||||
.Where(m => !m.Equals(property))
|
||||
|
||||
@@ -14,16 +14,16 @@ public class ParameterMustHaveUniqueNameAnalyzer : AnalyzerBase
|
||||
public ParameterMustHaveUniqueNameAnalyzer()
|
||||
: base(
|
||||
"Parameters must have unique names",
|
||||
"This parameter's name must be unique within the command (comparison IS NOT case sensitive). " +
|
||||
"Specified name: `{0}`. " +
|
||||
"Property bound to another parameter with the same name: `{1}`.")
|
||||
{
|
||||
}
|
||||
"This parameter's name must be unique within the command (comparison IS NOT case sensitive). "
|
||||
+ "Specified name: `{0}`. "
|
||||
+ "Property bound to another parameter with the same name: `{1}`."
|
||||
) { }
|
||||
|
||||
private void Analyze(
|
||||
SyntaxNodeAnalysisContext context,
|
||||
PropertyDeclarationSyntax propertyDeclaration,
|
||||
IPropertySymbol property)
|
||||
IPropertySymbol property
|
||||
)
|
||||
{
|
||||
if (property.ContainingType is null)
|
||||
return;
|
||||
@@ -35,8 +35,7 @@ public class ParameterMustHaveUniqueNameAnalyzer : AnalyzerBase
|
||||
if (string.IsNullOrWhiteSpace(parameter.Name))
|
||||
return;
|
||||
|
||||
var otherProperties = property
|
||||
.ContainingType
|
||||
var otherProperties = property.ContainingType
|
||||
.GetMembers()
|
||||
.OfType<IPropertySymbol>()
|
||||
.Where(m => !m.Equals(property))
|
||||
@@ -51,7 +50,13 @@ public class ParameterMustHaveUniqueNameAnalyzer : AnalyzerBase
|
||||
if (string.IsNullOrWhiteSpace(otherParameter.Name))
|
||||
continue;
|
||||
|
||||
if (string.Equals(parameter.Name, otherParameter.Name, StringComparison.OrdinalIgnoreCase))
|
||||
if (
|
||||
string.Equals(
|
||||
parameter.Name,
|
||||
otherParameter.Name,
|
||||
StringComparison.OrdinalIgnoreCase
|
||||
)
|
||||
)
|
||||
{
|
||||
context.ReportDiagnostic(
|
||||
CreateDiagnostic(
|
||||
|
||||
@@ -13,16 +13,16 @@ public class ParameterMustHaveUniqueOrderAnalyzer : AnalyzerBase
|
||||
public ParameterMustHaveUniqueOrderAnalyzer()
|
||||
: base(
|
||||
"Parameters must have unique order",
|
||||
"This parameter's order must be unique within the command. " +
|
||||
"Specified order: {0}. " +
|
||||
"Property bound to another parameter with the same order: `{1}`.")
|
||||
{
|
||||
}
|
||||
"This parameter's order must be unique within the command. "
|
||||
+ "Specified order: {0}. "
|
||||
+ "Property bound to another parameter with the same order: `{1}`."
|
||||
) { }
|
||||
|
||||
private void Analyze(
|
||||
SyntaxNodeAnalysisContext context,
|
||||
PropertyDeclarationSyntax propertyDeclaration,
|
||||
IPropertySymbol property)
|
||||
IPropertySymbol property
|
||||
)
|
||||
{
|
||||
if (property.ContainingType is null)
|
||||
return;
|
||||
@@ -31,8 +31,7 @@ public class ParameterMustHaveUniqueOrderAnalyzer : AnalyzerBase
|
||||
if (parameter is null)
|
||||
return;
|
||||
|
||||
var otherProperties = property
|
||||
.ContainingType
|
||||
var otherProperties = property.ContainingType
|
||||
.GetMembers()
|
||||
.OfType<IPropertySymbol>()
|
||||
.Where(m => !m.Equals(property))
|
||||
|
||||
@@ -13,14 +13,14 @@ public class ParameterMustHaveValidConverterAnalyzer : AnalyzerBase
|
||||
public ParameterMustHaveValidConverterAnalyzer()
|
||||
: base(
|
||||
$"Parameter converters must derive from `{SymbolNames.CliFxBindingConverterClass}`",
|
||||
$"Converter specified for this parameter must derive from a compatible `{SymbolNames.CliFxBindingConverterClass}`.")
|
||||
{
|
||||
}
|
||||
$"Converter specified for this parameter must derive from a compatible `{SymbolNames.CliFxBindingConverterClass}`."
|
||||
) { }
|
||||
|
||||
private void Analyze(
|
||||
SyntaxNodeAnalysisContext context,
|
||||
PropertyDeclarationSyntax propertyDeclaration,
|
||||
IPropertySymbol property)
|
||||
IPropertySymbol property
|
||||
)
|
||||
{
|
||||
var parameter = CommandParameterSymbol.TryResolve(property);
|
||||
if (parameter is null)
|
||||
@@ -29,21 +29,26 @@ public class ParameterMustHaveValidConverterAnalyzer : AnalyzerBase
|
||||
if (parameter.ConverterType is null)
|
||||
return;
|
||||
|
||||
var converterValueType = parameter
|
||||
.ConverterType
|
||||
var converterValueType = parameter.ConverterType
|
||||
.GetBaseTypes()
|
||||
.FirstOrDefault(t => t.ConstructedFrom.DisplayNameMatches(SymbolNames.CliFxBindingConverterClass))?
|
||||
.TypeArguments
|
||||
.FirstOrDefault();
|
||||
.FirstOrDefault(
|
||||
t => t.ConstructedFrom.DisplayNameMatches(SymbolNames.CliFxBindingConverterClass)
|
||||
)
|
||||
?.TypeArguments.FirstOrDefault();
|
||||
|
||||
// Value returned by the converter must be assignable to the property type
|
||||
var isCompatible =
|
||||
converterValueType is not null && (parameter.IsScalar()
|
||||
converterValueType is not null
|
||||
&& (
|
||||
parameter.IsScalar()
|
||||
// Scalar
|
||||
? context.Compilation.IsAssignable(converterValueType, property.Type)
|
||||
// Non-scalar (assume we can handle all IEnumerable types for simplicity)
|
||||
: property.Type.TryGetEnumerableUnderlyingType() is { } enumerableUnderlyingType &&
|
||||
context.Compilation.IsAssignable(converterValueType, enumerableUnderlyingType)
|
||||
: property.Type.TryGetEnumerableUnderlyingType() is { } enumerableUnderlyingType
|
||||
&& context.Compilation.IsAssignable(
|
||||
converterValueType,
|
||||
enumerableUnderlyingType
|
||||
)
|
||||
);
|
||||
|
||||
if (!isCompatible)
|
||||
|
||||
@@ -13,14 +13,14 @@ public class ParameterMustHaveValidValidatorsAnalyzer : AnalyzerBase
|
||||
public ParameterMustHaveValidValidatorsAnalyzer()
|
||||
: base(
|
||||
$"Parameter validators must derive from `{SymbolNames.CliFxBindingValidatorClass}`",
|
||||
$"Each validator specified for this parameter must derive from a compatible `{SymbolNames.CliFxBindingValidatorClass}`.")
|
||||
{
|
||||
}
|
||||
$"Each validator specified for this parameter must derive from a compatible `{SymbolNames.CliFxBindingValidatorClass}`."
|
||||
) { }
|
||||
|
||||
private void Analyze(
|
||||
SyntaxNodeAnalysisContext context,
|
||||
PropertyDeclarationSyntax propertyDeclaration,
|
||||
IPropertySymbol property)
|
||||
IPropertySymbol property
|
||||
)
|
||||
{
|
||||
var parameter = CommandParameterSymbol.TryResolve(property);
|
||||
if (parameter is null)
|
||||
@@ -30,14 +30,16 @@ public class ParameterMustHaveValidValidatorsAnalyzer : AnalyzerBase
|
||||
{
|
||||
var validatorValueType = validatorType
|
||||
.GetBaseTypes()
|
||||
.FirstOrDefault(t => t.ConstructedFrom.DisplayNameMatches(SymbolNames.CliFxBindingValidatorClass))?
|
||||
.TypeArguments
|
||||
.FirstOrDefault();
|
||||
.FirstOrDefault(
|
||||
t =>
|
||||
t.ConstructedFrom.DisplayNameMatches(SymbolNames.CliFxBindingValidatorClass)
|
||||
)
|
||||
?.TypeArguments.FirstOrDefault();
|
||||
|
||||
// Value passed to the validator must be assignable from the property type
|
||||
var isCompatible =
|
||||
validatorValueType is not null &&
|
||||
context.Compilation.IsAssignable(property.Type, validatorValueType);
|
||||
validatorValueType is not null
|
||||
&& context.Compilation.IsAssignable(property.Type, validatorValueType);
|
||||
|
||||
if (!isCompatible)
|
||||
{
|
||||
|
||||
@@ -15,13 +15,13 @@ public class SystemConsoleShouldBeAvoidedAnalyzer : AnalyzerBase
|
||||
: base(
|
||||
$"Avoid calling `System.Console` where `{SymbolNames.CliFxConsoleInterface}` is available",
|
||||
$"Use the provided `{SymbolNames.CliFxConsoleInterface}` abstraction instead of `System.Console` to ensure that the command can be tested in isolation.",
|
||||
DiagnosticSeverity.Warning)
|
||||
{
|
||||
}
|
||||
DiagnosticSeverity.Warning
|
||||
) { }
|
||||
|
||||
private MemberAccessExpressionSyntax? TryGetSystemConsoleMemberAccess(
|
||||
SyntaxNodeAnalysisContext context,
|
||||
SyntaxNode node)
|
||||
SyntaxNode node
|
||||
)
|
||||
{
|
||||
var currentNode = node;
|
||||
|
||||
@@ -53,8 +53,7 @@ public class SystemConsoleShouldBeAvoidedAnalyzer : AnalyzerBase
|
||||
return;
|
||||
|
||||
// Check if IConsole is available in scope as an alternative to System.Console
|
||||
var isConsoleInterfaceAvailable = context
|
||||
.Node
|
||||
var isConsoleInterfaceAvailable = context.Node
|
||||
.Ancestors()
|
||||
.OfType<MethodDeclarationSyntax>()
|
||||
.SelectMany(m => m.ParameterList.Parameters)
|
||||
@@ -65,9 +64,7 @@ public class SystemConsoleShouldBeAvoidedAnalyzer : AnalyzerBase
|
||||
|
||||
if (isConsoleInterfaceAvailable)
|
||||
{
|
||||
context.ReportDiagnostic(
|
||||
CreateDiagnostic(systemConsoleMemberAccess.GetLocation())
|
||||
);
|
||||
context.ReportDiagnostic(CreateDiagnostic(systemConsoleMemberAccess.GetLocation()));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -29,14 +29,19 @@ internal static class RoslynExtensions
|
||||
}
|
||||
}
|
||||
|
||||
public static ITypeSymbol? TryGetEnumerableUnderlyingType(this ITypeSymbol type) => type
|
||||
.AllInterfaces
|
||||
.FirstOrDefault(i => i.ConstructedFrom.SpecialType == SpecialType.System_Collections_Generic_IEnumerable_T)?
|
||||
.TypeArguments[0];
|
||||
public static ITypeSymbol? TryGetEnumerableUnderlyingType(this ITypeSymbol type) =>
|
||||
type.AllInterfaces
|
||||
.FirstOrDefault(
|
||||
i =>
|
||||
i.ConstructedFrom.SpecialType
|
||||
== SpecialType.System_Collections_Generic_IEnumerable_T
|
||||
)
|
||||
?.TypeArguments[0];
|
||||
|
||||
// Detect if the property is required through roundabout means so as to not have to take dependency
|
||||
// on higher versions of the C# compiler.
|
||||
public static bool IsRequired(this IPropertySymbol property) => property
|
||||
public static bool IsRequired(this IPropertySymbol property) =>
|
||||
property
|
||||
// Can't rely on the RequiredMemberAttribute because it's generated by the compiler, not added by the user,
|
||||
// so we have to check for the presence of the `required` modifier in the syntax tree instead.
|
||||
.DeclaringSyntaxReferences
|
||||
@@ -45,14 +50,19 @@ internal static class RoslynExtensions
|
||||
.SelectMany(p => p.Modifiers)
|
||||
.Any(m => m.IsKind((SyntaxKind)8447));
|
||||
|
||||
public static bool IsAssignable(this Compilation compilation, ITypeSymbol source, ITypeSymbol destination) =>
|
||||
compilation.ClassifyConversion(source, destination).Exists;
|
||||
public static bool IsAssignable(
|
||||
this Compilation compilation,
|
||||
ITypeSymbol source,
|
||||
ITypeSymbol destination
|
||||
) => compilation.ClassifyConversion(source, destination).Exists;
|
||||
|
||||
public static void HandleClassDeclaration(
|
||||
this AnalysisContext analysisContext,
|
||||
Action<SyntaxNodeAnalysisContext, ClassDeclarationSyntax, ITypeSymbol> analyze)
|
||||
Action<SyntaxNodeAnalysisContext, ClassDeclarationSyntax, ITypeSymbol> analyze
|
||||
)
|
||||
{
|
||||
analysisContext.RegisterSyntaxNodeAction(ctx =>
|
||||
analysisContext.RegisterSyntaxNodeAction(
|
||||
ctx =>
|
||||
{
|
||||
if (ctx.Node is not ClassDeclarationSyntax classDeclaration)
|
||||
return;
|
||||
@@ -62,14 +72,18 @@ internal static class RoslynExtensions
|
||||
return;
|
||||
|
||||
analyze(ctx, classDeclaration, type);
|
||||
}, SyntaxKind.ClassDeclaration);
|
||||
},
|
||||
SyntaxKind.ClassDeclaration
|
||||
);
|
||||
}
|
||||
|
||||
public static void HandlePropertyDeclaration(
|
||||
this AnalysisContext analysisContext,
|
||||
Action<SyntaxNodeAnalysisContext, PropertyDeclarationSyntax, IPropertySymbol> analyze)
|
||||
Action<SyntaxNodeAnalysisContext, PropertyDeclarationSyntax, IPropertySymbol> analyze
|
||||
)
|
||||
{
|
||||
analysisContext.RegisterSyntaxNodeAction(ctx =>
|
||||
analysisContext.RegisterSyntaxNodeAction(
|
||||
ctx =>
|
||||
{
|
||||
if (ctx.Node is not PropertyDeclarationSyntax propertyDeclaration)
|
||||
return;
|
||||
@@ -79,6 +93,8 @@ internal static class RoslynExtensions
|
||||
return;
|
||||
|
||||
analyze(ctx, propertyDeclaration, property);
|
||||
}, SyntaxKind.PropertyDeclaration);
|
||||
},
|
||||
SyntaxKind.PropertyDeclaration
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,8 @@ internal static class StringExtensions
|
||||
public static string TrimEnd(
|
||||
this string str,
|
||||
string sub,
|
||||
StringComparison comparison = StringComparison.Ordinal)
|
||||
StringComparison comparison = StringComparison.Ordinal
|
||||
)
|
||||
{
|
||||
while (str.EndsWith(sub, comparison))
|
||||
str = str[..^sub.Length];
|
||||
|
||||
@@ -16,9 +16,7 @@ public partial class Benchmarks
|
||||
[NamedArgument('b', "bool", Constraint = NumArgsConstraint.Optional, Const = true)]
|
||||
public bool BoolOption { get; set; }
|
||||
|
||||
public void Execute()
|
||||
{
|
||||
}
|
||||
public void Execute() { }
|
||||
}
|
||||
|
||||
[Benchmark(Description = "Clipr")]
|
||||
|
||||
@@ -8,14 +8,10 @@ public partial class Benchmarks
|
||||
public class CoconaCommand
|
||||
{
|
||||
public void Execute(
|
||||
[Option("str", new []{'s'})]
|
||||
string? strOption,
|
||||
[Option("int", new []{'i'})]
|
||||
int intOption,
|
||||
[Option("bool", new []{'b'})]
|
||||
bool boolOption)
|
||||
{
|
||||
}
|
||||
[Option("str", new[] { 's' })] string? strOption,
|
||||
[Option("int", new[] { 'i' })] int intOption,
|
||||
[Option("bool", new[] { 'b' })] bool boolOption
|
||||
) { }
|
||||
}
|
||||
|
||||
[Benchmark(Description = "Cocona")]
|
||||
|
||||
@@ -16,9 +16,7 @@ public partial class Benchmarks
|
||||
[Option('b', "bool")]
|
||||
public bool BoolOption { get; set; }
|
||||
|
||||
public void Execute()
|
||||
{
|
||||
}
|
||||
public void Execute() { }
|
||||
}
|
||||
|
||||
[Benchmark(Description = "CommandLineParser")]
|
||||
|
||||
@@ -16,9 +16,7 @@ public partial class Benchmarks
|
||||
[ArgShortcut("--bool"), ArgShortcut("-b")]
|
||||
public bool BoolOption { get; set; }
|
||||
|
||||
public void Main()
|
||||
{
|
||||
}
|
||||
public void Main() { }
|
||||
}
|
||||
|
||||
[Benchmark(Description = "PowerArgs")]
|
||||
|
||||
@@ -15,18 +15,9 @@ public partial class Benchmarks
|
||||
{
|
||||
var command = new RootCommand
|
||||
{
|
||||
new Option(new[] {"--str", "-s"})
|
||||
{
|
||||
Argument = new Argument<string?>()
|
||||
},
|
||||
new Option(new[] {"--int", "-i"})
|
||||
{
|
||||
Argument = new Argument<int>()
|
||||
},
|
||||
new Option(new[] {"--bool", "-b"})
|
||||
{
|
||||
Argument = new Argument<bool>()
|
||||
}
|
||||
new Option(new[] { "--str", "-s" }) { Argument = new Argument<string?>() },
|
||||
new Option(new[] { "--int", "-i" }) { Argument = new Argument<int>() },
|
||||
new Option(new[] { "--bool", "-b" }) { Argument = new Argument<bool>() }
|
||||
};
|
||||
|
||||
command.Handler = CommandHandler.Create(
|
||||
|
||||
@@ -11,9 +11,8 @@ public partial class Benchmarks
|
||||
{
|
||||
private static readonly string[] Arguments = { "--str", "hello world", "-i", "13", "-b" };
|
||||
|
||||
public static void Main() => BenchmarkRunner.Run<Benchmarks>(
|
||||
DefaultConfig
|
||||
.Instance
|
||||
.WithOptions(ConfigOptions.DisableOptimizationsValidator)
|
||||
public static void Main() =>
|
||||
BenchmarkRunner.Run<Benchmarks>(
|
||||
DefaultConfig.Instance.WithOptions(ConfigOptions.DisableOptimizationsValidator)
|
||||
);
|
||||
}
|
||||
@@ -10,6 +10,7 @@
|
||||
<PackageReference Include="clipr" Version="1.6.1" />
|
||||
<PackageReference Include="Cocona" Version="2.2.0" />
|
||||
<PackageReference Include="CommandLineParser" Version="2.9.1" />
|
||||
<PackageReference Include="CSharpier.MsBuild" Version="0.25.0" PrivateAssets="all" />
|
||||
<PackageReference Include="McMaster.Extensions.CommandLineUtils" Version="4.0.2" />
|
||||
<PackageReference Include="PowerArgs" Version="4.0.2" />
|
||||
<PackageReference Include="System.CommandLine" Version="2.0.0-beta1.20574.7" />
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="CSharpier.MsBuild" Version="0.25.0" PrivateAssets="all" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
@@ -49,7 +49,8 @@ public partial class BookAddCommand
|
||||
{
|
||||
private static readonly Random Random = new();
|
||||
|
||||
private static DateTimeOffset CreateRandomDate() => new(
|
||||
private static DateTimeOffset CreateRandomDate() =>
|
||||
new(
|
||||
Random.Next(1800, 2020),
|
||||
Random.Next(1, 12),
|
||||
Random.Next(1, 28),
|
||||
@@ -59,7 +60,8 @@ public partial class BookAddCommand
|
||||
TimeSpan.Zero
|
||||
);
|
||||
|
||||
private static Isbn CreateRandomIsbn() => new(
|
||||
private static Isbn CreateRandomIsbn() =>
|
||||
new(
|
||||
Random.Next(0, 999),
|
||||
Random.Next(0, 99),
|
||||
Random.Next(0, 99999),
|
||||
|
||||
@@ -2,7 +2,13 @@
|
||||
|
||||
namespace CliFx.Demo.Domain;
|
||||
|
||||
public partial record Isbn(int EanPrefix, int RegistrationGroup, int Registrant, int Publication, int CheckDigit)
|
||||
public partial record Isbn(
|
||||
int EanPrefix,
|
||||
int RegistrationGroup,
|
||||
int Registrant,
|
||||
int Publication,
|
||||
int CheckDigit
|
||||
)
|
||||
{
|
||||
public override string ToString() =>
|
||||
$"{EanPrefix:000}-{RegistrationGroup:00}-{Registrant:00000}-{Publication:00}-{CheckDigit:0}";
|
||||
|
||||
@@ -6,7 +6,8 @@ namespace CliFx.Demo.Domain;
|
||||
|
||||
public class LibraryProvider
|
||||
{
|
||||
private static string StorageFilePath { get; } = Path.Combine(Directory.GetCurrentDirectory(), "Library.json");
|
||||
private static string StorageFilePath { get; } =
|
||||
Path.Combine(Directory.GetCurrentDirectory(), "Library.json");
|
||||
|
||||
private void StoreLibrary(Library library)
|
||||
{
|
||||
@@ -24,7 +25,8 @@ public class LibraryProvider
|
||||
return JsonSerializer.Deserialize<Library>(data) ?? Library.Empty;
|
||||
}
|
||||
|
||||
public Book? TryGetBook(string title) => GetLibrary().Books.FirstOrDefault(b => b.Title == title);
|
||||
public Book? TryGetBook(string title) =>
|
||||
GetLibrary().Books.FirstOrDefault(b => b.Title == title);
|
||||
|
||||
public void AddBook(Book book)
|
||||
{
|
||||
|
||||
@@ -6,6 +6,10 @@
|
||||
<ApplicationIcon>../favicon.ico</ApplicationIcon>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="CSharpier.MsBuild" Version="0.25.0" PrivateAssets="all" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\CliFx\CliFx.csproj" />
|
||||
<ProjectReference Include="..\CliFx.Analyzers\CliFx.Analyzers.csproj" ReferenceOutputAssembly="false" OutputItemType="analyzer" />
|
||||
|
||||
@@ -14,10 +14,7 @@ public class CancellationTestCommand : ICommand
|
||||
{
|
||||
console.Output.WriteLine("Started.");
|
||||
|
||||
await Task.Delay(
|
||||
TimeSpan.FromSeconds(3),
|
||||
console.RegisterCancellationHandler()
|
||||
);
|
||||
await Task.Delay(TimeSpan.FromSeconds(3), console.RegisterCancellationHandler());
|
||||
|
||||
console.Output.WriteLine("Completed.");
|
||||
}
|
||||
|
||||
@@ -10,7 +10,8 @@ namespace CliFx.Tests.Dummy;
|
||||
public static class Program
|
||||
{
|
||||
// Path to the apphost
|
||||
public static string FilePath { get; } = Path.ChangeExtension(
|
||||
public static string FilePath { get; } =
|
||||
Path.ChangeExtension(
|
||||
Assembly.GetExecutingAssembly().Location,
|
||||
RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "exe" : null
|
||||
);
|
||||
@@ -23,9 +24,6 @@ public static class Program
|
||||
"false"
|
||||
);
|
||||
|
||||
await new CliApplicationBuilder()
|
||||
.AddCommandsFromThisAssembly()
|
||||
.Build()
|
||||
.RunAsync();
|
||||
await new CliApplicationBuilder().AddCommandsFromThisAssembly().Build().RunAsync();
|
||||
}
|
||||
}
|
||||
@@ -11,9 +11,7 @@ namespace CliFx.Tests;
|
||||
public class ApplicationSpecs : SpecsBase
|
||||
{
|
||||
public ApplicationSpecs(ITestOutputHelper testOutput)
|
||||
: base(testOutput)
|
||||
{
|
||||
}
|
||||
: base(testOutput) { }
|
||||
|
||||
[Fact]
|
||||
public async Task I_can_create_an_application_with_the_default_configuration()
|
||||
@@ -24,10 +22,7 @@ public class ApplicationSpecs : SpecsBase
|
||||
.UseConsole(FakeConsole)
|
||||
.Build();
|
||||
|
||||
var exitCode = await app.RunAsync(
|
||||
Array.Empty<string>(),
|
||||
new Dictionary<string, string>()
|
||||
);
|
||||
var exitCode = await app.RunAsync(Array.Empty<string>(), new Dictionary<string, string>());
|
||||
|
||||
// Assert
|
||||
exitCode.Should().Be(0);
|
||||
@@ -53,10 +48,7 @@ public class ApplicationSpecs : SpecsBase
|
||||
.UseTypeActivator(Activator.CreateInstance!)
|
||||
.Build();
|
||||
|
||||
var exitCode = await app.RunAsync(
|
||||
Array.Empty<string>(),
|
||||
new Dictionary<string, string>()
|
||||
);
|
||||
var exitCode = await app.RunAsync(Array.Empty<string>(), new Dictionary<string, string>());
|
||||
|
||||
// Assert
|
||||
exitCode.Should().Be(0);
|
||||
@@ -71,10 +63,7 @@ public class ApplicationSpecs : SpecsBase
|
||||
.UseConsole(FakeConsole)
|
||||
.Build();
|
||||
|
||||
var exitCode = await app.RunAsync(
|
||||
Array.Empty<string>(),
|
||||
new Dictionary<string, string>()
|
||||
);
|
||||
var exitCode = await app.RunAsync(Array.Empty<string>(), new Dictionary<string, string>());
|
||||
|
||||
// Assert
|
||||
exitCode.Should().NotBe(0);
|
||||
|
||||
@@ -15,9 +15,7 @@ namespace CliFx.Tests;
|
||||
public class CancellationSpecs : SpecsBase
|
||||
{
|
||||
public CancellationSpecs(ITestOutputHelper testOutput)
|
||||
: base(testOutput)
|
||||
{
|
||||
}
|
||||
: base(testOutput) { }
|
||||
|
||||
[Fact(Timeout = 15000)]
|
||||
public async Task I_can_configure_the_command_to_listen_to_the_interrupt_signal()
|
||||
@@ -41,12 +39,11 @@ public class CancellationSpecs : SpecsBase
|
||||
PipeTarget.ToStringBuilder(stdOutBuffer)
|
||||
);
|
||||
|
||||
var command =
|
||||
Cli.Wrap(Dummy.Program.FilePath).WithArguments("cancel-test") |
|
||||
pipeTarget;
|
||||
var command = Cli.Wrap(Dummy.Program.FilePath).WithArguments("cancel-test") | pipeTarget;
|
||||
|
||||
// Act & assert
|
||||
await Assert.ThrowsAnyAsync<OperationCanceledException>(async () =>
|
||||
await Assert.ThrowsAnyAsync<OperationCanceledException>(
|
||||
async () =>
|
||||
await command.ExecuteAsync(
|
||||
// Forceful cancellation (not required because we have a timeout)
|
||||
CancellationToken.None,
|
||||
@@ -55,10 +52,7 @@ public class CancellationSpecs : SpecsBase
|
||||
)
|
||||
);
|
||||
|
||||
stdOutBuffer.ToString().Trim().Should().ConsistOfLines(
|
||||
"Started.",
|
||||
"Cancelled."
|
||||
);
|
||||
stdOutBuffer.ToString().Trim().Should().ConsistOfLines("Started.", "Cancelled.");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -111,9 +105,6 @@ public class CancellationSpecs : SpecsBase
|
||||
exitCode.Should().NotBe(0);
|
||||
|
||||
var stdOut = FakeConsole.ReadOutputString();
|
||||
stdOut.Trim().Should().ConsistOfLines(
|
||||
"Started.",
|
||||
"Cancelled."
|
||||
);
|
||||
stdOut.Trim().Should().ConsistOfLines("Started.", "Cancelled.");
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,7 @@
|
||||
<PackageReference Include="Basic.Reference.Assemblies.Net70" Version="1.4.2" />
|
||||
<PackageReference Include="CliWrap" Version="3.6.4" />
|
||||
<PackageReference Include="coverlet.collector" Version="6.0.0" PrivateAssets="all" />
|
||||
<PackageReference Include="CSharpier.MsBuild" Version="0.25.0" PrivateAssets="all" />
|
||||
<PackageReference Include="FluentAssertions" Version="6.11.0" />
|
||||
<PackageReference Include="GitHubActionsTestLogger" Version="2.3.2" PrivateAssets="all" />
|
||||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.6.0" />
|
||||
|
||||
@@ -17,9 +17,7 @@ namespace CliFx.Tests;
|
||||
public class ConsoleSpecs : SpecsBase
|
||||
{
|
||||
public ConsoleSpecs(ITestOutputHelper testOutput)
|
||||
: base(testOutput)
|
||||
{
|
||||
}
|
||||
: base(testOutput) { }
|
||||
|
||||
[Fact(Timeout = 15000)]
|
||||
public async Task I_can_run_the_application_with_the_default_console_implementation_to_interact_with_the_system_console()
|
||||
@@ -28,8 +26,7 @@ public class ConsoleSpecs : SpecsBase
|
||||
|
||||
// Arrange
|
||||
var command =
|
||||
"Hello world" |
|
||||
Cli.Wrap(Dummy.Program.FilePath).WithArguments("console-test");
|
||||
"Hello world" | Cli.Wrap(Dummy.Program.FilePath).WithArguments("console-test");
|
||||
|
||||
// Act
|
||||
var result = await command.ExecuteBufferedAsync();
|
||||
@@ -205,10 +202,6 @@ public class ConsoleSpecs : SpecsBase
|
||||
exitCode.Should().Be(0);
|
||||
|
||||
var stdOut = FakeConsole.ReadOutputString();
|
||||
stdOut.Trim().Should().ConsistOfLines(
|
||||
"D0",
|
||||
"A",
|
||||
"Backspace"
|
||||
);
|
||||
stdOut.Trim().Should().ConsistOfLines("D0", "A", "Backspace");
|
||||
}
|
||||
}
|
||||
@@ -11,9 +11,7 @@ namespace CliFx.Tests;
|
||||
public class ConversionSpecs : SpecsBase
|
||||
{
|
||||
public ConversionSpecs(ITestOutputHelper testOutput)
|
||||
: base(testOutput)
|
||||
{
|
||||
}
|
||||
: base(testOutput) { }
|
||||
|
||||
[Fact]
|
||||
public async Task I_can_bind_a_parameter_or_an_option_to_a_string_property()
|
||||
@@ -133,12 +131,7 @@ public class ConversionSpecs : SpecsBase
|
||||
|
||||
// Act
|
||||
var exitCode = await application.RunAsync(
|
||||
new[]
|
||||
{
|
||||
"-f", "true",
|
||||
"-b", "false",
|
||||
"-c"
|
||||
},
|
||||
new[] { "-f", "true", "-b", "false", "-c" },
|
||||
new Dictionary<string, string>()
|
||||
);
|
||||
|
||||
@@ -146,11 +139,7 @@ public class ConversionSpecs : SpecsBase
|
||||
exitCode.Should().Be(0);
|
||||
|
||||
var stdOut = FakeConsole.ReadOutputString();
|
||||
stdOut.Should().ConsistOfLines(
|
||||
"Foo = True",
|
||||
"Bar = False",
|
||||
"Baz = True"
|
||||
);
|
||||
stdOut.Should().ConsistOfLines("Foo = True", "Bar = False", "Baz = True");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -397,10 +386,7 @@ public class ConversionSpecs : SpecsBase
|
||||
exitCode.Should().Be(0);
|
||||
|
||||
var stdOut = FakeConsole.ReadOutputString();
|
||||
stdOut.Should().ConsistOfLines(
|
||||
"Foo = ",
|
||||
"Bar = 123"
|
||||
);
|
||||
stdOut.Should().ConsistOfLines("Foo = ", "Bar = 123");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -447,10 +433,7 @@ public class ConversionSpecs : SpecsBase
|
||||
exitCode.Should().Be(0);
|
||||
|
||||
var stdOut = FakeConsole.ReadOutputString();
|
||||
stdOut.Should().ConsistOfLines(
|
||||
"Foo = ",
|
||||
"Bar = 2"
|
||||
);
|
||||
stdOut.Should().ConsistOfLines("Foo = ", "Bar = 2");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -562,10 +545,7 @@ public class ConversionSpecs : SpecsBase
|
||||
exitCode.Should().Be(0);
|
||||
|
||||
var stdOut = FakeConsole.ReadOutputString();
|
||||
stdOut.Should().ConsistOfLines(
|
||||
"Foo = hello",
|
||||
"Bar = world"
|
||||
);
|
||||
stdOut.Should().ConsistOfLines("Foo = hello", "Bar = world");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -653,11 +633,7 @@ public class ConversionSpecs : SpecsBase
|
||||
exitCode.Should().Be(0);
|
||||
|
||||
var stdOut = FakeConsole.ReadOutputString();
|
||||
stdOut.Should().ConsistOfLines(
|
||||
"one",
|
||||
"two",
|
||||
"three"
|
||||
);
|
||||
stdOut.Should().ConsistOfLines("one", "two", "three");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -699,11 +675,7 @@ public class ConversionSpecs : SpecsBase
|
||||
exitCode.Should().Be(0);
|
||||
|
||||
var stdOut = FakeConsole.ReadOutputString();
|
||||
stdOut.Should().ConsistOfLines(
|
||||
"one",
|
||||
"two",
|
||||
"three"
|
||||
);
|
||||
stdOut.Should().ConsistOfLines("one", "two", "three");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -745,11 +717,7 @@ public class ConversionSpecs : SpecsBase
|
||||
exitCode.Should().Be(0);
|
||||
|
||||
var stdOut = FakeConsole.ReadOutputString();
|
||||
stdOut.Should().ConsistOfLines(
|
||||
"one",
|
||||
"two",
|
||||
"three"
|
||||
);
|
||||
stdOut.Should().ConsistOfLines("one", "two", "three");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -791,11 +759,7 @@ public class ConversionSpecs : SpecsBase
|
||||
exitCode.Should().Be(0);
|
||||
|
||||
var stdOut = FakeConsole.ReadOutputString();
|
||||
stdOut.Should().ConsistOfLines(
|
||||
"1",
|
||||
"13",
|
||||
"27"
|
||||
);
|
||||
stdOut.Should().ConsistOfLines("1", "13", "27");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
||||
@@ -14,9 +14,7 @@ namespace CliFx.Tests;
|
||||
public class DirectivesSpecs : SpecsBase
|
||||
{
|
||||
public DirectivesSpecs(ITestOutputHelper testOutput)
|
||||
: base(testOutput)
|
||||
{
|
||||
}
|
||||
: base(testOutput) { }
|
||||
|
||||
[Fact(Timeout = 15000)]
|
||||
public async Task I_can_use_the_debug_directive_to_make_the_application_wait_for_the_debugger_to_attach()
|
||||
@@ -32,9 +30,7 @@ public class DirectivesSpecs : SpecsBase
|
||||
cts.Cancel();
|
||||
}
|
||||
|
||||
var command =
|
||||
Cli.Wrap(Dummy.Program.FilePath).WithArguments("[debug]") |
|
||||
HandleStdOut;
|
||||
var command = Cli.Wrap(Dummy.Program.FilePath).WithArguments("[debug]") | HandleStdOut;
|
||||
|
||||
// Act & assert
|
||||
try
|
||||
@@ -71,21 +67,28 @@ public class DirectivesSpecs : SpecsBase
|
||||
// Act
|
||||
var exitCode = await application.RunAsync(
|
||||
new[] { "[preview]", "cmd", "param", "-abc", "--option", "foo" },
|
||||
new Dictionary<string, string>
|
||||
{
|
||||
["ENV_QOP"] = "hello",
|
||||
["ENV_KIL"] = "world"
|
||||
}
|
||||
new Dictionary<string, string> { ["ENV_QOP"] = "hello", ["ENV_KIL"] = "world" }
|
||||
);
|
||||
|
||||
// Assert
|
||||
exitCode.Should().Be(0);
|
||||
|
||||
var stdOut = FakeConsole.ReadOutputString();
|
||||
stdOut.Should().ContainAllInOrder(
|
||||
"cmd", "<param>", "[-a]", "[-b]", "[-c]", "[--option \"foo\"]",
|
||||
"ENV_QOP", "=", "\"hello\"",
|
||||
"ENV_KIL", "=", "\"world\""
|
||||
stdOut
|
||||
.Should()
|
||||
.ContainAllInOrder(
|
||||
"cmd",
|
||||
"<param>",
|
||||
"[-a]",
|
||||
"[-b]",
|
||||
"[-c]",
|
||||
"[--option \"foo\"]",
|
||||
"ENV_QOP",
|
||||
"=",
|
||||
"\"hello\"",
|
||||
"ENV_KIL",
|
||||
"=",
|
||||
"\"world\""
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -15,9 +15,7 @@ namespace CliFx.Tests;
|
||||
public class EnvironmentSpecs : SpecsBase
|
||||
{
|
||||
public EnvironmentSpecs(ITestOutputHelper testOutput)
|
||||
: base(testOutput)
|
||||
{
|
||||
}
|
||||
: base(testOutput) { }
|
||||
|
||||
[Fact]
|
||||
public async Task I_can_configure_an_option_to_fall_back_to_an_environment_variable_if_the_user_does_not_provide_the_corresponding_argument()
|
||||
@@ -54,21 +52,14 @@ public class EnvironmentSpecs : SpecsBase
|
||||
// Act
|
||||
var exitCode = await application.RunAsync(
|
||||
new[] { "--foo", "42" },
|
||||
new Dictionary<string, string>
|
||||
{
|
||||
["ENV_FOO"] = "100",
|
||||
["ENV_BAR"] = "200"
|
||||
}
|
||||
new Dictionary<string, string> { ["ENV_FOO"] = "100", ["ENV_BAR"] = "200" }
|
||||
);
|
||||
|
||||
// Assert
|
||||
exitCode.Should().Be(0);
|
||||
|
||||
var stdOut = FakeConsole.ReadOutputString();
|
||||
stdOut.Trim().Should().ConsistOfLines(
|
||||
"42",
|
||||
"200"
|
||||
);
|
||||
stdOut.Trim().Should().ConsistOfLines("42", "200");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -103,20 +94,14 @@ public class EnvironmentSpecs : SpecsBase
|
||||
// Act
|
||||
var exitCode = await application.RunAsync(
|
||||
Array.Empty<string>(),
|
||||
new Dictionary<string, string>
|
||||
{
|
||||
["ENV_FOO"] = $"bar{Path.PathSeparator}baz"
|
||||
}
|
||||
new Dictionary<string, string> { ["ENV_FOO"] = $"bar{Path.PathSeparator}baz" }
|
||||
);
|
||||
|
||||
// Assert
|
||||
exitCode.Should().Be(0);
|
||||
|
||||
var stdOut = FakeConsole.ReadOutputString();
|
||||
stdOut.Should().ConsistOfLines(
|
||||
"bar",
|
||||
"baz"
|
||||
);
|
||||
stdOut.Should().ConsistOfLines("bar", "baz");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -149,10 +134,7 @@ public class EnvironmentSpecs : SpecsBase
|
||||
// Act
|
||||
var exitCode = await application.RunAsync(
|
||||
Array.Empty<string>(),
|
||||
new Dictionary<string, string>
|
||||
{
|
||||
["ENV_FOO"] = $"bar{Path.PathSeparator}baz"
|
||||
}
|
||||
new Dictionary<string, string> { ["ENV_FOO"] = $"bar{Path.PathSeparator}baz" }
|
||||
);
|
||||
|
||||
// Assert
|
||||
@@ -171,9 +153,7 @@ public class EnvironmentSpecs : SpecsBase
|
||||
// Arrange
|
||||
var command = Cli.Wrap(Dummy.Program.FilePath)
|
||||
.WithArguments("env-test")
|
||||
.WithEnvironmentVariables(e => e
|
||||
.Set("ENV_TARGET", "Mars")
|
||||
);
|
||||
.WithEnvironmentVariables(e => e.Set("ENV_TARGET", "Mars"));
|
||||
|
||||
// Act
|
||||
var result = await command.ExecuteBufferedAsync();
|
||||
|
||||
@@ -12,9 +12,7 @@ namespace CliFx.Tests;
|
||||
public class ErrorReportingSpecs : SpecsBase
|
||||
{
|
||||
public ErrorReportingSpecs(ITestOutputHelper testOutput)
|
||||
: base(testOutput)
|
||||
{
|
||||
}
|
||||
: base(testOutput) { }
|
||||
|
||||
[Fact]
|
||||
public async Task I_can_throw_an_exception_in_a_command_to_report_an_error_with_a_stacktrace()
|
||||
@@ -50,10 +48,9 @@ public class ErrorReportingSpecs : SpecsBase
|
||||
stdOut.Should().BeEmpty();
|
||||
|
||||
var stdErr = FakeConsole.ReadErrorString();
|
||||
stdErr.Should().ContainAllInOrder(
|
||||
"System.Exception", "Something went wrong",
|
||||
"at", "CliFx."
|
||||
);
|
||||
stdErr
|
||||
.Should()
|
||||
.ContainAllInOrder("System.Exception", "Something went wrong", "at", "CliFx.");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -90,10 +87,15 @@ public class ErrorReportingSpecs : SpecsBase
|
||||
stdOut.Should().BeEmpty();
|
||||
|
||||
var stdErr = FakeConsole.ReadErrorString();
|
||||
stdErr.Should().ContainAllInOrder(
|
||||
"System.Exception", "Something went wrong",
|
||||
"System.Exception", "Another exception",
|
||||
"at", "CliFx."
|
||||
stdErr
|
||||
.Should()
|
||||
.ContainAllInOrder(
|
||||
"System.Exception",
|
||||
"Something went wrong",
|
||||
"System.Exception",
|
||||
"Another exception",
|
||||
"at",
|
||||
"CliFx."
|
||||
);
|
||||
}
|
||||
|
||||
@@ -168,10 +170,7 @@ public class ErrorReportingSpecs : SpecsBase
|
||||
stdOut.Should().BeEmpty();
|
||||
|
||||
var stdErr = FakeConsole.ReadErrorString();
|
||||
stdErr.Should().ContainAllInOrder(
|
||||
"CliFx.Exceptions.CommandException",
|
||||
"at", "CliFx."
|
||||
);
|
||||
stdErr.Should().ContainAllInOrder("CliFx.Exceptions.CommandException", "at", "CliFx.");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
||||
@@ -12,9 +12,7 @@ namespace CliFx.Tests;
|
||||
public class HelpTextSpecs : SpecsBase
|
||||
{
|
||||
public HelpTextSpecs(ITestOutputHelper testOutput)
|
||||
: base(testOutput)
|
||||
{
|
||||
}
|
||||
: base(testOutput) { }
|
||||
|
||||
[Fact]
|
||||
public async Task I_can_request_the_help_text_by_running_the_application_without_arguments_if_the_default_command_is_not_defined()
|
||||
@@ -249,11 +247,7 @@ public class HelpTextSpecs : SpecsBase
|
||||
exitCode.Should().Be(0);
|
||||
|
||||
var stdOut = FakeConsole.ReadOutputString();
|
||||
stdOut.Should().ContainAll(
|
||||
"App title",
|
||||
"App description",
|
||||
"App version"
|
||||
);
|
||||
stdOut.Should().ContainAll("App title", "App description", "App version");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -286,10 +280,7 @@ public class HelpTextSpecs : SpecsBase
|
||||
exitCode.Should().Be(0);
|
||||
|
||||
var stdOut = FakeConsole.ReadOutputString();
|
||||
stdOut.Should().ContainAllInOrder(
|
||||
"DESCRIPTION",
|
||||
"Description of the default command."
|
||||
);
|
||||
stdOut.Should().ContainAllInOrder("DESCRIPTION", "Description of the default command.");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -328,10 +319,7 @@ public class HelpTextSpecs : SpecsBase
|
||||
exitCode.Should().Be(0);
|
||||
|
||||
var stdOut = FakeConsole.ReadOutputString();
|
||||
stdOut.Should().ContainAllInOrder(
|
||||
"USAGE",
|
||||
"[command]", "[...]"
|
||||
);
|
||||
stdOut.Should().ContainAllInOrder("USAGE", "[command]", "[...]");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -373,10 +361,7 @@ public class HelpTextSpecs : SpecsBase
|
||||
exitCode.Should().Be(0);
|
||||
|
||||
var stdOut = FakeConsole.ReadOutputString();
|
||||
stdOut.Should().ContainAllInOrder(
|
||||
"USAGE",
|
||||
"<foo>", "<bar>", "<baz...>"
|
||||
);
|
||||
stdOut.Should().ContainAllInOrder("USAGE", "<foo>", "<bar>", "<baz...>");
|
||||
}
|
||||
|
||||
// https://github.com/Tyrrrz/CliFx/issues/117
|
||||
@@ -425,10 +410,7 @@ public class HelpTextSpecs : SpecsBase
|
||||
exitCode.Should().Be(0);
|
||||
|
||||
var stdOut = FakeConsole.ReadOutputString();
|
||||
stdOut.Should().ContainAllInOrder(
|
||||
"USAGE",
|
||||
"<foo>", "<bar>", "<baz...>"
|
||||
);
|
||||
stdOut.Should().ContainAllInOrder("USAGE", "<foo>", "<bar>", "<baz...>");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -470,10 +452,9 @@ public class HelpTextSpecs : SpecsBase
|
||||
exitCode.Should().Be(0);
|
||||
|
||||
var stdOut = FakeConsole.ReadOutputString();
|
||||
stdOut.Should().ContainAllInOrder(
|
||||
"USAGE",
|
||||
"--foo <value>", "--baz <values...>", "[options]"
|
||||
);
|
||||
stdOut
|
||||
.Should()
|
||||
.ContainAllInOrder("USAGE", "--foo <value>", "--baz <values...>", "[options]");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -512,11 +493,15 @@ public class HelpTextSpecs : SpecsBase
|
||||
exitCode.Should().Be(0);
|
||||
|
||||
var stdOut = FakeConsole.ReadOutputString();
|
||||
stdOut.Should().ContainAllInOrder(
|
||||
stdOut
|
||||
.Should()
|
||||
.ContainAllInOrder(
|
||||
"PARAMETERS",
|
||||
"foo", "Description of foo.",
|
||||
"foo",
|
||||
"Description of foo.",
|
||||
"OPTIONS",
|
||||
"--bar", "Description of bar."
|
||||
"--bar",
|
||||
"Description of bar."
|
||||
);
|
||||
}
|
||||
|
||||
@@ -550,10 +535,15 @@ public class HelpTextSpecs : SpecsBase
|
||||
exitCode.Should().Be(0);
|
||||
|
||||
var stdOut = FakeConsole.ReadOutputString();
|
||||
stdOut.Should().ContainAllInOrder(
|
||||
stdOut
|
||||
.Should()
|
||||
.ContainAllInOrder(
|
||||
"OPTIONS",
|
||||
"-h", "--help", "Shows help text",
|
||||
"--version", "Shows version information"
|
||||
"-h",
|
||||
"--help",
|
||||
"Shows help text",
|
||||
"--version",
|
||||
"Shows version information"
|
||||
);
|
||||
}
|
||||
|
||||
@@ -588,14 +578,9 @@ public class HelpTextSpecs : SpecsBase
|
||||
|
||||
var stdOut = FakeConsole.ReadOutputString();
|
||||
|
||||
stdOut.Should().ContainAllInOrder(
|
||||
"OPTIONS",
|
||||
"-h", "--help", "Shows help text"
|
||||
);
|
||||
stdOut.Should().ContainAllInOrder("OPTIONS", "-h", "--help", "Shows help text");
|
||||
|
||||
stdOut.Should().NotContainAny(
|
||||
"--version", "Shows version information"
|
||||
);
|
||||
stdOut.Should().NotContainAny("--version", "Shows version information");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -636,11 +621,21 @@ public class HelpTextSpecs : SpecsBase
|
||||
exitCode.Should().Be(0);
|
||||
|
||||
var stdOut = FakeConsole.ReadOutputString();
|
||||
stdOut.Should().ContainAllInOrder(
|
||||
stdOut
|
||||
.Should()
|
||||
.ContainAllInOrder(
|
||||
"PARAMETERS",
|
||||
"foo", "Choices:", "One", "Two", "Three",
|
||||
"foo",
|
||||
"Choices:",
|
||||
"One",
|
||||
"Two",
|
||||
"Three",
|
||||
"OPTIONS",
|
||||
"--bar", "Choices:", "One", "Two", "Three"
|
||||
"--bar",
|
||||
"Choices:",
|
||||
"One",
|
||||
"Two",
|
||||
"Three"
|
||||
);
|
||||
}
|
||||
|
||||
@@ -682,11 +677,21 @@ public class HelpTextSpecs : SpecsBase
|
||||
exitCode.Should().Be(0);
|
||||
|
||||
var stdOut = FakeConsole.ReadOutputString();
|
||||
stdOut.Should().ContainAllInOrder(
|
||||
stdOut
|
||||
.Should()
|
||||
.ContainAllInOrder(
|
||||
"PARAMETERS",
|
||||
"foo", "Choices:", "One", "Two", "Three",
|
||||
"foo",
|
||||
"Choices:",
|
||||
"One",
|
||||
"Two",
|
||||
"Three",
|
||||
"OPTIONS",
|
||||
"--bar", "Choices:", "One", "Two", "Three"
|
||||
"--bar",
|
||||
"Choices:",
|
||||
"One",
|
||||
"Two",
|
||||
"Three"
|
||||
);
|
||||
}
|
||||
|
||||
@@ -728,11 +733,21 @@ public class HelpTextSpecs : SpecsBase
|
||||
exitCode.Should().Be(0);
|
||||
|
||||
var stdOut = FakeConsole.ReadOutputString();
|
||||
stdOut.Should().ContainAllInOrder(
|
||||
stdOut
|
||||
.Should()
|
||||
.ContainAllInOrder(
|
||||
"PARAMETERS",
|
||||
"foo", "Choices:", "One", "Two", "Three",
|
||||
"foo",
|
||||
"Choices:",
|
||||
"One",
|
||||
"Two",
|
||||
"Three",
|
||||
"OPTIONS",
|
||||
"--bar", "Choices:", "One", "Two", "Three"
|
||||
"--bar",
|
||||
"Choices:",
|
||||
"One",
|
||||
"Two",
|
||||
"Three"
|
||||
);
|
||||
}
|
||||
|
||||
@@ -774,10 +789,16 @@ public class HelpTextSpecs : SpecsBase
|
||||
exitCode.Should().Be(0);
|
||||
|
||||
var stdOut = FakeConsole.ReadOutputString();
|
||||
stdOut.Should().ContainAllInOrder(
|
||||
stdOut
|
||||
.Should()
|
||||
.ContainAllInOrder(
|
||||
"OPTIONS",
|
||||
"--foo", "Environment variable:", "ENV_FOO",
|
||||
"--bar", "Environment variable:", "ENV_BAR"
|
||||
"--foo",
|
||||
"Environment variable:",
|
||||
"ENV_FOO",
|
||||
"--bar",
|
||||
"Environment variable:",
|
||||
"ENV_BAR"
|
||||
);
|
||||
}
|
||||
|
||||
@@ -838,15 +859,33 @@ public class HelpTextSpecs : SpecsBase
|
||||
|
||||
var stdOut = FakeConsole.ReadOutputString();
|
||||
|
||||
stdOut.Should().ContainAllInOrder(
|
||||
stdOut
|
||||
.Should()
|
||||
.ContainAllInOrder(
|
||||
"OPTIONS",
|
||||
"--foo", "Default:", "42",
|
||||
"--bar", "Default:", "hello",
|
||||
"--baz", "Default:", "one", "two", "three",
|
||||
"--qwe", "Default:", "True",
|
||||
"--qop", "Default:", "1337",
|
||||
"--zor", "Default:", "02:03:00",
|
||||
"--lol", "Default:", "Two"
|
||||
"--foo",
|
||||
"Default:",
|
||||
"42",
|
||||
"--bar",
|
||||
"Default:",
|
||||
"hello",
|
||||
"--baz",
|
||||
"Default:",
|
||||
"one",
|
||||
"two",
|
||||
"three",
|
||||
"--qwe",
|
||||
"Default:",
|
||||
"True",
|
||||
"--qop",
|
||||
"Default:",
|
||||
"1337",
|
||||
"--zor",
|
||||
"Default:",
|
||||
"02:03:00",
|
||||
"--lol",
|
||||
"Default:",
|
||||
"Two"
|
||||
);
|
||||
|
||||
stdOut.Should().NotContain("not printed");
|
||||
@@ -901,20 +940,23 @@ public class HelpTextSpecs : SpecsBase
|
||||
|
||||
var stdOut = FakeConsole.ReadOutputString();
|
||||
|
||||
stdOut.Should().ContainAllInOrder(
|
||||
stdOut
|
||||
.Should()
|
||||
.ContainAllInOrder(
|
||||
"COMMANDS",
|
||||
"cmd1", "Description of one command.",
|
||||
"cmd2", "Description of another command.",
|
||||
"cmd1",
|
||||
"Description of one command.",
|
||||
"cmd2",
|
||||
"Description of another command.",
|
||||
// `cmd2 child` will appear as an immediate command because it does not
|
||||
// have a more specific parent.
|
||||
"cmd3 child", "Description of an orphaned command."
|
||||
"cmd3 child",
|
||||
"Description of an orphaned command."
|
||||
);
|
||||
|
||||
// `cmd2 child` will still appear in the list of `cmd2` subcommands,
|
||||
// but its description will not be visible.
|
||||
stdOut.Should().NotContain(
|
||||
"Description of another command's child command."
|
||||
);
|
||||
stdOut.Should().NotContain("Description of another command's child command.");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -971,10 +1013,17 @@ public class HelpTextSpecs : SpecsBase
|
||||
exitCode.Should().Be(0);
|
||||
|
||||
var stdOut = FakeConsole.ReadOutputString();
|
||||
stdOut.Should().ContainAllInOrder(
|
||||
stdOut
|
||||
.Should()
|
||||
.ContainAllInOrder(
|
||||
"COMMANDS",
|
||||
"cmd1", "Subcommands:", "cmd1 child1",
|
||||
"cmd2", "Subcommands:", "cmd2 child1", "cmd2 child2"
|
||||
"cmd1",
|
||||
"Subcommands:",
|
||||
"cmd1 child1",
|
||||
"cmd2",
|
||||
"Subcommands:",
|
||||
"cmd2 child1",
|
||||
"cmd2 child2"
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -12,9 +12,7 @@ namespace CliFx.Tests;
|
||||
public class OptionBindingSpecs : SpecsBase
|
||||
{
|
||||
public OptionBindingSpecs(ITestOutputHelper testOutput)
|
||||
: base(testOutput)
|
||||
{
|
||||
}
|
||||
: base(testOutput) { }
|
||||
|
||||
[Fact]
|
||||
public async Task I_can_bind_an_option_to_a_property_and_get_the_value_from_the_corresponding_argument_by_name()
|
||||
@@ -84,10 +82,7 @@ public class OptionBindingSpecs : SpecsBase
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
var exitCode = await application.RunAsync(
|
||||
new[] {"-f"},
|
||||
new Dictionary<string, string>()
|
||||
);
|
||||
var exitCode = await application.RunAsync(new[] { "-f" }, new Dictionary<string, string>());
|
||||
|
||||
// Assert
|
||||
exitCode.Should().Be(0);
|
||||
@@ -130,11 +125,7 @@ public class OptionBindingSpecs : SpecsBase
|
||||
|
||||
// Act
|
||||
var exitCode = await application.RunAsync(
|
||||
new[]
|
||||
{
|
||||
"--foo", "one",
|
||||
"--bar", "two"
|
||||
},
|
||||
new[] { "--foo", "one", "--bar", "two" },
|
||||
new Dictionary<string, string>()
|
||||
);
|
||||
|
||||
@@ -142,10 +133,7 @@ public class OptionBindingSpecs : SpecsBase
|
||||
exitCode.Should().Be(0);
|
||||
|
||||
var stdOut = FakeConsole.ReadOutputString();
|
||||
stdOut.Should().ConsistOfLines(
|
||||
"Foo = one",
|
||||
"Bar = two"
|
||||
);
|
||||
stdOut.Should().ConsistOfLines("Foo = one", "Bar = two");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -182,11 +170,7 @@ public class OptionBindingSpecs : SpecsBase
|
||||
|
||||
// Act
|
||||
var exitCode = await application.RunAsync(
|
||||
new[]
|
||||
{
|
||||
"-f", "one",
|
||||
"-b", "two"
|
||||
},
|
||||
new[] { "-f", "one", "-b", "two" },
|
||||
new Dictionary<string, string>()
|
||||
);
|
||||
|
||||
@@ -194,10 +178,7 @@ public class OptionBindingSpecs : SpecsBase
|
||||
exitCode.Should().Be(0);
|
||||
|
||||
var stdOut = FakeConsole.ReadOutputString();
|
||||
stdOut.Should().ConsistOfLines(
|
||||
"Foo = one",
|
||||
"Bar = two"
|
||||
);
|
||||
stdOut.Should().ConsistOfLines("Foo = one", "Bar = two");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -242,10 +223,7 @@ public class OptionBindingSpecs : SpecsBase
|
||||
exitCode.Should().Be(0);
|
||||
|
||||
var stdOut = FakeConsole.ReadOutputString();
|
||||
stdOut.Should().ConsistOfLines(
|
||||
"Foo = ",
|
||||
"Bar = value"
|
||||
);
|
||||
stdOut.Should().ConsistOfLines("Foo = ", "Bar = value");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -287,11 +265,7 @@ public class OptionBindingSpecs : SpecsBase
|
||||
exitCode.Should().Be(0);
|
||||
|
||||
var stdOut = FakeConsole.ReadOutputString();
|
||||
stdOut.Should().ConsistOfLines(
|
||||
"one",
|
||||
"two",
|
||||
"three"
|
||||
);
|
||||
stdOut.Should().ConsistOfLines("one", "two", "three");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -333,11 +307,7 @@ public class OptionBindingSpecs : SpecsBase
|
||||
exitCode.Should().Be(0);
|
||||
|
||||
var stdOut = FakeConsole.ReadOutputString();
|
||||
stdOut.Should().ConsistOfLines(
|
||||
"one",
|
||||
"two",
|
||||
"three"
|
||||
);
|
||||
stdOut.Should().ConsistOfLines("one", "two", "three");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -371,12 +341,7 @@ public class OptionBindingSpecs : SpecsBase
|
||||
|
||||
// Act
|
||||
var exitCode = await application.RunAsync(
|
||||
new[]
|
||||
{
|
||||
"--foo", "one",
|
||||
"--foo", "two",
|
||||
"--foo", "three"
|
||||
},
|
||||
new[] { "--foo", "one", "--foo", "two", "--foo", "three" },
|
||||
new Dictionary<string, string>()
|
||||
);
|
||||
|
||||
@@ -384,11 +349,7 @@ public class OptionBindingSpecs : SpecsBase
|
||||
exitCode.Should().Be(0);
|
||||
|
||||
var stdOut = FakeConsole.ReadOutputString();
|
||||
stdOut.Should().ConsistOfLines(
|
||||
"one",
|
||||
"two",
|
||||
"three"
|
||||
);
|
||||
stdOut.Should().ConsistOfLines("one", "two", "three");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -422,12 +383,7 @@ public class OptionBindingSpecs : SpecsBase
|
||||
|
||||
// Act
|
||||
var exitCode = await application.RunAsync(
|
||||
new[]
|
||||
{
|
||||
"-f", "one",
|
||||
"-f", "two",
|
||||
"-f", "three"
|
||||
},
|
||||
new[] { "-f", "one", "-f", "two", "-f", "three" },
|
||||
new Dictionary<string, string>()
|
||||
);
|
||||
|
||||
@@ -435,11 +391,7 @@ public class OptionBindingSpecs : SpecsBase
|
||||
exitCode.Should().Be(0);
|
||||
|
||||
var stdOut = FakeConsole.ReadOutputString();
|
||||
stdOut.Should().ConsistOfLines(
|
||||
"one",
|
||||
"two",
|
||||
"three"
|
||||
);
|
||||
stdOut.Should().ConsistOfLines("one", "two", "three");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -473,12 +425,7 @@ public class OptionBindingSpecs : SpecsBase
|
||||
|
||||
// Act
|
||||
var exitCode = await application.RunAsync(
|
||||
new[]
|
||||
{
|
||||
"--foo", "one",
|
||||
"-f", "two",
|
||||
"--foo", "three"
|
||||
},
|
||||
new[] { "--foo", "one", "-f", "two", "--foo", "three" },
|
||||
new Dictionary<string, string>()
|
||||
);
|
||||
|
||||
@@ -486,11 +433,7 @@ public class OptionBindingSpecs : SpecsBase
|
||||
exitCode.Should().Be(0);
|
||||
|
||||
var stdOut = FakeConsole.ReadOutputString();
|
||||
stdOut.Should().ConsistOfLines(
|
||||
"one",
|
||||
"two",
|
||||
"three"
|
||||
);
|
||||
stdOut.Should().ConsistOfLines("one", "two", "three");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -535,10 +478,7 @@ public class OptionBindingSpecs : SpecsBase
|
||||
exitCode.Should().Be(0);
|
||||
|
||||
var stdOut = FakeConsole.ReadOutputString();
|
||||
stdOut.Should().ConsistOfLines(
|
||||
"Foo = one",
|
||||
"Bar = hello"
|
||||
);
|
||||
stdOut.Should().ConsistOfLines("Foo = one", "Bar = hello");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -604,24 +544,13 @@ public class OptionBindingSpecs : SpecsBase
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
var exitCode = await application.RunAsync(
|
||||
new[]
|
||||
{
|
||||
"--foo", "42",
|
||||
"--bar",
|
||||
"--baz", "xyz"
|
||||
}
|
||||
);
|
||||
var exitCode = await application.RunAsync(new[] { "--foo", "42", "--bar", "--baz", "xyz" });
|
||||
|
||||
// Assert
|
||||
exitCode.Should().Be(0);
|
||||
|
||||
var stdOut = FakeConsole.ReadOutputString();
|
||||
stdOut.Should().ConsistOfLines(
|
||||
"Foo = 42",
|
||||
"Bar = True",
|
||||
"Baz = xyz"
|
||||
);
|
||||
stdOut.Should().ConsistOfLines("Foo = 42", "Bar = True", "Baz = xyz");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -797,11 +726,7 @@ public class OptionBindingSpecs : SpecsBase
|
||||
|
||||
// Act
|
||||
var exitCode = await application.RunAsync(
|
||||
new[]
|
||||
{
|
||||
"--foo", "one",
|
||||
"--bar", "two"
|
||||
},
|
||||
new[] { "--foo", "one", "--bar", "two" },
|
||||
new Dictionary<string, string>()
|
||||
);
|
||||
|
||||
|
||||
@@ -11,9 +11,7 @@ namespace CliFx.Tests;
|
||||
public class ParameterBindingSpecs : SpecsBase
|
||||
{
|
||||
public ParameterBindingSpecs(ITestOutputHelper testOutput)
|
||||
: base(testOutput)
|
||||
{
|
||||
}
|
||||
: base(testOutput) { }
|
||||
|
||||
[Fact]
|
||||
public async Task I_can_bind_a_parameter_to_a_property_and_get_the_value_from_the_corresponding_argument()
|
||||
@@ -57,10 +55,7 @@ public class ParameterBindingSpecs : SpecsBase
|
||||
exitCode.Should().Be(0);
|
||||
|
||||
var stdOut = FakeConsole.ReadOutputString();
|
||||
stdOut.Should().ConsistOfLines(
|
||||
"Foo = one",
|
||||
"Bar = two"
|
||||
);
|
||||
stdOut.Should().ConsistOfLines("Foo = one", "Bar = two");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -114,13 +109,9 @@ public class ParameterBindingSpecs : SpecsBase
|
||||
exitCode.Should().Be(0);
|
||||
|
||||
var stdOut = FakeConsole.ReadOutputString();
|
||||
stdOut.Should().ConsistOfLines(
|
||||
"Foo = one",
|
||||
"Bar = two",
|
||||
"Baz = three",
|
||||
"Baz = four",
|
||||
"Baz = five"
|
||||
);
|
||||
stdOut
|
||||
.Should()
|
||||
.ConsistOfLines("Foo = one", "Bar = two", "Baz = three", "Baz = four", "Baz = five");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -243,10 +234,7 @@ public class ParameterBindingSpecs : SpecsBase
|
||||
exitCode.Should().Be(0);
|
||||
|
||||
var stdOut = FakeConsole.ReadOutputString();
|
||||
stdOut.Should().ConsistOfLines(
|
||||
"Foo = abc",
|
||||
"Bar = xyz"
|
||||
);
|
||||
stdOut.Should().ConsistOfLines("Foo = abc", "Bar = xyz");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
||||
@@ -11,9 +11,7 @@ namespace CliFx.Tests;
|
||||
public class RoutingSpecs : SpecsBase
|
||||
{
|
||||
public RoutingSpecs(ITestOutputHelper testOutput)
|
||||
: base(testOutput)
|
||||
{
|
||||
}
|
||||
: base(testOutput) { }
|
||||
|
||||
[Fact]
|
||||
public async Task I_can_configure_a_command_to_be_executed_by_default_when_the_user_does_not_specify_a_command_name()
|
||||
|
||||
@@ -11,8 +11,7 @@ public abstract class SpecsBase : IDisposable
|
||||
|
||||
public FakeInMemoryConsole FakeConsole { get; } = new();
|
||||
|
||||
protected SpecsBase(ITestOutputHelper testOutput) =>
|
||||
TestOutput = testOutput;
|
||||
protected SpecsBase(ITestOutputHelper testOutput) => TestOutput = testOutput;
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
|
||||
@@ -13,9 +13,7 @@ namespace CliFx.Tests;
|
||||
public class TypeActivationSpecs : SpecsBase
|
||||
{
|
||||
public TypeActivationSpecs(ITestOutputHelper testOutput)
|
||||
: base(testOutput)
|
||||
{
|
||||
}
|
||||
: base(testOutput) { }
|
||||
|
||||
[Fact]
|
||||
public async Task I_can_configure_the_application_to_use_the_default_type_activator_to_initialize_types_through_parameterless_constructors()
|
||||
|
||||
@@ -31,8 +31,7 @@ internal static class DynamicCommandBuilder
|
||||
};
|
||||
|
||||
// Get default CliFx namespaces
|
||||
var defaultCliFxNamespaces = typeof(ICommand)
|
||||
.Assembly
|
||||
var defaultCliFxNamespaces = typeof(ICommand).Assembly
|
||||
.GetTypes()
|
||||
.Where(t => t.IsPublic)
|
||||
.Select(t => t.Namespace)
|
||||
@@ -41,10 +40,10 @@ internal static class DynamicCommandBuilder
|
||||
|
||||
// Append default imports to the source code
|
||||
var sourceCodeWithUsings =
|
||||
string.Join(Environment.NewLine, defaultSystemNamespaces.Select(n => $"using {n};")) +
|
||||
string.Join(Environment.NewLine, defaultCliFxNamespaces.Select(n => $"using {n};")) +
|
||||
Environment.NewLine +
|
||||
sourceCode;
|
||||
string.Join(Environment.NewLine, defaultSystemNamespaces.Select(n => $"using {n};"))
|
||||
+ string.Join(Environment.NewLine, defaultCliFxNamespaces.Select(n => $"using {n};"))
|
||||
+ Environment.NewLine
|
||||
+ sourceCode;
|
||||
|
||||
// Parse the source code
|
||||
var ast = SyntaxFactory.ParseSyntaxTree(
|
||||
@@ -58,7 +57,11 @@ internal static class DynamicCommandBuilder
|
||||
new[] { ast },
|
||||
Net70.References.All
|
||||
.Append(MetadataReference.CreateFromFile(typeof(ICommand).Assembly.Location))
|
||||
.Append(MetadataReference.CreateFromFile(typeof(DynamicCommandBuilder).Assembly.Location)),
|
||||
.Append(
|
||||
MetadataReference.CreateFromFile(
|
||||
typeof(DynamicCommandBuilder).Assembly.Location
|
||||
)
|
||||
),
|
||||
// DLL to avoid having to define the Main() method
|
||||
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)
|
||||
);
|
||||
@@ -82,8 +85,7 @@ internal static class DynamicCommandBuilder
|
||||
using var buffer = new MemoryStream();
|
||||
var emit = compilation.Emit(buffer);
|
||||
|
||||
var emitErrors = emit
|
||||
.Diagnostics
|
||||
var emitErrors = emit.Diagnostics
|
||||
.Where(d => d.Severity >= DiagnosticSeverity.Error)
|
||||
.ToArray();
|
||||
|
||||
|
||||
@@ -8,22 +8,22 @@ namespace CliFx.Tests.Utils.Extensions;
|
||||
|
||||
internal static class AssertionExtensions
|
||||
{
|
||||
public static void ConsistOfLines(
|
||||
this StringAssertions assertions,
|
||||
IEnumerable<string> lines)
|
||||
public static void ConsistOfLines(this StringAssertions assertions, IEnumerable<string> lines)
|
||||
{
|
||||
var actualLines = assertions.Subject.Split(new[] {'\n', '\r'}, StringSplitOptions.RemoveEmptyEntries);
|
||||
var actualLines = assertions.Subject.Split(
|
||||
new[] { '\n', '\r' },
|
||||
StringSplitOptions.RemoveEmptyEntries
|
||||
);
|
||||
actualLines.Should().Equal(lines);
|
||||
}
|
||||
|
||||
public static void ConsistOfLines(
|
||||
this StringAssertions assertions,
|
||||
params string[] lines) =>
|
||||
public static void ConsistOfLines(this StringAssertions assertions, params string[] lines) =>
|
||||
assertions.ConsistOfLines((IEnumerable<string>)lines);
|
||||
|
||||
public static AndConstraint<StringAssertions> ContainAllInOrder(
|
||||
this StringAssertions assertions,
|
||||
IEnumerable<string> values)
|
||||
IEnumerable<string> values
|
||||
)
|
||||
{
|
||||
var lastIndex = 0;
|
||||
|
||||
@@ -46,6 +46,6 @@ internal static class AssertionExtensions
|
||||
|
||||
public static AndConstraint<StringAssertions> ContainAllInOrder(
|
||||
this StringAssertions assertions,
|
||||
params string[] values) =>
|
||||
assertions.ContainAllInOrder((IEnumerable<string>) values);
|
||||
params string[] values
|
||||
) => assertions.ContainAllInOrder((IEnumerable<string>)values);
|
||||
}
|
||||
@@ -5,7 +5,10 @@ namespace CliFx.Tests.Utils.Extensions;
|
||||
|
||||
internal static class ConsoleExtensions
|
||||
{
|
||||
public static void DumpToTestOutput(this FakeInMemoryConsole console, ITestOutputHelper testOutputHelper)
|
||||
public static void DumpToTestOutput(
|
||||
this FakeInMemoryConsole console,
|
||||
ITestOutputHelper testOutputHelper
|
||||
)
|
||||
{
|
||||
testOutputHelper.WriteLine("[*] Captured standard output:");
|
||||
testOutputHelper.WriteLine(console.ReadOutputString());
|
||||
|
||||
@@ -24,6 +24,7 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="CSharpier.MsBuild" Version="0.25.0" PrivateAssets="all" />
|
||||
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="all" />
|
||||
<PackageReference Include="PolyShim" Version="1.7.0" PrivateAssets="all" />
|
||||
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.5.4" Condition="'$(TargetFramework)' == 'netstandard2.0'" />
|
||||
|
||||
Reference in New Issue
Block a user