mirror of
https://github.com/Tyrrrz/CliFx.git
synced 2025-10-25 15:19:17 +00:00
Compare commits
51 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b17341b56c | ||
|
|
5bda964fb5 | ||
|
|
432430489a | ||
|
|
9a20101f30 | ||
|
|
b491818779 | ||
|
|
69c24c8dfc | ||
|
|
004f906148 | ||
|
|
ac83233dc2 | ||
|
|
082910c968 | ||
|
|
11e3e0f85d | ||
|
|
42f4d7d5a7 | ||
|
|
bed22b6500 | ||
|
|
17449e0794 | ||
|
|
4732166f5f | ||
|
|
f5e37b96fc | ||
|
|
4cef596fe8 | ||
|
|
19b87717c1 | ||
|
|
7e4c6b20ff | ||
|
|
fb2071ed2b | ||
|
|
7d2f934310 | ||
|
|
95a00b0952 | ||
|
|
cb3fee65f3 | ||
|
|
65628b145a | ||
|
|
802bbfccc6 | ||
|
|
6e7742a4f3 | ||
|
|
f6a1a40471 | ||
|
|
33ca4da260 | ||
|
|
cbb72b16ae | ||
|
|
c58629e999 | ||
|
|
387fb72718 | ||
|
|
e04f0da318 | ||
|
|
d25873ee10 | ||
|
|
a28223fc8b | ||
|
|
1dab27de55 | ||
|
|
698629b153 | ||
|
|
65b66b0d27 | ||
|
|
7d3ba612c4 | ||
|
|
8c3b8d1f49 | ||
|
|
fdd39855ad | ||
|
|
671532efce | ||
|
|
5b124345b0 | ||
|
|
b812bd1423 | ||
|
|
c854f5fb8d | ||
|
|
f38bd32510 | ||
|
|
765fa5503e | ||
|
|
57f168723b | ||
|
|
79e1a2e3d7 | ||
|
|
f4f6d04857 | ||
|
|
015ede0d15 | ||
|
|
4fd7f7c3ca | ||
|
|
896dd49eb4 |
6
.github/workflows/CD.yml
vendored
6
.github/workflows/CD.yml
vendored
@@ -11,10 +11,10 @@ jobs:
|
||||
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v1
|
||||
uses: actions/checkout@v2
|
||||
|
||||
- name: Install .NET Core
|
||||
uses: actions/setup-dotnet@v1
|
||||
uses: actions/setup-dotnet@v1.4.0
|
||||
with:
|
||||
dotnet-version: 3.1.100
|
||||
|
||||
@@ -22,4 +22,4 @@ jobs:
|
||||
run: dotnet pack CliFx --configuration Release
|
||||
|
||||
- name: Deploy
|
||||
run: dotnet nuget push CliFx/bin/Release/*.nupkg -s nuget.org -k ${{secrets.NUGET_TOKEN}}
|
||||
run: dotnet nuget push CliFx/bin/Release/*.nupkg -s nuget.org -k ${{ secrets.NUGET_TOKEN }}
|
||||
|
||||
25
.github/workflows/CI.yml
vendored
25
.github/workflows/CI.yml
vendored
@@ -4,19 +4,32 @@ on: [push, pull_request]
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
runs-on: ${{ matrix.os }}
|
||||
|
||||
strategy:
|
||||
matrix:
|
||||
os: [ubuntu-latest, windows-latest, macos-latest]
|
||||
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v1
|
||||
uses: actions/checkout@v2
|
||||
|
||||
- name: Install .NET Core
|
||||
uses: actions/setup-dotnet@v1
|
||||
uses: actions/setup-dotnet@v1.4.0
|
||||
with:
|
||||
dotnet-version: 3.1.100
|
||||
|
||||
- name: Build & test
|
||||
run: dotnet test --configuration Release
|
||||
run: dotnet test --configuration Release --logger GitHubActions
|
||||
|
||||
- name: Coverage
|
||||
run: curl -s https://codecov.io/bash | bash -s -- -f CliFx.Tests/bin/Release/Coverage.xml -t ${{secrets.CODECOV_TOKEN}} -Z
|
||||
- name: Upload coverage
|
||||
uses: codecov/codecov-action@v1.0.5
|
||||
with:
|
||||
token: ${{ secrets.CODECOV_TOKEN }}
|
||||
file: CliFx.Tests/bin/Release/Coverage.xml
|
||||
|
||||
- name: Upload coverage (analyzers)
|
||||
uses: codecov/codecov-action@v1.0.5
|
||||
with:
|
||||
token: ${{ secrets.CODECOV_TOKEN }}
|
||||
file: CliFx.Analyzers.Tests/bin/Release/Coverage.xml
|
||||
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 20 KiB After Width: | Height: | Size: 24 KiB |
34
Changelog.md
34
Changelog.md
@@ -0,0 +1,34 @@
|
||||
### v1.3.2 (31-Jul-2020)
|
||||
|
||||
- Fixed an issue where a command was incorrectly allowed to execute when the user did not specify any value for a non-scalar parameter. Since they are always required, a parameter needs to be bound to (at least) one value. (Thanks [@Daniel Hix](https://github.com/ADustyOldMuffin))
|
||||
- Fixed an issue where `CliApplication.RunAsync(...)` threw `ArgumentException` if there were two environment variables, whose names differed only in case. Environment variable names are now treated case-sensitively. (Thanks [@Ron Myers](https://github.com/ron-myers))
|
||||
|
||||
### v1.3.1 (19-Jul-2020)
|
||||
|
||||
- Running the application with the debug directive (`myapp [debug]`) will now also try to launch a debugger instance. In most cases it will save time as you won't need to attach the debugger manually. (Thanks [@Volodymyr Shkolka](https://github.com/BlackGad))
|
||||
- Fixed an issue where unhandled generic exceptions (i.e. not `CommandException`) sometimes caused the application to incorrectly return successful exit code due to an overflow issue on Unix systems. Starting from this version, all unhandled generic exceptions will produce `1` as the exit code when thrown. Instances of `CommandException` can still be configured to return any specified exit code, but it's recommended to constrain the values between `1` and `255` to avoid overflow issues. (Thanks [@Ihor Nechyporuk](https://github.com/inech))
|
||||
|
||||
### v1.3 (23-May-2020)
|
||||
|
||||
- Changed analyzers to report errors instead of warnings. If you find that some analyzer works incorrectly, please report it on GitHub. You can also configure inspection severity overrides in your project if you need to.
|
||||
- Improved help text by showing default values for non-required options. This only works on types that have a custom override for `ToString()` method. Additionally, if the type implements `IFormattable`, the overload with a format provider will be used instead. (Thanks [@Domn Werner](https://github.com/domn1995))
|
||||
- Changed default version text to only show 3 version components instead of 4, if the last component (revision) is not specified or is zero. This makes the default version text compliant with semantic versioning.
|
||||
- Fixed an issue where it was possible to define a command with an option that has the same name or short name as built-in help or version options. Previously it would lead to the user-defined option being ignored in favor of the built-in option. Now this will throw an exception instead.
|
||||
- Changed the underlying representation of `StreamReader`/`StreamWriter` objects used in `SystemConsole` and `VirtualConsole` to be thread-safe.
|
||||
|
||||
### v1.2 (11-May-2020)
|
||||
|
||||
- Added built-in Roslyn analyzers that help catch incorrect usage of the library. Currently, all analyzers report issues as warnings so as to not prevent the project from building. In the future that may change.
|
||||
- Added an optional parameter to `new CommandException(...)` called `showHelp` which can be used to instruct CliFx to show help for the current command after printing the error. (Thanks [@Domn Werner](https://github.com/domn1995))
|
||||
- Improved help text shown for enum options and parameters by providing the list of valid values that the enum can accept. (Thanks [@Domn Werner](https://github.com/domn1995))
|
||||
- Fixed an issue where it was possible to set an option without providing a value, while the option was marked as required.
|
||||
- Fixed an issue where it was possible to configure an option with an empty name or a name consisting of a single character. If you want to use a single character as a name, you should set the option's short name instead.
|
||||
- Added `CursorLeft` and `CursorTop` properties to `IConsole` and its implementations. In `VirtualConsole`, these are just auto-properties.
|
||||
- Improved exception messages.
|
||||
- Improved exceptions related to user input by also showing help text after the error message. (Thanks [@Domn Werner](https://github.com/domn1995))
|
||||
|
||||
### v1.1 (16-Mar-2020)
|
||||
|
||||
- Changed `IConsole` interface (and as a result, `SystemConsole` and `VirtualConsole`) to support writing binary data. Instead of `TextReader`/`TextWriter` instances, the streams are now exposed as `StreamReader`/`StreamWriter` which provide the `BaseStream` property that allows raw access. Existing usages inside commands should remain the same because `StreamReader`/`StreamWriter` are compatible with their base classes `TextReader`/`TextWriter`, but if you were using `VirtualConsole` in tests, you may have to update it to the new API. Refer to the readme for more info.
|
||||
- Changed argument binding behavior so that an error is produced if the user provides an argument that doesn't match with any parameter or option. This is done in order to improve user experience, as otherwise the user may make a typo without knowing that their input wasn't taken into account.
|
||||
- Changed argument binding behavior so that options can be set to multiple argument values while specifying them with mixed naming. For example, `--option value1 -o value2 --option value3` would result in the option being set to corresponding three values, assuming `--option` and `-o` match with the same option.
|
||||
|
||||
43
CliFx.Analyzers.Tests/AnalyzerTestCase.cs
Normal file
43
CliFx.Analyzers.Tests/AnalyzerTestCase.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.CodeAnalysis;
|
||||
|
||||
namespace CliFx.Analyzers.Tests
|
||||
{
|
||||
public class AnalyzerTestCase
|
||||
{
|
||||
public string Name { get; }
|
||||
|
||||
public IReadOnlyList<DiagnosticDescriptor> TestedDiagnostics { get; }
|
||||
|
||||
public IReadOnlyList<string> SourceCodes { get; }
|
||||
|
||||
public AnalyzerTestCase(
|
||||
string name,
|
||||
IReadOnlyList<DiagnosticDescriptor> testedDiagnostics,
|
||||
IReadOnlyList<string> sourceCodes)
|
||||
{
|
||||
Name = name;
|
||||
TestedDiagnostics = testedDiagnostics;
|
||||
SourceCodes = sourceCodes;
|
||||
}
|
||||
|
||||
public AnalyzerTestCase(
|
||||
string name,
|
||||
IReadOnlyList<DiagnosticDescriptor> testedDiagnostics,
|
||||
string sourceCode)
|
||||
: this(name, testedDiagnostics, new[] {sourceCode})
|
||||
{
|
||||
}
|
||||
|
||||
public AnalyzerTestCase(
|
||||
string name,
|
||||
DiagnosticDescriptor testedDiagnostic,
|
||||
string sourceCode)
|
||||
: this(name, new[] {testedDiagnostic}, sourceCode)
|
||||
{
|
||||
}
|
||||
|
||||
public override string ToString() => $"{Name} [{string.Join(", ", TestedDiagnostics.Select(d => d.Id))}]";
|
||||
}
|
||||
}
|
||||
29
CliFx.Analyzers.Tests/CliFx.Analyzers.Tests.csproj
Normal file
29
CliFx.Analyzers.Tests/CliFx.Analyzers.Tests.csproj
Normal file
@@ -0,0 +1,29 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Import Project="../CliFx.props" />
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
<IsPackable>false</IsPackable>
|
||||
<IsTestProject>true</IsTestProject>
|
||||
<CollectCoverage>true</CollectCoverage>
|
||||
<CoverletOutputFormat>opencover</CoverletOutputFormat>
|
||||
<CoverletOutput>bin/$(Configuration)/Coverage.xml</CoverletOutput>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Gu.Roslyn.Asserts" Version="3.3.0" />
|
||||
<PackageReference Include="GitHubActionsTestLogger" Version="1.0.0" />
|
||||
<PackageReference Include="FluentAssertions" Version="5.10.2" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
|
||||
<PackageReference Include="xunit" Version="2.4.0" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
|
||||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.4.0" />
|
||||
<PackageReference Include="coverlet.msbuild" Version="2.8.0" PrivateAssets="all" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\CliFx.Analyzers\CliFx.Analyzers.csproj" />
|
||||
<ProjectReference Include="..\CliFx\CliFx.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
489
CliFx.Analyzers.Tests/CommandSchemaAnalyzerTests.cs
Normal file
489
CliFx.Analyzers.Tests/CommandSchemaAnalyzerTests.cs
Normal file
@@ -0,0 +1,489 @@
|
||||
using System.Collections.Generic;
|
||||
using CliFx.Analyzers.Tests.Internal;
|
||||
using Microsoft.CodeAnalysis.Diagnostics;
|
||||
using Xunit;
|
||||
|
||||
namespace CliFx.Analyzers.Tests
|
||||
{
|
||||
public class CommandSchemaAnalyzerTests
|
||||
{
|
||||
private static DiagnosticAnalyzer Analyzer { get; } = new CommandSchemaAnalyzer();
|
||||
|
||||
public static IEnumerable<object[]> GetValidCases()
|
||||
{
|
||||
yield return new object[]
|
||||
{
|
||||
new AnalyzerTestCase(
|
||||
"Non-command type",
|
||||
Analyzer.SupportedDiagnostics,
|
||||
|
||||
// language=cs
|
||||
@"
|
||||
public class Foo
|
||||
{
|
||||
public int Bar { get; set; } = 5;
|
||||
}"
|
||||
)
|
||||
};
|
||||
|
||||
yield return new object[]
|
||||
{
|
||||
new AnalyzerTestCase(
|
||||
"Command implements interface and has attribute",
|
||||
Analyzer.SupportedDiagnostics,
|
||||
|
||||
// language=cs
|
||||
@"
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}"
|
||||
)
|
||||
};
|
||||
|
||||
yield return new object[]
|
||||
{
|
||||
new AnalyzerTestCase(
|
||||
"Command doesn't have an attribute but is an abstract type",
|
||||
Analyzer.SupportedDiagnostics,
|
||||
|
||||
// language=cs
|
||||
@"
|
||||
public abstract class MyCommand : ICommand
|
||||
{
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}"
|
||||
)
|
||||
};
|
||||
|
||||
yield return new object[]
|
||||
{
|
||||
new AnalyzerTestCase(
|
||||
"Parameters with unique order",
|
||||
Analyzer.SupportedDiagnostics,
|
||||
|
||||
// language=cs
|
||||
@"
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
[CommandParameter(13)]
|
||||
public string ParamA { get; set; }
|
||||
|
||||
[CommandParameter(15)]
|
||||
public string ParamB { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}"
|
||||
)
|
||||
};
|
||||
|
||||
yield return new object[]
|
||||
{
|
||||
new AnalyzerTestCase(
|
||||
"Parameters with unique names",
|
||||
Analyzer.SupportedDiagnostics,
|
||||
|
||||
// language=cs
|
||||
@"
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
[CommandParameter(13, Name = ""foo"")]
|
||||
public string ParamA { get; set; }
|
||||
|
||||
[CommandParameter(15, Name = ""bar"")]
|
||||
public string ParamB { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}"
|
||||
)
|
||||
};
|
||||
|
||||
yield return new object[]
|
||||
{
|
||||
new AnalyzerTestCase(
|
||||
"Single non-scalar parameter",
|
||||
Analyzer.SupportedDiagnostics,
|
||||
|
||||
// language=cs
|
||||
@"
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
[CommandParameter(1)]
|
||||
public string ParamA { get; set; }
|
||||
|
||||
[CommandParameter(2)]
|
||||
public HashSet<string> ParamB { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}"
|
||||
)
|
||||
};
|
||||
|
||||
yield return new object[]
|
||||
{
|
||||
new AnalyzerTestCase(
|
||||
"Non-scalar parameter is last in order",
|
||||
Analyzer.SupportedDiagnostics,
|
||||
|
||||
// language=cs
|
||||
@"
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
[CommandParameter(1)]
|
||||
public string ParamA { get; set; }
|
||||
|
||||
[CommandParameter(2)]
|
||||
public IReadOnlyList<string> ParamB { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}"
|
||||
)
|
||||
};
|
||||
|
||||
yield return new object[]
|
||||
{
|
||||
new AnalyzerTestCase(
|
||||
"Option with a proper name",
|
||||
Analyzer.SupportedDiagnostics,
|
||||
|
||||
// language=cs
|
||||
@"
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
[CommandOption(""foo"")]
|
||||
public string Param { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}"
|
||||
)
|
||||
};
|
||||
|
||||
yield return new object[]
|
||||
{
|
||||
new AnalyzerTestCase(
|
||||
"Option with a proper name and short name",
|
||||
Analyzer.SupportedDiagnostics,
|
||||
|
||||
// language=cs
|
||||
@"
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
[CommandOption(""foo"", 'f')]
|
||||
public string Param { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}"
|
||||
)
|
||||
};
|
||||
|
||||
yield return new object[]
|
||||
{
|
||||
new AnalyzerTestCase(
|
||||
"Options with unique names",
|
||||
Analyzer.SupportedDiagnostics,
|
||||
|
||||
// language=cs
|
||||
@"
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
[CommandOption(""foo"")]
|
||||
public string ParamA { get; set; }
|
||||
|
||||
[CommandOption(""bar"")]
|
||||
public string ParamB { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}"
|
||||
)
|
||||
};
|
||||
|
||||
yield return new object[]
|
||||
{
|
||||
new AnalyzerTestCase(
|
||||
"Options with unique short names",
|
||||
Analyzer.SupportedDiagnostics,
|
||||
|
||||
// language=cs
|
||||
@"
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
[CommandOption('f')]
|
||||
public string ParamA { get; set; }
|
||||
|
||||
[CommandOption('x')]
|
||||
public string ParamB { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}"
|
||||
)
|
||||
};
|
||||
|
||||
yield return new object[]
|
||||
{
|
||||
new AnalyzerTestCase(
|
||||
"Options with unique environment variable names",
|
||||
Analyzer.SupportedDiagnostics,
|
||||
|
||||
// language=cs
|
||||
@"
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
[CommandOption('a', EnvironmentVariableName = ""env_var_a"")]
|
||||
public string ParamA { get; set; }
|
||||
|
||||
[CommandOption('b', EnvironmentVariableName = ""env_var_b"")]
|
||||
public string ParamB { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}"
|
||||
)
|
||||
};
|
||||
}
|
||||
|
||||
public static IEnumerable<object[]> GetInvalidCases()
|
||||
{
|
||||
yield return new object[]
|
||||
{
|
||||
new AnalyzerTestCase(
|
||||
"Command is missing the attribute",
|
||||
DiagnosticDescriptors.CliFx0002,
|
||||
|
||||
// language=cs
|
||||
@"
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}"
|
||||
)
|
||||
};
|
||||
|
||||
yield return new object[]
|
||||
{
|
||||
new AnalyzerTestCase(
|
||||
"Command doesn't implement the interface",
|
||||
DiagnosticDescriptors.CliFx0001,
|
||||
|
||||
// language=cs
|
||||
@"
|
||||
[Command]
|
||||
public class MyCommand
|
||||
{
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}"
|
||||
)
|
||||
};
|
||||
|
||||
yield return new object[]
|
||||
{
|
||||
new AnalyzerTestCase(
|
||||
"Parameters with duplicate order",
|
||||
DiagnosticDescriptors.CliFx0021,
|
||||
|
||||
// language=cs
|
||||
@"
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
[CommandParameter(13)]
|
||||
public string ParamA { get; set; }
|
||||
|
||||
[CommandParameter(13)]
|
||||
public string ParamB { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}"
|
||||
)
|
||||
};
|
||||
|
||||
yield return new object[]
|
||||
{
|
||||
new AnalyzerTestCase(
|
||||
"Parameters with duplicate names",
|
||||
DiagnosticDescriptors.CliFx0022,
|
||||
|
||||
// language=cs
|
||||
@"
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
[CommandParameter(13, Name = ""foo"")]
|
||||
public string ParamA { get; set; }
|
||||
|
||||
[CommandParameter(15, Name = ""foo"")]
|
||||
public string ParamB { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}"
|
||||
)
|
||||
};
|
||||
|
||||
yield return new object[]
|
||||
{
|
||||
new AnalyzerTestCase(
|
||||
"Multiple non-scalar parameters",
|
||||
DiagnosticDescriptors.CliFx0023,
|
||||
|
||||
// language=cs
|
||||
@"
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
[CommandParameter(1)]
|
||||
public IReadOnlyList<string> ParamA { get; set; }
|
||||
|
||||
[CommandParameter(2)]
|
||||
public HashSet<string> ParamB { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}"
|
||||
)
|
||||
};
|
||||
|
||||
yield return new object[]
|
||||
{
|
||||
new AnalyzerTestCase(
|
||||
"Non-last non-scalar parameter",
|
||||
DiagnosticDescriptors.CliFx0024,
|
||||
|
||||
// language=cs
|
||||
@"
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
[CommandParameter(1)]
|
||||
public IReadOnlyList<string> ParamA { get; set; }
|
||||
|
||||
[CommandParameter(2)]
|
||||
public string ParamB { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}"
|
||||
)
|
||||
};
|
||||
|
||||
yield return new object[]
|
||||
{
|
||||
new AnalyzerTestCase(
|
||||
"Option with an empty name",
|
||||
DiagnosticDescriptors.CliFx0041,
|
||||
|
||||
// language=cs
|
||||
@"
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
[CommandOption("""")]
|
||||
public string Param { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}"
|
||||
)
|
||||
};
|
||||
|
||||
yield return new object[]
|
||||
{
|
||||
new AnalyzerTestCase(
|
||||
"Option with a name which is too short",
|
||||
DiagnosticDescriptors.CliFx0042,
|
||||
|
||||
// language=cs
|
||||
@"
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
[CommandOption(""a"")]
|
||||
public string Param { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}"
|
||||
)
|
||||
};
|
||||
|
||||
yield return new object[]
|
||||
{
|
||||
new AnalyzerTestCase(
|
||||
"Options with duplicate names",
|
||||
DiagnosticDescriptors.CliFx0043,
|
||||
|
||||
// language=cs
|
||||
@"
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
[CommandOption(""foo"")]
|
||||
public string ParamA { get; set; }
|
||||
|
||||
[CommandOption(""foo"")]
|
||||
public string ParamB { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}"
|
||||
)
|
||||
};
|
||||
|
||||
yield return new object[]
|
||||
{
|
||||
new AnalyzerTestCase(
|
||||
"Options with duplicate short names",
|
||||
DiagnosticDescriptors.CliFx0044,
|
||||
|
||||
// language=cs
|
||||
@"
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
[CommandOption('f')]
|
||||
public string ParamA { get; set; }
|
||||
|
||||
[CommandOption('f')]
|
||||
public string ParamB { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}"
|
||||
)
|
||||
};
|
||||
|
||||
yield return new object[]
|
||||
{
|
||||
new AnalyzerTestCase(
|
||||
"Options with duplicate environment variable names",
|
||||
DiagnosticDescriptors.CliFx0045,
|
||||
|
||||
// language=cs
|
||||
@"
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
[CommandOption('a', EnvironmentVariableName = ""env_var"")]
|
||||
public string ParamA { get; set; }
|
||||
|
||||
[CommandOption('b', EnvironmentVariableName = ""env_var"")]
|
||||
public string ParamB { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}"
|
||||
)
|
||||
};
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(GetValidCases))]
|
||||
public void Valid(AnalyzerTestCase testCase) =>
|
||||
Analyzer.Should().NotProduceDiagnostics(testCase);
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(GetInvalidCases))]
|
||||
public void Invalid(AnalyzerTestCase testCase) =>
|
||||
Analyzer.Should().ProduceDiagnostics(testCase);
|
||||
}
|
||||
}
|
||||
144
CliFx.Analyzers.Tests/ConsoleUsageAnalyzerTests.cs
Normal file
144
CliFx.Analyzers.Tests/ConsoleUsageAnalyzerTests.cs
Normal file
@@ -0,0 +1,144 @@
|
||||
using System.Collections.Generic;
|
||||
using CliFx.Analyzers.Tests.Internal;
|
||||
using Microsoft.CodeAnalysis.Diagnostics;
|
||||
using Xunit;
|
||||
|
||||
namespace CliFx.Analyzers.Tests
|
||||
{
|
||||
public class ConsoleUsageAnalyzerTests
|
||||
{
|
||||
private static DiagnosticAnalyzer Analyzer { get; } = new ConsoleUsageAnalyzer();
|
||||
|
||||
public static IEnumerable<object[]> GetValidCases()
|
||||
{
|
||||
yield return new object[]
|
||||
{
|
||||
new AnalyzerTestCase(
|
||||
"Using console abstraction",
|
||||
Analyzer.SupportedDiagnostics,
|
||||
|
||||
// language=cs
|
||||
@"
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
public ValueTask ExecuteAsync(IConsole console)
|
||||
{
|
||||
console.Output.WriteLine(""Hello world"");
|
||||
return default;
|
||||
}
|
||||
}"
|
||||
)
|
||||
};
|
||||
|
||||
yield return new object[]
|
||||
{
|
||||
new AnalyzerTestCase(
|
||||
"Console abstraction is not available in scope",
|
||||
Analyzer.SupportedDiagnostics,
|
||||
|
||||
// language=cs
|
||||
@"
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
public void SomeOtherMethod() => Console.WriteLine(""Test"");
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}"
|
||||
)
|
||||
};
|
||||
}
|
||||
|
||||
public static IEnumerable<object[]> GetInvalidCases()
|
||||
{
|
||||
yield return new object[]
|
||||
{
|
||||
new AnalyzerTestCase(
|
||||
"Not using available console abstraction in the ExecuteAsync method",
|
||||
Analyzer.SupportedDiagnostics,
|
||||
|
||||
// language=cs
|
||||
@"
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
public ValueTask ExecuteAsync(IConsole console)
|
||||
{
|
||||
Console.WriteLine(""Hello world"");
|
||||
return default;
|
||||
}
|
||||
}"
|
||||
)
|
||||
};
|
||||
|
||||
yield return new object[]
|
||||
{
|
||||
new AnalyzerTestCase(
|
||||
"Not using available console abstraction in the ExecuteAsync method when writing stderr",
|
||||
Analyzer.SupportedDiagnostics,
|
||||
|
||||
// language=cs
|
||||
@"
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
public ValueTask ExecuteAsync(IConsole console)
|
||||
{
|
||||
Console.Error.WriteLine(""Hello world"");
|
||||
return default;
|
||||
}
|
||||
}"
|
||||
)
|
||||
};
|
||||
|
||||
yield return new object[]
|
||||
{
|
||||
new AnalyzerTestCase(
|
||||
"Not using available console abstraction while referencing System.Console by full name",
|
||||
Analyzer.SupportedDiagnostics,
|
||||
|
||||
// language=cs
|
||||
@"
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
public ValueTask ExecuteAsync(IConsole console)
|
||||
{
|
||||
System.Console.Error.WriteLine(""Hello world"");
|
||||
return default;
|
||||
}
|
||||
}"
|
||||
)
|
||||
};
|
||||
|
||||
yield return new object[]
|
||||
{
|
||||
new AnalyzerTestCase(
|
||||
"Not using available console abstraction in another method",
|
||||
DiagnosticDescriptors.CliFx0100,
|
||||
|
||||
// language=cs
|
||||
@"
|
||||
[Command]
|
||||
public class MyCommand : ICommand
|
||||
{
|
||||
public void SomeOtherMethod(IConsole console) => Console.WriteLine(""Test"");
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}"
|
||||
)
|
||||
};
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(GetValidCases))]
|
||||
public void Valid(AnalyzerTestCase testCase) =>
|
||||
Analyzer.Should().NotProduceDiagnostics(testCase);
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(GetInvalidCases))]
|
||||
public void Invalid(AnalyzerTestCase testCase) =>
|
||||
Analyzer.Should().ProduceDiagnostics(testCase);
|
||||
}
|
||||
}
|
||||
107
CliFx.Analyzers.Tests/Internal/AnalyzerAssertions.cs
Normal file
107
CliFx.Analyzers.Tests/Internal/AnalyzerAssertions.cs
Normal file
@@ -0,0 +1,107 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using FluentAssertions.Execution;
|
||||
using FluentAssertions.Primitives;
|
||||
using Gu.Roslyn.Asserts;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.CSharp;
|
||||
using Microsoft.CodeAnalysis.Diagnostics;
|
||||
|
||||
namespace CliFx.Analyzers.Tests.Internal
|
||||
{
|
||||
internal partial class AnalyzerAssertions : ReferenceTypeAssertions<DiagnosticAnalyzer, AnalyzerAssertions>
|
||||
{
|
||||
protected override string Identifier { get; } = "analyzer";
|
||||
|
||||
public AnalyzerAssertions(DiagnosticAnalyzer analyzer)
|
||||
: base(analyzer)
|
||||
{
|
||||
}
|
||||
|
||||
public void ProduceDiagnostics(
|
||||
IReadOnlyList<DiagnosticDescriptor> diagnostics,
|
||||
IReadOnlyList<string> sourceCodes)
|
||||
{
|
||||
var producedDiagnostics = GetProducedDiagnostics(Subject, sourceCodes);
|
||||
|
||||
var expectedIds = diagnostics.Select(d => d.Id).Distinct().OrderBy(d => d).ToArray();
|
||||
var producedIds = producedDiagnostics.Select(d => d.Id).Distinct().OrderBy(d => d).ToArray();
|
||||
|
||||
var result = expectedIds.Intersect(producedIds).Count() == expectedIds.Length;
|
||||
|
||||
Execute.Assertion.ForCondition(result).FailWith($@"
|
||||
Expected and produced diagnostics do not match.
|
||||
|
||||
Expected: {string.Join(", ", expectedIds)}
|
||||
Produced: {(producedIds.Any() ? string.Join(", ", producedIds) : "<none>")}
|
||||
".Trim());
|
||||
}
|
||||
|
||||
public void ProduceDiagnostics(AnalyzerTestCase testCase) =>
|
||||
ProduceDiagnostics(testCase.TestedDiagnostics, testCase.SourceCodes);
|
||||
|
||||
public void NotProduceDiagnostics(
|
||||
IReadOnlyList<DiagnosticDescriptor> diagnostics,
|
||||
IReadOnlyList<string> sourceCodes)
|
||||
{
|
||||
var producedDiagnostics = GetProducedDiagnostics(Subject, sourceCodes);
|
||||
|
||||
var expectedIds = diagnostics.Select(d => d.Id).Distinct().OrderBy(d => d).ToArray();
|
||||
var producedIds = producedDiagnostics.Select(d => d.Id).Distinct().OrderBy(d => d).ToArray();
|
||||
|
||||
var result = !expectedIds.Intersect(producedIds).Any();
|
||||
|
||||
Execute.Assertion.ForCondition(result).FailWith($@"
|
||||
Expected no produced diagnostics.
|
||||
|
||||
Produced: {string.Join(", ", producedIds)}
|
||||
".Trim());
|
||||
}
|
||||
|
||||
public void NotProduceDiagnostics(AnalyzerTestCase testCase) =>
|
||||
NotProduceDiagnostics(testCase.TestedDiagnostics, testCase.SourceCodes);
|
||||
}
|
||||
|
||||
internal partial class AnalyzerAssertions
|
||||
{
|
||||
private static IReadOnlyList<MetadataReference> DefaultMetadataReferences { get; } =
|
||||
MetadataReferences.Transitive(typeof(CliApplication).Assembly).ToArray();
|
||||
|
||||
private static string WrapCodeWithUsingDirectives(string code)
|
||||
{
|
||||
var usingDirectives = new[]
|
||||
{
|
||||
"using System;",
|
||||
"using System.Collections.Generic;",
|
||||
"using System.Threading.Tasks;",
|
||||
"using CliFx;",
|
||||
"using CliFx.Attributes;",
|
||||
"using CliFx.Exceptions;",
|
||||
"using CliFx.Utilities;"
|
||||
};
|
||||
|
||||
return
|
||||
string.Join(Environment.NewLine, usingDirectives) +
|
||||
Environment.NewLine +
|
||||
code;
|
||||
}
|
||||
|
||||
private static IReadOnlyList<Diagnostic> GetProducedDiagnostics(
|
||||
DiagnosticAnalyzer analyzer,
|
||||
IReadOnlyList<string> sourceCodes)
|
||||
{
|
||||
var compilationOptions = new CSharpCompilationOptions(OutputKind.ConsoleApplication);
|
||||
var wrappedSourceCodes = sourceCodes.Select(WrapCodeWithUsingDirectives).ToArray();
|
||||
|
||||
return Analyze.GetDiagnostics(analyzer, wrappedSourceCodes, compilationOptions, DefaultMetadataReferences)
|
||||
.SelectMany(d => d)
|
||||
.ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
internal static class AnalyzerAssertionsExtensions
|
||||
{
|
||||
public static AnalyzerAssertions Should(this DiagnosticAnalyzer analyzer) => new AnalyzerAssertions(analyzer);
|
||||
}
|
||||
}
|
||||
13
CliFx.Analyzers/CliFx.Analyzers.csproj
Normal file
13
CliFx.Analyzers/CliFx.Analyzers.csproj
Normal file
@@ -0,0 +1,13 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Import Project="../CliFx.props" />
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
<Nullable>annotations</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.4.0" PrivateAssets="all" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
297
CliFx.Analyzers/CommandSchemaAnalyzer.cs
Normal file
297
CliFx.Analyzers/CommandSchemaAnalyzer.cs
Normal file
@@ -0,0 +1,297 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Linq;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.Diagnostics;
|
||||
|
||||
namespace CliFx.Analyzers
|
||||
{
|
||||
[DiagnosticAnalyzer(LanguageNames.CSharp)]
|
||||
public class CommandSchemaAnalyzer : DiagnosticAnalyzer
|
||||
{
|
||||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(
|
||||
DiagnosticDescriptors.CliFx0001,
|
||||
DiagnosticDescriptors.CliFx0002,
|
||||
DiagnosticDescriptors.CliFx0021,
|
||||
DiagnosticDescriptors.CliFx0022,
|
||||
DiagnosticDescriptors.CliFx0023,
|
||||
DiagnosticDescriptors.CliFx0024,
|
||||
DiagnosticDescriptors.CliFx0041,
|
||||
DiagnosticDescriptors.CliFx0042,
|
||||
DiagnosticDescriptors.CliFx0043,
|
||||
DiagnosticDescriptors.CliFx0044,
|
||||
DiagnosticDescriptors.CliFx0045
|
||||
);
|
||||
|
||||
private static bool IsScalarType(ITypeSymbol typeSymbol) =>
|
||||
KnownSymbols.IsSystemString(typeSymbol) ||
|
||||
!typeSymbol.AllInterfaces.Select(i => i.ConstructedFrom).Any(KnownSymbols.IsSystemCollectionsGenericIEnumerable);
|
||||
|
||||
private static void CheckCommandParameterProperties(
|
||||
SymbolAnalysisContext context,
|
||||
IReadOnlyList<IPropertySymbol> properties)
|
||||
{
|
||||
var parameters = properties
|
||||
.Select(p =>
|
||||
{
|
||||
var attribute = p
|
||||
.GetAttributes()
|
||||
.First(a => KnownSymbols.IsCommandParameterAttribute(a.AttributeClass));
|
||||
|
||||
var order = attribute
|
||||
.ConstructorArguments
|
||||
.Select(a => a.Value)
|
||||
.FirstOrDefault() as int?;
|
||||
|
||||
var name = attribute
|
||||
.NamedArguments
|
||||
.Where(a => a.Key == "Name")
|
||||
.Select(a => a.Value.Value)
|
||||
.FirstOrDefault() as string;
|
||||
|
||||
return new
|
||||
{
|
||||
Property = p,
|
||||
Order = order,
|
||||
Name = name
|
||||
};
|
||||
})
|
||||
.ToArray();
|
||||
|
||||
// Duplicate order
|
||||
var duplicateOrderParameters = parameters
|
||||
.Where(p => p.Order != null)
|
||||
.GroupBy(p => p.Order)
|
||||
.Where(g => g.Count() > 1)
|
||||
.SelectMany(g => g.AsEnumerable())
|
||||
.ToArray();
|
||||
|
||||
foreach (var parameter in duplicateOrderParameters)
|
||||
{
|
||||
context.ReportDiagnostic(
|
||||
Diagnostic.Create(DiagnosticDescriptors.CliFx0021, parameter.Property.Locations.First()));
|
||||
}
|
||||
|
||||
// Duplicate name
|
||||
var duplicateNameParameters = parameters
|
||||
.Where(p => !string.IsNullOrWhiteSpace(p.Name))
|
||||
.GroupBy(p => p.Name, StringComparer.OrdinalIgnoreCase)
|
||||
.Where(g => g.Count() > 1)
|
||||
.SelectMany(g => g.AsEnumerable())
|
||||
.ToArray();
|
||||
|
||||
foreach (var parameter in duplicateNameParameters)
|
||||
{
|
||||
context.ReportDiagnostic(
|
||||
Diagnostic.Create(DiagnosticDescriptors.CliFx0022, parameter.Property.Locations.First()));
|
||||
}
|
||||
|
||||
// Multiple non-scalar
|
||||
var nonScalarParameters = parameters
|
||||
.Where(p => !IsScalarType(p.Property.Type))
|
||||
.ToArray();
|
||||
|
||||
if (nonScalarParameters.Length > 1)
|
||||
{
|
||||
foreach (var parameter in nonScalarParameters)
|
||||
{
|
||||
context.ReportDiagnostic(
|
||||
Diagnostic.Create(DiagnosticDescriptors.CliFx0023, parameter.Property.Locations.First()));
|
||||
}
|
||||
}
|
||||
|
||||
// Non-last non-scalar
|
||||
var nonLastNonScalarParameter = parameters
|
||||
.OrderByDescending(a => a.Order)
|
||||
.Skip(1)
|
||||
.LastOrDefault(p => !IsScalarType(p.Property.Type));
|
||||
|
||||
if (nonLastNonScalarParameter != null)
|
||||
{
|
||||
context.ReportDiagnostic(
|
||||
Diagnostic.Create(DiagnosticDescriptors.CliFx0024, nonLastNonScalarParameter.Property.Locations.First()));
|
||||
}
|
||||
}
|
||||
|
||||
private static void CheckCommandOptionProperties(
|
||||
SymbolAnalysisContext context,
|
||||
IReadOnlyList<IPropertySymbol> properties)
|
||||
{
|
||||
var options = properties
|
||||
.Select(p =>
|
||||
{
|
||||
var attribute = p
|
||||
.GetAttributes()
|
||||
.First(a => KnownSymbols.IsCommandOptionAttribute(a.AttributeClass));
|
||||
|
||||
var name = attribute
|
||||
.ConstructorArguments
|
||||
.Where(a => KnownSymbols.IsSystemString(a.Type))
|
||||
.Select(a => a.Value)
|
||||
.FirstOrDefault() as string;
|
||||
|
||||
var shortName = attribute
|
||||
.ConstructorArguments
|
||||
.Where(a => KnownSymbols.IsSystemChar(a.Type))
|
||||
.Select(a => a.Value)
|
||||
.FirstOrDefault() as char?;
|
||||
|
||||
var envVarName = attribute
|
||||
.NamedArguments
|
||||
.Where(a => a.Key == "EnvironmentVariableName")
|
||||
.Select(a => a.Value.Value)
|
||||
.FirstOrDefault() as string;
|
||||
|
||||
return new
|
||||
{
|
||||
Property = p,
|
||||
Name = name,
|
||||
ShortName = shortName,
|
||||
EnvironmentVariableName = envVarName
|
||||
};
|
||||
})
|
||||
.ToArray();
|
||||
|
||||
// No name
|
||||
var noNameOptions = options
|
||||
.Where(o => string.IsNullOrWhiteSpace(o.Name) && o.ShortName == null)
|
||||
.ToArray();
|
||||
|
||||
foreach (var option in noNameOptions)
|
||||
{
|
||||
context.ReportDiagnostic(
|
||||
Diagnostic.Create(DiagnosticDescriptors.CliFx0041, option.Property.Locations.First()));
|
||||
}
|
||||
|
||||
// Too short name
|
||||
var invalidNameLengthOptions = options
|
||||
.Where(o => !string.IsNullOrWhiteSpace(o.Name) && o.Name.Length <= 1)
|
||||
.ToArray();
|
||||
|
||||
foreach (var option in invalidNameLengthOptions)
|
||||
{
|
||||
context.ReportDiagnostic(
|
||||
Diagnostic.Create(DiagnosticDescriptors.CliFx0042, option.Property.Locations.First()));
|
||||
}
|
||||
|
||||
// Duplicate name
|
||||
var duplicateNameOptions = options
|
||||
.Where(p => !string.IsNullOrWhiteSpace(p.Name))
|
||||
.GroupBy(p => p.Name, StringComparer.OrdinalIgnoreCase)
|
||||
.Where(g => g.Count() > 1)
|
||||
.SelectMany(g => g.AsEnumerable())
|
||||
.ToArray();
|
||||
|
||||
foreach (var option in duplicateNameOptions)
|
||||
{
|
||||
context.ReportDiagnostic(
|
||||
Diagnostic.Create(DiagnosticDescriptors.CliFx0043, option.Property.Locations.First()));
|
||||
}
|
||||
|
||||
// Duplicate name
|
||||
var duplicateShortNameOptions = options
|
||||
.Where(p => p.ShortName != null)
|
||||
.GroupBy(p => p.ShortName)
|
||||
.Where(g => g.Count() > 1)
|
||||
.SelectMany(g => g.AsEnumerable())
|
||||
.ToArray();
|
||||
|
||||
foreach (var option in duplicateShortNameOptions)
|
||||
{
|
||||
context.ReportDiagnostic(
|
||||
Diagnostic.Create(DiagnosticDescriptors.CliFx0044, option.Property.Locations.First()));
|
||||
}
|
||||
|
||||
// Duplicate environment variable name
|
||||
var duplicateEnvironmentVariableNameOptions = options
|
||||
.Where(p => !string.IsNullOrWhiteSpace(p.EnvironmentVariableName))
|
||||
.GroupBy(p => p.EnvironmentVariableName, StringComparer.Ordinal)
|
||||
.Where(g => g.Count() > 1)
|
||||
.SelectMany(g => g.AsEnumerable())
|
||||
.ToArray();
|
||||
|
||||
foreach (var option in duplicateEnvironmentVariableNameOptions)
|
||||
{
|
||||
context.ReportDiagnostic(
|
||||
Diagnostic.Create(DiagnosticDescriptors.CliFx0045, option.Property.Locations.First()));
|
||||
}
|
||||
}
|
||||
|
||||
private static void CheckCommandType(SymbolAnalysisContext context)
|
||||
{
|
||||
// Named type: MyCommand
|
||||
if (!(context.Symbol is INamedTypeSymbol namedTypeSymbol))
|
||||
return;
|
||||
|
||||
// Only classes
|
||||
if (namedTypeSymbol.TypeKind != TypeKind.Class)
|
||||
return;
|
||||
|
||||
// Implements ICommand?
|
||||
var implementsCommandInterface = namedTypeSymbol
|
||||
.AllInterfaces
|
||||
.Any(KnownSymbols.IsCommandInterface);
|
||||
|
||||
// Has CommandAttribute?
|
||||
var hasCommandAttribute = namedTypeSymbol
|
||||
.GetAttributes()
|
||||
.Select(a => a.AttributeClass)
|
||||
.Any(KnownSymbols.IsCommandAttribute);
|
||||
|
||||
var isValidCommandType =
|
||||
// implements interface
|
||||
implementsCommandInterface && (
|
||||
// and either abstract class or has attribute
|
||||
namedTypeSymbol.IsAbstract || hasCommandAttribute
|
||||
);
|
||||
|
||||
if (!isValidCommandType)
|
||||
{
|
||||
// See if this was meant to be a command type (either interface or attribute present)
|
||||
var isAlmostValidCommandType = implementsCommandInterface ^ hasCommandAttribute;
|
||||
|
||||
if (isAlmostValidCommandType && !implementsCommandInterface)
|
||||
context.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.CliFx0001, namedTypeSymbol.Locations.First()));
|
||||
|
||||
if (isAlmostValidCommandType && !hasCommandAttribute)
|
||||
context.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.CliFx0002, namedTypeSymbol.Locations.First()));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
var properties = namedTypeSymbol
|
||||
.GetMembers()
|
||||
.Where(m => m.Kind == SymbolKind.Property)
|
||||
.OfType<IPropertySymbol>().ToArray();
|
||||
|
||||
// Check parameters
|
||||
var parameterProperties = properties
|
||||
.Where(p => p
|
||||
.GetAttributes()
|
||||
.Select(a => a.AttributeClass)
|
||||
.Any(KnownSymbols.IsCommandParameterAttribute))
|
||||
.ToArray();
|
||||
|
||||
CheckCommandParameterProperties(context, parameterProperties);
|
||||
|
||||
// Check options
|
||||
var optionsProperties = properties
|
||||
.Where(p => p
|
||||
.GetAttributes()
|
||||
.Select(a => a.AttributeClass)
|
||||
.Any(KnownSymbols.IsCommandOptionAttribute))
|
||||
.ToArray();
|
||||
|
||||
CheckCommandOptionProperties(context, optionsProperties);
|
||||
}
|
||||
|
||||
public override void Initialize(AnalysisContext context)
|
||||
{
|
||||
context.EnableConcurrentExecution();
|
||||
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
|
||||
|
||||
context.RegisterSymbolAction(CheckCommandType, SymbolKind.NamedType);
|
||||
}
|
||||
}
|
||||
}
|
||||
80
CliFx.Analyzers/ConsoleUsageAnalyzer.cs
Normal file
80
CliFx.Analyzers/ConsoleUsageAnalyzer.cs
Normal file
@@ -0,0 +1,80 @@
|
||||
using System.Collections.Immutable;
|
||||
using System.Linq;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.CSharp;
|
||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||
using Microsoft.CodeAnalysis.Diagnostics;
|
||||
|
||||
namespace CliFx.Analyzers
|
||||
{
|
||||
[DiagnosticAnalyzer(LanguageNames.CSharp)]
|
||||
public class ConsoleUsageAnalyzer : DiagnosticAnalyzer
|
||||
{
|
||||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(
|
||||
DiagnosticDescriptors.CliFx0100
|
||||
);
|
||||
|
||||
private static bool IsSystemConsoleInvocation(
|
||||
SyntaxNodeAnalysisContext context,
|
||||
InvocationExpressionSyntax invocationSyntax)
|
||||
{
|
||||
// Get the method member access (Console.WriteLine or Console.Error.WriteLine)
|
||||
if (!(invocationSyntax.Expression is MemberAccessExpressionSyntax memberAccessSyntax))
|
||||
return false;
|
||||
|
||||
// Get the semantic model for the invoked method
|
||||
if (!(context.SemanticModel.GetSymbolInfo(memberAccessSyntax).Symbol is IMethodSymbol methodSymbol))
|
||||
return false;
|
||||
|
||||
// Check if contained within System.Console
|
||||
if (KnownSymbols.IsSystemConsole(methodSymbol.ContainingType))
|
||||
return true;
|
||||
|
||||
// In case with Console.Error.WriteLine that wouldn't work, we need to check parent member access too
|
||||
if (!(memberAccessSyntax.Expression is MemberAccessExpressionSyntax parentMemberAccessSyntax))
|
||||
return false;
|
||||
|
||||
// Get the semantic model for the parent member
|
||||
if (!(context.SemanticModel.GetSymbolInfo(parentMemberAccessSyntax).Symbol is IPropertySymbol propertySymbol))
|
||||
return false;
|
||||
|
||||
// Check if contained within System.Console
|
||||
if (KnownSymbols.IsSystemConsole(propertySymbol.ContainingType))
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static void CheckSystemConsoleUsage(SyntaxNodeAnalysisContext context)
|
||||
{
|
||||
if (!(context.Node is InvocationExpressionSyntax invocationSyntax))
|
||||
return;
|
||||
|
||||
if (!IsSystemConsoleInvocation(context, invocationSyntax))
|
||||
return;
|
||||
|
||||
// Check if IConsole is available in the scope as a viable alternative
|
||||
var isConsoleInterfaceAvailable = invocationSyntax
|
||||
.Ancestors()
|
||||
.OfType<MethodDeclarationSyntax>()
|
||||
.SelectMany(m => m.ParameterList.Parameters)
|
||||
.Select(p => p.Type)
|
||||
.Select(t => context.SemanticModel.GetSymbolInfo(t).Symbol)
|
||||
.Where(s => s != null)
|
||||
.Any(KnownSymbols.IsConsoleInterface!);
|
||||
|
||||
if (!isConsoleInterfaceAvailable)
|
||||
return;
|
||||
|
||||
context.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.CliFx0100, invocationSyntax.GetLocation()));
|
||||
}
|
||||
|
||||
public override void Initialize(AnalysisContext context)
|
||||
{
|
||||
context.EnableConcurrentExecution();
|
||||
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
|
||||
|
||||
context.RegisterSyntaxNodeAction(CheckSystemConsoleUsage, SyntaxKind.InvocationExpression);
|
||||
}
|
||||
}
|
||||
}
|
||||
79
CliFx.Analyzers/DiagnosticDescriptors.cs
Normal file
79
CliFx.Analyzers/DiagnosticDescriptors.cs
Normal file
@@ -0,0 +1,79 @@
|
||||
using Microsoft.CodeAnalysis;
|
||||
|
||||
namespace CliFx.Analyzers
|
||||
{
|
||||
public static class DiagnosticDescriptors
|
||||
{
|
||||
public static readonly DiagnosticDescriptor CliFx0001 =
|
||||
new DiagnosticDescriptor(nameof(CliFx0001),
|
||||
"Type must implement the 'CliFx.ICommand' interface in order to be a valid command",
|
||||
"Type must implement the 'CliFx.ICommand' interface in order to be a valid command",
|
||||
"Usage", DiagnosticSeverity.Error, true);
|
||||
|
||||
public static readonly DiagnosticDescriptor CliFx0002 =
|
||||
new DiagnosticDescriptor(nameof(CliFx0002),
|
||||
"Type must be annotated with the 'CliFx.Attributes.CommandAttribute' in order to be a valid command",
|
||||
"Type must be annotated with the 'CliFx.Attributes.CommandAttribute' in order to be a valid command",
|
||||
"Usage", DiagnosticSeverity.Error, true);
|
||||
|
||||
public static readonly DiagnosticDescriptor CliFx0021 =
|
||||
new DiagnosticDescriptor(nameof(CliFx0021),
|
||||
"Parameter order must be unique within its command",
|
||||
"Parameter order must be unique within its command",
|
||||
"Usage", DiagnosticSeverity.Error, true);
|
||||
|
||||
public static readonly DiagnosticDescriptor CliFx0022 =
|
||||
new DiagnosticDescriptor(nameof(CliFx0022),
|
||||
"Parameter order must have unique name within its command",
|
||||
"Parameter order must have unique name within its command",
|
||||
"Usage", DiagnosticSeverity.Error, true);
|
||||
|
||||
public static readonly DiagnosticDescriptor CliFx0023 =
|
||||
new DiagnosticDescriptor(nameof(CliFx0023),
|
||||
"Only one non-scalar parameter per command is allowed",
|
||||
"Only one non-scalar parameter per command is allowed",
|
||||
"Usage", DiagnosticSeverity.Error, true);
|
||||
|
||||
public static readonly DiagnosticDescriptor CliFx0024 =
|
||||
new DiagnosticDescriptor(nameof(CliFx0024),
|
||||
"Non-scalar parameter must be last in order",
|
||||
"Non-scalar parameter must be last in order",
|
||||
"Usage", DiagnosticSeverity.Error, true);
|
||||
|
||||
public static readonly DiagnosticDescriptor CliFx0041 =
|
||||
new DiagnosticDescriptor(nameof(CliFx0041),
|
||||
"Option must have a name or short name specified",
|
||||
"Option must have a name or short name specified",
|
||||
"Usage", DiagnosticSeverity.Error, true);
|
||||
|
||||
public static readonly DiagnosticDescriptor CliFx0042 =
|
||||
new DiagnosticDescriptor(nameof(CliFx0042),
|
||||
"Option name must be at least 2 characters long",
|
||||
"Option name must be at least 2 characters long",
|
||||
"Usage", DiagnosticSeverity.Error, true);
|
||||
|
||||
public static readonly DiagnosticDescriptor CliFx0043 =
|
||||
new DiagnosticDescriptor(nameof(CliFx0043),
|
||||
"Option name must be unique within its command",
|
||||
"Option name must be unique within its command",
|
||||
"Usage", DiagnosticSeverity.Error, true);
|
||||
|
||||
public static readonly DiagnosticDescriptor CliFx0044 =
|
||||
new DiagnosticDescriptor(nameof(CliFx0044),
|
||||
"Option short name must be unique within its command",
|
||||
"Option short name must be unique within its command",
|
||||
"Usage", DiagnosticSeverity.Error, true);
|
||||
|
||||
public static readonly DiagnosticDescriptor CliFx0045 =
|
||||
new DiagnosticDescriptor(nameof(CliFx0045),
|
||||
"Option environment variable name must be unique within its command",
|
||||
"Option environment variable name must be unique within its command",
|
||||
"Usage", DiagnosticSeverity.Error, true);
|
||||
|
||||
public static readonly DiagnosticDescriptor CliFx0100 =
|
||||
new DiagnosticDescriptor(nameof(CliFx0100),
|
||||
"Use the provided IConsole abstraction instead of System.Console to ensure that the command can be tested in isolation",
|
||||
"Use the provided IConsole abstraction instead of System.Console to ensure that the command can be tested in isolation",
|
||||
"Usage", DiagnosticSeverity.Warning, true);
|
||||
}
|
||||
}
|
||||
11
CliFx.Analyzers/Internal/RoslynExtensions.cs
Normal file
11
CliFx.Analyzers/Internal/RoslynExtensions.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
using System;
|
||||
using Microsoft.CodeAnalysis;
|
||||
|
||||
namespace CliFx.Analyzers.Internal
|
||||
{
|
||||
internal static class RoslynExtensions
|
||||
{
|
||||
public static bool DisplayNameMatches(this ISymbol symbol, string name) =>
|
||||
string.Equals(symbol.ToDisplayString(), name, StringComparison.Ordinal);
|
||||
}
|
||||
}
|
||||
37
CliFx.Analyzers/KnownSymbols.cs
Normal file
37
CliFx.Analyzers/KnownSymbols.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
using CliFx.Analyzers.Internal;
|
||||
using Microsoft.CodeAnalysis;
|
||||
|
||||
namespace CliFx.Analyzers
|
||||
{
|
||||
public static class KnownSymbols
|
||||
{
|
||||
public static bool IsSystemString(ISymbol symbol) =>
|
||||
symbol.DisplayNameMatches("string") ||
|
||||
symbol.DisplayNameMatches("System.String");
|
||||
|
||||
public static bool IsSystemChar(ISymbol symbol) =>
|
||||
symbol.DisplayNameMatches("char") ||
|
||||
symbol.DisplayNameMatches("System.Char");
|
||||
|
||||
public static bool IsSystemCollectionsGenericIEnumerable(ISymbol symbol) =>
|
||||
symbol.DisplayNameMatches("System.Collections.Generic.IEnumerable<T>");
|
||||
|
||||
public static bool IsSystemConsole(ISymbol symbol) =>
|
||||
symbol.DisplayNameMatches("System.Console");
|
||||
|
||||
public static bool IsConsoleInterface(ISymbol symbol) =>
|
||||
symbol.DisplayNameMatches("CliFx.IConsole");
|
||||
|
||||
public static bool IsCommandInterface(ISymbol symbol) =>
|
||||
symbol.DisplayNameMatches("CliFx.ICommand");
|
||||
|
||||
public static bool IsCommandAttribute(ISymbol symbol) =>
|
||||
symbol.DisplayNameMatches("CliFx.Attributes.CommandAttribute");
|
||||
|
||||
public static bool IsCommandParameterAttribute(ISymbol symbol) =>
|
||||
symbol.DisplayNameMatches("CliFx.Attributes.CommandParameterAttribute");
|
||||
|
||||
public static bool IsCommandOptionAttribute(ISymbol symbol) =>
|
||||
symbol.DisplayNameMatches("CliFx.Attributes.CommandOptionAttribute");
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,9 @@
|
||||
using System.Threading.Tasks;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using BenchmarkDotNet.Attributes;
|
||||
using BenchmarkDotNet.Configs;
|
||||
using BenchmarkDotNet.Order;
|
||||
using BenchmarkDotNet.Running;
|
||||
using CliFx.Benchmarks.Commands;
|
||||
using CommandLine;
|
||||
|
||||
@@ -9,13 +12,13 @@ namespace CliFx.Benchmarks
|
||||
[SimpleJob]
|
||||
[RankColumn]
|
||||
[Orderer(SummaryOrderPolicy.FastestToSlowest)]
|
||||
public class Benchmark
|
||||
public class Benchmarks
|
||||
{
|
||||
private static readonly string[] Arguments = {"--str", "hello world", "-i", "13", "-b"};
|
||||
|
||||
[Benchmark(Description = "CliFx", Baseline = true)]
|
||||
public async ValueTask<int> ExecuteWithCliFx() =>
|
||||
await new CliApplicationBuilder().AddCommand(typeof(CliFxCommand)).Build().RunAsync(Arguments);
|
||||
await new CliApplicationBuilder().AddCommand(typeof(CliFxCommand)).Build().RunAsync(Arguments, new Dictionary<string, string>());
|
||||
|
||||
[Benchmark(Description = "System.CommandLine")]
|
||||
public async Task<int> ExecuteWithSystemCommandLine() =>
|
||||
@@ -42,5 +45,8 @@ namespace CliFx.Benchmarks
|
||||
[Benchmark(Description = "Cocona")]
|
||||
public void ExecuteWithCocona() =>
|
||||
Cocona.CoconaApp.Run<CoconaCommand>(Arguments);
|
||||
|
||||
public static void Main() =>
|
||||
BenchmarkRunner.Run<Benchmarks>(DefaultConfig.Instance.With(ConfigOptions.DisableOptimizationsValidator));
|
||||
}
|
||||
}
|
||||
@@ -9,9 +9,9 @@
|
||||
<ItemGroup>
|
||||
<PackageReference Include="BenchmarkDotNet" Version="0.12.0" />
|
||||
<PackageReference Include="clipr" Version="1.6.1" />
|
||||
<PackageReference Include="Cocona" Version="1.0.0" />
|
||||
<PackageReference Include="Cocona" Version="1.3.0" />
|
||||
<PackageReference Include="CommandLineParser" Version="2.7.82" />
|
||||
<PackageReference Include="McMaster.Extensions.CommandLineUtils" Version="2.5.0" />
|
||||
<PackageReference Include="McMaster.Extensions.CommandLineUtils" Version="2.6.0" />
|
||||
<PackageReference Include="PowerArgs" Version="3.6.0" />
|
||||
<PackageReference Include="System.CommandLine.Experimental" Version="0.3.0-alpha.19317.1" />
|
||||
</ItemGroup>
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
using BenchmarkDotNet.Configs;
|
||||
using BenchmarkDotNet.Running;
|
||||
|
||||
namespace CliFx.Benchmarks
|
||||
{
|
||||
public static class Program
|
||||
{
|
||||
public static void Main() =>
|
||||
BenchmarkRunner.Run(typeof(Program).Assembly, DefaultConfig.Instance
|
||||
.With(ConfigOptions.DisableOptimizationsValidator));
|
||||
}
|
||||
}
|
||||
@@ -7,12 +7,13 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.1" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.2" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\CliFx\CliFx.csproj" />
|
||||
<ProjectReference Include="..\CliFx.Analyzers\CliFx.Analyzers.csproj" ReferenceOutputAssembly="false" OutputItemType="Analyzer" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -8,6 +8,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\CliFx\CliFx.csproj" />
|
||||
<ProjectReference Include="..\CliFx.Analyzers\CliFx.Analyzers.csproj" ReferenceOutputAssembly="false" OutputItemType="Analyzer" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
23
CliFx.Tests.Dummy/Commands/ConsoleTestCommand.cs
Normal file
23
CliFx.Tests.Dummy/Commands/ConsoleTestCommand.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using CliFx.Attributes;
|
||||
|
||||
namespace CliFx.Tests.Dummy.Commands
|
||||
{
|
||||
[Command("console-test")]
|
||||
public class ConsoleTestCommand : ICommand
|
||||
{
|
||||
public ValueTask ExecuteAsync(IConsole console)
|
||||
{
|
||||
var input = console.Input.ReadToEnd();
|
||||
|
||||
console.WithColors(ConsoleColor.Black, ConsoleColor.White, () =>
|
||||
{
|
||||
console.Output.WriteLine(input);
|
||||
console.Error.WriteLine(input);
|
||||
});
|
||||
|
||||
return default;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,16 @@
|
||||
using System.Threading.Tasks;
|
||||
using System.Reflection;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CliFx.Tests.Dummy
|
||||
{
|
||||
public class Program
|
||||
public static partial class Program
|
||||
{
|
||||
public static Assembly Assembly { get; } = typeof(Program).Assembly;
|
||||
|
||||
public static string Location { get; } = Assembly.Location;
|
||||
}
|
||||
|
||||
public static partial class Program
|
||||
{
|
||||
public static async Task Main() =>
|
||||
await new CliApplicationBuilder()
|
||||
|
||||
179
CliFx.Tests/ApplicationSpecs.Commands.cs
Normal file
179
CliFx.Tests/ApplicationSpecs.Commands.cs
Normal file
@@ -0,0 +1,179 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using CliFx.Attributes;
|
||||
|
||||
namespace CliFx.Tests
|
||||
{
|
||||
public partial class ApplicationSpecs
|
||||
{
|
||||
[Command]
|
||||
private class DefaultCommand : ICommand
|
||||
{
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
|
||||
[Command]
|
||||
private class AnotherDefaultCommand : ICommand
|
||||
{
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
|
||||
[Command]
|
||||
private class NonImplementedCommand
|
||||
{
|
||||
}
|
||||
|
||||
private class NonAnnotatedCommand : ICommand
|
||||
{
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
|
||||
[Command("dup")]
|
||||
private class DuplicateNameCommandA : ICommand
|
||||
{
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
|
||||
[Command("dup")]
|
||||
private class DuplicateNameCommandB : ICommand
|
||||
{
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
|
||||
[Command]
|
||||
private class DuplicateParameterOrderCommand : ICommand
|
||||
{
|
||||
[CommandParameter(13)]
|
||||
public string? ParameterA { get; set; }
|
||||
|
||||
[CommandParameter(13)]
|
||||
public string? ParameterB { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
|
||||
[Command]
|
||||
private class DuplicateParameterNameCommand : ICommand
|
||||
{
|
||||
[CommandParameter(0, Name = "param")]
|
||||
public string? ParameterA { get; set; }
|
||||
|
||||
[CommandParameter(1, Name = "param")]
|
||||
public string? ParameterB { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
|
||||
[Command]
|
||||
private class MultipleNonScalarParametersCommand : ICommand
|
||||
{
|
||||
[CommandParameter(0)]
|
||||
public IReadOnlyList<string>? ParameterA { get; set; }
|
||||
|
||||
[CommandParameter(1)]
|
||||
public IReadOnlyList<string>? ParameterB { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
|
||||
[Command]
|
||||
private class NonLastNonScalarParameterCommand : ICommand
|
||||
{
|
||||
[CommandParameter(0)]
|
||||
public IReadOnlyList<string>? ParameterA { get; set; }
|
||||
|
||||
[CommandParameter(1)]
|
||||
public string? ParameterB { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
|
||||
[Command]
|
||||
private class EmptyOptionNameCommand : ICommand
|
||||
{
|
||||
[CommandOption("")]
|
||||
public string? Apples { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
|
||||
[Command]
|
||||
private class SingleCharacterOptionNameCommand : ICommand
|
||||
{
|
||||
[CommandOption("a")]
|
||||
public string? Apples { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
|
||||
[Command]
|
||||
private class DuplicateOptionNamesCommand : ICommand
|
||||
{
|
||||
[CommandOption("fruits")]
|
||||
public string? Apples { get; set; }
|
||||
|
||||
[CommandOption("fruits")]
|
||||
public string? Oranges { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
|
||||
[Command]
|
||||
private class DuplicateOptionShortNamesCommand : ICommand
|
||||
{
|
||||
[CommandOption('x')]
|
||||
public string? OptionA { get; set; }
|
||||
|
||||
[CommandOption('x')]
|
||||
public string? OptionB { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
|
||||
[Command]
|
||||
private class ConflictWithHelpOptionCommand : ICommand
|
||||
{
|
||||
[CommandOption("option-h", 'h')]
|
||||
public string? OptionH { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
|
||||
[Command]
|
||||
private class ConflictWithVersionOptionCommand : ICommand
|
||||
{
|
||||
[CommandOption("version")]
|
||||
public string? Version { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
|
||||
[Command]
|
||||
private class DuplicateOptionEnvironmentVariableNamesCommand : ICommand
|
||||
{
|
||||
[CommandOption("option-a", EnvironmentVariableName = "ENV_VAR")]
|
||||
public string? OptionA { get; set; }
|
||||
|
||||
[CommandOption("option-b", EnvironmentVariableName = "ENV_VAR")]
|
||||
public string? OptionB { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
|
||||
[Command("hidden", Description = "Description")]
|
||||
private class HiddenPropertiesCommand : ICommand
|
||||
{
|
||||
[CommandParameter(13, Name = "param", Description = "Param description")]
|
||||
public string? Parameter { get; set; }
|
||||
|
||||
[CommandOption("option", 'o', Description = "Option description", EnvironmentVariableName = "ENV")]
|
||||
public string? Option { get; set; }
|
||||
|
||||
public string? HiddenA { get; set; }
|
||||
|
||||
public bool? HiddenB { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
}
|
||||
}
|
||||
269
CliFx.Tests/ApplicationSpecs.cs
Normal file
269
CliFx.Tests/ApplicationSpecs.cs
Normal file
@@ -0,0 +1,269 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using CliFx.Domain;
|
||||
using CliFx.Exceptions;
|
||||
using FluentAssertions;
|
||||
using Xunit;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
namespace CliFx.Tests
|
||||
{
|
||||
public partial class ApplicationSpecs
|
||||
{
|
||||
private readonly ITestOutputHelper _output;
|
||||
|
||||
public ApplicationSpecs(ITestOutputHelper output) => _output = output;
|
||||
|
||||
[Fact]
|
||||
public void Application_can_be_created_with_a_default_configuration()
|
||||
{
|
||||
// Act
|
||||
var app = new CliApplicationBuilder()
|
||||
.AddCommandsFromThisAssembly()
|
||||
.Build();
|
||||
|
||||
// Assert
|
||||
app.Should().NotBeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Application_can_be_created_with_a_custom_configuration()
|
||||
{
|
||||
// Act
|
||||
var app = new CliApplicationBuilder()
|
||||
.AddCommand(typeof(DefaultCommand))
|
||||
.AddCommandsFrom(typeof(DefaultCommand).Assembly)
|
||||
.AddCommands(new[] {typeof(DefaultCommand)})
|
||||
.AddCommandsFrom(new[] {typeof(DefaultCommand).Assembly})
|
||||
.AddCommandsFromThisAssembly()
|
||||
.AllowDebugMode()
|
||||
.AllowPreviewMode()
|
||||
.UseTitle("test")
|
||||
.UseExecutableName("test")
|
||||
.UseVersionText("test")
|
||||
.UseDescription("test")
|
||||
.UseConsole(new VirtualConsole(Stream.Null))
|
||||
.UseTypeActivator(Activator.CreateInstance!)
|
||||
.Build();
|
||||
|
||||
// Assert
|
||||
app.Should().NotBeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void At_least_one_command_must_be_defined_in_an_application()
|
||||
{
|
||||
// Arrange
|
||||
var commandTypes = Array.Empty<Type>();
|
||||
|
||||
// Act & assert
|
||||
var ex = Assert.Throws<CliFxException>(() => RootSchema.Resolve(commandTypes));
|
||||
_output.WriteLine(ex.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Commands_must_implement_the_corresponding_interface()
|
||||
{
|
||||
// Arrange
|
||||
var commandTypes = new[] {typeof(NonImplementedCommand)};
|
||||
|
||||
// Act & assert
|
||||
var ex = Assert.Throws<CliFxException>(() => RootSchema.Resolve(commandTypes));
|
||||
_output.WriteLine(ex.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Commands_must_be_annotated_by_an_attribute()
|
||||
{
|
||||
// Arrange
|
||||
var commandTypes = new[] {typeof(NonAnnotatedCommand)};
|
||||
|
||||
// Act & assert
|
||||
var ex = Assert.Throws<CliFxException>(() => RootSchema.Resolve(commandTypes));
|
||||
_output.WriteLine(ex.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Commands_must_have_unique_names()
|
||||
{
|
||||
// Arrange
|
||||
var commandTypes = new[] {typeof(DuplicateNameCommandA), typeof(DuplicateNameCommandB)};
|
||||
|
||||
// Act & assert
|
||||
var ex = Assert.Throws<CliFxException>(() => RootSchema.Resolve(commandTypes));
|
||||
_output.WriteLine(ex.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Command_can_be_default_but_only_if_it_is_the_only_such_command()
|
||||
{
|
||||
// Arrange
|
||||
var commandTypes = new[] {typeof(DefaultCommand), typeof(AnotherDefaultCommand)};
|
||||
|
||||
// Act & assert
|
||||
var ex = Assert.Throws<CliFxException>(() => RootSchema.Resolve(commandTypes));
|
||||
_output.WriteLine(ex.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Command_parameters_must_have_unique_order()
|
||||
{
|
||||
// Arrange
|
||||
var commandTypes = new[] {typeof(DuplicateParameterOrderCommand)};
|
||||
|
||||
// Act & assert
|
||||
var ex = Assert.Throws<CliFxException>(() => RootSchema.Resolve(commandTypes));
|
||||
_output.WriteLine(ex.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Command_parameters_must_have_unique_names()
|
||||
{
|
||||
// Arrange
|
||||
var commandTypes = new[] {typeof(DuplicateParameterNameCommand)};
|
||||
|
||||
// Act & assert
|
||||
var ex = Assert.Throws<CliFxException>(() => RootSchema.Resolve(commandTypes));
|
||||
_output.WriteLine(ex.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Command_parameter_can_be_non_scalar_only_if_no_other_such_parameter_is_present()
|
||||
{
|
||||
// Arrange
|
||||
var commandTypes = new[] {typeof(MultipleNonScalarParametersCommand)};
|
||||
|
||||
// Act & assert
|
||||
var ex = Assert.Throws<CliFxException>(() => RootSchema.Resolve(commandTypes));
|
||||
_output.WriteLine(ex.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Command_parameter_can_be_non_scalar_only_if_it_is_the_last_in_order()
|
||||
{
|
||||
// Arrange
|
||||
var commandTypes = new[] {typeof(NonLastNonScalarParameterCommand)};
|
||||
|
||||
// Act & assert
|
||||
var ex = Assert.Throws<CliFxException>(() => RootSchema.Resolve(commandTypes));
|
||||
_output.WriteLine(ex.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Command_options_must_have_names_that_are_not_empty()
|
||||
{
|
||||
// Arrange
|
||||
var commandTypes = new[] {typeof(EmptyOptionNameCommand)};
|
||||
|
||||
// Act & assert
|
||||
var ex = Assert.Throws<CliFxException>(() => RootSchema.Resolve(commandTypes));
|
||||
_output.WriteLine(ex.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Command_options_must_have_names_that_are_longer_than_one_character()
|
||||
{
|
||||
// Arrange
|
||||
var commandTypes = new[] {typeof(SingleCharacterOptionNameCommand)};
|
||||
|
||||
// Act & assert
|
||||
var ex = Assert.Throws<CliFxException>(() => RootSchema.Resolve(commandTypes));
|
||||
_output.WriteLine(ex.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Command_options_must_have_unique_names()
|
||||
{
|
||||
// Arrange
|
||||
var commandTypes = new[] {typeof(DuplicateOptionNamesCommand)};
|
||||
|
||||
// Act & assert
|
||||
var ex = Assert.Throws<CliFxException>(() => RootSchema.Resolve(commandTypes));
|
||||
_output.WriteLine(ex.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Command_options_must_have_unique_short_names()
|
||||
{
|
||||
// Arrange
|
||||
var commandTypes = new[] {typeof(DuplicateOptionShortNamesCommand)};
|
||||
|
||||
// Act & assert
|
||||
var ex = Assert.Throws<CliFxException>(() => RootSchema.Resolve(commandTypes));
|
||||
_output.WriteLine(ex.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Command_options_must_not_have_conflicts_with_the_implicit_help_option()
|
||||
{
|
||||
// Arrange
|
||||
var commandTypes = new[] {typeof(ConflictWithHelpOptionCommand)};
|
||||
|
||||
// Act & assert
|
||||
var ex = Assert.Throws<CliFxException>(() => RootSchema.Resolve(commandTypes));
|
||||
_output.WriteLine(ex.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Command_options_must_not_have_conflicts_with_the_implicit_version_option()
|
||||
{
|
||||
// Arrange
|
||||
var commandTypes = new[] {typeof(ConflictWithVersionOptionCommand)};
|
||||
|
||||
// Act & assert
|
||||
var ex = Assert.Throws<CliFxException>(() => RootSchema.Resolve(commandTypes));
|
||||
_output.WriteLine(ex.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Command_options_must_have_unique_environment_variable_names()
|
||||
{
|
||||
// Arrange
|
||||
var commandTypes = new[] {typeof(DuplicateOptionEnvironmentVariableNamesCommand)};
|
||||
|
||||
// Act & assert
|
||||
var ex = Assert.Throws<CliFxException>(() => RootSchema.Resolve(commandTypes));
|
||||
_output.WriteLine(ex.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Command_options_and_parameters_must_be_annotated_by_corresponding_attributes()
|
||||
{
|
||||
// Arrange
|
||||
var commandTypes = new[] {typeof(HiddenPropertiesCommand)};
|
||||
|
||||
// Act
|
||||
var schema = RootSchema.Resolve(commandTypes);
|
||||
|
||||
// Assert
|
||||
schema.Should().BeEquivalentTo(new RootSchema(new[]
|
||||
{
|
||||
new CommandSchema(
|
||||
typeof(HiddenPropertiesCommand),
|
||||
"hidden",
|
||||
"Description",
|
||||
new[]
|
||||
{
|
||||
new CommandParameterSchema(
|
||||
typeof(HiddenPropertiesCommand).GetProperty(nameof(HiddenPropertiesCommand.Parameter))!,
|
||||
13,
|
||||
"param",
|
||||
"Param description")
|
||||
},
|
||||
new[]
|
||||
{
|
||||
new CommandOptionSchema(
|
||||
typeof(HiddenPropertiesCommand).GetProperty(nameof(HiddenPropertiesCommand.Option))!,
|
||||
"option",
|
||||
'o',
|
||||
"ENV",
|
||||
false,
|
||||
"Option description"),
|
||||
CommandOptionSchema.HelpOption
|
||||
})
|
||||
}));
|
||||
|
||||
schema.ToString().Should().NotBeNullOrWhiteSpace(); // this is only for coverage, I'm sorry
|
||||
}
|
||||
}
|
||||
}
|
||||
197
CliFx.Tests/ArgumentBindingSpecs.Commands.cs
Normal file
197
CliFx.Tests/ArgumentBindingSpecs.Commands.cs
Normal file
@@ -0,0 +1,197 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using CliFx.Attributes;
|
||||
|
||||
namespace CliFx.Tests
|
||||
{
|
||||
public partial class ArgumentBindingSpecs
|
||||
{
|
||||
[Command]
|
||||
private class AllSupportedTypesCommand : ICommand
|
||||
{
|
||||
[CommandOption(nameof(Object))]
|
||||
public object? Object { get; set; } = 42;
|
||||
|
||||
[CommandOption(nameof(String))]
|
||||
public string? String { get; set; } = "foo bar";
|
||||
|
||||
[CommandOption(nameof(Bool))]
|
||||
public bool Bool { get; set; }
|
||||
|
||||
[CommandOption(nameof(Char))]
|
||||
public char Char { get; set; }
|
||||
|
||||
[CommandOption(nameof(Sbyte))]
|
||||
public sbyte Sbyte { get; set; }
|
||||
|
||||
[CommandOption(nameof(Byte))]
|
||||
public byte Byte { get; set; }
|
||||
|
||||
[CommandOption(nameof(Short))]
|
||||
public short Short { get; set; }
|
||||
|
||||
[CommandOption(nameof(Ushort))]
|
||||
public ushort Ushort { get; set; }
|
||||
|
||||
[CommandOption(nameof(Int))]
|
||||
public int Int { get; set; }
|
||||
|
||||
[CommandOption(nameof(Uint))]
|
||||
public uint Uint { get; set; }
|
||||
|
||||
[CommandOption(nameof(Long))]
|
||||
public long Long { get; set; }
|
||||
|
||||
[CommandOption(nameof(Ulong))]
|
||||
public ulong Ulong { get; set; }
|
||||
|
||||
[CommandOption(nameof(Float))]
|
||||
public float Float { get; set; }
|
||||
|
||||
[CommandOption(nameof(Double))]
|
||||
public double Double { get; set; }
|
||||
|
||||
[CommandOption(nameof(Decimal))]
|
||||
public decimal Decimal { get; set; }
|
||||
|
||||
[CommandOption(nameof(DateTime))]
|
||||
public DateTime DateTime { get; set; }
|
||||
|
||||
[CommandOption(nameof(DateTimeOffset))]
|
||||
public DateTimeOffset DateTimeOffset { get; set; }
|
||||
|
||||
[CommandOption(nameof(TimeSpan))]
|
||||
public TimeSpan TimeSpan { get; set; }
|
||||
|
||||
[CommandOption(nameof(CustomEnum))]
|
||||
public CustomEnum CustomEnum { get; set; }
|
||||
|
||||
[CommandOption(nameof(IntNullable))]
|
||||
public int? IntNullable { get; set; }
|
||||
|
||||
[CommandOption(nameof(CustomEnumNullable))]
|
||||
public CustomEnum? CustomEnumNullable { get; set; }
|
||||
|
||||
[CommandOption(nameof(TimeSpanNullable))]
|
||||
public TimeSpan? TimeSpanNullable { get; set; }
|
||||
|
||||
[CommandOption(nameof(TestStringConstructable))]
|
||||
public StringConstructable? TestStringConstructable { get; set; }
|
||||
|
||||
[CommandOption(nameof(TestStringParseable))]
|
||||
public StringParseable? TestStringParseable { get; set; }
|
||||
|
||||
[CommandOption(nameof(TestStringParseableWithFormatProvider))]
|
||||
public StringParseableWithFormatProvider? TestStringParseableWithFormatProvider { get; set; }
|
||||
|
||||
[CommandOption(nameof(ObjectArray))]
|
||||
public object[]? ObjectArray { get; set; }
|
||||
|
||||
[CommandOption(nameof(StringArray))]
|
||||
public string[]? StringArray { get; set; }
|
||||
|
||||
[CommandOption(nameof(IntArray))]
|
||||
public int[]? IntArray { get; set; }
|
||||
|
||||
[CommandOption(nameof(CustomEnumArray))]
|
||||
public CustomEnum[]? CustomEnumArray { get; set; }
|
||||
|
||||
[CommandOption(nameof(IntNullableArray))]
|
||||
public int?[]? IntNullableArray { get; set; }
|
||||
|
||||
[CommandOption(nameof(TestStringConstructableArray))]
|
||||
public StringConstructable[]? TestStringConstructableArray { get; set; }
|
||||
|
||||
[CommandOption(nameof(Enumerable))]
|
||||
public IEnumerable? Enumerable { get; set; }
|
||||
|
||||
[CommandOption(nameof(StringEnumerable))]
|
||||
public IEnumerable<string>? StringEnumerable { get; set; }
|
||||
|
||||
[CommandOption(nameof(StringReadOnlyList))]
|
||||
public IReadOnlyList<string>? StringReadOnlyList { get; set; }
|
||||
|
||||
[CommandOption(nameof(StringList))]
|
||||
public List<string>? StringList { get; set; }
|
||||
|
||||
[CommandOption(nameof(StringHashSet))]
|
||||
public HashSet<string>? StringHashSet { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
|
||||
[Command]
|
||||
private class ArrayOptionCommand : ICommand
|
||||
{
|
||||
[CommandOption("option", 'o')]
|
||||
public IReadOnlyList<string>? Option { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
|
||||
[Command]
|
||||
private class RequiredOptionCommand : ICommand
|
||||
{
|
||||
[CommandOption(nameof(Option), IsRequired = true)]
|
||||
public string? Option { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
|
||||
[Command]
|
||||
private class RequiredArrayOptionCommand : ICommand
|
||||
{
|
||||
[CommandOption(nameof(Option), IsRequired = true)]
|
||||
public IReadOnlyList<string>? Option { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
|
||||
[Command]
|
||||
private class ParametersCommand : ICommand
|
||||
{
|
||||
[CommandParameter(0)]
|
||||
public string? ParameterA { get; set; }
|
||||
|
||||
[CommandParameter(1)]
|
||||
public string? ParameterB { get; set; }
|
||||
|
||||
[CommandParameter(2)]
|
||||
public IReadOnlyList<string>? ParameterC { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
|
||||
[Command]
|
||||
private class UnsupportedPropertyTypeCommand : ICommand
|
||||
{
|
||||
[CommandOption(nameof(Option))]
|
||||
public DummyType? Option { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
|
||||
[Command]
|
||||
private class UnsupportedEnumerablePropertyTypeCommand : ICommand
|
||||
{
|
||||
[CommandOption(nameof(Option))]
|
||||
public CustomEnumerable<string>? Option { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
|
||||
[Command]
|
||||
private class NoParameterCommand : ICommand
|
||||
{
|
||||
[CommandOption(nameof(OptionA))]
|
||||
public string? OptionA { get; set; }
|
||||
|
||||
[CommandOption(nameof(OptionB))]
|
||||
public string? OptionB { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
}
|
||||
}
|
||||
53
CliFx.Tests/ArgumentBindingSpecs.Types.cs
Normal file
53
CliFx.Tests/ArgumentBindingSpecs.Types.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace CliFx.Tests
|
||||
{
|
||||
public partial class ArgumentBindingSpecs
|
||||
{
|
||||
private enum CustomEnum
|
||||
{
|
||||
Value1 = 1,
|
||||
Value2 = 2,
|
||||
Value3 = 3
|
||||
}
|
||||
|
||||
private class StringConstructable
|
||||
{
|
||||
public string Value { get; }
|
||||
|
||||
public StringConstructable(string value) => Value = value;
|
||||
}
|
||||
|
||||
private class StringParseable
|
||||
{
|
||||
public string Value { get; }
|
||||
|
||||
private StringParseable(string value) => Value = value;
|
||||
|
||||
public static StringParseable Parse(string value) => new StringParseable(value);
|
||||
}
|
||||
|
||||
private class StringParseableWithFormatProvider
|
||||
{
|
||||
public string Value { get; }
|
||||
|
||||
private StringParseableWithFormatProvider(string value) => Value = value;
|
||||
|
||||
public static StringParseableWithFormatProvider Parse(string value, IFormatProvider formatProvider) =>
|
||||
new StringParseableWithFormatProvider(value + " " + formatProvider);
|
||||
}
|
||||
|
||||
private class DummyType
|
||||
{
|
||||
}
|
||||
|
||||
public class CustomEnumerable<T> : IEnumerable<T>
|
||||
{
|
||||
public IEnumerator<T> GetEnumerator() => ((IEnumerable<T>) Array.Empty<T>()).GetEnumerator();
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
||||
}
|
||||
}
|
||||
}
|
||||
1018
CliFx.Tests/ArgumentBindingSpecs.cs
Normal file
1018
CliFx.Tests/ArgumentBindingSpecs.cs
Normal file
File diff suppressed because it is too large
Load Diff
378
CliFx.Tests/ArgumentSyntaxSpecs.cs
Normal file
378
CliFx.Tests/ArgumentSyntaxSpecs.cs
Normal file
@@ -0,0 +1,378 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using CliFx.Domain;
|
||||
using CliFx.Tests.Internal;
|
||||
using FluentAssertions;
|
||||
using Xunit;
|
||||
|
||||
namespace CliFx.Tests
|
||||
{
|
||||
public class ArgumentSyntaxSpecs
|
||||
{
|
||||
[Fact]
|
||||
public void Input_is_empty_if_no_arguments_are_provided()
|
||||
{
|
||||
// Arrange
|
||||
var arguments = Array.Empty<string>();
|
||||
var commandNames = Array.Empty<string>();
|
||||
|
||||
// Act
|
||||
var input = CommandInput.Parse(arguments, commandNames);
|
||||
|
||||
// Assert
|
||||
input.Should().BeEquivalentTo(CommandInput.Empty);
|
||||
}
|
||||
|
||||
public static object[][] DirectivesTestData => new[]
|
||||
{
|
||||
new object[]
|
||||
{
|
||||
new[] {"[preview]"},
|
||||
new CommandInputBuilder()
|
||||
.AddDirective("preview")
|
||||
.Build()
|
||||
},
|
||||
|
||||
new object[]
|
||||
{
|
||||
new[] {"[preview]", "[debug]"},
|
||||
new CommandInputBuilder()
|
||||
.AddDirective("preview")
|
||||
.AddDirective("debug")
|
||||
.Build()
|
||||
}
|
||||
};
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(DirectivesTestData))]
|
||||
internal void Directive_can_be_enabled_by_specifying_its_name_in_square_brackets(IReadOnlyList<string> arguments, CommandInput expectedInput)
|
||||
{
|
||||
// Arrange
|
||||
var commandNames = Array.Empty<string>();
|
||||
|
||||
// Act
|
||||
var input = CommandInput.Parse(arguments, commandNames);
|
||||
|
||||
// Assert
|
||||
input.Should().BeEquivalentTo(expectedInput);
|
||||
}
|
||||
|
||||
public static object[][] OptionsTestData => new[]
|
||||
{
|
||||
new object[]
|
||||
{
|
||||
new[] {"--option"},
|
||||
new CommandInputBuilder()
|
||||
.AddOption("option")
|
||||
.Build()
|
||||
},
|
||||
|
||||
new object[]
|
||||
{
|
||||
new[] {"--option", "value"},
|
||||
new CommandInputBuilder()
|
||||
.AddOption("option", "value")
|
||||
.Build()
|
||||
},
|
||||
|
||||
new object[]
|
||||
{
|
||||
new[] {"--option", "value1", "value2"},
|
||||
new CommandInputBuilder()
|
||||
.AddOption("option", "value1", "value2")
|
||||
.Build()
|
||||
},
|
||||
|
||||
new object[]
|
||||
{
|
||||
new[] {"--option", "same value"},
|
||||
new CommandInputBuilder()
|
||||
.AddOption("option", "same value")
|
||||
.Build()
|
||||
},
|
||||
|
||||
new object[]
|
||||
{
|
||||
new[] {"--option1", "--option2"},
|
||||
new CommandInputBuilder()
|
||||
.AddOption("option1")
|
||||
.AddOption("option2")
|
||||
.Build()
|
||||
},
|
||||
|
||||
new object[]
|
||||
{
|
||||
new[] {"--option1", "value1", "--option2", "value2"},
|
||||
new CommandInputBuilder()
|
||||
.AddOption("option1", "value1")
|
||||
.AddOption("option2", "value2")
|
||||
.Build()
|
||||
},
|
||||
|
||||
new object[]
|
||||
{
|
||||
new[] {"--option1", "value1", "value2", "--option2", "value3", "value4"},
|
||||
new CommandInputBuilder()
|
||||
.AddOption("option1", "value1", "value2")
|
||||
.AddOption("option2", "value3", "value4")
|
||||
.Build()
|
||||
},
|
||||
|
||||
new object[]
|
||||
{
|
||||
new[] {"--option1", "value1", "value2", "--option2"},
|
||||
new CommandInputBuilder()
|
||||
.AddOption("option1", "value1", "value2")
|
||||
.AddOption("option2")
|
||||
.Build()
|
||||
}
|
||||
};
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(OptionsTestData))]
|
||||
internal void Option_can_be_set_by_specifying_its_name_after_two_dashes(IReadOnlyList<string> arguments, CommandInput expectedInput)
|
||||
{
|
||||
// Arrange
|
||||
var commandNames = Array.Empty<string>();
|
||||
|
||||
// Act
|
||||
var input = CommandInput.Parse(arguments, commandNames);
|
||||
|
||||
// Assert
|
||||
input.Should().BeEquivalentTo(expectedInput);
|
||||
}
|
||||
|
||||
public static object[][] ShortOptionsTestData => new[]
|
||||
{
|
||||
new object[]
|
||||
{
|
||||
new[] {"-o"},
|
||||
new CommandInputBuilder()
|
||||
.AddOption("o")
|
||||
.Build()
|
||||
},
|
||||
|
||||
new object[]
|
||||
{
|
||||
new[] {"-o", "value"},
|
||||
new CommandInputBuilder()
|
||||
.AddOption("o", "value")
|
||||
.Build()
|
||||
},
|
||||
|
||||
new object[]
|
||||
{
|
||||
new[] {"-o", "value1", "value2"},
|
||||
new CommandInputBuilder()
|
||||
.AddOption("o", "value1", "value2")
|
||||
.Build()
|
||||
},
|
||||
|
||||
new object[]
|
||||
{
|
||||
new[] {"-o", "same value"},
|
||||
new CommandInputBuilder()
|
||||
.AddOption("o", "same value")
|
||||
.Build()
|
||||
},
|
||||
|
||||
new object[]
|
||||
{
|
||||
new[] {"-a", "-b"},
|
||||
new CommandInputBuilder()
|
||||
.AddOption("a")
|
||||
.AddOption("b")
|
||||
.Build()
|
||||
},
|
||||
|
||||
new object[]
|
||||
{
|
||||
new[] {"-a", "value1", "-b", "value2"},
|
||||
new CommandInputBuilder()
|
||||
.AddOption("a", "value1")
|
||||
.AddOption("b", "value2")
|
||||
.Build()
|
||||
},
|
||||
|
||||
new object[]
|
||||
{
|
||||
new[] {"-a", "value1", "value2", "-b", "value3", "value4"},
|
||||
new CommandInputBuilder()
|
||||
.AddOption("a", "value1", "value2")
|
||||
.AddOption("b", "value3", "value4")
|
||||
.Build()
|
||||
},
|
||||
|
||||
new object[]
|
||||
{
|
||||
new[] {"-a", "value1", "value2", "-b"},
|
||||
new CommandInputBuilder()
|
||||
.AddOption("a", "value1", "value2")
|
||||
.AddOption("b")
|
||||
.Build()
|
||||
},
|
||||
|
||||
new object[]
|
||||
{
|
||||
new[] {"-abc"},
|
||||
new CommandInputBuilder()
|
||||
.AddOption("a")
|
||||
.AddOption("b")
|
||||
.AddOption("c")
|
||||
.Build()
|
||||
},
|
||||
|
||||
new object[]
|
||||
{
|
||||
new[] {"-abc", "value"},
|
||||
new CommandInputBuilder()
|
||||
.AddOption("a")
|
||||
.AddOption("b")
|
||||
.AddOption("c", "value")
|
||||
.Build()
|
||||
},
|
||||
|
||||
new object[]
|
||||
{
|
||||
new[] {"-abc", "value1", "value2"},
|
||||
new CommandInputBuilder()
|
||||
.AddOption("a")
|
||||
.AddOption("b")
|
||||
.AddOption("c", "value1", "value2")
|
||||
.Build()
|
||||
}
|
||||
};
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(ShortOptionsTestData))]
|
||||
internal void Option_can_be_set_by_specifying_its_short_name_after_a_single_dash(IReadOnlyList<string> arguments, CommandInput expectedInput)
|
||||
{
|
||||
// Arrange
|
||||
var commandNames = Array.Empty<string>();
|
||||
|
||||
// Act
|
||||
var input = CommandInput.Parse(arguments, commandNames);
|
||||
|
||||
// Assert
|
||||
input.Should().BeEquivalentTo(expectedInput);
|
||||
}
|
||||
|
||||
public static object[][] ParametersTestData => new[]
|
||||
{
|
||||
new object[]
|
||||
{
|
||||
new[] {"foo"},
|
||||
new CommandInputBuilder()
|
||||
.AddParameter("foo")
|
||||
.Build()
|
||||
},
|
||||
|
||||
new object[]
|
||||
{
|
||||
new[] {"foo", "bar"},
|
||||
new CommandInputBuilder()
|
||||
.AddParameter("foo")
|
||||
.AddParameter("bar")
|
||||
.Build()
|
||||
},
|
||||
|
||||
new object[]
|
||||
{
|
||||
new[] {"[preview]", "foo"},
|
||||
new CommandInputBuilder()
|
||||
.AddDirective("preview")
|
||||
.AddParameter("foo")
|
||||
.Build()
|
||||
},
|
||||
|
||||
new object[]
|
||||
{
|
||||
new[] {"foo", "--option", "value", "-abc"},
|
||||
new CommandInputBuilder()
|
||||
.AddParameter("foo")
|
||||
.AddOption("option", "value")
|
||||
.AddOption("a")
|
||||
.AddOption("b")
|
||||
.AddOption("c")
|
||||
.Build()
|
||||
},
|
||||
|
||||
new object[]
|
||||
{
|
||||
new[] {"[preview]", "[debug]", "foo", "bar", "--option", "value", "-abc"},
|
||||
new CommandInputBuilder()
|
||||
.AddDirective("preview")
|
||||
.AddDirective("debug")
|
||||
.AddParameter("foo")
|
||||
.AddParameter("bar")
|
||||
.AddOption("option", "value")
|
||||
.AddOption("a")
|
||||
.AddOption("b")
|
||||
.AddOption("c")
|
||||
.Build()
|
||||
}
|
||||
};
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(ParametersTestData))]
|
||||
internal void Parameter_can_be_set_by_specifying_the_value_directly(IReadOnlyList<string> arguments, CommandInput expectedInput)
|
||||
{
|
||||
// Arrange
|
||||
var commandNames = Array.Empty<string>();
|
||||
|
||||
// Act
|
||||
var input = CommandInput.Parse(arguments, commandNames);
|
||||
|
||||
// Assert
|
||||
input.Should().BeEquivalentTo(expectedInput);
|
||||
}
|
||||
|
||||
public static object[][] CommandNameTestData => new[]
|
||||
{
|
||||
new object[]
|
||||
{
|
||||
new[] {"cmd"},
|
||||
new[] {"cmd"},
|
||||
new CommandInputBuilder()
|
||||
.SetCommandName("cmd")
|
||||
.Build()
|
||||
},
|
||||
|
||||
new object[]
|
||||
{
|
||||
new[] {"cmd"},
|
||||
new[] {"cmd", "foo", "bar", "-o", "value"},
|
||||
new CommandInputBuilder()
|
||||
.SetCommandName("cmd")
|
||||
.AddParameter("foo")
|
||||
.AddParameter("bar")
|
||||
.AddOption("o", "value")
|
||||
.Build()
|
||||
},
|
||||
|
||||
new object[]
|
||||
{
|
||||
new[] {"cmd", "cmd sub"},
|
||||
new[] {"cmd", "sub", "foo"},
|
||||
new CommandInputBuilder()
|
||||
.SetCommandName("cmd sub")
|
||||
.AddParameter("foo")
|
||||
.Build()
|
||||
}
|
||||
};
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(CommandNameTestData))]
|
||||
internal void Command_name_is_matched_from_arguments_that_come_before_parameters(
|
||||
IReadOnlyList<string> commandNames,
|
||||
IReadOnlyList<string> arguments,
|
||||
CommandInput expectedInput)
|
||||
{
|
||||
// Act
|
||||
var input = CommandInput.Parse(arguments, commandNames);
|
||||
|
||||
// Assert
|
||||
input.Should().BeEquivalentTo(expectedInput);
|
||||
}
|
||||
}
|
||||
}
|
||||
27
CliFx.Tests/CancellationSpecs.Commands.cs
Normal file
27
CliFx.Tests/CancellationSpecs.Commands.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using CliFx.Attributes;
|
||||
|
||||
namespace CliFx.Tests
|
||||
{
|
||||
public partial class CancellationSpecs
|
||||
{
|
||||
[Command("cancel")]
|
||||
private class CancellableCommand : ICommand
|
||||
{
|
||||
public async ValueTask ExecuteAsync(IConsole console)
|
||||
{
|
||||
try
|
||||
{
|
||||
await Task.Delay(TimeSpan.FromSeconds(3), console.GetCancellationToken());
|
||||
console.Output.WriteLine("Never printed");
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
console.Output.WriteLine("Cancellation requested");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
43
CliFx.Tests/CancellationSpecs.cs
Normal file
43
CliFx.Tests/CancellationSpecs.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using FluentAssertions;
|
||||
using Xunit;
|
||||
|
||||
namespace CliFx.Tests
|
||||
{
|
||||
public partial class CancellationSpecs
|
||||
{
|
||||
[Fact]
|
||||
public async Task Command_can_perform_additional_cleanup_if_cancellation_is_requested()
|
||||
{
|
||||
// Can't test it with a real console because CliWrap can't send Ctrl+C
|
||||
|
||||
// Arrange
|
||||
using var cts = new CancellationTokenSource();
|
||||
|
||||
await using var stdOut = new MemoryStream();
|
||||
var console = new VirtualConsole(output: stdOut, cancellationToken: cts.Token);
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand(typeof(CancellableCommand))
|
||||
.UseConsole(console)
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
cts.CancelAfter(TimeSpan.FromSeconds(0.2));
|
||||
|
||||
var exitCode = await application.RunAsync(
|
||||
new[] {"cancel"},
|
||||
new Dictionary<string, string>());
|
||||
|
||||
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
|
||||
|
||||
// Assert
|
||||
exitCode.Should().NotBe(0);
|
||||
stdOutData.Should().Be("Cancellation requested");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,45 +0,0 @@
|
||||
using NUnit.Framework;
|
||||
using System;
|
||||
using System.IO;
|
||||
using CliFx.Tests.TestCommands;
|
||||
|
||||
namespace CliFx.Tests
|
||||
{
|
||||
[TestFixture]
|
||||
public class CliApplicationBuilderTests
|
||||
{
|
||||
[Test(Description = "All builder methods must return without exceptions")]
|
||||
public void Smoke_Test()
|
||||
{
|
||||
// Arrange
|
||||
var builder = new CliApplicationBuilder();
|
||||
|
||||
// Act
|
||||
builder
|
||||
.AddCommand(typeof(HelloWorldDefaultCommand))
|
||||
.AddCommandsFrom(typeof(HelloWorldDefaultCommand).Assembly)
|
||||
.AddCommands(new[] {typeof(HelloWorldDefaultCommand)})
|
||||
.AddCommandsFrom(new[] {typeof(HelloWorldDefaultCommand).Assembly})
|
||||
.AddCommandsFromThisAssembly()
|
||||
.AllowDebugMode()
|
||||
.AllowPreviewMode()
|
||||
.UseTitle("test")
|
||||
.UseExecutableName("test")
|
||||
.UseVersionText("test")
|
||||
.UseDescription("test")
|
||||
.UseConsole(new VirtualConsole(TextWriter.Null))
|
||||
.UseTypeActivator(Activator.CreateInstance)
|
||||
.Build();
|
||||
}
|
||||
|
||||
[Test(Description = "Builder must be able to produce an application when no parameters are specified")]
|
||||
public void Build_Test()
|
||||
{
|
||||
// Arrange
|
||||
var builder = new CliApplicationBuilder();
|
||||
|
||||
// Act
|
||||
builder.Build();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,451 +0,0 @@
|
||||
using FluentAssertions;
|
||||
using NUnit.Framework;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using CliFx.Tests.TestCommands;
|
||||
|
||||
namespace CliFx.Tests
|
||||
{
|
||||
[TestFixture]
|
||||
public class CliApplicationTests
|
||||
{
|
||||
private const string TestAppName = "TestApp";
|
||||
private const string TestVersionText = "v1.0";
|
||||
|
||||
private static IEnumerable<TestCaseData> GetTestCases_RunAsync()
|
||||
{
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(HelloWorldDefaultCommand)},
|
||||
new string[0],
|
||||
new Dictionary<string, string>(),
|
||||
"Hello world."
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(ConcatCommand)},
|
||||
new[] {"concat", "-i", "foo", "-i", "bar", "-s", " "},
|
||||
new Dictionary<string, string>(),
|
||||
"foo bar"
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(ConcatCommand)},
|
||||
new[] {"concat", "-i", "one", "two", "three", "-s", ", "},
|
||||
new Dictionary<string, string>(),
|
||||
"one, two, three"
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(DivideCommand)},
|
||||
new[] {"div", "-D", "24", "-d", "8"},
|
||||
new Dictionary<string, string>(),
|
||||
"3"
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(HelloWorldDefaultCommand)},
|
||||
new[] {"--version"},
|
||||
new Dictionary<string, string>(),
|
||||
TestVersionText
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(ConcatCommand)},
|
||||
new[] {"--version"},
|
||||
new Dictionary<string, string>(),
|
||||
TestVersionText
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(ConcatCommand)},
|
||||
new string[0],
|
||||
new Dictionary<string, string>(),
|
||||
null
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(ConcatCommand)},
|
||||
new[] {"-h"},
|
||||
new Dictionary<string, string>(),
|
||||
null
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(ConcatCommand)},
|
||||
new[] {"--help"},
|
||||
new Dictionary<string, string>(),
|
||||
null
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(ConcatCommand)},
|
||||
new[] {"concat", "-h"},
|
||||
new Dictionary<string, string>(),
|
||||
null
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(ExceptionCommand)},
|
||||
new[] {"exc", "-h"},
|
||||
new Dictionary<string, string>(),
|
||||
null
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(CommandExceptionCommand)},
|
||||
new[] {"exc", "-h"},
|
||||
new Dictionary<string, string>(),
|
||||
null
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(ConcatCommand)},
|
||||
new[] {"[preview]"},
|
||||
new Dictionary<string, string>(),
|
||||
null
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(ExceptionCommand)},
|
||||
new[] {"[preview]", "exc"},
|
||||
new Dictionary<string, string>(),
|
||||
null
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(ConcatCommand)},
|
||||
new[] {"[preview]", "concat", "-o", "value"},
|
||||
new Dictionary<string, string>(),
|
||||
null
|
||||
);
|
||||
}
|
||||
|
||||
private static IEnumerable<TestCaseData> GetTestCases_RunAsync_Negative()
|
||||
{
|
||||
yield return new TestCaseData(
|
||||
new Type[0],
|
||||
new string[0],
|
||||
new Dictionary<string, string>(),
|
||||
null, null
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(ConcatCommand)},
|
||||
new[] {"non-existing"},
|
||||
new Dictionary<string, string>(),
|
||||
null, null
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(ExceptionCommand)},
|
||||
new[] {"exc"},
|
||||
new Dictionary<string, string>(),
|
||||
null, null
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(CommandExceptionCommand)},
|
||||
new[] {"exc"},
|
||||
new Dictionary<string, string>(),
|
||||
null, null
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(CommandExceptionCommand)},
|
||||
new[] {"exc"},
|
||||
new Dictionary<string, string>(),
|
||||
null, null
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(CommandExceptionCommand)},
|
||||
new[] {"exc", "-m", "foo bar"},
|
||||
new Dictionary<string, string>(),
|
||||
"foo bar", null
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(CommandExceptionCommand)},
|
||||
new[] {"exc", "-m", "foo bar", "-c", "666"},
|
||||
new Dictionary<string, string>(),
|
||||
"foo bar", 666
|
||||
);
|
||||
}
|
||||
|
||||
private static IEnumerable<TestCaseData> GetTestCases_RunAsync_Help()
|
||||
{
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(HelpDefaultCommand), typeof(HelpNamedCommand), typeof(HelpSubCommand)},
|
||||
new[] {"--help"},
|
||||
new[]
|
||||
{
|
||||
TestVersionText,
|
||||
"Description",
|
||||
"HelpDefaultCommand description.",
|
||||
"Usage",
|
||||
TestAppName, "[command]", "[options]",
|
||||
"Options",
|
||||
"-a|--option-a", "OptionA description.",
|
||||
"-b|--option-b", "OptionB description.",
|
||||
"-h|--help", "Shows help text.",
|
||||
"--version", "Shows version information.",
|
||||
"Commands",
|
||||
"cmd", "HelpNamedCommand description.",
|
||||
"You can run", "to show help on a specific command."
|
||||
}
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(HelpSubCommand)},
|
||||
new[] {"--help"},
|
||||
new[]
|
||||
{
|
||||
TestVersionText,
|
||||
"Usage",
|
||||
TestAppName, "[command]",
|
||||
"Options",
|
||||
"-h|--help", "Shows help text.",
|
||||
"--version", "Shows version information.",
|
||||
"Commands",
|
||||
"cmd sub", "HelpSubCommand description.",
|
||||
"You can run", "to show help on a specific command."
|
||||
}
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(HelpDefaultCommand), typeof(HelpNamedCommand), typeof(HelpSubCommand)},
|
||||
new[] {"cmd", "--help"},
|
||||
new[]
|
||||
{
|
||||
"Description",
|
||||
"HelpNamedCommand description.",
|
||||
"Usage",
|
||||
TestAppName, "cmd", "[command]", "[options]",
|
||||
"Options",
|
||||
"-c|--option-c", "OptionC description.",
|
||||
"-d|--option-d", "OptionD description.",
|
||||
"-h|--help", "Shows help text.",
|
||||
"Commands",
|
||||
"sub", "HelpSubCommand description.",
|
||||
"You can run", "to show help on a specific command."
|
||||
}
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(HelpDefaultCommand), typeof(HelpNamedCommand), typeof(HelpSubCommand)},
|
||||
new[] {"cmd", "sub", "--help"},
|
||||
new[]
|
||||
{
|
||||
"Description",
|
||||
"HelpSubCommand description.",
|
||||
"Usage",
|
||||
TestAppName, "cmd sub", "[options]",
|
||||
"Options",
|
||||
"-e|--option-e", "OptionE description.",
|
||||
"-h|--help", "Shows help text."
|
||||
}
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(ParameterCommand)},
|
||||
new[] {"param", "cmd", "--help"},
|
||||
new[]
|
||||
{
|
||||
"Description",
|
||||
"Command using positional parameters",
|
||||
"Usage",
|
||||
TestAppName, "param cmd", "<first>", "<parameterb>", "<third list...>", "[options]",
|
||||
"Parameters",
|
||||
"* first",
|
||||
"* parameterb",
|
||||
"* third list", "A list of numbers",
|
||||
"Options",
|
||||
"-o|--option",
|
||||
"-h|--help", "Shows help text."
|
||||
}
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(AllRequiredOptionsCommand)},
|
||||
new[] {"allrequired", "--help"},
|
||||
new[]
|
||||
{
|
||||
"Description",
|
||||
"AllRequiredOptionsCommand description.",
|
||||
"Usage",
|
||||
TestAppName, "allrequired --option-f <value> --option-g <value>"
|
||||
}
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(SomeRequiredOptionsCommand)},
|
||||
new[] {"somerequired", "--help"},
|
||||
new[]
|
||||
{
|
||||
"Description",
|
||||
"SomeRequiredOptionsCommand description.",
|
||||
"Usage",
|
||||
TestAppName, "somerequired --option-f <value> [options]"
|
||||
}
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(EnvironmentVariableCommand)},
|
||||
new[] {"--help"},
|
||||
new[]
|
||||
{
|
||||
"Environment variable:", "ENV_SINGLE_VALUE"
|
||||
}
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(ConcatCommand)},
|
||||
new[] {"concat", "--help"},
|
||||
new[]
|
||||
{
|
||||
"Usage",
|
||||
TestAppName, "concat", "-i", "<values...>", "[options]",
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
[TestCaseSource(nameof(GetTestCases_RunAsync))]
|
||||
public async Task RunAsync_Test(
|
||||
IReadOnlyList<Type> commandTypes,
|
||||
IReadOnlyList<string> commandLineArguments,
|
||||
IReadOnlyDictionary<string, string> environmentVariables,
|
||||
string? expectedStdOut = null)
|
||||
{
|
||||
// Arrange
|
||||
await using var stdOutStream = new StringWriter();
|
||||
var console = new VirtualConsole(stdOutStream);
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommands(commandTypes)
|
||||
.UseTitle(TestAppName)
|
||||
.UseExecutableName(TestAppName)
|
||||
.UseVersionText(TestVersionText)
|
||||
.UseConsole(console)
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
var exitCode = await application.RunAsync(commandLineArguments, environmentVariables);
|
||||
var stdOut = stdOutStream.ToString().Trim();
|
||||
|
||||
// Assert
|
||||
exitCode.Should().Be(0);
|
||||
stdOut.Should().NotBeNullOrWhiteSpace();
|
||||
|
||||
if (expectedStdOut != null)
|
||||
stdOut.Should().Be(expectedStdOut);
|
||||
|
||||
Console.WriteLine(stdOut);
|
||||
}
|
||||
|
||||
[TestCaseSource(nameof(GetTestCases_RunAsync_Negative))]
|
||||
public async Task RunAsync_Negative_Test(
|
||||
IReadOnlyList<Type> commandTypes,
|
||||
IReadOnlyList<string> commandLineArguments,
|
||||
IReadOnlyDictionary<string, string> environmentVariables,
|
||||
string? expectedStdErr = null,
|
||||
int? expectedExitCode = null)
|
||||
{
|
||||
// Arrange
|
||||
await using var stdErrStream = new StringWriter();
|
||||
var console = new VirtualConsole(TextWriter.Null, stdErrStream);
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommands(commandTypes)
|
||||
.UseTitle(TestAppName)
|
||||
.UseExecutableName(TestAppName)
|
||||
.UseVersionText(TestVersionText)
|
||||
.UseConsole(console)
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
var exitCode = await application.RunAsync(commandLineArguments, environmentVariables);
|
||||
var stderr = stdErrStream.ToString().Trim();
|
||||
|
||||
// Assert
|
||||
exitCode.Should().NotBe(0);
|
||||
stderr.Should().NotBeNullOrWhiteSpace();
|
||||
|
||||
if (expectedExitCode != null)
|
||||
exitCode.Should().Be(expectedExitCode);
|
||||
|
||||
if (expectedStdErr != null)
|
||||
stderr.Should().Be(expectedStdErr);
|
||||
|
||||
Console.WriteLine(stderr);
|
||||
}
|
||||
|
||||
[TestCaseSource(nameof(GetTestCases_RunAsync_Help))]
|
||||
public async Task RunAsync_Help_Test(
|
||||
IReadOnlyList<Type> commandTypes,
|
||||
IReadOnlyList<string> commandLineArguments,
|
||||
IReadOnlyList<string>? expectedSubstrings = null)
|
||||
{
|
||||
// Arrange
|
||||
await using var stdOutStream = new StringWriter();
|
||||
var console = new VirtualConsole(stdOutStream);
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommands(commandTypes)
|
||||
.UseTitle(TestAppName)
|
||||
.UseExecutableName(TestAppName)
|
||||
.UseVersionText(TestVersionText)
|
||||
.UseConsole(console)
|
||||
.Build();
|
||||
|
||||
var environmentVariables = new Dictionary<string, string>();
|
||||
|
||||
// Act
|
||||
var exitCode = await application.RunAsync(commandLineArguments, environmentVariables);
|
||||
var stdOut = stdOutStream.ToString().Trim();
|
||||
|
||||
// Assert
|
||||
exitCode.Should().Be(0);
|
||||
stdOut.Should().NotBeNullOrWhiteSpace();
|
||||
|
||||
if (expectedSubstrings != null)
|
||||
stdOut.Should().ContainAll(expectedSubstrings);
|
||||
|
||||
Console.WriteLine(stdOut);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task RunAsync_Cancellation_Test()
|
||||
{
|
||||
// Arrange
|
||||
using var cancellationTokenSource = new CancellationTokenSource();
|
||||
|
||||
await using var stdOutStream = new StringWriter();
|
||||
await using var stdErrStream = new StringWriter();
|
||||
var console = new VirtualConsole(stdOutStream, stdErrStream, cancellationTokenSource.Token);
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand(typeof(CancellableCommand))
|
||||
.UseConsole(console)
|
||||
.Build();
|
||||
|
||||
var commandLineArguments = new[] {"cancel"};
|
||||
var environmentVariables = new Dictionary<string, string>();
|
||||
|
||||
// Act
|
||||
cancellationTokenSource.CancelAfter(TimeSpan.FromSeconds(0.2));
|
||||
var exitCode = await application.RunAsync(commandLineArguments, environmentVariables);
|
||||
var stdOut = stdOutStream.ToString().Trim();
|
||||
var stdErr = stdErrStream.ToString().Trim();
|
||||
|
||||
// Assert
|
||||
exitCode.Should().NotBe(0);
|
||||
stdOut.Should().BeNullOrWhiteSpace();
|
||||
stdErr.Should().NotBeNullOrWhiteSpace();
|
||||
|
||||
Console.WriteLine(stdErr);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,11 +11,16 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="CliWrap" Version="2.5.0" />
|
||||
<PackageReference Include="FluentAssertions" Version="5.10.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
|
||||
<PackageReference Include="NUnit" Version="3.12.0" />
|
||||
<PackageReference Include="NUnit3TestAdapter" Version="3.16.1" />
|
||||
<Content Include="xunit.runner.json" CopyToOutputDirectory="PreserveNewest" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="CliWrap" Version="3.0.0" />
|
||||
<PackageReference Include="FluentAssertions" Version="5.10.2" />
|
||||
<PackageReference Include="GitHubActionsTestLogger" Version="1.0.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
|
||||
<PackageReference Include="xunit" Version="2.4.1" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
|
||||
<PackageReference Include="coverlet.msbuild" Version="2.8.0" PrivateAssets="all" />
|
||||
</ItemGroup>
|
||||
|
||||
@@ -24,8 +29,12 @@
|
||||
<ProjectReference Include="..\CliFx\CliFx.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<Target Name="Copy dummy's runtime config" AfterTargets="AfterBuild">
|
||||
<Copy SourceFiles="../CliFx.Tests.Dummy/bin/$(Configuration)/$(TargetFramework)/CliFx.Tests.Dummy.runtimeconfig.json" DestinationFiles="$(OutputPath)CliFx.Tests.Dummy.runtimeconfig.json" />
|
||||
</Target>
|
||||
<ItemGroup>
|
||||
<None Include="../CliFx.Tests.Dummy/bin/$(Configuration)/$(TargetFramework)/CliFx.Tests.Dummy.runtimeconfig.json">
|
||||
<Link>CliFx.Tests.Dummy.runtimeconfig.json</Link>
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
<Visible>False</Visible>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
74
CliFx.Tests/ConsoleSpecs.cs
Normal file
74
CliFx.Tests/ConsoleSpecs.cs
Normal file
@@ -0,0 +1,74 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using CliWrap;
|
||||
using CliWrap.Buffered;
|
||||
using FluentAssertions;
|
||||
using Xunit;
|
||||
|
||||
namespace CliFx.Tests
|
||||
{
|
||||
public class ConsoleSpecs
|
||||
{
|
||||
[Fact]
|
||||
public async Task Real_implementation_of_console_maps_directly_to_system_console()
|
||||
{
|
||||
// Arrange
|
||||
var command = "Hello world" | Cli.Wrap("dotnet")
|
||||
.WithArguments(a => a
|
||||
.Add(Dummy.Program.Location)
|
||||
.Add("console-test"));
|
||||
|
||||
// Act
|
||||
var result = await command.ExecuteBufferedAsync();
|
||||
|
||||
// Assert
|
||||
result.StandardOutput.TrimEnd().Should().Be("Hello world");
|
||||
result.StandardError.TrimEnd().Should().Be("Hello world");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Fake_implementation_of_console_can_be_used_to_execute_commands_in_isolation()
|
||||
{
|
||||
// Arrange
|
||||
using var stdIn = new MemoryStream(Console.InputEncoding.GetBytes("input"));
|
||||
using var stdOut = new MemoryStream();
|
||||
using var stdErr = new MemoryStream();
|
||||
|
||||
var console = new VirtualConsole(
|
||||
input: stdIn,
|
||||
output: stdOut,
|
||||
error: stdErr);
|
||||
|
||||
// Act
|
||||
console.Output.Write("output");
|
||||
console.Error.Write("error");
|
||||
|
||||
var stdInData = console.Input.ReadToEnd();
|
||||
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray());
|
||||
var stdErrData = console.Error.Encoding.GetString(stdErr.ToArray());
|
||||
|
||||
console.ResetColor();
|
||||
console.ForegroundColor = ConsoleColor.DarkMagenta;
|
||||
console.BackgroundColor = ConsoleColor.DarkMagenta;
|
||||
console.CursorLeft = 42;
|
||||
console.CursorTop = 24;
|
||||
|
||||
// Assert
|
||||
stdInData.Should().Be("input");
|
||||
stdOutData.Should().Be("output");
|
||||
stdErrData.Should().Be("error");
|
||||
|
||||
console.Input.Should().NotBeSameAs(Console.In);
|
||||
console.Output.Should().NotBeSameAs(Console.Out);
|
||||
console.Error.Should().NotBeSameAs(Console.Error);
|
||||
|
||||
console.IsInputRedirected.Should().BeTrue();
|
||||
console.IsOutputRedirected.Should().BeTrue();
|
||||
console.IsErrorRedirected.Should().BeTrue();
|
||||
|
||||
console.ForegroundColor.Should().NotBe(Console.ForegroundColor);
|
||||
console.BackgroundColor.Should().NotBe(Console.BackgroundColor);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,48 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using CliFx.Exceptions;
|
||||
using CliFx.Tests.TestCommands;
|
||||
using CliFx.Tests.TestCustomTypes;
|
||||
using FluentAssertions;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace CliFx.Tests
|
||||
{
|
||||
[TestFixture]
|
||||
public class DefaultCommandFactoryTests
|
||||
{
|
||||
private static IEnumerable<TestCaseData> GetTestCases_CreateInstance()
|
||||
{
|
||||
yield return new TestCaseData(typeof(HelloWorldDefaultCommand));
|
||||
}
|
||||
|
||||
private static IEnumerable<TestCaseData> GetTestCases_CreateInstance_Negative()
|
||||
{
|
||||
yield return new TestCaseData(typeof(TestNonStringParseable));
|
||||
}
|
||||
|
||||
[TestCaseSource(nameof(GetTestCases_CreateInstance))]
|
||||
public void CreateInstance_Test(Type type)
|
||||
{
|
||||
// Arrange
|
||||
var activator = new DefaultTypeActivator();
|
||||
|
||||
// Act
|
||||
var obj = activator.CreateInstance(type);
|
||||
|
||||
// Assert
|
||||
obj.Should().BeOfType(type);
|
||||
}
|
||||
|
||||
[TestCaseSource(nameof(GetTestCases_CreateInstance_Negative))]
|
||||
public void CreateInstance_Negative_Test(Type type)
|
||||
{
|
||||
// Arrange
|
||||
var activator = new DefaultTypeActivator();
|
||||
|
||||
// Act & Assert
|
||||
var ex = Assert.Throws<CliFxException>(() => activator.CreateInstance(type));
|
||||
Console.WriteLine(ex.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,53 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using CliFx.Exceptions;
|
||||
using CliFx.Tests.TestCommands;
|
||||
using FluentAssertions;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace CliFx.Tests
|
||||
{
|
||||
[TestFixture]
|
||||
public class DelegateCommandFactoryTests
|
||||
{
|
||||
private static IEnumerable<TestCaseData> GetTestCases_CreateInstance()
|
||||
{
|
||||
yield return new TestCaseData(
|
||||
new Func<Type, object>(Activator.CreateInstance),
|
||||
typeof(HelloWorldDefaultCommand)
|
||||
);
|
||||
}
|
||||
|
||||
private static IEnumerable<TestCaseData> GetTestCases_CreateInstance_Negative()
|
||||
{
|
||||
yield return new TestCaseData(
|
||||
new Func<Type, object>(_ => null),
|
||||
typeof(HelloWorldDefaultCommand)
|
||||
);
|
||||
}
|
||||
|
||||
[TestCaseSource(nameof(GetTestCases_CreateInstance))]
|
||||
public void CreateInstance_Test(Func<Type, object> activatorFunc, Type type)
|
||||
{
|
||||
// Arrange
|
||||
var activator = new DelegateTypeActivator(activatorFunc);
|
||||
|
||||
// Act
|
||||
var obj = activator.CreateInstance(type);
|
||||
|
||||
// Assert
|
||||
obj.Should().BeOfType(type);
|
||||
}
|
||||
|
||||
[TestCaseSource(nameof(GetTestCases_CreateInstance_Negative))]
|
||||
public void CreateInstance_Negative_Test(Func<Type, object> activatorFunc, Type type)
|
||||
{
|
||||
// Arrange
|
||||
var activator = new DelegateTypeActivator(activatorFunc);
|
||||
|
||||
// Act & Assert
|
||||
var ex = Assert.Throws<CliFxException>(() => activator.CreateInstance(type));
|
||||
Console.WriteLine(ex.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
37
CliFx.Tests/DependencyInjectionSpecs.Commands.cs
Normal file
37
CliFx.Tests/DependencyInjectionSpecs.Commands.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
using System.Threading.Tasks;
|
||||
using CliFx.Attributes;
|
||||
|
||||
namespace CliFx.Tests
|
||||
{
|
||||
public partial class DependencyInjectionSpecs
|
||||
{
|
||||
[Command]
|
||||
private class WithoutDependenciesCommand : ICommand
|
||||
{
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
|
||||
private class DependencyA
|
||||
{
|
||||
}
|
||||
|
||||
private class DependencyB
|
||||
{
|
||||
}
|
||||
|
||||
[Command]
|
||||
private class WithDependenciesCommand : ICommand
|
||||
{
|
||||
private readonly DependencyA _dependencyA;
|
||||
private readonly DependencyB _dependencyB;
|
||||
|
||||
public WithDependenciesCommand(DependencyA dependencyA, DependencyB dependencyB)
|
||||
{
|
||||
_dependencyA = dependencyA;
|
||||
_dependencyB = dependencyB;
|
||||
}
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
}
|
||||
}
|
||||
63
CliFx.Tests/DependencyInjectionSpecs.cs
Normal file
63
CliFx.Tests/DependencyInjectionSpecs.cs
Normal file
@@ -0,0 +1,63 @@
|
||||
using CliFx.Exceptions;
|
||||
using FluentAssertions;
|
||||
using Xunit;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
namespace CliFx.Tests
|
||||
{
|
||||
public partial class DependencyInjectionSpecs
|
||||
{
|
||||
private readonly ITestOutputHelper _output;
|
||||
|
||||
public DependencyInjectionSpecs(ITestOutputHelper output) => _output = output;
|
||||
|
||||
[Fact]
|
||||
public void Default_type_activator_can_initialize_a_command_if_it_has_a_parameterless_constructor()
|
||||
{
|
||||
// Arrange
|
||||
var activator = new DefaultTypeActivator();
|
||||
|
||||
// Act
|
||||
var obj = activator.CreateInstance(typeof(WithoutDependenciesCommand));
|
||||
|
||||
// Assert
|
||||
obj.Should().BeOfType<WithoutDependenciesCommand>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Default_type_activator_cannot_initialize_a_command_if_it_does_not_have_a_parameterless_constructor()
|
||||
{
|
||||
// Arrange
|
||||
var activator = new DefaultTypeActivator();
|
||||
|
||||
// Act & assert
|
||||
var ex = Assert.Throws<CliFxException>(() => activator.CreateInstance(typeof(WithDependenciesCommand)));
|
||||
_output.WriteLine(ex.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Delegate_type_activator_can_initialize_a_command_using_a_custom_function()
|
||||
{
|
||||
// Arrange
|
||||
var activator = new DelegateTypeActivator(_ =>
|
||||
new WithDependenciesCommand(new DependencyA(), new DependencyB()));
|
||||
|
||||
// Act
|
||||
var obj = activator.CreateInstance(typeof(WithDependenciesCommand));
|
||||
|
||||
// Assert
|
||||
obj.Should().BeOfType<WithDependenciesCommand>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Delegate_type_activator_throws_if_the_underlying_function_returns_null()
|
||||
{
|
||||
// Arrange
|
||||
var activator = new DelegateTypeActivator(_ => null!);
|
||||
|
||||
// Act & assert
|
||||
var ex = Assert.Throws<CliFxException>(() => activator.CreateInstance(typeof(WithDependenciesCommand)));
|
||||
_output.WriteLine(ex.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
14
CliFx.Tests/DirectivesSpecs.Commands.cs
Normal file
14
CliFx.Tests/DirectivesSpecs.Commands.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using System.Threading.Tasks;
|
||||
using CliFx.Attributes;
|
||||
|
||||
namespace CliFx.Tests
|
||||
{
|
||||
public partial class DirectivesSpecs
|
||||
{
|
||||
[Command("cmd")]
|
||||
private class NamedCommand : ICommand
|
||||
{
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
}
|
||||
}
|
||||
36
CliFx.Tests/DirectivesSpecs.cs
Normal file
36
CliFx.Tests/DirectivesSpecs.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using FluentAssertions;
|
||||
using Xunit;
|
||||
|
||||
namespace CliFx.Tests
|
||||
{
|
||||
public partial class DirectivesSpecs
|
||||
{
|
||||
[Fact]
|
||||
public async Task Preview_directive_can_be_specified_to_print_provided_arguments_as_they_were_parsed()
|
||||
{
|
||||
// Arrange
|
||||
await using var stdOut = new MemoryStream();
|
||||
var console = new VirtualConsole(output: stdOut);
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand(typeof(NamedCommand))
|
||||
.UseConsole(console)
|
||||
.AllowPreviewMode()
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
var exitCode = await application.RunAsync(
|
||||
new[] {"[preview]", "cmd", "param", "-abc", "--option", "foo"},
|
||||
new Dictionary<string, string>());
|
||||
|
||||
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
|
||||
|
||||
// Assert
|
||||
exitCode.Should().Be(0);
|
||||
stdOutData.Should().ContainAll("cmd", "<param>", "[-a]", "[-b]", "[-c]", "[--option \"foo\"]");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,888 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using CliFx.Domain;
|
||||
using CliFx.Exceptions;
|
||||
using CliFx.Tests.TestCommands;
|
||||
using CliFx.Tests.TestCustomTypes;
|
||||
using FluentAssertions;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace CliFx.Tests.Domain
|
||||
{
|
||||
[TestFixture]
|
||||
internal partial class ApplicationSchemaTests
|
||||
{
|
||||
private static IEnumerable<TestCaseData> GetTestCases_Resolve()
|
||||
{
|
||||
yield return new TestCaseData(
|
||||
new[]
|
||||
{
|
||||
typeof(DivideCommand),
|
||||
typeof(ConcatCommand),
|
||||
typeof(EnvironmentVariableCommand)
|
||||
},
|
||||
new[]
|
||||
{
|
||||
new CommandSchema(typeof(DivideCommand), "div", "Divide one number by another.",
|
||||
new CommandParameterSchema[0], new[]
|
||||
{
|
||||
new CommandOptionSchema(typeof(DivideCommand).GetProperty(nameof(DivideCommand.Dividend)),
|
||||
"dividend", 'D', null, true, "The number to divide."),
|
||||
new CommandOptionSchema(typeof(DivideCommand).GetProperty(nameof(DivideCommand.Divisor)),
|
||||
"divisor", 'd', null, true, "The number to divide by.")
|
||||
}),
|
||||
new CommandSchema(typeof(ConcatCommand), "concat", "Concatenate strings.",
|
||||
new CommandParameterSchema[0],
|
||||
new[]
|
||||
{
|
||||
new CommandOptionSchema(typeof(ConcatCommand).GetProperty(nameof(ConcatCommand.Inputs)),
|
||||
null, 'i', null, true, "Input strings."),
|
||||
new CommandOptionSchema(typeof(ConcatCommand).GetProperty(nameof(ConcatCommand.Separator)),
|
||||
null, 's', null, false, "String separator.")
|
||||
}),
|
||||
new CommandSchema(typeof(EnvironmentVariableCommand), null, "Reads option values from environment variables.",
|
||||
new CommandParameterSchema[0],
|
||||
new[]
|
||||
{
|
||||
new CommandOptionSchema(typeof(EnvironmentVariableCommand).GetProperty(nameof(EnvironmentVariableCommand.Option)),
|
||||
"opt", null, "ENV_SINGLE_VALUE", false, null)
|
||||
}
|
||||
)
|
||||
}
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(SimpleParameterCommand)},
|
||||
new[]
|
||||
{
|
||||
new CommandSchema(typeof(SimpleParameterCommand), "param cmd2", "Command using positional parameters",
|
||||
new[]
|
||||
{
|
||||
new CommandParameterSchema(typeof(SimpleParameterCommand).GetProperty(nameof(SimpleParameterCommand.ParameterA)),
|
||||
0, "first", null),
|
||||
new CommandParameterSchema(typeof(SimpleParameterCommand).GetProperty(nameof(SimpleParameterCommand.ParameterB)),
|
||||
10, null, null)
|
||||
},
|
||||
new[]
|
||||
{
|
||||
new CommandOptionSchema(typeof(SimpleParameterCommand).GetProperty(nameof(SimpleParameterCommand.OptionA)),
|
||||
"option", 'o', null, false, null)
|
||||
})
|
||||
}
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(HelloWorldDefaultCommand)},
|
||||
new[]
|
||||
{
|
||||
new CommandSchema(typeof(HelloWorldDefaultCommand), null, null,
|
||||
new CommandParameterSchema[0],
|
||||
new CommandOptionSchema[0])
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
private static IEnumerable<TestCaseData> GetTestCases_Resolve_Negative()
|
||||
{
|
||||
yield return new TestCaseData(new object[]
|
||||
{
|
||||
new Type[0]
|
||||
});
|
||||
|
||||
// Command validation failure
|
||||
|
||||
yield return new TestCaseData(new object[]
|
||||
{
|
||||
new[] {typeof(NonImplementedCommand)}
|
||||
});
|
||||
|
||||
yield return new TestCaseData(new object[]
|
||||
{
|
||||
// Same name
|
||||
new[] {typeof(ExceptionCommand), typeof(CommandExceptionCommand)}
|
||||
});
|
||||
|
||||
yield return new TestCaseData(new object[]
|
||||
{
|
||||
new[] {typeof(NonAnnotatedCommand)}
|
||||
});
|
||||
|
||||
// Parameter validation failure
|
||||
|
||||
yield return new TestCaseData(new object[]
|
||||
{
|
||||
new[] {typeof(DuplicateParameterOrderCommand)}
|
||||
});
|
||||
|
||||
yield return new TestCaseData(new object[]
|
||||
{
|
||||
new[] {typeof(DuplicateParameterNameCommand)}
|
||||
});
|
||||
|
||||
yield return new TestCaseData(new object[]
|
||||
{
|
||||
new[] {typeof(MultipleNonScalarParametersCommand)}
|
||||
});
|
||||
|
||||
yield return new TestCaseData(new object[]
|
||||
{
|
||||
new[] {typeof(NonLastNonScalarParameterCommand)}
|
||||
});
|
||||
|
||||
// Option validation failure
|
||||
|
||||
yield return new TestCaseData(new object[]
|
||||
{
|
||||
new[] {typeof(DuplicateOptionNamesCommand)}
|
||||
});
|
||||
|
||||
yield return new TestCaseData(new object[]
|
||||
{
|
||||
new[] {typeof(DuplicateOptionShortNamesCommand)}
|
||||
});
|
||||
|
||||
yield return new TestCaseData(new object[]
|
||||
{
|
||||
new[] {typeof(DuplicateOptionEnvironmentVariableNamesCommand)}
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
[TestCaseSource(nameof(GetTestCases_Resolve))]
|
||||
public void Resolve_Test(
|
||||
IReadOnlyList<Type> commandTypes,
|
||||
IReadOnlyList<CommandSchema> expectedCommandSchemas)
|
||||
{
|
||||
// Act
|
||||
var applicationSchema = ApplicationSchema.Resolve(commandTypes);
|
||||
|
||||
// Assert
|
||||
applicationSchema.Commands.Should().BeEquivalentTo(expectedCommandSchemas);
|
||||
}
|
||||
|
||||
[Test]
|
||||
[TestCaseSource(nameof(GetTestCases_Resolve_Negative))]
|
||||
public void Resolve_Negative_Test(IReadOnlyList<Type> commandTypes)
|
||||
{
|
||||
// Act & Assert
|
||||
var ex = Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
|
||||
Console.WriteLine(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
internal partial class ApplicationSchemaTests
|
||||
{
|
||||
private static IEnumerable<TestCaseData> GetTestCases_InitializeEntryPoint()
|
||||
{
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(AllSupportedTypesCommand)},
|
||||
new CommandLineInput(
|
||||
new[]
|
||||
{
|
||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.Object), "value")
|
||||
}),
|
||||
new Dictionary<string, string>(),
|
||||
new AllSupportedTypesCommand {Object = "value"}
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(AllSupportedTypesCommand)},
|
||||
new CommandLineInput(
|
||||
new[]
|
||||
{
|
||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.String), "value")
|
||||
}),
|
||||
new Dictionary<string, string>(),
|
||||
new AllSupportedTypesCommand {String = "value"}
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(AllSupportedTypesCommand)},
|
||||
new CommandLineInput(
|
||||
new[]
|
||||
{
|
||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.Bool), "true")
|
||||
}),
|
||||
new Dictionary<string, string>(),
|
||||
new AllSupportedTypesCommand {Bool = true}
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(AllSupportedTypesCommand)},
|
||||
new CommandLineInput(
|
||||
new[]
|
||||
{
|
||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.Bool), "false")
|
||||
}),
|
||||
new Dictionary<string, string>(),
|
||||
new AllSupportedTypesCommand {Bool = false}
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(AllSupportedTypesCommand)},
|
||||
new CommandLineInput(
|
||||
new[]
|
||||
{
|
||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.Bool))
|
||||
}),
|
||||
new Dictionary<string, string>(),
|
||||
new AllSupportedTypesCommand {Bool = true}
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(AllSupportedTypesCommand)},
|
||||
new CommandLineInput(
|
||||
new[]
|
||||
{
|
||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.Char), "a")
|
||||
}),
|
||||
new Dictionary<string, string>(),
|
||||
new AllSupportedTypesCommand {Char = 'a'}
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(AllSupportedTypesCommand)},
|
||||
new CommandLineInput(
|
||||
new[]
|
||||
{
|
||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.Sbyte), "15")
|
||||
}),
|
||||
new Dictionary<string, string>(),
|
||||
new AllSupportedTypesCommand {Sbyte = 15}
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(AllSupportedTypesCommand)},
|
||||
new CommandLineInput(
|
||||
new[]
|
||||
{
|
||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.Byte), "15")
|
||||
}),
|
||||
new Dictionary<string, string>(),
|
||||
new AllSupportedTypesCommand {Byte = 15}
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(AllSupportedTypesCommand)},
|
||||
new CommandLineInput(
|
||||
new[]
|
||||
{
|
||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.Short), "15")
|
||||
}),
|
||||
new Dictionary<string, string>(),
|
||||
new AllSupportedTypesCommand {Short = 15}
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(AllSupportedTypesCommand)},
|
||||
new CommandLineInput(
|
||||
new[]
|
||||
{
|
||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.Ushort), "15")
|
||||
}),
|
||||
new Dictionary<string, string>(),
|
||||
new AllSupportedTypesCommand {Ushort = 15}
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(AllSupportedTypesCommand)},
|
||||
new CommandLineInput(
|
||||
new[]
|
||||
{
|
||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.Int), "15")
|
||||
}),
|
||||
new Dictionary<string, string>(),
|
||||
new AllSupportedTypesCommand {Int = 15}
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(AllSupportedTypesCommand)},
|
||||
new CommandLineInput(
|
||||
new[]
|
||||
{
|
||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.Uint), "15")
|
||||
}),
|
||||
new Dictionary<string, string>(),
|
||||
new AllSupportedTypesCommand {Uint = 15}
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(AllSupportedTypesCommand)},
|
||||
new CommandLineInput(
|
||||
new[]
|
||||
{
|
||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.Long), "15")
|
||||
}),
|
||||
new Dictionary<string, string>(),
|
||||
new AllSupportedTypesCommand {Long = 15}
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(AllSupportedTypesCommand)},
|
||||
new CommandLineInput(
|
||||
new[]
|
||||
{
|
||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.Ulong), "15")
|
||||
}),
|
||||
new Dictionary<string, string>(),
|
||||
new AllSupportedTypesCommand {Ulong = 15}
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(AllSupportedTypesCommand)},
|
||||
new CommandLineInput(
|
||||
new[]
|
||||
{
|
||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.Float), "123.45")
|
||||
}),
|
||||
new Dictionary<string, string>(),
|
||||
new AllSupportedTypesCommand {Float = 123.45f}
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(AllSupportedTypesCommand)},
|
||||
new CommandLineInput(
|
||||
new[]
|
||||
{
|
||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.Double), "123.45")
|
||||
}),
|
||||
new Dictionary<string, string>(),
|
||||
new AllSupportedTypesCommand {Double = 123.45}
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(AllSupportedTypesCommand)},
|
||||
new CommandLineInput(
|
||||
new[]
|
||||
{
|
||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.Decimal), "123.45")
|
||||
}),
|
||||
new Dictionary<string, string>(),
|
||||
new AllSupportedTypesCommand {Decimal = 123.45m}
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(AllSupportedTypesCommand)},
|
||||
new CommandLineInput(
|
||||
new[]
|
||||
{
|
||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.DateTime), "28 Apr 1995")
|
||||
}),
|
||||
new Dictionary<string, string>(),
|
||||
new AllSupportedTypesCommand {DateTime = new DateTime(1995, 04, 28)}
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(AllSupportedTypesCommand)},
|
||||
new CommandLineInput(
|
||||
new[]
|
||||
{
|
||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.DateTimeOffset), "28 Apr 1995")
|
||||
}),
|
||||
new Dictionary<string, string>(),
|
||||
new AllSupportedTypesCommand {DateTimeOffset = new DateTime(1995, 04, 28)}
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(AllSupportedTypesCommand)},
|
||||
new CommandLineInput(
|
||||
new[]
|
||||
{
|
||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.TimeSpan), "00:14:59")
|
||||
}),
|
||||
new Dictionary<string, string>(),
|
||||
new AllSupportedTypesCommand {TimeSpan = new TimeSpan(00, 14, 59)}
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(AllSupportedTypesCommand)},
|
||||
new CommandLineInput(
|
||||
new[]
|
||||
{
|
||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.TestEnum), "value2")
|
||||
}),
|
||||
new Dictionary<string, string>(),
|
||||
new AllSupportedTypesCommand {TestEnum = TestEnum.Value2}
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(AllSupportedTypesCommand)},
|
||||
new CommandLineInput(
|
||||
new[]
|
||||
{
|
||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.IntNullable), "666")
|
||||
}),
|
||||
new Dictionary<string, string>(),
|
||||
new AllSupportedTypesCommand {IntNullable = 666}
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(AllSupportedTypesCommand)},
|
||||
new CommandLineInput(
|
||||
new[]
|
||||
{
|
||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.IntNullable))
|
||||
}),
|
||||
new Dictionary<string, string>(),
|
||||
new AllSupportedTypesCommand {IntNullable = null}
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(AllSupportedTypesCommand)},
|
||||
new CommandLineInput(
|
||||
new[]
|
||||
{
|
||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.TestEnumNullable), "value3")
|
||||
}),
|
||||
new Dictionary<string, string>(),
|
||||
new AllSupportedTypesCommand {TestEnumNullable = TestEnum.Value3}
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(AllSupportedTypesCommand)},
|
||||
new CommandLineInput(
|
||||
new[]
|
||||
{
|
||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.TestEnumNullable))
|
||||
}),
|
||||
new Dictionary<string, string>(),
|
||||
new AllSupportedTypesCommand {TestEnumNullable = null}
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(AllSupportedTypesCommand)},
|
||||
new CommandLineInput(
|
||||
new[]
|
||||
{
|
||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.TimeSpanNullable), "01:00:00")
|
||||
}),
|
||||
new Dictionary<string, string>(),
|
||||
new AllSupportedTypesCommand {TimeSpanNullable = new TimeSpan(01, 00, 00)}
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(AllSupportedTypesCommand)},
|
||||
new CommandLineInput(
|
||||
new[]
|
||||
{
|
||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.TimeSpanNullable))
|
||||
}),
|
||||
new Dictionary<string, string>(),
|
||||
new AllSupportedTypesCommand {TimeSpanNullable = null}
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(AllSupportedTypesCommand)},
|
||||
new CommandLineInput(
|
||||
new[]
|
||||
{
|
||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.TestStringConstructable), "value")
|
||||
}),
|
||||
new Dictionary<string, string>(),
|
||||
new AllSupportedTypesCommand {TestStringConstructable = new TestStringConstructable("value")}
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(AllSupportedTypesCommand)},
|
||||
new CommandLineInput(
|
||||
new[]
|
||||
{
|
||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.TestStringParseable), "value")
|
||||
}),
|
||||
new Dictionary<string, string>(),
|
||||
new AllSupportedTypesCommand {TestStringParseable = TestStringParseable.Parse("value")}
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(AllSupportedTypesCommand)},
|
||||
new CommandLineInput(
|
||||
new[]
|
||||
{
|
||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.TestStringParseableWithFormatProvider), "value")
|
||||
}),
|
||||
new Dictionary<string, string>(),
|
||||
new AllSupportedTypesCommand
|
||||
{
|
||||
TestStringParseableWithFormatProvider =
|
||||
TestStringParseableWithFormatProvider.Parse("value", CultureInfo.InvariantCulture)
|
||||
}
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(AllSupportedTypesCommand)},
|
||||
new CommandLineInput(
|
||||
new[]
|
||||
{
|
||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.ObjectArray), new[] {"value1", "value2"})
|
||||
}),
|
||||
new Dictionary<string, string>(),
|
||||
new AllSupportedTypesCommand {ObjectArray = new object[] {"value1", "value2"}}
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(AllSupportedTypesCommand)},
|
||||
new CommandLineInput(
|
||||
new[]
|
||||
{
|
||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.StringArray), new[] {"value1", "value2"})
|
||||
}),
|
||||
new Dictionary<string, string>(),
|
||||
new AllSupportedTypesCommand {StringArray = new[] {"value1", "value2"}}
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(AllSupportedTypesCommand)},
|
||||
new CommandLineInput(
|
||||
new[]
|
||||
{
|
||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.StringArray))
|
||||
}),
|
||||
new Dictionary<string, string>(),
|
||||
new AllSupportedTypesCommand {StringArray = new string[0]}
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(AllSupportedTypesCommand)},
|
||||
new CommandLineInput(
|
||||
new[]
|
||||
{
|
||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.IntArray), new[] {"47", "69"})
|
||||
}),
|
||||
new Dictionary<string, string>(),
|
||||
new AllSupportedTypesCommand {IntArray = new[] {47, 69}}
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(AllSupportedTypesCommand)},
|
||||
new CommandLineInput(
|
||||
new[]
|
||||
{
|
||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.TestEnumArray), new[] {"value1", "value3"})
|
||||
}),
|
||||
new Dictionary<string, string>(),
|
||||
new AllSupportedTypesCommand {TestEnumArray = new[] {TestEnum.Value1, TestEnum.Value3}}
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(AllSupportedTypesCommand)},
|
||||
new CommandLineInput(
|
||||
new[]
|
||||
{
|
||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.IntNullableArray), new[] {"1337", "2441"})
|
||||
}),
|
||||
new Dictionary<string, string>(),
|
||||
new AllSupportedTypesCommand {IntNullableArray = new int?[] {1337, 2441}}
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(AllSupportedTypesCommand)},
|
||||
new CommandLineInput(
|
||||
new[]
|
||||
{
|
||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.TestStringConstructableArray), new[] {"value1", "value2"})
|
||||
}),
|
||||
new Dictionary<string, string>(),
|
||||
new AllSupportedTypesCommand
|
||||
{
|
||||
TestStringConstructableArray = new[]
|
||||
{
|
||||
new TestStringConstructable("value1"),
|
||||
new TestStringConstructable("value2")
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(AllSupportedTypesCommand)},
|
||||
new CommandLineInput(
|
||||
new[]
|
||||
{
|
||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.Enumerable), new[] {"value1", "value3"})
|
||||
}),
|
||||
new Dictionary<string, string>(),
|
||||
new AllSupportedTypesCommand {Enumerable = new[] {"value1", "value3"}}
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(AllSupportedTypesCommand)},
|
||||
new CommandLineInput(
|
||||
new[]
|
||||
{
|
||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.StringEnumerable), new[] {"value1", "value3"})
|
||||
}),
|
||||
new Dictionary<string, string>(),
|
||||
new AllSupportedTypesCommand {StringEnumerable = new[] {"value1", "value3"}}
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(AllSupportedTypesCommand)},
|
||||
new CommandLineInput(
|
||||
new[]
|
||||
{
|
||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.StringReadOnlyList), new[] {"value1", "value3"})
|
||||
}),
|
||||
new Dictionary<string, string>(),
|
||||
new AllSupportedTypesCommand {StringReadOnlyList = new[] {"value1", "value3"}}
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(AllSupportedTypesCommand)},
|
||||
new CommandLineInput(
|
||||
new[]
|
||||
{
|
||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.StringList), new[] {"value1", "value3"})
|
||||
}),
|
||||
new Dictionary<string, string>(),
|
||||
new AllSupportedTypesCommand {StringList = new List<string> {"value1", "value3"}}
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(AllSupportedTypesCommand)},
|
||||
new CommandLineInput(
|
||||
new[]
|
||||
{
|
||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.StringHashSet), new[] {"value1", "value3"})
|
||||
}),
|
||||
new Dictionary<string, string>(),
|
||||
new AllSupportedTypesCommand {StringHashSet = new HashSet<string> {"value1", "value3"}}
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(DivideCommand)},
|
||||
new CommandLineInput(
|
||||
new[] {"div"},
|
||||
new[]
|
||||
{
|
||||
new CommandOptionInput("dividend", "13"),
|
||||
new CommandOptionInput("divisor", "8"),
|
||||
}),
|
||||
new Dictionary<string, string>(),
|
||||
new DivideCommand {Dividend = 13, Divisor = 8}
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(DivideCommand)},
|
||||
new CommandLineInput(
|
||||
new[] {"div"},
|
||||
new[]
|
||||
{
|
||||
new CommandOptionInput("D", "13"),
|
||||
new CommandOptionInput("d", "8"),
|
||||
}),
|
||||
new Dictionary<string, string>(),
|
||||
new DivideCommand {Dividend = 13, Divisor = 8}
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(DivideCommand)},
|
||||
new CommandLineInput(
|
||||
new[] {"div"},
|
||||
new[]
|
||||
{
|
||||
new CommandOptionInput("dividend", "13"),
|
||||
new CommandOptionInput("d", "8"),
|
||||
}),
|
||||
new Dictionary<string, string>(),
|
||||
new DivideCommand {Dividend = 13, Divisor = 8}
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(ConcatCommand)},
|
||||
new CommandLineInput(
|
||||
new[] {"concat"},
|
||||
new[] {new CommandOptionInput("i", new[] {"foo", " ", "bar"}),}),
|
||||
new Dictionary<string, string>(),
|
||||
new ConcatCommand {Inputs = new[] {"foo", " ", "bar"}}
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(ConcatCommand)},
|
||||
new CommandLineInput(
|
||||
new[] {"concat"},
|
||||
new[]
|
||||
{
|
||||
new CommandOptionInput("i", new[] {"foo", "bar"}),
|
||||
new CommandOptionInput("s", " "),
|
||||
}),
|
||||
new Dictionary<string, string>(),
|
||||
new ConcatCommand {Inputs = new[] {"foo", "bar"}, Separator = " "}
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(EnvironmentVariableCommand)},
|
||||
CommandLineInput.Empty,
|
||||
new Dictionary<string, string>
|
||||
{
|
||||
["ENV_SINGLE_VALUE"] = "A"
|
||||
},
|
||||
new EnvironmentVariableCommand {Option = "A"}
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(EnvironmentVariableWithMultipleValuesCommand)},
|
||||
CommandLineInput.Empty,
|
||||
new Dictionary<string, string>
|
||||
{
|
||||
["ENV_MULTIPLE_VALUES"] = string.Join(Path.PathSeparator, "A", "B", "C")
|
||||
},
|
||||
new EnvironmentVariableWithMultipleValuesCommand {Option = new[] {"A", "B", "C"}}
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(EnvironmentVariableCommand)},
|
||||
new CommandLineInput(new[] {new CommandOptionInput("opt", "X")}),
|
||||
new Dictionary<string, string>
|
||||
{
|
||||
["ENV_SINGLE_VALUE"] = "A"
|
||||
},
|
||||
new EnvironmentVariableCommand {Option = "X"}
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(EnvironmentVariableWithoutCollectionPropertyCommand)},
|
||||
CommandLineInput.Empty,
|
||||
new Dictionary<string, string>
|
||||
{
|
||||
["ENV_MULTIPLE_VALUES"] = string.Join(Path.PathSeparator, "A", "B", "C")
|
||||
},
|
||||
new EnvironmentVariableWithoutCollectionPropertyCommand {Option = string.Join(Path.PathSeparator, "A", "B", "C")}
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(ParameterCommand)},
|
||||
new CommandLineInput(
|
||||
new[] {"param", "cmd", "abc", "123", "1", "2"},
|
||||
new[] {new CommandOptionInput("o", "option value")}),
|
||||
new Dictionary<string, string>(),
|
||||
new ParameterCommand
|
||||
{
|
||||
ParameterA = "abc",
|
||||
ParameterB = 123,
|
||||
ParameterC = new[] {1, 2},
|
||||
OptionA = "option value"
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
private static IEnumerable<TestCaseData> GetTestCases_InitializeEntryPoint_Negative()
|
||||
{
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(AllSupportedTypesCommand)},
|
||||
new CommandLineInput(
|
||||
new[] {new CommandOptionInput(nameof(AllSupportedTypesCommand.Int), "1234.5")}),
|
||||
new Dictionary<string, string>()
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(AllSupportedTypesCommand)},
|
||||
new CommandLineInput(
|
||||
new[] {new CommandOptionInput(nameof(AllSupportedTypesCommand.Int), new[] {"123", "456"})}),
|
||||
new Dictionary<string, string>()
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(AllSupportedTypesCommand)},
|
||||
new CommandLineInput(
|
||||
new[] {new CommandOptionInput(nameof(AllSupportedTypesCommand.Int))}),
|
||||
new Dictionary<string, string>()
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(AllSupportedTypesCommand)},
|
||||
new CommandLineInput(
|
||||
new[] {new CommandOptionInput(nameof(AllSupportedTypesCommand.NonConvertible), "123")}),
|
||||
new Dictionary<string, string>()
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(DivideCommand)},
|
||||
new CommandLineInput(new[] {"div"}),
|
||||
new Dictionary<string, string>()
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(DivideCommand)},
|
||||
new CommandLineInput(new[] {"div", "-D", "13"}),
|
||||
new Dictionary<string, string>()
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(ConcatCommand)},
|
||||
new CommandLineInput(new[] {"concat"}),
|
||||
new Dictionary<string, string>()
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(ConcatCommand)},
|
||||
new CommandLineInput(
|
||||
new[] {"concat"},
|
||||
new[] {new CommandOptionInput("s", "_")}),
|
||||
new Dictionary<string, string>()
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(ParameterCommand)},
|
||||
new CommandLineInput(
|
||||
new[] {"param", "cmd"},
|
||||
new[] {new CommandOptionInput("o", "option value")}),
|
||||
new Dictionary<string, string>()
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(ParameterCommand)},
|
||||
new CommandLineInput(
|
||||
new[] {"param", "cmd", "abc", "123", "invalid"},
|
||||
new[] {new CommandOptionInput("o", "option value")}),
|
||||
new Dictionary<string, string>()
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(DivideCommand)},
|
||||
new CommandLineInput(new[] {"non-existing"}),
|
||||
new Dictionary<string, string>()
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {typeof(BrokenEnumerableCommand)},
|
||||
new CommandLineInput(new[] {"value1", "value2"}),
|
||||
new Dictionary<string, string>()
|
||||
);
|
||||
}
|
||||
|
||||
[TestCaseSource(nameof(GetTestCases_InitializeEntryPoint))]
|
||||
public void InitializeEntryPoint_Test(
|
||||
IReadOnlyList<Type> commandTypes,
|
||||
CommandLineInput commandLineInput,
|
||||
IReadOnlyDictionary<string, string> environmentVariables,
|
||||
ICommand expectedResult)
|
||||
{
|
||||
// Arrange
|
||||
var applicationSchema = ApplicationSchema.Resolve(commandTypes);
|
||||
var typeActivator = new DefaultTypeActivator();
|
||||
|
||||
// Act
|
||||
var command = applicationSchema.InitializeEntryPoint(commandLineInput, environmentVariables, typeActivator);
|
||||
|
||||
// Assert
|
||||
command.Should().BeEquivalentTo(expectedResult, o => o.RespectingRuntimeTypes());
|
||||
}
|
||||
|
||||
[TestCaseSource(nameof(GetTestCases_InitializeEntryPoint_Negative))]
|
||||
public void InitializeEntryPoint_Negative_Test(
|
||||
IReadOnlyList<Type> commandTypes,
|
||||
CommandLineInput commandLineInput,
|
||||
IReadOnlyDictionary<string, string> environmentVariables)
|
||||
{
|
||||
// Arrange
|
||||
var applicationSchema = ApplicationSchema.Resolve(commandTypes);
|
||||
var typeActivator = new DefaultTypeActivator();
|
||||
|
||||
// Act & Assert
|
||||
var ex = Assert.Throws<CliFxException>(() =>
|
||||
applicationSchema.InitializeEntryPoint(commandLineInput, environmentVariables, typeActivator));
|
||||
Console.WriteLine(ex.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,264 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
using CliFx.Domain;
|
||||
using FluentAssertions;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace CliFx.Tests.Domain
|
||||
{
|
||||
[TestFixture]
|
||||
internal class CommandLineInputTests
|
||||
{
|
||||
private static IEnumerable<TestCaseData> GetTestCases_Parse()
|
||||
{
|
||||
yield return new TestCaseData(
|
||||
new string[0],
|
||||
CommandLineInput.Empty
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {"param"},
|
||||
new CommandLineInput(
|
||||
new[] {"param"})
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {"cmd", "param"},
|
||||
new CommandLineInput(
|
||||
new[] {"cmd", "param"})
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {"--option", "value"},
|
||||
new CommandLineInput(
|
||||
new[]
|
||||
{
|
||||
new CommandOptionInput("option", "value")
|
||||
})
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {"--option1", "value1", "--option2", "value2"},
|
||||
new CommandLineInput(
|
||||
new[]
|
||||
{
|
||||
new CommandOptionInput("option1", "value1"),
|
||||
new CommandOptionInput("option2", "value2")
|
||||
})
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {"--option", "value1", "value2"},
|
||||
new CommandLineInput(
|
||||
new[]
|
||||
{
|
||||
new CommandOptionInput("option", new[] {"value1", "value2"})
|
||||
})
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {"--option", "value1", "--option", "value2"},
|
||||
new CommandLineInput(
|
||||
new[]
|
||||
{
|
||||
new CommandOptionInput("option", new[] {"value1", "value2"})
|
||||
})
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {"-a", "value"},
|
||||
new CommandLineInput(
|
||||
new[]
|
||||
{
|
||||
new CommandOptionInput("a", "value")
|
||||
})
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {"-a", "value1", "-b", "value2"},
|
||||
new CommandLineInput(
|
||||
new[]
|
||||
{
|
||||
new CommandOptionInput("a", "value1"),
|
||||
new CommandOptionInput("b", "value2")
|
||||
})
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {"-a", "value1", "value2"},
|
||||
new CommandLineInput(
|
||||
new[]
|
||||
{
|
||||
new CommandOptionInput("a", new[] {"value1", "value2"})
|
||||
})
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {"-a", "value1", "-a", "value2"},
|
||||
new CommandLineInput(
|
||||
new[]
|
||||
{
|
||||
new CommandOptionInput("a", new[] {"value1", "value2"})
|
||||
})
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {"--option1", "value1", "-b", "value2"},
|
||||
new CommandLineInput(
|
||||
new[]
|
||||
{
|
||||
new CommandOptionInput("option1", "value1"),
|
||||
new CommandOptionInput("b", "value2")
|
||||
})
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {"--switch"},
|
||||
new CommandLineInput(
|
||||
new[]
|
||||
{
|
||||
new CommandOptionInput("switch")
|
||||
})
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {"--switch1", "--switch2"},
|
||||
new CommandLineInput(
|
||||
new[]
|
||||
{
|
||||
new CommandOptionInput("switch1"),
|
||||
new CommandOptionInput("switch2")
|
||||
})
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {"-s"},
|
||||
new CommandLineInput(
|
||||
new[]
|
||||
{
|
||||
new CommandOptionInput("s")
|
||||
})
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {"-a", "-b"},
|
||||
new CommandLineInput(
|
||||
new[]
|
||||
{
|
||||
new CommandOptionInput("a"),
|
||||
new CommandOptionInput("b")
|
||||
})
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {"-ab"},
|
||||
new CommandLineInput(
|
||||
new[]
|
||||
{
|
||||
new CommandOptionInput("a"),
|
||||
new CommandOptionInput("b")
|
||||
})
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {"-ab", "value"},
|
||||
new CommandLineInput(
|
||||
new[]
|
||||
{
|
||||
new CommandOptionInput("a"),
|
||||
new CommandOptionInput("b", "value")
|
||||
})
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {"cmd", "--option", "value"},
|
||||
new CommandLineInput(
|
||||
new[] {"cmd"},
|
||||
new[]
|
||||
{
|
||||
new CommandOptionInput("option", "value")
|
||||
})
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {"[debug]"},
|
||||
new CommandLineInput(
|
||||
new[] {"debug"},
|
||||
new string[0],
|
||||
new CommandOptionInput[0])
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {"[debug]", "[preview]"},
|
||||
new CommandLineInput(
|
||||
new[] {"debug", "preview"},
|
||||
new string[0],
|
||||
new CommandOptionInput[0])
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {"cmd", "param1", "param2", "--option", "value"},
|
||||
new CommandLineInput(
|
||||
new[] {"cmd", "param1", "param2"},
|
||||
new[]
|
||||
{
|
||||
new CommandOptionInput("option", "value")
|
||||
})
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {"[debug]", "[preview]", "-o", "value"},
|
||||
new CommandLineInput(
|
||||
new[] {"debug", "preview"},
|
||||
new string[0],
|
||||
new[]
|
||||
{
|
||||
new CommandOptionInput("o", "value")
|
||||
})
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {"cmd", "[debug]", "[preview]", "-o", "value"},
|
||||
new CommandLineInput(
|
||||
new[] {"debug", "preview"},
|
||||
new[] {"cmd"},
|
||||
new[]
|
||||
{
|
||||
new CommandOptionInput("o", "value")
|
||||
})
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {"cmd", "[debug]", "[preview]", "-o", "value"},
|
||||
new CommandLineInput(
|
||||
new[] {"debug", "preview"},
|
||||
new[] {"cmd"},
|
||||
new[]
|
||||
{
|
||||
new CommandOptionInput("o", "value")
|
||||
})
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {"cmd", "param", "[debug]", "[preview]", "-o", "value"},
|
||||
new CommandLineInput(
|
||||
new[] {"debug", "preview"},
|
||||
new[] {"cmd", "param"},
|
||||
new[]
|
||||
{
|
||||
new CommandOptionInput("o", "value")
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
[Test]
|
||||
[TestCaseSource(nameof(GetTestCases_Parse))]
|
||||
public void Parse_Test(IReadOnlyList<string> commandLineArguments, CommandLineInput expectedResult)
|
||||
{
|
||||
// Act
|
||||
var result = CommandLineInput.Parse(commandLineArguments);
|
||||
|
||||
// Assert
|
||||
result.Should().BeEquivalentTo(expectedResult);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,82 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Threading.Tasks;
|
||||
using CliWrap;
|
||||
using FluentAssertions;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace CliFx.Tests
|
||||
{
|
||||
[TestFixture]
|
||||
public class DummyTests
|
||||
{
|
||||
private static Assembly DummyAssembly { get; } = typeof(Dummy.Program).Assembly;
|
||||
|
||||
private static IEnumerable<TestCaseData> GetTestCases_RunAsync()
|
||||
{
|
||||
yield return new TestCaseData(
|
||||
new[] {"--version"},
|
||||
new Dictionary<string, string>(),
|
||||
$"v{DummyAssembly.GetName().Version}"
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new string[0],
|
||||
new Dictionary<string, string>(),
|
||||
"Hello World!"
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {"--target", "Earth"},
|
||||
new Dictionary<string, string>(),
|
||||
"Hello Earth!"
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new string[0],
|
||||
new Dictionary<string, string>
|
||||
{
|
||||
["ENV_TARGET"] = "Mars"
|
||||
},
|
||||
"Hello Mars!"
|
||||
);
|
||||
|
||||
yield return new TestCaseData(
|
||||
new[] {"--target", "Earth"},
|
||||
new Dictionary<string, string>
|
||||
{
|
||||
["ENV_TARGET"] = "Mars"
|
||||
},
|
||||
"Hello Earth!"
|
||||
);
|
||||
}
|
||||
|
||||
[TestCaseSource(nameof(GetTestCases_RunAsync))]
|
||||
public async Task RunAsync_Test(
|
||||
IReadOnlyList<string> arguments,
|
||||
IReadOnlyDictionary<string, string> environmentVariables,
|
||||
string expectedStdOut)
|
||||
{
|
||||
// Arrange
|
||||
var cli = Cli.Wrap("dotnet")
|
||||
.SetArguments(arguments.Prepend(DummyAssembly.Location).ToArray())
|
||||
.EnableExitCodeValidation()
|
||||
.EnableStandardErrorValidation()
|
||||
.SetStandardOutputCallback(Console.WriteLine)
|
||||
.SetStandardErrorCallback(Console.WriteLine);
|
||||
|
||||
foreach (var (key, value) in environmentVariables)
|
||||
cli.SetEnvironmentVariable(key, value);
|
||||
|
||||
// Act
|
||||
var result = await cli.ExecuteAsync();
|
||||
|
||||
// Assert
|
||||
result.ExitCode.Should().Be(0);
|
||||
result.StandardError.Should().BeNullOrWhiteSpace();
|
||||
result.StandardOutput.TrimEnd().Should().Be(expectedStdOut);
|
||||
}
|
||||
}
|
||||
}
|
||||
27
CliFx.Tests/EnvironmentVariablesSpecs.Commands.cs
Normal file
27
CliFx.Tests/EnvironmentVariablesSpecs.Commands.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using CliFx.Attributes;
|
||||
|
||||
namespace CliFx.Tests
|
||||
{
|
||||
public partial class EnvironmentVariablesSpecs
|
||||
{
|
||||
[Command]
|
||||
private class EnvironmentVariableCollectionCommand : ICommand
|
||||
{
|
||||
[CommandOption("opt", EnvironmentVariableName = "ENV_OPT")]
|
||||
public IReadOnlyList<string>? Option { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
|
||||
[Command]
|
||||
private class EnvironmentVariableCommand : ICommand
|
||||
{
|
||||
[CommandOption("opt", EnvironmentVariableName = "ENV_OPT")]
|
||||
public string? Option { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
}
|
||||
}
|
||||
115
CliFx.Tests/EnvironmentVariablesSpecs.cs
Normal file
115
CliFx.Tests/EnvironmentVariablesSpecs.cs
Normal file
@@ -0,0 +1,115 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using CliFx.Domain;
|
||||
using CliFx.Tests.Internal;
|
||||
using CliWrap;
|
||||
using CliWrap.Buffered;
|
||||
using FluentAssertions;
|
||||
using Xunit;
|
||||
|
||||
namespace CliFx.Tests
|
||||
{
|
||||
public partial class EnvironmentVariablesSpecs
|
||||
{
|
||||
// This test uses a real application to make sure environment variables are actually read correctly
|
||||
[Fact]
|
||||
public async Task Option_can_use_a_specific_environment_variable_as_fallback()
|
||||
{
|
||||
// Arrange
|
||||
var command = Cli.Wrap("dotnet")
|
||||
.WithArguments(a => a
|
||||
.Add(Dummy.Program.Location))
|
||||
.WithEnvironmentVariables(e => e
|
||||
.Set("ENV_TARGET", "Mars"));
|
||||
|
||||
// Act
|
||||
var stdOut = await command.ExecuteBufferedAsync().Select(r => r.StandardOutput);
|
||||
|
||||
// Assert
|
||||
stdOut.TrimEnd().Should().Be("Hello Mars!");
|
||||
}
|
||||
|
||||
// This test uses a real application to make sure environment variables are actually read correctly
|
||||
[Fact]
|
||||
public async Task Option_only_uses_environment_variable_as_fallback_if_the_value_was_not_directly_provided()
|
||||
{
|
||||
// Arrange
|
||||
var command = Cli.Wrap("dotnet")
|
||||
.WithArguments(a => a
|
||||
.Add(Dummy.Program.Location)
|
||||
.Add("--target")
|
||||
.Add("Jupiter"))
|
||||
.WithEnvironmentVariables(e => e
|
||||
.Set("ENV_TARGET", "Mars"));
|
||||
|
||||
// Act
|
||||
var stdOut = await command.ExecuteBufferedAsync().Select(r => r.StandardOutput);
|
||||
|
||||
// Assert
|
||||
stdOut.TrimEnd().Should().Be("Hello Jupiter!");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Option_of_non_scalar_type_can_take_multiple_separated_values_from_an_environment_variable()
|
||||
{
|
||||
// Arrange
|
||||
var input = CommandInput.Empty;
|
||||
var envVars = new Dictionary<string, string>
|
||||
{
|
||||
["ENV_OPT"] = $"foo{Path.PathSeparator}bar"
|
||||
};
|
||||
|
||||
// Act
|
||||
var instance = CommandHelper.ResolveCommand<EnvironmentVariableCollectionCommand>(input, envVars);
|
||||
|
||||
// Assert
|
||||
instance.Should().BeEquivalentTo(new EnvironmentVariableCollectionCommand
|
||||
{
|
||||
Option = new[] {"foo", "bar"}
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Option_of_scalar_type_can_only_take_a_single_value_from_an_environment_variable_even_if_it_contains_separators()
|
||||
{
|
||||
// Arrange
|
||||
var input = CommandInput.Empty;
|
||||
var envVars = new Dictionary<string, string>
|
||||
{
|
||||
["ENV_OPT"] = $"foo{Path.PathSeparator}bar"
|
||||
};
|
||||
|
||||
// Act
|
||||
var instance = CommandHelper.ResolveCommand<EnvironmentVariableCommand>(input, envVars);
|
||||
|
||||
// Assert
|
||||
instance.Should().BeEquivalentTo(new EnvironmentVariableCommand
|
||||
{
|
||||
Option = $"foo{Path.PathSeparator}bar"
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Option_can_use_a_specific_environment_variable_as_fallback_while_respecting_case()
|
||||
{
|
||||
// Arrange
|
||||
const string expected = "foobar";
|
||||
var input = CommandInput.Empty;
|
||||
var envVars = new Dictionary<string, string>
|
||||
{
|
||||
["ENV_OPT"] = expected,
|
||||
["env_opt"] = "2"
|
||||
};
|
||||
|
||||
// Act
|
||||
var instance = CommandHelper.ResolveCommand<EnvironmentVariableCommand>(input, envVars);
|
||||
|
||||
// Assert
|
||||
instance.Should().BeEquivalentTo(new EnvironmentVariableCommand
|
||||
{
|
||||
Option = expected
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
34
CliFx.Tests/ErrorReportingSpecs.Commands.cs
Normal file
34
CliFx.Tests/ErrorReportingSpecs.Commands.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using CliFx.Attributes;
|
||||
using CliFx.Exceptions;
|
||||
|
||||
namespace CliFx.Tests
|
||||
{
|
||||
public partial class ErrorReportingSpecs
|
||||
{
|
||||
[Command("exc")]
|
||||
private class GenericExceptionCommand : ICommand
|
||||
{
|
||||
[CommandOption("msg", 'm')]
|
||||
public string? Message { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => throw new Exception(Message);
|
||||
}
|
||||
|
||||
[Command("exc")]
|
||||
private class CommandExceptionCommand : ICommand
|
||||
{
|
||||
[CommandOption("code", 'c')]
|
||||
public int ExitCode { get; set; } = 133;
|
||||
|
||||
[CommandOption("msg", 'm')]
|
||||
public string? Message { get; set; }
|
||||
|
||||
[CommandOption("show-help")]
|
||||
public bool ShowHelp { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => throw new CommandException(Message, ExitCode, ShowHelp);
|
||||
}
|
||||
}
|
||||
}
|
||||
172
CliFx.Tests/ErrorReportingSpecs.cs
Normal file
172
CliFx.Tests/ErrorReportingSpecs.cs
Normal file
@@ -0,0 +1,172 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using FluentAssertions;
|
||||
using Xunit;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
namespace CliFx.Tests
|
||||
{
|
||||
public partial class ErrorReportingSpecs
|
||||
{
|
||||
private readonly ITestOutputHelper _output;
|
||||
|
||||
public ErrorReportingSpecs(ITestOutputHelper output) => _output = output;
|
||||
|
||||
[Fact]
|
||||
public async Task Command_may_throw_a_generic_exception_which_exits_and_prints_error_message_and_stack_trace()
|
||||
{
|
||||
// Arrange
|
||||
await using var stdErr = new MemoryStream();
|
||||
var console = new VirtualConsole(error: stdErr);
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand(typeof(GenericExceptionCommand))
|
||||
.UseConsole(console)
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
var exitCode = await application.RunAsync(
|
||||
new[] {"exc", "-m", "Kaput"},
|
||||
new Dictionary<string, string>());
|
||||
|
||||
var stdErrData = console.Error.Encoding.GetString(stdErr.ToArray()).TrimEnd();
|
||||
|
||||
// Assert
|
||||
exitCode.Should().NotBe(0);
|
||||
stdErrData.Should().ContainAll(
|
||||
"System.Exception:",
|
||||
"Kaput", "at",
|
||||
"CliFx.Tests");
|
||||
|
||||
_output.WriteLine(stdErrData);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Command_may_throw_a_specialized_exception_which_exits_with_custom_code_and_prints_minimal_error_details()
|
||||
{
|
||||
// Arrange
|
||||
await using var stdErr = new MemoryStream();
|
||||
var console = new VirtualConsole(error: stdErr);
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand(typeof(CommandExceptionCommand))
|
||||
.UseConsole(console)
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
var exitCode = await application.RunAsync(
|
||||
new[] {"exc", "-m", "Kaput", "-c", "69"},
|
||||
new Dictionary<string, string>());
|
||||
|
||||
var stdErrData = console.Error.Encoding.GetString(stdErr.ToArray()).TrimEnd();
|
||||
|
||||
// Assert
|
||||
exitCode.Should().Be(69);
|
||||
stdErrData.Should().Be("Kaput");
|
||||
|
||||
_output.WriteLine(stdErrData);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Command_may_throw_a_specialized_exception_without_error_message_which_exits_and_prints_full_error_details()
|
||||
{
|
||||
// Arrange
|
||||
await using var stdErr = new MemoryStream();
|
||||
var console = new VirtualConsole(error: stdErr);
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand(typeof(CommandExceptionCommand))
|
||||
.UseConsole(console)
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
var exitCode = await application.RunAsync(
|
||||
new[] {"exc"},
|
||||
new Dictionary<string, string>());
|
||||
|
||||
var stdErrData = console.Error.Encoding.GetString(stdErr.ToArray()).TrimEnd();
|
||||
|
||||
// Assert
|
||||
exitCode.Should().NotBe(0);
|
||||
stdErrData.Should().ContainAll(
|
||||
"CliFx.Exceptions.CommandException:",
|
||||
"at",
|
||||
"CliFx.Tests");
|
||||
|
||||
_output.WriteLine(stdErrData);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Command_may_throw_a_specialized_exception_which_exits_and_prints_help_text()
|
||||
{
|
||||
// Arrange
|
||||
await using var stdOut = new MemoryStream();
|
||||
await using var stdErr = new MemoryStream();
|
||||
|
||||
var console = new VirtualConsole(output: stdOut, error: stdErr);
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand(typeof(CommandExceptionCommand))
|
||||
.UseConsole(console)
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
var exitCode = await application.RunAsync(
|
||||
new[] {"exc", "-m", "Kaput", "--show-help"},
|
||||
new Dictionary<string, string>());
|
||||
|
||||
var stdErrData = console.Error.Encoding.GetString(stdErr.ToArray()).TrimEnd();
|
||||
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
|
||||
|
||||
// Assert
|
||||
exitCode.Should().NotBe(0);
|
||||
stdErrData.Should().Be("Kaput");
|
||||
|
||||
stdOutData.Should().ContainAll(
|
||||
"Usage",
|
||||
"Options",
|
||||
"-h|--help", "Shows help text."
|
||||
);
|
||||
|
||||
_output.WriteLine(stdErrData);
|
||||
_output.WriteLine(stdOutData);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Command_shows_help_text_on_invalid_user_input()
|
||||
{
|
||||
// Arrange
|
||||
await using var stdOut = new MemoryStream();
|
||||
await using var stdErr = new MemoryStream();
|
||||
var console = new VirtualConsole(output: stdOut, error: stdErr);
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand(typeof(CommandExceptionCommand))
|
||||
.UseConsole(console)
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
var exitCode = await application.RunAsync(
|
||||
new[] {"not-a-valid-command", "-r", "foo"},
|
||||
new Dictionary<string, string>());
|
||||
|
||||
var stdErrData = console.Error.Encoding.GetString(stdErr.ToArray()).TrimEnd();
|
||||
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
|
||||
|
||||
// Assert
|
||||
exitCode.Should().NotBe(0);
|
||||
stdErrData.Should().NotBeNullOrWhiteSpace();
|
||||
|
||||
stdOutData.Should().ContainAll(
|
||||
"Usage",
|
||||
"[command]",
|
||||
"Options",
|
||||
"-h|--help", "Shows help text."
|
||||
);
|
||||
|
||||
_output.WriteLine(stdErrData);
|
||||
_output.WriteLine(stdOutData);
|
||||
}
|
||||
}
|
||||
}
|
||||
155
CliFx.Tests/HelpTextSpecs.Commands.cs
Normal file
155
CliFx.Tests/HelpTextSpecs.Commands.cs
Normal file
@@ -0,0 +1,155 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using CliFx.Attributes;
|
||||
|
||||
namespace CliFx.Tests
|
||||
{
|
||||
public partial class HelpTextSpecs
|
||||
{
|
||||
[Command(Description = "DefaultCommand description.")]
|
||||
private class DefaultCommand : ICommand
|
||||
{
|
||||
[CommandOption("option-a", 'a', Description = "OptionA description.")]
|
||||
public string? OptionA { get; set; }
|
||||
|
||||
[CommandOption("option-b", 'b', Description = "OptionB description.")]
|
||||
public string? OptionB { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
|
||||
[Command("cmd", Description = "NamedCommand description.")]
|
||||
private class NamedCommand : ICommand
|
||||
{
|
||||
[CommandParameter(0, Name = "param-a", Description = "ParameterA description.")]
|
||||
public string? ParameterA { get; set; }
|
||||
|
||||
[CommandOption("option-c", 'c', Description = "OptionC description.")]
|
||||
public string? OptionC { get; set; }
|
||||
|
||||
[CommandOption("option-d", 'd', Description = "OptionD description.")]
|
||||
public string? OptionD { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
|
||||
[Command("cmd sub", Description = "NamedSubCommand description.")]
|
||||
private class NamedSubCommand : ICommand
|
||||
{
|
||||
[CommandParameter(0, Name = "param-b", Description = "ParameterB description.")]
|
||||
public string? ParameterB { get; set; }
|
||||
|
||||
[CommandParameter(1, Name = "param-c", Description = "ParameterC description.")]
|
||||
public string? ParameterC { get; set; }
|
||||
|
||||
[CommandOption("option-e", 'e', Description = "OptionE description.")]
|
||||
public string? OptionE { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
|
||||
[Command("cmd-with-params")]
|
||||
private class ParametersCommand : ICommand
|
||||
{
|
||||
[CommandParameter(0, Name = "first")]
|
||||
public string? ParameterA { get; set; }
|
||||
|
||||
[CommandParameter(10)]
|
||||
public int? ParameterB { get; set; }
|
||||
|
||||
[CommandParameter(20, Description = "A list of numbers", Name = "third list")]
|
||||
public IEnumerable<int>? ParameterC { get; set; }
|
||||
|
||||
[CommandOption("option", 'o')]
|
||||
public string? Option { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
|
||||
[Command("cmd-with-req-opts")]
|
||||
private class RequiredOptionsCommand : ICommand
|
||||
{
|
||||
[CommandOption("option-a", 'a', IsRequired = true)]
|
||||
public string? OptionA { get; set; }
|
||||
|
||||
[CommandOption("option-b", 'b', IsRequired = true)]
|
||||
public IEnumerable<int>? OptionB { get; set; }
|
||||
|
||||
[CommandOption("option-c", 'c')]
|
||||
public string? OptionC { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
|
||||
[Command("cmd-with-enum-args")]
|
||||
private class EnumArgumentsCommand : ICommand
|
||||
{
|
||||
public enum CustomEnum { Value1, Value2, Value3 };
|
||||
|
||||
[CommandParameter(0, Name = "value", Description = "Enum parameter.")]
|
||||
public CustomEnum ParamA { get; set; }
|
||||
|
||||
[CommandOption("value", Description = "Enum option.", IsRequired = true)]
|
||||
public CustomEnum OptionA { get; set; } = CustomEnum.Value1;
|
||||
|
||||
[CommandOption("nullable-value", Description = "Nullable enum option.")]
|
||||
public CustomEnum? OptionB { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
|
||||
[Command("cmd-with-env-vars")]
|
||||
private class EnvironmentVariableCommand : ICommand
|
||||
{
|
||||
[CommandOption("option-a", 'a', IsRequired = true, EnvironmentVariableName = "ENV_OPT_A")]
|
||||
public string? OptionA { get; set; }
|
||||
|
||||
[CommandOption("option-b", 'b', EnvironmentVariableName = "ENV_OPT_B")]
|
||||
public string? OptionB { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
|
||||
[Command("cmd-with-defaults")]
|
||||
private class ArgumentsWithDefaultValuesCommand : ICommand
|
||||
{
|
||||
public enum CustomEnum { Value1, Value2, Value3 };
|
||||
|
||||
[CommandOption(nameof(Object))]
|
||||
public object? Object { get; set; } = 42;
|
||||
|
||||
[CommandOption(nameof(String))]
|
||||
public string? String { get; set; } = "foo";
|
||||
|
||||
[CommandOption(nameof(EmptyString))]
|
||||
public string EmptyString { get; set; } = "";
|
||||
|
||||
[CommandOption(nameof(Bool))]
|
||||
public bool Bool { get; set; } = true;
|
||||
|
||||
[CommandOption(nameof(Char))]
|
||||
public char Char { get; set; } = 't';
|
||||
|
||||
[CommandOption(nameof(Int))]
|
||||
public int Int { get; set; } = 1337;
|
||||
|
||||
[CommandOption(nameof(TimeSpan))]
|
||||
public TimeSpan TimeSpan { get; set; } = TimeSpan.FromMinutes(123);
|
||||
|
||||
[CommandOption(nameof(Enum))]
|
||||
public CustomEnum Enum { get; set; } = CustomEnum.Value2;
|
||||
|
||||
[CommandOption(nameof(IntNullable))]
|
||||
public int? IntNullable { get; set; } = 1337;
|
||||
|
||||
[CommandOption(nameof(StringArray))]
|
||||
public string[]? StringArray { get; set; } = { "foo", "bar", "baz" };
|
||||
|
||||
[CommandOption(nameof(IntArray))]
|
||||
public int[]? IntArray { get; set; } = { 1, 2, 3 };
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
}
|
||||
}
|
||||
334
CliFx.Tests/HelpTextSpecs.cs
Normal file
334
CliFx.Tests/HelpTextSpecs.cs
Normal file
@@ -0,0 +1,334 @@
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using FluentAssertions;
|
||||
using Xunit;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
namespace CliFx.Tests
|
||||
{
|
||||
public partial class HelpTextSpecs
|
||||
{
|
||||
private readonly ITestOutputHelper _output;
|
||||
|
||||
public HelpTextSpecs(ITestOutputHelper output) => _output = output;
|
||||
|
||||
[Fact]
|
||||
public async Task Version_information_can_be_requested_by_providing_the_version_option_without_other_arguments()
|
||||
{
|
||||
// Arrange
|
||||
await using var stdOut = new MemoryStream();
|
||||
var console = new VirtualConsole(output: stdOut);
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand(typeof(DefaultCommand))
|
||||
.AddCommand(typeof(NamedCommand))
|
||||
.AddCommand(typeof(NamedSubCommand))
|
||||
.UseVersionText("v6.9")
|
||||
.UseConsole(console)
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
var exitCode = await application.RunAsync(new[] {"--version"});
|
||||
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
|
||||
|
||||
// Assert
|
||||
exitCode.Should().Be(0);
|
||||
stdOutData.Should().Be("v6.9");
|
||||
|
||||
_output.WriteLine(stdOutData);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Help_text_can_be_requested_by_providing_the_help_option()
|
||||
{
|
||||
// Arrange
|
||||
await using var stdOut = new MemoryStream();
|
||||
var console = new VirtualConsole(output: stdOut);
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand(typeof(DefaultCommand))
|
||||
.AddCommand(typeof(NamedCommand))
|
||||
.AddCommand(typeof(NamedSubCommand))
|
||||
.UseTitle("AppTitle")
|
||||
.UseVersionText("AppVer")
|
||||
.UseDescription("AppDesc")
|
||||
.UseExecutableName("AppExe")
|
||||
.UseConsole(console)
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
await application.RunAsync(new[] {"--help"});
|
||||
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
|
||||
|
||||
// Assert
|
||||
stdOutData.Should().ContainAll(
|
||||
"AppTitle", "AppVer",
|
||||
"AppDesc",
|
||||
"Usage",
|
||||
"AppExe", "[command]", "[options]",
|
||||
"Options",
|
||||
"-a|--option-a", "OptionA description.",
|
||||
"-b|--option-b", "OptionB description.",
|
||||
"-h|--help", "Shows help text.",
|
||||
"--version", "Shows version information.",
|
||||
"Commands",
|
||||
"cmd", "NamedCommand description.",
|
||||
"You can run", "to show help on a specific command."
|
||||
);
|
||||
|
||||
_output.WriteLine(stdOutData);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Help_text_can_be_requested_on_a_specific_named_command()
|
||||
{
|
||||
// Arrange
|
||||
await using var stdOut = new MemoryStream();
|
||||
var console = new VirtualConsole(output: stdOut);
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand(typeof(DefaultCommand))
|
||||
.AddCommand(typeof(NamedCommand))
|
||||
.AddCommand(typeof(NamedSubCommand))
|
||||
.UseConsole(console)
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
await application.RunAsync(new[] {"cmd", "--help"});
|
||||
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
|
||||
|
||||
// Assert
|
||||
stdOutData.Should().ContainAll(
|
||||
"Description",
|
||||
"NamedCommand description.",
|
||||
"Usage",
|
||||
"cmd", "[command]", "<param-a>", "[options]",
|
||||
"Parameters",
|
||||
"* param-a", "ParameterA description.",
|
||||
"Options",
|
||||
"-c|--option-c", "OptionC description.",
|
||||
"-d|--option-d", "OptionD description.",
|
||||
"-h|--help", "Shows help text.",
|
||||
"Commands",
|
||||
"sub", "SubCommand description.",
|
||||
"You can run", "to show help on a specific command."
|
||||
);
|
||||
|
||||
_output.WriteLine(stdOutData);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Help_text_can_be_requested_on_a_specific_named_sub_command()
|
||||
{
|
||||
// Arrange
|
||||
await using var stdOut = new MemoryStream();
|
||||
var console = new VirtualConsole(output: stdOut);
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand(typeof(DefaultCommand))
|
||||
.AddCommand(typeof(NamedCommand))
|
||||
.AddCommand(typeof(NamedSubCommand))
|
||||
.UseConsole(console)
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
await application.RunAsync(new[] {"cmd", "sub", "--help"});
|
||||
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
|
||||
|
||||
// Assert
|
||||
stdOutData.Should().ContainAll(
|
||||
"Description",
|
||||
"SubCommand description.",
|
||||
"Usage",
|
||||
"cmd sub", "<param-b>", "<param-c>", "[options]",
|
||||
"Parameters",
|
||||
"* param-b", "ParameterB description.",
|
||||
"* param-c", "ParameterC description.",
|
||||
"Options",
|
||||
"-e|--option-e", "OptionE description.",
|
||||
"-h|--help", "Shows help text."
|
||||
);
|
||||
|
||||
_output.WriteLine(stdOutData);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Help_text_can_be_requested_without_specifying_command_even_if_default_command_is_not_defined()
|
||||
{
|
||||
// Arrange
|
||||
await using var stdOut = new MemoryStream();
|
||||
var console = new VirtualConsole(output: stdOut);
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand(typeof(NamedCommand))
|
||||
.AddCommand(typeof(NamedSubCommand))
|
||||
.UseConsole(console)
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
await application.RunAsync(new[] {"--help"});
|
||||
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
|
||||
|
||||
// Assert
|
||||
stdOutData.Should().ContainAll(
|
||||
"Usage",
|
||||
"[command]",
|
||||
"Options",
|
||||
"-h|--help", "Shows help text.",
|
||||
"--version", "Shows version information.",
|
||||
"Commands",
|
||||
"cmd", "NamedCommand description.",
|
||||
"You can run", "to show help on a specific command."
|
||||
);
|
||||
|
||||
_output.WriteLine(stdOutData);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Help_text_shows_usage_format_which_lists_all_parameters()
|
||||
{
|
||||
// Arrange
|
||||
await using var stdOut = new MemoryStream();
|
||||
var console = new VirtualConsole(output: stdOut);
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand(typeof(ParametersCommand))
|
||||
.UseConsole(console)
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
await application.RunAsync(new[] {"cmd-with-params", "--help"});
|
||||
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
|
||||
|
||||
// Assert
|
||||
stdOutData.Should().ContainAll(
|
||||
"Usage",
|
||||
"cmd-with-params", "<first>", "<parameterb>", "<third list...>", "[options]"
|
||||
);
|
||||
|
||||
_output.WriteLine(stdOutData);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Help_text_shows_usage_format_which_lists_all_required_options()
|
||||
{
|
||||
// Arrange
|
||||
await using var stdOut = new MemoryStream();
|
||||
var console = new VirtualConsole(output: stdOut);
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand(typeof(RequiredOptionsCommand))
|
||||
.UseConsole(console)
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
await application.RunAsync(new[] {"cmd-with-req-opts", "--help"});
|
||||
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
|
||||
|
||||
// Assert
|
||||
stdOutData.Should().ContainAll(
|
||||
"Usage",
|
||||
"cmd-with-req-opts", "--option-a <value>", "--option-b <values...>", "[options]",
|
||||
"Options",
|
||||
"* -a|--option-a",
|
||||
"* -b|--option-b",
|
||||
"-c|--option-c"
|
||||
);
|
||||
|
||||
_output.WriteLine(stdOutData);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Help_text_lists_all_valid_values_for_enum_arguments()
|
||||
{
|
||||
// Arrange
|
||||
await using var stdOut = new MemoryStream();
|
||||
var console = new VirtualConsole(output: stdOut);
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand(typeof(EnumArgumentsCommand))
|
||||
.UseConsole(console)
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
await application.RunAsync(new[] {"cmd-with-enum-args", "--help"});
|
||||
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
|
||||
|
||||
// Assert
|
||||
stdOutData.Should().ContainAll(
|
||||
"Usage",
|
||||
"cmd-with-enum-args", "[options]",
|
||||
"Parameters",
|
||||
"value", "Valid values: \"Value1\", \"Value2\", \"Value3\".",
|
||||
"Options",
|
||||
"* --value", "Enum option.", "Valid values: \"Value1\", \"Value2\", \"Value3\".",
|
||||
"--nullable-value", "Nullable enum option.", "Valid values: \"Value1\", \"Value2\", \"Value3\"."
|
||||
);
|
||||
|
||||
_output.WriteLine(stdOutData);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Help_text_lists_environment_variable_names_for_options_that_have_them_defined()
|
||||
{
|
||||
// Arrange
|
||||
await using var stdOut = new MemoryStream();
|
||||
var console = new VirtualConsole(output: stdOut);
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand(typeof(EnvironmentVariableCommand))
|
||||
.UseConsole(console)
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
await application.RunAsync(new[] {"cmd-with-env-vars", "--help"});
|
||||
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
|
||||
|
||||
// Assert
|
||||
stdOutData.Should().ContainAll(
|
||||
"Options",
|
||||
"* -a|--option-a", "Environment variable:", "ENV_OPT_A",
|
||||
"-b|--option-b", "Environment variable:", "ENV_OPT_B"
|
||||
);
|
||||
|
||||
_output.WriteLine(stdOutData);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Help_text_shows_default_values_for_non_required_options()
|
||||
{
|
||||
// Arrange
|
||||
await using var stdOut = new MemoryStream();
|
||||
var console = new VirtualConsole(output: stdOut);
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand(typeof(ArgumentsWithDefaultValuesCommand))
|
||||
.UseConsole(console)
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
await application.RunAsync(new[] {"cmd-with-defaults", "--help"});
|
||||
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
|
||||
|
||||
// Assert
|
||||
stdOutData.Should().ContainAll(
|
||||
"Usage",
|
||||
"cmd-with-defaults", "[options]",
|
||||
"Options",
|
||||
"--Object", "Default: \"42\"",
|
||||
"--String", "Default: \"foo\"",
|
||||
"--EmptyString", "Default: \"\"",
|
||||
"--Bool", "Default: \"True\"",
|
||||
"--Char", "Default: \"t\"",
|
||||
"--Int", "Default: \"1337\"",
|
||||
"--TimeSpan", "Default: \"02:03:00\"",
|
||||
"--Enum", "Default: \"Value2\"",
|
||||
"--IntNullable", "Default: \"1337\"",
|
||||
"--StringArray", "Default: \"foo\" \"bar\" \"baz\"",
|
||||
"--IntArray", "Default: \"1\" \"2\" \"3\""
|
||||
);
|
||||
|
||||
_output.WriteLine(stdOutData);
|
||||
}
|
||||
}
|
||||
}
|
||||
23
CliFx.Tests/Internal/CommandHelper.cs
Normal file
23
CliFx.Tests/Internal/CommandHelper.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
using System.Collections.Generic;
|
||||
using CliFx.Domain;
|
||||
|
||||
namespace CliFx.Tests.Internal
|
||||
{
|
||||
internal static class CommandHelper
|
||||
{
|
||||
public static TCommand ResolveCommand<TCommand>(CommandInput input, IReadOnlyDictionary<string, string> environmentVariables)
|
||||
where TCommand : ICommand, new()
|
||||
{
|
||||
var schema = CommandSchema.TryResolve(typeof(TCommand))!;
|
||||
|
||||
var instance = new TCommand();
|
||||
schema.Bind(instance, input, environmentVariables);
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
public static TCommand ResolveCommand<TCommand>(CommandInput input)
|
||||
where TCommand : ICommand, new() =>
|
||||
ResolveCommand<TCommand>(input, new Dictionary<string, string>());
|
||||
}
|
||||
}
|
||||
45
CliFx.Tests/Internal/CommandInputBuilder.cs
Normal file
45
CliFx.Tests/Internal/CommandInputBuilder.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
using System.Collections.Generic;
|
||||
using CliFx.Domain;
|
||||
|
||||
namespace CliFx.Tests.Internal
|
||||
{
|
||||
internal class CommandInputBuilder
|
||||
{
|
||||
private readonly List<CommandDirectiveInput> _directives = new List<CommandDirectiveInput>();
|
||||
private readonly List<CommandParameterInput> _parameters = new List<CommandParameterInput>();
|
||||
private readonly List<CommandOptionInput> _options = new List<CommandOptionInput>();
|
||||
|
||||
private string? _commandName;
|
||||
|
||||
public CommandInputBuilder SetCommandName(string commandName)
|
||||
{
|
||||
_commandName = commandName;
|
||||
return this;
|
||||
}
|
||||
|
||||
public CommandInputBuilder AddDirective(string directive)
|
||||
{
|
||||
_directives.Add(new CommandDirectiveInput(directive));
|
||||
return this;
|
||||
}
|
||||
|
||||
public CommandInputBuilder AddParameter(string parameter)
|
||||
{
|
||||
_parameters.Add(new CommandParameterInput(parameter));
|
||||
return this;
|
||||
}
|
||||
|
||||
public CommandInputBuilder AddOption(string alias, params string[] values)
|
||||
{
|
||||
_options.Add(new CommandOptionInput(alias, values));
|
||||
return this;
|
||||
}
|
||||
|
||||
public CommandInput Build() => new CommandInput(
|
||||
_directives,
|
||||
_commandName,
|
||||
_parameters,
|
||||
_options
|
||||
);
|
||||
}
|
||||
}
|
||||
19
CliFx.Tests/Internal/TaskExtensions.cs
Normal file
19
CliFx.Tests/Internal/TaskExtensions.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CliFx.Tests.Internal
|
||||
{
|
||||
internal static class TaskExtensions
|
||||
{
|
||||
public static async Task IgnoreCancellation(this Task task)
|
||||
{
|
||||
try
|
||||
{
|
||||
await task;
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
52
CliFx.Tests/RoutingSpecs.Commands.cs
Normal file
52
CliFx.Tests/RoutingSpecs.Commands.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using CliFx.Attributes;
|
||||
|
||||
namespace CliFx.Tests
|
||||
{
|
||||
public partial class RoutingSpecs
|
||||
{
|
||||
[Command]
|
||||
private class DefaultCommand : ICommand
|
||||
{
|
||||
public ValueTask ExecuteAsync(IConsole console)
|
||||
{
|
||||
console.Output.WriteLine("Hello world!");
|
||||
return default;
|
||||
}
|
||||
}
|
||||
|
||||
[Command("concat", Description = "Concatenate strings.")]
|
||||
private class ConcatCommand : ICommand
|
||||
{
|
||||
[CommandOption('i', IsRequired = true, Description = "Input strings.")]
|
||||
public IReadOnlyList<string> Inputs { get; set; } = Array.Empty<string>();
|
||||
|
||||
[CommandOption('s', Description = "String separator.")]
|
||||
public string Separator { get; set; } = "";
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console)
|
||||
{
|
||||
console.Output.WriteLine(string.Join(Separator, Inputs));
|
||||
return default;
|
||||
}
|
||||
}
|
||||
|
||||
[Command("div", Description = "Divide one number by another.")]
|
||||
private class DivideCommand : ICommand
|
||||
{
|
||||
[CommandOption("dividend", 'D', IsRequired = true, Description = "The number to divide.")]
|
||||
public double Dividend { get; set; } = 0;
|
||||
|
||||
[CommandOption("divisor", 'd', IsRequired = true, Description = "The number to divide by.")]
|
||||
public double Divisor { get; set; } = 0;
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console)
|
||||
{
|
||||
console.Output.WriteLine(Dividend / Divisor);
|
||||
return default;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
90
CliFx.Tests/RoutingSpecs.cs
Normal file
90
CliFx.Tests/RoutingSpecs.cs
Normal file
@@ -0,0 +1,90 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using FluentAssertions;
|
||||
using Xunit;
|
||||
|
||||
namespace CliFx.Tests
|
||||
{
|
||||
public partial class RoutingSpecs
|
||||
{
|
||||
[Fact]
|
||||
public async Task Default_command_is_executed_if_provided_arguments_do_not_match_any_named_command()
|
||||
{
|
||||
// Arrange
|
||||
await using var stdOut = new MemoryStream();
|
||||
var console = new VirtualConsole(output: stdOut);
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand(typeof(DefaultCommand))
|
||||
.AddCommand(typeof(ConcatCommand))
|
||||
.AddCommand(typeof(DivideCommand))
|
||||
.UseConsole(console)
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
var exitCode = await application.RunAsync(
|
||||
Array.Empty<string>(),
|
||||
new Dictionary<string, string>());
|
||||
|
||||
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
|
||||
|
||||
// Assert
|
||||
exitCode.Should().Be(0);
|
||||
stdOutData.Should().Be("Hello world!");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Help_text_is_printed_if_no_arguments_were_provided_and_default_command_is_not_defined()
|
||||
{
|
||||
// Arrange
|
||||
await using var stdOut = new MemoryStream();
|
||||
var console = new VirtualConsole(output: stdOut);
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand(typeof(ConcatCommand))
|
||||
.AddCommand(typeof(DivideCommand))
|
||||
.UseConsole(console)
|
||||
.UseDescription("This will be visible in help")
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
var exitCode = await application.RunAsync(
|
||||
Array.Empty<string>(),
|
||||
new Dictionary<string, string>());
|
||||
|
||||
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
|
||||
|
||||
// Assert
|
||||
exitCode.Should().Be(0);
|
||||
stdOutData.Should().Contain("This will be visible in help");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Specific_named_command_is_executed_if_provided_arguments_match_its_name()
|
||||
{
|
||||
// Arrange
|
||||
await using var stdOut = new MemoryStream();
|
||||
var console = new VirtualConsole(output: stdOut);
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand(typeof(DefaultCommand))
|
||||
.AddCommand(typeof(ConcatCommand))
|
||||
.AddCommand(typeof(DivideCommand))
|
||||
.UseConsole(console)
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
var exitCode = await application.RunAsync(
|
||||
new[] {"concat", "-i", "foo", "bar", "-s", ", "},
|
||||
new Dictionary<string, string>());
|
||||
|
||||
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
|
||||
|
||||
// Assert
|
||||
exitCode.Should().Be(0);
|
||||
stdOutData.Should().Be("foo, bar");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
using System;
|
||||
using FluentAssertions;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace CliFx.Tests
|
||||
{
|
||||
[TestFixture]
|
||||
public class SystemConsoleTests
|
||||
{
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
// Reset console color so it doesn't carry on into the next tests
|
||||
Console.ResetColor();
|
||||
}
|
||||
|
||||
[Test(Description = "Must be in sync with system console")]
|
||||
public void Smoke_Test()
|
||||
{
|
||||
// Arrange
|
||||
var console = new SystemConsole();
|
||||
|
||||
// Act
|
||||
console.ResetColor();
|
||||
console.ForegroundColor = ConsoleColor.DarkMagenta;
|
||||
console.BackgroundColor = ConsoleColor.DarkMagenta;
|
||||
|
||||
// Assert
|
||||
console.Input.Should().BeSameAs(Console.In);
|
||||
console.IsInputRedirected.Should().Be(Console.IsInputRedirected);
|
||||
console.Output.Should().BeSameAs(Console.Out);
|
||||
console.IsOutputRedirected.Should().Be(Console.IsOutputRedirected);
|
||||
console.Error.Should().BeSameAs(Console.Error);
|
||||
console.IsErrorRedirected.Should().Be(Console.IsErrorRedirected);
|
||||
console.ForegroundColor.Should().Be(Console.ForegroundColor);
|
||||
console.BackgroundColor.Should().Be(Console.BackgroundColor);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
using System.Threading.Tasks;
|
||||
using CliFx.Attributes;
|
||||
|
||||
namespace CliFx.Tests.TestCommands
|
||||
{
|
||||
[Command("allrequired", Description = "AllRequiredOptionsCommand description.")]
|
||||
public class AllRequiredOptionsCommand : ICommand
|
||||
{
|
||||
[CommandOption("option-f", 'f', IsRequired = true, Description = "OptionF description.")]
|
||||
public string? OptionF { get; set; }
|
||||
|
||||
[CommandOption("option-g", 'g', IsRequired = true, Description = "OptionG description.")]
|
||||
public string? OptionFG { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
}
|
||||
@@ -1,126 +0,0 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using CliFx.Attributes;
|
||||
using CliFx.Tests.TestCustomTypes;
|
||||
|
||||
namespace CliFx.Tests.TestCommands
|
||||
{
|
||||
[Command]
|
||||
public class AllSupportedTypesCommand : ICommand
|
||||
{
|
||||
[CommandOption(nameof(Object))]
|
||||
public object? Object { get; set; } = 42;
|
||||
|
||||
[CommandOption(nameof(String))]
|
||||
public string? String { get; set; } = "foo bar";
|
||||
|
||||
[CommandOption(nameof(Bool))]
|
||||
public bool Bool { get; set; }
|
||||
|
||||
[CommandOption(nameof(Char))]
|
||||
public char Char { get; set; }
|
||||
|
||||
[CommandOption(nameof(Sbyte))]
|
||||
public sbyte Sbyte { get; set; }
|
||||
|
||||
[CommandOption(nameof(Byte))]
|
||||
public byte Byte { get; set; }
|
||||
|
||||
[CommandOption(nameof(Short))]
|
||||
public short Short { get; set; }
|
||||
|
||||
[CommandOption(nameof(Ushort))]
|
||||
public ushort Ushort { get; set; }
|
||||
|
||||
[CommandOption(nameof(Int))]
|
||||
public int Int { get; set; }
|
||||
|
||||
[CommandOption(nameof(Uint))]
|
||||
public uint Uint { get; set; }
|
||||
|
||||
[CommandOption(nameof(Long))]
|
||||
public long Long { get; set; }
|
||||
|
||||
[CommandOption(nameof(Ulong))]
|
||||
public ulong Ulong { get; set; }
|
||||
|
||||
[CommandOption(nameof(Float))]
|
||||
public float Float { get; set; }
|
||||
|
||||
[CommandOption(nameof(Double))]
|
||||
public double Double { get; set; }
|
||||
|
||||
[CommandOption(nameof(Decimal))]
|
||||
public decimal Decimal { get; set; }
|
||||
|
||||
[CommandOption(nameof(DateTime))]
|
||||
public DateTime DateTime { get; set; }
|
||||
|
||||
[CommandOption(nameof(DateTimeOffset))]
|
||||
public DateTimeOffset DateTimeOffset { get; set; }
|
||||
|
||||
[CommandOption(nameof(TimeSpan))]
|
||||
public TimeSpan TimeSpan { get; set; }
|
||||
|
||||
[CommandOption(nameof(TestEnum))]
|
||||
public TestEnum TestEnum { get; set; }
|
||||
|
||||
[CommandOption(nameof(IntNullable))]
|
||||
public int? IntNullable { get; set; }
|
||||
|
||||
[CommandOption(nameof(TestEnumNullable))]
|
||||
public TestEnum? TestEnumNullable { get; set; }
|
||||
|
||||
[CommandOption(nameof(TimeSpanNullable))]
|
||||
public TimeSpan? TimeSpanNullable { get; set; }
|
||||
|
||||
[CommandOption(nameof(TestStringConstructable))]
|
||||
public TestStringConstructable? TestStringConstructable { get; set; }
|
||||
|
||||
[CommandOption(nameof(TestStringParseable))]
|
||||
public TestStringParseable? TestStringParseable { get; set; }
|
||||
|
||||
[CommandOption(nameof(TestStringParseableWithFormatProvider))]
|
||||
public TestStringParseableWithFormatProvider? TestStringParseableWithFormatProvider { get; set; }
|
||||
|
||||
[CommandOption(nameof(ObjectArray))]
|
||||
public object[]? ObjectArray { get; set; }
|
||||
|
||||
[CommandOption(nameof(StringArray))]
|
||||
public string[]? StringArray { get; set; }
|
||||
|
||||
[CommandOption(nameof(IntArray))]
|
||||
public int[]? IntArray { get; set; }
|
||||
|
||||
[CommandOption(nameof(TestEnumArray))]
|
||||
public TestEnum[]? TestEnumArray { get; set; }
|
||||
|
||||
[CommandOption(nameof(IntNullableArray))]
|
||||
public int?[]? IntNullableArray { get; set; }
|
||||
|
||||
[CommandOption(nameof(TestStringConstructableArray))]
|
||||
public TestStringConstructable[]? TestStringConstructableArray { get; set; }
|
||||
|
||||
[CommandOption(nameof(Enumerable))]
|
||||
public IEnumerable? Enumerable { get; set; }
|
||||
|
||||
[CommandOption(nameof(StringEnumerable))]
|
||||
public IEnumerable<string>? StringEnumerable { get; set; }
|
||||
|
||||
[CommandOption(nameof(StringReadOnlyList))]
|
||||
public IReadOnlyList<string>? StringReadOnlyList { get; set; }
|
||||
|
||||
[CommandOption(nameof(StringList))]
|
||||
public List<string>? StringList { get; set; }
|
||||
|
||||
[CommandOption(nameof(StringHashSet))]
|
||||
public HashSet<string>? StringHashSet { get; set; }
|
||||
|
||||
[CommandOption(nameof(NonConvertible))]
|
||||
public TestNonStringParseable? NonConvertible { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
using System.Threading.Tasks;
|
||||
using CliFx.Attributes;
|
||||
using CliFx.Tests.TestCustomTypes;
|
||||
|
||||
namespace CliFx.Tests.TestCommands
|
||||
{
|
||||
[Command]
|
||||
public class BrokenEnumerableCommand : ICommand
|
||||
{
|
||||
[CommandParameter(0)]
|
||||
public TestCustomEnumerable<string>? Test { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using CliFx.Attributes;
|
||||
|
||||
namespace CliFx.Tests.TestCommands
|
||||
{
|
||||
[Command("cancel")]
|
||||
public class CancellableCommand : ICommand
|
||||
{
|
||||
public async ValueTask ExecuteAsync(IConsole console)
|
||||
{
|
||||
await Task.Delay(TimeSpan.FromSeconds(3), console.GetCancellationToken());
|
||||
console.Output.WriteLine("Never printed");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
using System.Threading.Tasks;
|
||||
using CliFx.Attributes;
|
||||
using CliFx.Exceptions;
|
||||
|
||||
namespace CliFx.Tests.TestCommands
|
||||
{
|
||||
[Command("exc")]
|
||||
public class CommandExceptionCommand : ICommand
|
||||
{
|
||||
[CommandOption("code", 'c')]
|
||||
public int ExitCode { get; set; } = 1337;
|
||||
|
||||
[CommandOption("msg", 'm')]
|
||||
public string? Message { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => throw new CommandException(Message, ExitCode);
|
||||
}
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using CliFx.Attributes;
|
||||
|
||||
namespace CliFx.Tests.TestCommands
|
||||
{
|
||||
[Command("concat", Description = "Concatenate strings.")]
|
||||
public class ConcatCommand : ICommand
|
||||
{
|
||||
[CommandOption('i', IsRequired = true, Description = "Input strings.")]
|
||||
public IReadOnlyList<string> Inputs { get; set; }
|
||||
|
||||
[CommandOption('s', Description = "String separator.")]
|
||||
public string Separator { get; set; } = "";
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console)
|
||||
{
|
||||
console.Output.WriteLine(string.Join(Separator, Inputs));
|
||||
return default;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
using System.Threading.Tasks;
|
||||
using CliFx.Attributes;
|
||||
|
||||
namespace CliFx.Tests.TestCommands
|
||||
{
|
||||
[Command("div", Description = "Divide one number by another.")]
|
||||
public class DivideCommand : ICommand
|
||||
{
|
||||
[CommandOption("dividend", 'D', IsRequired = true, Description = "The number to divide.")]
|
||||
public double Dividend { get; set; }
|
||||
|
||||
[CommandOption("divisor", 'd', IsRequired = true, Description = "The number to divide by.")]
|
||||
public double Divisor { get; set; }
|
||||
|
||||
// This property should be ignored by resolver
|
||||
public bool NotAnOption { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console)
|
||||
{
|
||||
console.Output.WriteLine(Dividend / Divisor);
|
||||
return default;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
using System.Threading.Tasks;
|
||||
using CliFx.Attributes;
|
||||
|
||||
namespace CliFx.Tests.TestCommands
|
||||
{
|
||||
[Command]
|
||||
public class DuplicateOptionEnvironmentVariableNamesCommand : ICommand
|
||||
{
|
||||
[CommandOption("option-a", EnvironmentVariableName = "ENV_VAR")]
|
||||
public string? OptionA { get; set; }
|
||||
|
||||
[CommandOption("option-b", EnvironmentVariableName = "ENV_VAR")]
|
||||
public string? OptionB { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
using System.Threading.Tasks;
|
||||
using CliFx.Attributes;
|
||||
|
||||
namespace CliFx.Tests.TestCommands
|
||||
{
|
||||
[Command]
|
||||
public class DuplicateOptionNamesCommand : ICommand
|
||||
{
|
||||
[CommandOption("fruits")]
|
||||
public string? Apples { get; set; }
|
||||
|
||||
[CommandOption("fruits")]
|
||||
public string? Oranges { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
using System.Threading.Tasks;
|
||||
using CliFx.Attributes;
|
||||
|
||||
namespace CliFx.Tests.TestCommands
|
||||
{
|
||||
[Command]
|
||||
public class DuplicateOptionShortNamesCommand : ICommand
|
||||
{
|
||||
[CommandOption('x')]
|
||||
public string? OptionA { get; set; }
|
||||
|
||||
[CommandOption('x')]
|
||||
public string? OptionB { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
using System.Threading.Tasks;
|
||||
using CliFx.Attributes;
|
||||
|
||||
namespace CliFx.Tests.TestCommands
|
||||
{
|
||||
[Command]
|
||||
public class DuplicateParameterNameCommand : ICommand
|
||||
{
|
||||
[CommandParameter(0, Name = "param")]
|
||||
public string? ParameterA { get; set; }
|
||||
|
||||
[CommandParameter(1, Name = "param")]
|
||||
public string? ParameterB { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
using System.Threading.Tasks;
|
||||
using CliFx.Attributes;
|
||||
|
||||
namespace CliFx.Tests.TestCommands
|
||||
{
|
||||
[Command]
|
||||
public class DuplicateParameterOrderCommand : ICommand
|
||||
{
|
||||
[CommandParameter(13)]
|
||||
public string? ParameterA { get; set; }
|
||||
|
||||
[CommandParameter(13)]
|
||||
public string? ParameterB { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
using System.Threading.Tasks;
|
||||
using CliFx.Attributes;
|
||||
|
||||
namespace CliFx.Tests.TestCommands
|
||||
{
|
||||
[Command(Description = "Reads option values from environment variables.")]
|
||||
public class EnvironmentVariableCommand : ICommand
|
||||
{
|
||||
[CommandOption("opt", EnvironmentVariableName = "ENV_SINGLE_VALUE")]
|
||||
public string? Option { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using CliFx.Attributes;
|
||||
|
||||
namespace CliFx.Tests.TestCommands
|
||||
{
|
||||
[Command(Description = "Reads multiple option values from environment variables.")]
|
||||
public class EnvironmentVariableWithMultipleValuesCommand : ICommand
|
||||
{
|
||||
[CommandOption("opt", EnvironmentVariableName = "ENV_MULTIPLE_VALUES")]
|
||||
public IEnumerable<string>? Option { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
using System.Threading.Tasks;
|
||||
using CliFx.Attributes;
|
||||
|
||||
namespace CliFx.Tests.TestCommands
|
||||
{
|
||||
[Command(Description = "Reads one option value from environment variables because target property is not a collection.")]
|
||||
public class EnvironmentVariableWithoutCollectionPropertyCommand : ICommand
|
||||
{
|
||||
[CommandOption("opt", EnvironmentVariableName = "ENV_MULTIPLE_VALUES")]
|
||||
public string? Option { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using CliFx.Attributes;
|
||||
|
||||
namespace CliFx.Tests.TestCommands
|
||||
{
|
||||
[Command("exc")]
|
||||
public class ExceptionCommand : ICommand
|
||||
{
|
||||
[CommandOption("msg", 'm')]
|
||||
public string? Message { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => throw new Exception(Message);
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
using System.Threading.Tasks;
|
||||
using CliFx.Attributes;
|
||||
|
||||
namespace CliFx.Tests.TestCommands
|
||||
{
|
||||
[Command]
|
||||
public class HelloWorldDefaultCommand : ICommand
|
||||
{
|
||||
public ValueTask ExecuteAsync(IConsole console)
|
||||
{
|
||||
console.Output.WriteLine("Hello world.");
|
||||
return default;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
using System.Threading.Tasks;
|
||||
using CliFx.Attributes;
|
||||
|
||||
namespace CliFx.Tests.TestCommands
|
||||
{
|
||||
[Command(Description = "HelpDefaultCommand description.")]
|
||||
public class HelpDefaultCommand : ICommand
|
||||
{
|
||||
[CommandOption("option-a", 'a', Description = "OptionA description.")]
|
||||
public string? OptionA { get; set; }
|
||||
|
||||
[CommandOption("option-b", 'b', Description = "OptionB description.")]
|
||||
public string? OptionB { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
using System.Threading.Tasks;
|
||||
using CliFx.Attributes;
|
||||
|
||||
namespace CliFx.Tests.TestCommands
|
||||
{
|
||||
[Command("cmd", Description = "HelpNamedCommand description.")]
|
||||
public class HelpNamedCommand : ICommand
|
||||
{
|
||||
[CommandOption("option-c", 'c', Description = "OptionC description.")]
|
||||
public string? OptionC { get; set; }
|
||||
|
||||
[CommandOption("option-d", 'd', Description = "OptionD description.")]
|
||||
public string? OptionD { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
using System.Threading.Tasks;
|
||||
using CliFx.Attributes;
|
||||
|
||||
namespace CliFx.Tests.TestCommands
|
||||
{
|
||||
[Command("cmd sub", Description = "HelpSubCommand description.")]
|
||||
public class HelpSubCommand : ICommand
|
||||
{
|
||||
[CommandOption("option-e", 'e', Description = "OptionE description.")]
|
||||
public string? OptionE { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using CliFx.Attributes;
|
||||
|
||||
namespace CliFx.Tests.TestCommands
|
||||
{
|
||||
[Command]
|
||||
public class MultipleNonScalarParametersCommand : ICommand
|
||||
{
|
||||
[CommandParameter(0)]
|
||||
public IReadOnlyList<string>? ParameterA { get; set; }
|
||||
|
||||
[CommandParameter(1)]
|
||||
public IReadOnlyList<string>? ParameterB { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CliFx.Tests.TestCommands
|
||||
{
|
||||
public class NonAnnotatedCommand : ICommand
|
||||
{
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
using CliFx.Attributes;
|
||||
|
||||
namespace CliFx.Tests.TestCommands
|
||||
{
|
||||
[Command]
|
||||
public class NonImplementedCommand
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using CliFx.Attributes;
|
||||
|
||||
namespace CliFx.Tests.TestCommands
|
||||
{
|
||||
[Command]
|
||||
public class NonLastNonScalarParameterCommand : ICommand
|
||||
{
|
||||
[CommandParameter(0)]
|
||||
public IReadOnlyList<string>? ParameterA { get; set; }
|
||||
|
||||
[CommandParameter(1)]
|
||||
public string? ParameterB { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using CliFx.Attributes;
|
||||
|
||||
namespace CliFx.Tests.TestCommands
|
||||
{
|
||||
[Command("param cmd", Description = "Command using positional parameters")]
|
||||
public class ParameterCommand : ICommand
|
||||
{
|
||||
[CommandParameter(0, Name = "first")]
|
||||
public string? ParameterA { get; set; }
|
||||
|
||||
[CommandParameter(10)]
|
||||
public int? ParameterB { get; set; }
|
||||
|
||||
[CommandParameter(20, Description = "A list of numbers", Name = "third list")]
|
||||
public IEnumerable<int>? ParameterC { get; set; }
|
||||
|
||||
[CommandOption("option", 'o')]
|
||||
public string? OptionA { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
using System.Threading.Tasks;
|
||||
using CliFx.Attributes;
|
||||
|
||||
namespace CliFx.Tests.TestCommands
|
||||
{
|
||||
[Command("param cmd2", Description = "Command using positional parameters")]
|
||||
public class SimpleParameterCommand : ICommand
|
||||
{
|
||||
[CommandParameter(0, Name = "first")]
|
||||
public string? ParameterA { get; set; }
|
||||
|
||||
[CommandParameter(10)]
|
||||
public int? ParameterB { get; set; }
|
||||
|
||||
[CommandOption("option", 'o')]
|
||||
public string? OptionA { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
using System.Threading.Tasks;
|
||||
using CliFx.Attributes;
|
||||
|
||||
namespace CliFx.Tests.TestCommands
|
||||
{
|
||||
[Command("somerequired", Description = "SomeRequiredOptionsCommand description.")]
|
||||
public class SomeRequiredOptionsCommand : ICommand
|
||||
{
|
||||
[CommandOption("option-f", 'f', IsRequired = true, Description = "OptionF description.")]
|
||||
public string? OptionF { get; set; }
|
||||
|
||||
[CommandOption("option-g", 'g', Description = "OptionG description.")]
|
||||
public string? OptionFG { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace CliFx.Tests.TestCustomTypes
|
||||
{
|
||||
public class TestCustomEnumerable<T> : IEnumerable<T>
|
||||
{
|
||||
private readonly T[] _arr = new T[0];
|
||||
|
||||
public IEnumerator<T> GetEnumerator() => ((IEnumerable<T>) _arr).GetEnumerator();
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
namespace CliFx.Tests.TestCustomTypes
|
||||
{
|
||||
public enum TestEnum
|
||||
{
|
||||
Value1,
|
||||
Value2,
|
||||
Value3
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
namespace CliFx.Tests.TestCustomTypes
|
||||
{
|
||||
public class TestNonStringParseable
|
||||
{
|
||||
public int Value { get; }
|
||||
|
||||
public TestNonStringParseable(int value)
|
||||
{
|
||||
Value = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
namespace CliFx.Tests.TestCustomTypes
|
||||
{
|
||||
public class TestStringConstructable
|
||||
{
|
||||
public string Value { get; }
|
||||
|
||||
public TestStringConstructable(string value)
|
||||
{
|
||||
Value = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
namespace CliFx.Tests.TestCustomTypes
|
||||
{
|
||||
public class TestStringParseable
|
||||
{
|
||||
public string Value { get; }
|
||||
|
||||
private TestStringParseable(string value)
|
||||
{
|
||||
Value = value;
|
||||
}
|
||||
|
||||
public static TestStringParseable Parse(string value) => new TestStringParseable(value);
|
||||
}
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
using System;
|
||||
|
||||
namespace CliFx.Tests.TestCustomTypes
|
||||
{
|
||||
public class TestStringParseableWithFormatProvider
|
||||
{
|
||||
public string Value { get; }
|
||||
|
||||
private TestStringParseableWithFormatProvider(string value)
|
||||
{
|
||||
Value = value;
|
||||
}
|
||||
|
||||
public static TestStringParseableWithFormatProvider Parse(string value, IFormatProvider formatProvider) =>
|
||||
new TestStringParseableWithFormatProvider(value + " " + formatProvider);
|
||||
}
|
||||
}
|
||||
@@ -1,54 +0,0 @@
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using CliFx.Utilities;
|
||||
using FluentAssertions;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace CliFx.Tests.Utilities
|
||||
{
|
||||
[TestFixture]
|
||||
public class ProgressTickerTests
|
||||
{
|
||||
[Test]
|
||||
public void Report_Test()
|
||||
{
|
||||
// Arrange
|
||||
var formatProvider = CultureInfo.InvariantCulture;
|
||||
|
||||
using var stdout = new StringWriter(formatProvider);
|
||||
|
||||
var console = new VirtualConsole(TextReader.Null, false, stdout, false, TextWriter.Null, false);
|
||||
var ticker = console.CreateProgressTicker();
|
||||
|
||||
var progressValues = Enumerable.Range(0, 100).Select(p => p / 100.0).ToArray();
|
||||
var progressStringValues = progressValues.Select(p => p.ToString("P2", formatProvider)).ToArray();
|
||||
|
||||
// Act
|
||||
foreach (var progress in progressValues)
|
||||
ticker.Report(progress);
|
||||
|
||||
// Assert
|
||||
stdout.ToString().Should().ContainAll(progressStringValues);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Report_Redirected_Test()
|
||||
{
|
||||
// Arrange
|
||||
using var stdout = new StringWriter();
|
||||
|
||||
var console = new VirtualConsole(stdout);
|
||||
var ticker = console.CreateProgressTicker();
|
||||
|
||||
var progressValues = Enumerable.Range(0, 100).Select(p => p / 100.0).ToArray();
|
||||
|
||||
// Act
|
||||
foreach (var progress in progressValues)
|
||||
ticker.Report(progress);
|
||||
|
||||
// Assert
|
||||
stdout.ToString().Should().BeEmpty();
|
||||
}
|
||||
}
|
||||
}
|
||||
63
CliFx.Tests/UtilitiesSpecs.cs
Normal file
63
CliFx.Tests/UtilitiesSpecs.cs
Normal file
@@ -0,0 +1,63 @@
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using CliFx.Utilities;
|
||||
using FluentAssertions;
|
||||
using Xunit;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
namespace CliFx.Tests
|
||||
{
|
||||
public class UtilitiesSpecs
|
||||
{
|
||||
private readonly ITestOutputHelper _output;
|
||||
|
||||
public UtilitiesSpecs(ITestOutputHelper output) => _output = output;
|
||||
|
||||
[Fact]
|
||||
public void Progress_ticker_can_be_used_to_report_progress_to_console()
|
||||
{
|
||||
// Arrange
|
||||
using var stdOut = new MemoryStream();
|
||||
var console = new VirtualConsole(output: stdOut, isOutputRedirected: false);
|
||||
|
||||
var ticker = console.CreateProgressTicker();
|
||||
|
||||
var progressValues = Enumerable.Range(0, 100).Select(p => p / 100.0).ToArray();
|
||||
var progressStringValues = progressValues.Select(p => p.ToString("P2")).ToArray();
|
||||
|
||||
// Act
|
||||
foreach (var progress in progressValues)
|
||||
ticker.Report(progress);
|
||||
|
||||
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray());
|
||||
|
||||
// Assert
|
||||
stdOutData.Should().ContainAll(progressStringValues);
|
||||
|
||||
_output.WriteLine(stdOutData);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Progress_ticker_does_not_write_to_console_if_output_is_redirected()
|
||||
{
|
||||
// Arrange
|
||||
using var stdOut = new MemoryStream();
|
||||
var console = new VirtualConsole(output: stdOut);
|
||||
|
||||
var ticker = console.CreateProgressTicker();
|
||||
|
||||
var progressValues = Enumerable.Range(0, 100).Select(p => p / 100.0).ToArray();
|
||||
|
||||
// Act
|
||||
foreach (var progress in progressValues)
|
||||
ticker.Report(progress);
|
||||
|
||||
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray());
|
||||
|
||||
// Assert
|
||||
stdOutData.Should().BeEmpty();
|
||||
|
||||
_output.WriteLine(stdOutData);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using FluentAssertions;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace CliFx.Tests
|
||||
{
|
||||
[TestFixture]
|
||||
public class VirtualConsoleTests
|
||||
{
|
||||
[Test(Description = "Must not leak to system console")]
|
||||
public void Smoke_Test()
|
||||
{
|
||||
// Arrange
|
||||
using var stdin = new StringReader("hello world");
|
||||
using var stdout = new StringWriter();
|
||||
using var stderr = new StringWriter();
|
||||
|
||||
var console = new VirtualConsole(stdin, stdout, stderr);
|
||||
|
||||
// Act
|
||||
console.ResetColor();
|
||||
console.ForegroundColor = ConsoleColor.DarkMagenta;
|
||||
console.BackgroundColor = ConsoleColor.DarkMagenta;
|
||||
|
||||
// Assert
|
||||
console.Input.Should().BeSameAs(stdin);
|
||||
console.Input.Should().NotBeSameAs(Console.In);
|
||||
console.IsInputRedirected.Should().BeTrue();
|
||||
console.Output.Should().BeSameAs(stdout);
|
||||
console.Output.Should().NotBeSameAs(Console.Out);
|
||||
console.IsOutputRedirected.Should().BeTrue();
|
||||
console.Error.Should().BeSameAs(stderr);
|
||||
console.Error.Should().NotBeSameAs(Console.Error);
|
||||
console.IsErrorRedirected.Should().BeTrue();
|
||||
console.ForegroundColor.Should().NotBe(Console.ForegroundColor);
|
||||
console.BackgroundColor.Should().NotBe(Console.BackgroundColor);
|
||||
}
|
||||
}
|
||||
}
|
||||
5
CliFx.Tests/xunit.runner.json
Normal file
5
CliFx.Tests/xunit.runner.json
Normal file
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"$schema": "https://xunit.net/schema/current/xunit.runner.schema.json",
|
||||
"methodDisplayOptions": "all",
|
||||
"methodDisplay": "method"
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
<Project>
|
||||
|
||||
<PropertyGroup>
|
||||
<Version>1.0</Version>
|
||||
<Version>1.3.2</Version>
|
||||
<Company>Tyrrrz</Company>
|
||||
<Copyright>Copyright (C) Alexey Golub</Copyright>
|
||||
<LangVersion>latest</LangVersion>
|
||||
|
||||
32
CliFx.sln
32
CliFx.sln
@@ -10,16 +10,20 @@ EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{3AAE8166-BB8E-49DA-844C-3A0EE6BD40A0}"
|
||||
ProjectSection(SolutionItems) = preProject
|
||||
Changelog.md = Changelog.md
|
||||
CliFx.props = CliFx.props
|
||||
License.txt = License.txt
|
||||
Readme.md = Readme.md
|
||||
CliFx.props = CliFx.props
|
||||
EndProjectSection
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CliFx.Benchmarks", "CliFx.Benchmarks\CliFx.Benchmarks.csproj", "{8ACD6DC2-D768-4850-9223-5B7C83A78513}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CliFx.Demo", "CliFx.Demo\CliFx.Demo.csproj", "{AAB6844C-BF71-448F-A11B-89AEE459AB15}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CliFx.Tests.Dummy", "CliFx.Tests.Dummy\CliFx.Tests.Dummy.csproj", "{F717347D-8656-44DA-A4A2-BE515E8C4655}"
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CliFx.Tests.Dummy", "CliFx.Tests.Dummy\CliFx.Tests.Dummy.csproj", "{F717347D-8656-44DA-A4A2-BE515E8C4655}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CliFx.Analyzers", "CliFx.Analyzers\CliFx.Analyzers.csproj", "{F8460D69-F8CF-405C-A6ED-BED02A21DB42}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CliFx.Analyzers.Tests", "CliFx.Analyzers.Tests\CliFx.Analyzers.Tests.csproj", "{49878E75-2097-4C79-9151-B98A28FBB973}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
@@ -91,6 +95,30 @@ Global
|
||||
{F717347D-8656-44DA-A4A2-BE515E8C4655}.Release|x64.Build.0 = Release|Any CPU
|
||||
{F717347D-8656-44DA-A4A2-BE515E8C4655}.Release|x86.ActiveCfg = Release|Any CPU
|
||||
{F717347D-8656-44DA-A4A2-BE515E8C4655}.Release|x86.Build.0 = Release|Any CPU
|
||||
{F8460D69-F8CF-405C-A6ED-BED02A21DB42}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{F8460D69-F8CF-405C-A6ED-BED02A21DB42}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{F8460D69-F8CF-405C-A6ED-BED02A21DB42}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||
{F8460D69-F8CF-405C-A6ED-BED02A21DB42}.Debug|x64.Build.0 = Debug|Any CPU
|
||||
{F8460D69-F8CF-405C-A6ED-BED02A21DB42}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||
{F8460D69-F8CF-405C-A6ED-BED02A21DB42}.Debug|x86.Build.0 = Debug|Any CPU
|
||||
{F8460D69-F8CF-405C-A6ED-BED02A21DB42}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{F8460D69-F8CF-405C-A6ED-BED02A21DB42}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{F8460D69-F8CF-405C-A6ED-BED02A21DB42}.Release|x64.ActiveCfg = Release|Any CPU
|
||||
{F8460D69-F8CF-405C-A6ED-BED02A21DB42}.Release|x64.Build.0 = Release|Any CPU
|
||||
{F8460D69-F8CF-405C-A6ED-BED02A21DB42}.Release|x86.ActiveCfg = Release|Any CPU
|
||||
{F8460D69-F8CF-405C-A6ED-BED02A21DB42}.Release|x86.Build.0 = Release|Any CPU
|
||||
{49878E75-2097-4C79-9151-B98A28FBB973}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{49878E75-2097-4C79-9151-B98A28FBB973}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{49878E75-2097-4C79-9151-B98A28FBB973}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||
{49878E75-2097-4C79-9151-B98A28FBB973}.Debug|x64.Build.0 = Debug|Any CPU
|
||||
{49878E75-2097-4C79-9151-B98A28FBB973}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||
{49878E75-2097-4C79-9151-B98A28FBB973}.Debug|x86.Build.0 = Debug|Any CPU
|
||||
{49878E75-2097-4C79-9151-B98A28FBB973}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{49878E75-2097-4C79-9151-B98A28FBB973}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{49878E75-2097-4C79-9151-B98A28FBB973}.Release|x64.ActiveCfg = Release|Any CPU
|
||||
{49878E75-2097-4C79-9151-B98A28FBB973}.Release|x64.Build.0 = Release|Any CPU
|
||||
{49878E75-2097-4C79-9151-B98A28FBB973}.Release|x86.ActiveCfg = Release|Any CPU
|
||||
{49878E75-2097-4C79-9151-B98A28FBB973}.Release|x86.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
||||
@@ -28,7 +28,8 @@ namespace CliFx
|
||||
/// </summary>
|
||||
public ApplicationConfiguration(
|
||||
IReadOnlyList<Type> commandTypes,
|
||||
bool isDebugModeAllowed, bool isPreviewModeAllowed)
|
||||
bool isDebugModeAllowed,
|
||||
bool isPreviewModeAllowed)
|
||||
{
|
||||
CommandTypes = commandTypes;
|
||||
IsDebugModeAllowed = isDebugModeAllowed;
|
||||
|
||||
@@ -9,14 +9,14 @@ namespace CliFx.Attributes
|
||||
public class CommandOptionAttribute : Attribute
|
||||
{
|
||||
/// <summary>
|
||||
/// Option name.
|
||||
/// Option name (must be longer than a single character).
|
||||
/// Either <see cref="Name"/> or <see cref="ShortName"/> must be set.
|
||||
/// All options in a command must have different names (comparison is not case-sensitive).
|
||||
/// </summary>
|
||||
public string? Name { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Option short name.
|
||||
/// Option short name (single character).
|
||||
/// Either <see cref="Name"/> or <see cref="ShortName"/> must be set.
|
||||
/// All options in a command must have different short names (comparison is case-sensitive).
|
||||
/// </summary>
|
||||
|
||||
@@ -1,324 +0,0 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using CliFx.Domain;
|
||||
using CliFx.Internal;
|
||||
|
||||
namespace CliFx
|
||||
{
|
||||
public partial class CliApplication
|
||||
{
|
||||
private void RenderHelp(ApplicationSchema applicationSchema, CommandSchema command)
|
||||
{
|
||||
var column = 0;
|
||||
var row = 0;
|
||||
|
||||
var childCommands = applicationSchema.GetChildCommands(command.Name);
|
||||
|
||||
bool IsEmpty() => column == 0 && row == 0;
|
||||
|
||||
void Render(string text)
|
||||
{
|
||||
_console.Output.Write(text);
|
||||
|
||||
column += text.Length;
|
||||
}
|
||||
|
||||
void RenderNewLine()
|
||||
{
|
||||
_console.Output.WriteLine();
|
||||
|
||||
column = 0;
|
||||
row++;
|
||||
}
|
||||
|
||||
void RenderMargin(int lines = 1)
|
||||
{
|
||||
if (!IsEmpty())
|
||||
{
|
||||
for (var i = 0; i < lines; i++)
|
||||
RenderNewLine();
|
||||
}
|
||||
}
|
||||
|
||||
void RenderIndent(int spaces = 2)
|
||||
{
|
||||
Render(' '.Repeat(spaces));
|
||||
}
|
||||
|
||||
void RenderColumnIndent(int spaces = 20, int margin = 2)
|
||||
{
|
||||
if (column + margin < spaces)
|
||||
{
|
||||
RenderIndent(spaces - column);
|
||||
}
|
||||
else
|
||||
{
|
||||
Render(" ");
|
||||
}
|
||||
}
|
||||
|
||||
void RenderWithColor(string text, ConsoleColor foregroundColor)
|
||||
{
|
||||
_console.WithForegroundColor(foregroundColor, () => Render(text));
|
||||
}
|
||||
|
||||
void RenderHeader(string text)
|
||||
{
|
||||
RenderWithColor(text, ConsoleColor.Magenta);
|
||||
RenderNewLine();
|
||||
}
|
||||
|
||||
void RenderApplicationInfo()
|
||||
{
|
||||
if (!command.IsDefault)
|
||||
return;
|
||||
|
||||
// Title and version
|
||||
RenderWithColor(_metadata.Title, ConsoleColor.Yellow);
|
||||
Render(" ");
|
||||
RenderWithColor(_metadata.VersionText, ConsoleColor.Yellow);
|
||||
RenderNewLine();
|
||||
|
||||
// Description
|
||||
if (!string.IsNullOrWhiteSpace(_metadata.Description))
|
||||
{
|
||||
Render(_metadata.Description);
|
||||
RenderNewLine();
|
||||
}
|
||||
}
|
||||
|
||||
void RenderDescription()
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(command.Description))
|
||||
return;
|
||||
|
||||
RenderMargin();
|
||||
RenderHeader("Description");
|
||||
|
||||
RenderIndent();
|
||||
Render(command.Description);
|
||||
RenderNewLine();
|
||||
}
|
||||
|
||||
void RenderUsage()
|
||||
{
|
||||
RenderMargin();
|
||||
RenderHeader("Usage");
|
||||
|
||||
// Exe name
|
||||
RenderIndent();
|
||||
Render(_metadata.ExecutableName);
|
||||
|
||||
// Command name
|
||||
if (!string.IsNullOrWhiteSpace(command.Name))
|
||||
{
|
||||
Render(" ");
|
||||
RenderWithColor(command.Name, ConsoleColor.Cyan);
|
||||
}
|
||||
|
||||
// Child command placeholder
|
||||
if (childCommands.Any())
|
||||
{
|
||||
Render(" ");
|
||||
RenderWithColor("[command]", ConsoleColor.Cyan);
|
||||
}
|
||||
|
||||
// Parameters
|
||||
foreach (var parameter in command.Parameters)
|
||||
{
|
||||
Render(" ");
|
||||
Render(parameter.IsScalar
|
||||
? $"<{parameter.DisplayName}>"
|
||||
: $"<{parameter.DisplayName}...>");
|
||||
}
|
||||
|
||||
// Required options
|
||||
var requiredOptionSchemas = command.Options
|
||||
.Where(o => o.IsRequired)
|
||||
.ToArray();
|
||||
|
||||
foreach (var option in requiredOptionSchemas)
|
||||
{
|
||||
Render(" ");
|
||||
if (!string.IsNullOrWhiteSpace(option.Name))
|
||||
{
|
||||
RenderWithColor($"--{option.Name}", ConsoleColor.White);
|
||||
Render(" ");
|
||||
Render(option.IsScalar
|
||||
? "<value>"
|
||||
: "<values...>");
|
||||
}
|
||||
else
|
||||
{
|
||||
RenderWithColor($"-{option.ShortName}", ConsoleColor.White);
|
||||
Render(" ");
|
||||
Render(option.IsScalar
|
||||
? "<value>"
|
||||
: "<values...>");
|
||||
}
|
||||
}
|
||||
|
||||
// Options placeholder
|
||||
if (command.Options.Count != requiredOptionSchemas.Length)
|
||||
{
|
||||
Render(" ");
|
||||
RenderWithColor("[options]", ConsoleColor.White);
|
||||
}
|
||||
|
||||
RenderNewLine();
|
||||
}
|
||||
|
||||
void RenderParameters()
|
||||
{
|
||||
if (!command.Parameters.Any())
|
||||
return;
|
||||
|
||||
RenderMargin();
|
||||
RenderHeader("Parameters");
|
||||
|
||||
var parameters = command.Parameters
|
||||
.OrderBy(p => p.Order)
|
||||
.ToArray();
|
||||
|
||||
foreach (var parameter in parameters)
|
||||
{
|
||||
RenderWithColor("* ", ConsoleColor.Red);
|
||||
RenderWithColor($"{parameter.DisplayName}", ConsoleColor.White);
|
||||
|
||||
RenderColumnIndent();
|
||||
|
||||
// Description
|
||||
if (!string.IsNullOrWhiteSpace(parameter.Description))
|
||||
{
|
||||
Render(parameter.Description);
|
||||
}
|
||||
|
||||
RenderNewLine();
|
||||
}
|
||||
}
|
||||
|
||||
void RenderOptions()
|
||||
{
|
||||
RenderMargin();
|
||||
RenderHeader("Options");
|
||||
|
||||
var options = command.Options
|
||||
.OrderByDescending(o => o.IsRequired)
|
||||
.ToList();
|
||||
|
||||
// Add built-in options
|
||||
options.Add(CommandOptionSchema.HelpOption);
|
||||
if (command.IsDefault)
|
||||
options.Add(CommandOptionSchema.VersionOption);
|
||||
|
||||
foreach (var option in options)
|
||||
{
|
||||
if (option.IsRequired)
|
||||
{
|
||||
RenderWithColor("* ", ConsoleColor.Red);
|
||||
}
|
||||
else
|
||||
{
|
||||
RenderIndent();
|
||||
}
|
||||
|
||||
// Short name
|
||||
if (option.ShortName != null)
|
||||
{
|
||||
RenderWithColor($"-{option.ShortName}", ConsoleColor.White);
|
||||
}
|
||||
|
||||
// Delimiter
|
||||
if (!string.IsNullOrWhiteSpace(option.Name) && option.ShortName != null)
|
||||
{
|
||||
Render("|");
|
||||
}
|
||||
|
||||
// Name
|
||||
if (!string.IsNullOrWhiteSpace(option.Name))
|
||||
{
|
||||
RenderWithColor($"--{option.Name}", ConsoleColor.White);
|
||||
}
|
||||
|
||||
RenderColumnIndent();
|
||||
|
||||
// Description
|
||||
if (!string.IsNullOrWhiteSpace(option.Description))
|
||||
{
|
||||
Render(option.Description);
|
||||
Render(" ");
|
||||
}
|
||||
|
||||
// Environment variable
|
||||
if (!string.IsNullOrWhiteSpace(option.EnvironmentVariableName))
|
||||
{
|
||||
Render($"(Environment variable: {option.EnvironmentVariableName}).");
|
||||
Render(" ");
|
||||
}
|
||||
|
||||
RenderNewLine();
|
||||
}
|
||||
}
|
||||
|
||||
void RenderChildCommands()
|
||||
{
|
||||
if (!childCommands.Any())
|
||||
return;
|
||||
|
||||
RenderMargin();
|
||||
RenderHeader("Commands");
|
||||
|
||||
foreach (var childCommand in childCommands)
|
||||
{
|
||||
var relativeCommandName =
|
||||
string.IsNullOrWhiteSpace(childCommand.Name) || string.IsNullOrWhiteSpace(command.Name)
|
||||
? childCommand.Name
|
||||
: childCommand.Name.Substring(command.Name.Length + 1);
|
||||
|
||||
// Name
|
||||
RenderIndent();
|
||||
RenderWithColor(relativeCommandName, ConsoleColor.Cyan);
|
||||
|
||||
// Description
|
||||
if (!string.IsNullOrWhiteSpace(childCommand.Description))
|
||||
{
|
||||
RenderColumnIndent();
|
||||
Render(childCommand.Description);
|
||||
}
|
||||
|
||||
RenderNewLine();
|
||||
}
|
||||
|
||||
RenderMargin();
|
||||
|
||||
// Child command help tip
|
||||
Render("You can run `");
|
||||
Render(_metadata.ExecutableName);
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(command.Name))
|
||||
{
|
||||
Render(" ");
|
||||
RenderWithColor(command.Name, ConsoleColor.Cyan);
|
||||
}
|
||||
|
||||
Render(" ");
|
||||
RenderWithColor("[command]", ConsoleColor.Cyan);
|
||||
|
||||
Render(" ");
|
||||
RenderWithColor("--help", ConsoleColor.White);
|
||||
|
||||
Render("` to show help on a specific command.");
|
||||
|
||||
RenderNewLine();
|
||||
}
|
||||
|
||||
_console.ResetColor();
|
||||
RenderApplicationInfo();
|
||||
RenderDescription();
|
||||
RenderUsage();
|
||||
RenderParameters();
|
||||
RenderOptions();
|
||||
RenderChildCommands();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,8 +4,10 @@ using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using CliFx.Attributes;
|
||||
using CliFx.Domain;
|
||||
using CliFx.Exceptions;
|
||||
using CliFx.Internal;
|
||||
|
||||
namespace CliFx
|
||||
{
|
||||
@@ -19,6 +21,8 @@ namespace CliFx
|
||||
private readonly IConsole _console;
|
||||
private readonly ITypeActivator _typeActivator;
|
||||
|
||||
private readonly HelpTextWriter _helpTextWriter;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes an instance of <see cref="CliApplication"/>.
|
||||
/// </summary>
|
||||
@@ -30,44 +34,39 @@ namespace CliFx
|
||||
_configuration = configuration;
|
||||
_console = console;
|
||||
_typeActivator = typeActivator;
|
||||
|
||||
_helpTextWriter = new HelpTextWriter(metadata, console);
|
||||
}
|
||||
|
||||
private async ValueTask<int?> HandleDebugDirectiveAsync(CommandLineInput commandLineInput)
|
||||
private void WriteError(string message) => _console.WithForegroundColor(ConsoleColor.Red, () =>
|
||||
_console.Error.WriteLine(message));
|
||||
|
||||
private async ValueTask LaunchAndWaitForDebuggerAsync()
|
||||
{
|
||||
var isDebugMode = _configuration.IsDebugModeAllowed && commandLineInput.IsDebugDirectiveSpecified;
|
||||
if (!isDebugMode)
|
||||
return null;
|
||||
var processId = ProcessEx.GetCurrentProcessId();
|
||||
|
||||
_console.WithForegroundColor(ConsoleColor.Green, () =>
|
||||
_console.Output.WriteLine($"Attach debugger to PID {Process.GetCurrentProcess().Id} to continue."));
|
||||
_console.Output.WriteLine($"Attach debugger to PID {processId} to continue."));
|
||||
|
||||
Debugger.Launch();
|
||||
|
||||
while (!Debugger.IsAttached)
|
||||
await Task.Delay(100);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private int? HandlePreviewDirective(ApplicationSchema applicationSchema, CommandLineInput commandLineInput)
|
||||
private void WriteCommandLineInput(CommandInput input)
|
||||
{
|
||||
var isPreviewMode = _configuration.IsPreviewModeAllowed && commandLineInput.IsPreviewDirectiveSpecified;
|
||||
if (!isPreviewMode)
|
||||
return null;
|
||||
|
||||
var commandSchema = applicationSchema.TryFindCommand(commandLineInput, out var argumentOffset);
|
||||
|
||||
_console.Output.WriteLine("Parser preview:");
|
||||
|
||||
// Command name
|
||||
if (commandSchema != null && argumentOffset > 0)
|
||||
if (!string.IsNullOrWhiteSpace(input.CommandName))
|
||||
{
|
||||
_console.WithForegroundColor(ConsoleColor.Cyan, () =>
|
||||
_console.Output.Write(commandSchema.Name));
|
||||
_console.Output.Write(input.CommandName));
|
||||
|
||||
_console.Output.Write(' ');
|
||||
}
|
||||
|
||||
// Parameters
|
||||
foreach (var parameter in commandLineInput.Arguments.Skip(argumentOffset))
|
||||
foreach (var parameter in input.Parameters)
|
||||
{
|
||||
_console.Output.Write('<');
|
||||
|
||||
@@ -79,100 +78,132 @@ namespace CliFx
|
||||
}
|
||||
|
||||
// Options
|
||||
foreach (var option in commandLineInput.Options)
|
||||
foreach (var option in input.Options)
|
||||
{
|
||||
_console.Output.Write('[');
|
||||
|
||||
_console.WithForegroundColor(ConsoleColor.White, () =>
|
||||
_console.Output.Write(option));
|
||||
{
|
||||
// Alias
|
||||
_console.Output.Write(option.GetRawAlias());
|
||||
|
||||
// Values
|
||||
if (option.Values.Any())
|
||||
{
|
||||
_console.Output.Write(' ');
|
||||
_console.Output.Write(option.GetRawValues());
|
||||
}
|
||||
});
|
||||
|
||||
_console.Output.Write(']');
|
||||
_console.Output.Write(' ');
|
||||
}
|
||||
|
||||
_console.Output.WriteLine();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
private int? HandleVersionOption(CommandLineInput commandLineInput)
|
||||
{
|
||||
// Version option is available only on the default command (i.e. when arguments are not specified)
|
||||
var shouldRenderVersion = !commandLineInput.Arguments.Any() && commandLineInput.IsVersionOptionSpecified;
|
||||
if (!shouldRenderVersion)
|
||||
return null;
|
||||
|
||||
_console.Output.WriteLine(_metadata.VersionText);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
private int? HandleHelpOption(ApplicationSchema applicationSchema, CommandLineInput commandLineInput)
|
||||
{
|
||||
// Help is rendered either when it's requested or when the user provides no arguments and there is no default command
|
||||
var shouldRenderHelp =
|
||||
commandLineInput.IsHelpOptionSpecified ||
|
||||
!applicationSchema.Commands.Any(c => c.IsDefault) && !commandLineInput.Arguments.Any() && !commandLineInput.Options.Any();
|
||||
|
||||
if (!shouldRenderHelp)
|
||||
return null;
|
||||
|
||||
// Get the command schema that matches the input or use a dummy default command as a fallback
|
||||
var commandSchema =
|
||||
applicationSchema.TryFindCommand(commandLineInput) ??
|
||||
CommandSchema.StubDefaultCommand;
|
||||
|
||||
RenderHelp(applicationSchema, commandSchema);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
private async ValueTask<int> HandleCommandExecutionAsync(
|
||||
ApplicationSchema applicationSchema,
|
||||
CommandLineInput commandLineInput,
|
||||
IReadOnlyDictionary<string, string> environmentVariables)
|
||||
{
|
||||
await applicationSchema
|
||||
.InitializeEntryPoint(commandLineInput, environmentVariables, _typeActivator)
|
||||
.ExecuteAsync(_console);
|
||||
|
||||
return 0;
|
||||
}
|
||||
private ICommand GetCommandInstance(CommandSchema command) =>
|
||||
command != StubDefaultCommand.Schema
|
||||
? (ICommand) _typeActivator.CreateInstance(command.Type)
|
||||
: new StubDefaultCommand();
|
||||
|
||||
/// <summary>
|
||||
/// Runs the application with specified command line arguments and environment variables, and returns the exit code.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// If a <see cref="CommandException"/> is thrown during command execution, it will be handled and routed to the console.
|
||||
/// Additionally, if the debugger is not attached (i.e. the app is running in production), all other exceptions thrown within
|
||||
/// this method will be handled and routed to the console as well.
|
||||
/// </remarks>
|
||||
public async ValueTask<int> RunAsync(
|
||||
IReadOnlyList<string> commandLineArguments,
|
||||
IReadOnlyDictionary<string, string> environmentVariables)
|
||||
{
|
||||
try
|
||||
{
|
||||
var applicationSchema = ApplicationSchema.Resolve(_configuration.CommandTypes);
|
||||
var commandLineInput = CommandLineInput.Parse(commandLineArguments);
|
||||
var root = RootSchema.Resolve(_configuration.CommandTypes);
|
||||
var input = CommandInput.Parse(commandLineArguments, root.GetCommandNames());
|
||||
|
||||
return
|
||||
await HandleDebugDirectiveAsync(commandLineInput) ??
|
||||
HandlePreviewDirective(applicationSchema, commandLineInput) ??
|
||||
HandleVersionOption(commandLineInput) ??
|
||||
HandleHelpOption(applicationSchema, commandLineInput) ??
|
||||
await HandleCommandExecutionAsync(applicationSchema, commandLineInput, environmentVariables);
|
||||
}
|
||||
catch (Exception ex)
|
||||
// Debug mode
|
||||
if (_configuration.IsDebugModeAllowed && input.IsDebugDirectiveSpecified)
|
||||
{
|
||||
// We want to catch exceptions in order to print errors and return correct exit codes.
|
||||
// Doing this also gets rid of the annoying Windows troubleshooting dialog that shows up on unhandled exceptions.
|
||||
await LaunchAndWaitForDebuggerAsync();
|
||||
}
|
||||
|
||||
// Prefer showing message without stack trace on exceptions coming from CliFx or on CommandException
|
||||
var errorMessage = !string.IsNullOrWhiteSpace(ex.Message) && (ex is CliFxException || ex is CommandException)
|
||||
? ex.Message
|
||||
: ex.ToString();
|
||||
// Preview mode
|
||||
if (_configuration.IsPreviewModeAllowed && input.IsPreviewDirectiveSpecified)
|
||||
{
|
||||
WriteCommandLineInput(input);
|
||||
return ExitCode.Success;
|
||||
}
|
||||
|
||||
_console.WithForegroundColor(ConsoleColor.Red, () => _console.Error.WriteLine(errorMessage));
|
||||
// Try to get the command matching the input or fallback to default
|
||||
var command =
|
||||
root.TryFindCommand(input.CommandName) ??
|
||||
root.TryFindDefaultCommand() ??
|
||||
StubDefaultCommand.Schema;
|
||||
|
||||
return ex is CommandException commandException
|
||||
? commandException.ExitCode
|
||||
: ex.HResult;
|
||||
// Version option
|
||||
if (command.IsVersionOptionAvailable && input.IsVersionOptionSpecified)
|
||||
{
|
||||
_console.Output.WriteLine(_metadata.VersionText);
|
||||
return ExitCode.Success;
|
||||
}
|
||||
|
||||
// Get command instance (also used in help text)
|
||||
var instance = GetCommandInstance(command);
|
||||
|
||||
// To avoid instantiating the command twice, we need to get default values
|
||||
// before the arguments are bound to the properties
|
||||
var defaultValues = command.GetArgumentValues(instance);
|
||||
|
||||
// Help option
|
||||
if (command.IsHelpOptionAvailable && input.IsHelpOptionSpecified ||
|
||||
command == StubDefaultCommand.Schema && !input.Parameters.Any() && !input.Options.Any())
|
||||
{
|
||||
_helpTextWriter.Write(root, command, defaultValues);
|
||||
return ExitCode.Success;
|
||||
}
|
||||
|
||||
// Bind arguments
|
||||
try
|
||||
{
|
||||
command.Bind(instance, input, environmentVariables);
|
||||
}
|
||||
// This may throw exceptions which are useful only to the end-user
|
||||
catch (CliFxException ex)
|
||||
{
|
||||
WriteError(ex.ToString());
|
||||
_helpTextWriter.Write(root, command, defaultValues);
|
||||
|
||||
return ExitCode.FromException(ex);
|
||||
}
|
||||
|
||||
// Execute the command
|
||||
try
|
||||
{
|
||||
await instance.ExecuteAsync(_console);
|
||||
return ExitCode.Success;
|
||||
}
|
||||
// Swallow command exceptions and route them to the console
|
||||
catch (CommandException ex)
|
||||
{
|
||||
WriteError(ex.ToString());
|
||||
|
||||
if (ex.ShowHelp)
|
||||
_helpTextWriter.Write(root, command, defaultValues);
|
||||
|
||||
return ex.ExitCode;
|
||||
}
|
||||
}
|
||||
// To prevent the app from showing the annoying Windows troubleshooting dialog,
|
||||
// we handle all exceptions and route them to the console nicely.
|
||||
// However, we don't want to swallow unhandled exceptions when the debugger is attached,
|
||||
// because we still want the IDE to show them to the developer.
|
||||
catch (Exception ex) when (!Debugger.IsAttached)
|
||||
{
|
||||
WriteError(ex.ToString());
|
||||
return ExitCode.FromException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -180,11 +211,17 @@ namespace CliFx
|
||||
/// Runs the application with specified command line arguments and returns the exit code.
|
||||
/// Environment variables are retrieved automatically.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// If a <see cref="CommandException"/> is thrown during command execution, it will be handled and routed to the console.
|
||||
/// Additionally, if the debugger is not attached (i.e. the app is running in production), all other exceptions thrown within
|
||||
/// this method will be handled and routed to the console as well.
|
||||
/// </remarks>
|
||||
public async ValueTask<int> RunAsync(IReadOnlyList<string> commandLineArguments)
|
||||
{
|
||||
// Environment variable names are case-insensitive on Windows but are case-sensitive on Linux and macOS
|
||||
var environmentVariables = Environment.GetEnvironmentVariables()
|
||||
.Cast<DictionaryEntry>()
|
||||
.ToDictionary(e => (string) e.Key, e => (string) e.Value, StringComparer.OrdinalIgnoreCase);
|
||||
.ToDictionary(e => (string) e.Key, e => (string) e.Value, StringComparer.Ordinal);
|
||||
|
||||
return await RunAsync(commandLineArguments, environmentVariables);
|
||||
}
|
||||
@@ -193,6 +230,11 @@ namespace CliFx
|
||||
/// Runs the application and returns the exit code.
|
||||
/// Command line arguments and environment variables are retrieved automatically.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// If a <see cref="CommandException"/> is thrown during command execution, it will be handled and routed to the console.
|
||||
/// Additionally, if the debugger is not attached (i.e. the app is running in production), all other exceptions thrown within
|
||||
/// this method will be handled and routed to the console as well.
|
||||
/// </remarks>
|
||||
public async ValueTask<int> RunAsync()
|
||||
{
|
||||
var commandLineArguments = Environment.GetCommandLineArgs()
|
||||
@@ -202,4 +244,25 @@ namespace CliFx
|
||||
return await RunAsync(commandLineArguments);
|
||||
}
|
||||
}
|
||||
|
||||
public partial class CliApplication
|
||||
{
|
||||
private static class ExitCode
|
||||
{
|
||||
public const int Success = 0;
|
||||
|
||||
public static int FromException(Exception ex) =>
|
||||
ex is CommandException cmdEx
|
||||
? cmdEx.ExitCode
|
||||
: 1;
|
||||
}
|
||||
|
||||
[Command]
|
||||
private class StubDefaultCommand : ICommand
|
||||
{
|
||||
public static CommandSchema Schema { get; } = CommandSchema.TryResolve(typeof(StubDefaultCommand))!;
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using CliFx.Domain;
|
||||
using CliFx.Internal.Extensions;
|
||||
|
||||
namespace CliFx
|
||||
{
|
||||
@@ -46,7 +47,7 @@ namespace CliFx
|
||||
|
||||
/// <summary>
|
||||
/// Adds commands from the specified assembly to the application.
|
||||
/// Only the public types are added.
|
||||
/// Only adds public valid command types.
|
||||
/// </summary>
|
||||
public CliApplicationBuilder AddCommandsFrom(Assembly commandAssembly)
|
||||
{
|
||||
@@ -58,7 +59,7 @@ namespace CliFx
|
||||
|
||||
/// <summary>
|
||||
/// Adds commands from the specified assemblies to the application.
|
||||
/// Only the public types are added.
|
||||
/// Only adds public valid command types.
|
||||
/// </summary>
|
||||
public CliApplicationBuilder AddCommandsFrom(IEnumerable<Assembly> commandAssemblies)
|
||||
{
|
||||
@@ -70,7 +71,7 @@ namespace CliFx
|
||||
|
||||
/// <summary>
|
||||
/// Adds commands from the calling assembly to the application.
|
||||
/// Only the public types are added.
|
||||
/// Only adds public valid command types.
|
||||
/// </summary>
|
||||
public CliApplicationBuilder AddCommandsFromThisAssembly() => AddCommandsFrom(Assembly.GetCallingAssembly());
|
||||
|
||||
@@ -158,9 +159,9 @@ namespace CliFx
|
||||
/// </summary>
|
||||
public CliApplication Build()
|
||||
{
|
||||
_title ??= GetDefaultTitle() ?? "App";
|
||||
_executableName ??= GetDefaultExecutableName() ?? "app";
|
||||
_versionText ??= GetDefaultVersionText() ?? "v1.0";
|
||||
_title ??= TryGetDefaultTitle() ?? "App";
|
||||
_executableName ??= TryGetDefaultExecutableName() ?? "app";
|
||||
_versionText ??= TryGetDefaultVersionText() ?? "v1.0";
|
||||
_console ??= new SystemConsole();
|
||||
_typeActivator ??= new DefaultTypeActivator();
|
||||
|
||||
@@ -173,30 +174,28 @@ namespace CliFx
|
||||
|
||||
public partial class CliApplicationBuilder
|
||||
{
|
||||
private static readonly Lazy<Assembly> LazyEntryAssembly = new Lazy<Assembly>(Assembly.GetEntryAssembly);
|
||||
private static readonly Lazy<Assembly?> LazyEntryAssembly = new Lazy<Assembly?>(Assembly.GetEntryAssembly);
|
||||
|
||||
// Entry assembly is null in tests
|
||||
private static Assembly EntryAssembly => LazyEntryAssembly.Value;
|
||||
private static Assembly? EntryAssembly => LazyEntryAssembly.Value;
|
||||
|
||||
private static string? GetDefaultTitle() => EntryAssembly?.GetName().Name;
|
||||
private static string? TryGetDefaultTitle() => EntryAssembly?.GetName().Name;
|
||||
|
||||
private static string? GetDefaultExecutableName()
|
||||
private static string? TryGetDefaultExecutableName()
|
||||
{
|
||||
var entryAssemblyLocation = EntryAssembly?.Location;
|
||||
|
||||
// If it's a .dll assembly, prepend 'dotnet' and keep the file extension
|
||||
if (string.Equals(Path.GetExtension(entryAssemblyLocation), ".dll", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return "dotnet " + Path.GetFileName(entryAssemblyLocation);
|
||||
// The assembly can be an executable or a dll, depending on how it was packaged
|
||||
var isDll = string.Equals(Path.GetExtension(entryAssemblyLocation), ".dll", StringComparison.OrdinalIgnoreCase);
|
||||
|
||||
return isDll
|
||||
? "dotnet " + Path.GetFileName(entryAssemblyLocation)
|
||||
: Path.GetFileNameWithoutExtension(entryAssemblyLocation);
|
||||
}
|
||||
|
||||
// Otherwise just use assembly file name without extension
|
||||
return Path.GetFileNameWithoutExtension(entryAssemblyLocation);
|
||||
}
|
||||
|
||||
private static string? GetDefaultVersionText() =>
|
||||
private static string? TryGetDefaultVersionText() =>
|
||||
EntryAssembly != null
|
||||
? $"v{EntryAssembly.GetName().Version}"
|
||||
? $"v{EntryAssembly.GetName().Version.ToSemanticString()}"
|
||||
: null;
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
<Import Project="../CliFx.props" />
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>netstandard2.1;netstandard2.0;net45</TargetFrameworks>
|
||||
<TargetFrameworks>netstandard2.1;netstandard2.0</TargetFrameworks>
|
||||
<Authors>$(Company)</Authors>
|
||||
<Description>Declarative framework for CLI applications</Description>
|
||||
<PackageTags>command line executable interface framework parser arguments net core</PackageTags>
|
||||
@@ -10,32 +10,51 @@
|
||||
<PackageReleaseNotes>https://github.com/Tyrrrz/CliFx/blob/master/Changelog.md</PackageReleaseNotes>
|
||||
<PackageIcon>favicon.png</PackageIcon>
|
||||
<PackageLicenseExpression>MIT</PackageLicenseExpression>
|
||||
<PackageRequireLicenseAcceptance>True</PackageRequireLicenseAcceptance>
|
||||
<GenerateDocumentationFile>True</GenerateDocumentationFile>
|
||||
<PublishRepositoryUrl>True</PublishRepositoryUrl>
|
||||
<EmbedUntrackedSources>True</EmbedUntrackedSources>
|
||||
<IncludeSymbols>True</IncludeSymbols>
|
||||
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||
<PublishRepositoryUrl>true</PublishRepositoryUrl>
|
||||
<EmbedUntrackedSources>true</EmbedUntrackedSources>
|
||||
<IncludeSymbols>true</IncludeSymbols>
|
||||
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
|
||||
<TargetsForTfmSpecificContentInPackage>$(TargetsForTfmSpecificContentInPackage);CopyAnalyzerToPackage</TargetsForTfmSpecificContentInPackage>
|
||||
</PropertyGroup>
|
||||
|
||||
<!-- Disable nullability warnings on older frameworks because there is no nullability info for BCL -->
|
||||
<PropertyGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
|
||||
<Nullable>annotations</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
|
||||
<_Parameter1>$(AssemblyName).Tests</_Parameter1>
|
||||
</AssemblyAttribute>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Include="../favicon.png" Pack="True" PackagePath="" />
|
||||
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
|
||||
<_Parameter1>$(AssemblyName).Analyzers</_Parameter1>
|
||||
</AssemblyAttribute>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.0" PrivateAssets="all" />
|
||||
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="all" />
|
||||
<PackageReference Include="Nullable" Version="1.2.0" PrivateAssets="all" />
|
||||
<PackageReference Include="Nullable" Version="1.2.1" PrivateAssets="all" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0' or '$(TargetFramework)' == 'net45'">
|
||||
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
|
||||
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.5.3" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Include="../favicon.png" Pack="true" PackagePath="" />
|
||||
</ItemGroup>
|
||||
|
||||
<!-- The following item group and target ensure that the analyzer project is copied into the output NuGet package -->
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\CliFx.Analyzers\CliFx.Analyzers.csproj" ReferenceOutputAssembly="true" IncludeAssets="CliFx.Analyzers.dll" />
|
||||
</ItemGroup>
|
||||
|
||||
<Target Name="CopyAnalyzerToPackage">
|
||||
<ItemGroup>
|
||||
<TfmSpecificPackageFile Include="$(OutDir)/CliFx.Analyzers.dll" PackagePath="analyzers/dotnet/cs" BuildAction="none" />
|
||||
</ItemGroup>
|
||||
</Target>
|
||||
|
||||
</Project>
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user