mirror of
https://github.com/Tyrrrz/CliFx.git
synced 2025-10-25 15:19:17 +00:00
Compare commits
116 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
60a3b26fd1 | ||
|
|
3abdfb1acf | ||
|
|
9557d386e2 | ||
|
|
d0d024c427 | ||
|
|
f765af6061 | ||
|
|
7f2202e869 | ||
|
|
14ad9d5738 | ||
|
|
b120138de3 | ||
|
|
8df1d607c1 | ||
|
|
c06f2810b9 | ||
|
|
d52a205f13 | ||
|
|
0ec12e57c1 | ||
|
|
c322b7029c | ||
|
|
6a38c04c11 | ||
|
|
5e53107def | ||
|
|
36cea937de | ||
|
|
438d6b98ac | ||
|
|
8e1488c395 | ||
|
|
65d321b476 | ||
|
|
c6d2359d6b | ||
|
|
0d32876bad | ||
|
|
c063251d89 | ||
|
|
3831cfc7c0 | ||
|
|
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 | ||
|
|
4365ad457a | ||
|
|
fb3617980e | ||
|
|
7690aae456 | ||
|
|
076678a08c | ||
|
|
104279d6e9 | ||
|
|
515d51a91d | ||
|
|
4fdf543190 | ||
|
|
4e1ab096c9 | ||
|
|
8aa6911cca | ||
|
|
f0362019ed | ||
|
|
82895f2e42 | ||
|
|
4cf622abe5 | ||
|
|
d4e22a78d6 | ||
|
|
3883c831e9 | ||
|
|
63441688fe | ||
|
|
e48839b938 | ||
|
|
ed87373dc3 | ||
|
|
6ce52c70f7 | ||
|
|
d2b0b16121 | ||
|
|
d67a9fe762 | ||
|
|
ce2a3153e6 | ||
|
|
d4b54231fb | ||
|
|
70bfe0bf91 | ||
|
|
9690c380d3 | ||
|
|
85caa275ae | ||
|
|
32026e59c0 | ||
|
|
486ccb9685 | ||
|
|
7b766f70f3 | ||
|
|
f73e96488f | ||
|
|
af63fa5a1f | ||
|
|
e8f53c9463 | ||
|
|
9564cd5d30 | ||
|
|
ed458c3980 | ||
|
|
25538f99db | ||
|
|
36436e7a4b | ||
|
|
a6070332c9 | ||
|
|
25cbfdb4b8 | ||
|
|
d1b5107c2c | ||
|
|
03873d63cd | ||
|
|
89aba39964 | ||
|
|
ab57a103d1 | ||
|
|
d0b2ebc061 |
3
.github/FUNDING.yml
vendored
3
.github/FUNDING.yml
vendored
@@ -1,4 +1,3 @@
|
|||||||
github: Tyrrrz
|
github: Tyrrrz
|
||||||
patreon: Tyrrrz
|
patreon: Tyrrrz
|
||||||
open_collective: Tyrrrz
|
custom: ['buymeacoffee.com/Tyrrrz', 'tyrrrz.me/donate']
|
||||||
custom: ['buymeacoffee.com/Tyrrrz']
|
|
||||||
25
.github/workflows/CD.yml
vendored
Normal file
25
.github/workflows/CD.yml
vendored
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
name: CD
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
tags:
|
||||||
|
- '*'
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: Checkout
|
||||||
|
uses: actions/checkout@v2.3.3
|
||||||
|
|
||||||
|
- name: Install .NET Core
|
||||||
|
uses: actions/setup-dotnet@v1.7.2
|
||||||
|
with:
|
||||||
|
dotnet-version: 3.1.100
|
||||||
|
|
||||||
|
- name: Pack
|
||||||
|
run: dotnet pack CliFx --configuration Release
|
||||||
|
|
||||||
|
- name: Deploy
|
||||||
|
run: dotnet nuget push CliFx/bin/Release/*.nupkg -s nuget.org -k ${{ secrets.NUGET_TOKEN }}
|
||||||
35
.github/workflows/CI.yml
vendored
Normal file
35
.github/workflows/CI.yml
vendored
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
name: CI
|
||||||
|
|
||||||
|
on: [push, pull_request]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
runs-on: ${{ matrix.os }}
|
||||||
|
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
os: [ubuntu-latest, windows-latest, macos-latest]
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: Checkout
|
||||||
|
uses: actions/checkout@v2.3.3
|
||||||
|
|
||||||
|
- name: Install .NET Core
|
||||||
|
uses: actions/setup-dotnet@v1.7.2
|
||||||
|
with:
|
||||||
|
dotnet-version: 3.1.100
|
||||||
|
|
||||||
|
- name: Build & test
|
||||||
|
run: dotnet test --configuration Release --logger GitHubActions
|
||||||
|
|
||||||
|
- 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
|
||||||
1
.gitignore
vendored
1
.gitignore
vendored
@@ -143,6 +143,7 @@ _TeamCity*
|
|||||||
_NCrunch_*
|
_NCrunch_*
|
||||||
.*crunch*.local.xml
|
.*crunch*.local.xml
|
||||||
nCrunchTemp_*
|
nCrunchTemp_*
|
||||||
|
.ncrunchsolution
|
||||||
|
|
||||||
# MightyMoose
|
# MightyMoose
|
||||||
*.mm.*
|
*.mm.*
|
||||||
|
|||||||
BIN
.screenshots/help.png
Normal file
BIN
.screenshots/help.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 24 KiB |
45
Changelog.md
45
Changelog.md
@@ -0,0 +1,45 @@
|
|||||||
|
### v1.5 (23-Oct-2020)
|
||||||
|
|
||||||
|
- Added pretty-printing for unhandled exceptions thrown from within the application. This makes the errors easier to parse visually and should help in troubleshooting. This change does not affect `CommandException`, as it already has special treatment. (Thanks [@Mårten Åsberg](https://github.com/89netraM))
|
||||||
|
- Added support for custom value converters. You can now create a type that implements `CliFx.IArgumentValueConverter` and specify it as a converter for your parameters or options via the `Converter` named property. This should enable conversion between raw argument values and custom types which are not string-initializable. (Thanks [@Oleksandr Shustov](https://github.com/AlexandrShustov))
|
||||||
|
- Improved help text so that it also shows minimal usage examples for child and descendant commands, besides the actual command it was requested on. This should improve user experience for applications with many nested commands. (Thanks [@Nikiforov Alexey](https://github.com/NikiforovAll))
|
||||||
|
|
||||||
|
### v1.4 (20-Aug-2020)
|
||||||
|
|
||||||
|
- Added `VirtualConsole.CreateBuffered()` method to simplify test setup when using in-memory backing stores for output and error streams. Please refer to the readme for updated recommendations on how to test applications built with CliFx.
|
||||||
|
- Added generic `CliApplicationBuilder.AddCommand<TCommand>()`. This overload simplifies adding commands one-by-one as it also checks that the type implements `ICommand`.
|
||||||
|
|
||||||
|
### 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.1" />
|
||||||
|
<PackageReference Include="GitHubActionsTestLogger" Version="1.1.1" />
|
||||||
|
<PackageReference Include="FluentAssertions" Version="5.10.3" />
|
||||||
|
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
|
||||||
|
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.4.0" />
|
||||||
|
<PackageReference Include="xunit" Version="2.4.0" />
|
||||||
|
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" PrivateAssets="all" />
|
||||||
|
<PackageReference Include="coverlet.msbuild" Version="2.9.0" PrivateAssets="all" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\CliFx.Analyzers\CliFx.Analyzers.csproj" />
|
||||||
|
<ProjectReference Include="..\CliFx\CliFx.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
585
CliFx.Analyzers.Tests/CommandSchemaAnalyzerTests.cs
Normal file
585
CliFx.Analyzers.Tests/CommandSchemaAnalyzerTests.cs
Normal file
@@ -0,0 +1,585 @@
|
|||||||
|
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(
|
||||||
|
"Parameter with valid converter",
|
||||||
|
Analyzer.SupportedDiagnostics,
|
||||||
|
|
||||||
|
// language=cs
|
||||||
|
@"
|
||||||
|
public class MyConverter : IArgumentValueConverter
|
||||||
|
{
|
||||||
|
public object ConvertFrom(string value) => value;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command]
|
||||||
|
public class MyCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandParameter(0, Converter = typeof(MyConverter))]
|
||||||
|
public string Param { 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 Option { 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 Option { 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 OptionA { get; set; }
|
||||||
|
|
||||||
|
[CommandOption(""bar"")]
|
||||||
|
public string OptionB { 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 OptionA { get; set; }
|
||||||
|
|
||||||
|
[CommandOption('x')]
|
||||||
|
public string OptionB { 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 OptionA { get; set; }
|
||||||
|
|
||||||
|
[CommandOption('b', EnvironmentVariableName = ""env_var_b"")]
|
||||||
|
public string OptionB { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}"
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
yield return new object[]
|
||||||
|
{
|
||||||
|
new AnalyzerTestCase(
|
||||||
|
"Option with valid converter",
|
||||||
|
Analyzer.SupportedDiagnostics,
|
||||||
|
|
||||||
|
// language=cs
|
||||||
|
@"
|
||||||
|
public class MyConverter : IArgumentValueConverter
|
||||||
|
{
|
||||||
|
public object ConvertFrom(string value) => value;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command]
|
||||||
|
public class MyCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandOption('o', Converter = typeof(MyConverter))]
|
||||||
|
public string Option { 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(
|
||||||
|
"Parameter with invalid converter",
|
||||||
|
DiagnosticDescriptors.CliFx0025,
|
||||||
|
|
||||||
|
// language=cs
|
||||||
|
@"
|
||||||
|
public class MyConverter
|
||||||
|
{
|
||||||
|
public object ConvertFrom(string value) => value;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command]
|
||||||
|
public class MyCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandParameter(0, Converter = typeof(MyConverter))]
|
||||||
|
public string Param { 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 Option { 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 Option { 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 OptionA { get; set; }
|
||||||
|
|
||||||
|
[CommandOption(""foo"")]
|
||||||
|
public string OptionB { 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 OptionA { get; set; }
|
||||||
|
|
||||||
|
[CommandOption('f')]
|
||||||
|
public string OptionB { 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 OptionA { get; set; }
|
||||||
|
|
||||||
|
[CommandOption('b', EnvironmentVariableName = ""env_var"")]
|
||||||
|
public string OptionB { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}"
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
yield return new object[]
|
||||||
|
{
|
||||||
|
new AnalyzerTestCase(
|
||||||
|
"Option with invalid converter",
|
||||||
|
DiagnosticDescriptors.CliFx0046,
|
||||||
|
|
||||||
|
// language=cs
|
||||||
|
@"
|
||||||
|
public class MyConverter
|
||||||
|
{
|
||||||
|
public object ConvertFrom(string value) => value;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command]
|
||||||
|
public class MyCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandOption('o', Converter = typeof(MyConverter))]
|
||||||
|
public string Option { 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>
|
||||||
350
CliFx.Analyzers/CommandSchemaAnalyzer.cs
Normal file
350
CliFx.Analyzers/CommandSchemaAnalyzer.cs
Normal file
@@ -0,0 +1,350 @@
|
|||||||
|
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.CliFx0025,
|
||||||
|
DiagnosticDescriptors.CliFx0041,
|
||||||
|
DiagnosticDescriptors.CliFx0042,
|
||||||
|
DiagnosticDescriptors.CliFx0043,
|
||||||
|
DiagnosticDescriptors.CliFx0044,
|
||||||
|
DiagnosticDescriptors.CliFx0045,
|
||||||
|
DiagnosticDescriptors.CliFx0046
|
||||||
|
);
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
var converter = attribute
|
||||||
|
.NamedArguments
|
||||||
|
.Where(a => a.Key == "Converter")
|
||||||
|
.Select(a => a.Value.Value)
|
||||||
|
.FirstOrDefault() as ITypeSymbol;
|
||||||
|
|
||||||
|
return new
|
||||||
|
{
|
||||||
|
Property = p,
|
||||||
|
Order = order,
|
||||||
|
Name = name,
|
||||||
|
Converter = converter
|
||||||
|
};
|
||||||
|
})
|
||||||
|
.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()
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Invalid converter
|
||||||
|
var invalidConverterParameters = parameters
|
||||||
|
.Where(p =>
|
||||||
|
p.Converter != null &&
|
||||||
|
!p.Converter.AllInterfaces.Any(KnownSymbols.IsArgumentValueConverterInterface))
|
||||||
|
.ToArray();
|
||||||
|
|
||||||
|
foreach (var parameter in invalidConverterParameters)
|
||||||
|
{
|
||||||
|
context.ReportDiagnostic(Diagnostic.Create(
|
||||||
|
DiagnosticDescriptors.CliFx0025, parameter.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;
|
||||||
|
|
||||||
|
var converter = attribute
|
||||||
|
.NamedArguments
|
||||||
|
.Where(a => a.Key == "Converter")
|
||||||
|
.Select(a => a.Value.Value)
|
||||||
|
.FirstOrDefault() as ITypeSymbol;
|
||||||
|
|
||||||
|
return new
|
||||||
|
{
|
||||||
|
Property = p,
|
||||||
|
Name = name,
|
||||||
|
ShortName = shortName,
|
||||||
|
EnvironmentVariableName = envVarName,
|
||||||
|
Converter = converter
|
||||||
|
};
|
||||||
|
})
|
||||||
|
.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()
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Invalid converter
|
||||||
|
var invalidConverterOptions = options
|
||||||
|
.Where(o =>
|
||||||
|
o.Converter != null &&
|
||||||
|
!o.Converter.AllInterfaces.Any(KnownSymbols.IsArgumentValueConverterInterface))
|
||||||
|
.ToArray();
|
||||||
|
|
||||||
|
foreach (var option in invalidConverterOptions)
|
||||||
|
{
|
||||||
|
context.ReportDiagnostic(Diagnostic.Create(
|
||||||
|
DiagnosticDescriptors.CliFx0046, option.Property.Locations.First()
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void CheckCommandType(SymbolAnalysisContext context)
|
||||||
|
{
|
||||||
|
// Named type: MyCommand
|
||||||
|
if (!(context.Symbol is INamedTypeSymbol namedTypeSymbol) ||
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
74
CliFx.Analyzers/ConsoleUsageAnalyzer.cs
Normal file
74
CliFx.Analyzers/ConsoleUsageAnalyzer.cs
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
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)
|
||||||
|
{
|
||||||
|
if (invocationSyntax.Expression is MemberAccessExpressionSyntax memberAccessSyntax &&
|
||||||
|
context.SemanticModel.GetSymbolInfo(memberAccessSyntax).Symbol is IMethodSymbol methodSymbol)
|
||||||
|
{
|
||||||
|
// Direct call to System.Console (e.g. System.Console.WriteLine())
|
||||||
|
if (KnownSymbols.IsSystemConsole(methodSymbol.ContainingType))
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Indirect call to System.Console (e.g. System.Console.Error.WriteLine())
|
||||||
|
if (memberAccessSyntax.Expression is MemberAccessExpressionSyntax parentMemberAccessSyntax &&
|
||||||
|
context.SemanticModel.GetSymbolInfo(parentMemberAccessSyntax).Symbol is IPropertySymbol propertySymbol)
|
||||||
|
{
|
||||||
|
return KnownSymbols.IsSystemConsole(propertySymbol.ContainingType);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void CheckSystemConsoleUsage(SyntaxNodeAnalysisContext context)
|
||||||
|
{
|
||||||
|
if (context.Node is InvocationExpressionSyntax invocationSyntax &&
|
||||||
|
IsSystemConsoleInvocation(context, invocationSyntax))
|
||||||
|
{
|
||||||
|
// Check if IConsole is available in scope as alternative to System.Console
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
105
CliFx.Analyzers/DiagnosticDescriptors.cs
Normal file
105
CliFx.Analyzers/DiagnosticDescriptors.cs
Normal file
@@ -0,0 +1,105 @@
|
|||||||
|
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 CliFx0025 =
|
||||||
|
new DiagnosticDescriptor(nameof(CliFx0025),
|
||||||
|
"Parameter converter must implement 'CliFx.IArgumentValueConverter'",
|
||||||
|
"Parameter converter must implement 'CliFx.IArgumentValueConverter'",
|
||||||
|
"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 CliFx0046 =
|
||||||
|
new DiagnosticDescriptor(nameof(CliFx0046),
|
||||||
|
"Option converter must implement 'CliFx.IArgumentValueConverter'",
|
||||||
|
"Option converter must implement 'CliFx.IArgumentValueConverter'",
|
||||||
|
"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);
|
||||||
|
}
|
||||||
|
}
|
||||||
40
CliFx.Analyzers/KnownSymbols.cs
Normal file
40
CliFx.Analyzers/KnownSymbols.cs
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
using CliFx.Analyzers.Internal;
|
||||||
|
using Microsoft.CodeAnalysis;
|
||||||
|
|
||||||
|
namespace CliFx.Analyzers
|
||||||
|
{
|
||||||
|
internal 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 IsArgumentValueConverterInterface(ISymbol symbol) =>
|
||||||
|
symbol.DisplayNameMatches("CliFx.IArgumentValueConverter");
|
||||||
|
|
||||||
|
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,34 +0,0 @@
|
|||||||
using System.Threading.Tasks;
|
|
||||||
using BenchmarkDotNet.Attributes;
|
|
||||||
using CliFx.Benchmarks.Commands;
|
|
||||||
|
|
||||||
namespace CliFx.Benchmarks
|
|
||||||
{
|
|
||||||
[CoreJob]
|
|
||||||
[RankColumn]
|
|
||||||
public class Benchmark
|
|
||||||
{
|
|
||||||
private static readonly string[] Arguments = { "--str", "hello world", "-i", "13", "-b" };
|
|
||||||
|
|
||||||
[Benchmark(Description = "CliFx", Baseline = true)]
|
|
||||||
public Task<int> ExecuteWithCliFx() => new CliApplicationBuilder().AddCommand(typeof(CliFxCommand)).Build().RunAsync(Arguments);
|
|
||||||
|
|
||||||
[Benchmark(Description = "System.CommandLine")]
|
|
||||||
public Task<int> ExecuteWithSystemCommandLine() => new SystemCommandLineCommand().ExecuteAsync(Arguments);
|
|
||||||
|
|
||||||
[Benchmark(Description = "McMaster.Extensions.CommandLineUtils")]
|
|
||||||
public int ExecuteWithMcMaster() => McMaster.Extensions.CommandLineUtils.CommandLineApplication.Execute<McMasterCommand>(Arguments);
|
|
||||||
|
|
||||||
// Skipped because this benchmark freezes after a couple of iterations
|
|
||||||
// Probably wasn't designed to run multiple times in single process execution
|
|
||||||
//[Benchmark(Description = "CommandLineParser")]
|
|
||||||
public void ExecuteWithCommandLineParser()
|
|
||||||
{
|
|
||||||
var parsed = CommandLine.Parser.Default.ParseArguments(Arguments, typeof(CommandLineParserCommand));
|
|
||||||
CommandLine.ParserResultExtensions.WithParsed<CommandLineParserCommand>(parsed, c => c.Execute());
|
|
||||||
}
|
|
||||||
|
|
||||||
[Benchmark(Description = "PowerArgs")]
|
|
||||||
public void ExecuteWithPowerArgs() => PowerArgs.Args.InvokeMain<PowerArgsCommand>(Arguments);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
52
CliFx.Benchmarks/Benchmarks.cs
Normal file
52
CliFx.Benchmarks/Benchmarks.cs
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
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;
|
||||||
|
|
||||||
|
namespace CliFx.Benchmarks
|
||||||
|
{
|
||||||
|
[SimpleJob]
|
||||||
|
[RankColumn]
|
||||||
|
[Orderer(SummaryOrderPolicy.FastestToSlowest)]
|
||||||
|
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<CliFxCommand>().Build().RunAsync(Arguments, new Dictionary<string, string>());
|
||||||
|
|
||||||
|
[Benchmark(Description = "System.CommandLine")]
|
||||||
|
public async Task<int> ExecuteWithSystemCommandLine() =>
|
||||||
|
await new SystemCommandLineCommand().ExecuteAsync(Arguments);
|
||||||
|
|
||||||
|
[Benchmark(Description = "McMaster.Extensions.CommandLineUtils")]
|
||||||
|
public int ExecuteWithMcMaster() =>
|
||||||
|
McMaster.Extensions.CommandLineUtils.CommandLineApplication.Execute<McMasterCommand>(Arguments);
|
||||||
|
|
||||||
|
[Benchmark(Description = "CommandLineParser")]
|
||||||
|
public void ExecuteWithCommandLineParser() =>
|
||||||
|
new Parser()
|
||||||
|
.ParseArguments(Arguments, typeof(CommandLineParserCommand))
|
||||||
|
.WithParsed<CommandLineParserCommand>(c => c.Execute());
|
||||||
|
|
||||||
|
[Benchmark(Description = "PowerArgs")]
|
||||||
|
public void ExecuteWithPowerArgs() =>
|
||||||
|
PowerArgs.Args.InvokeMain<PowerArgsCommand>(Arguments);
|
||||||
|
|
||||||
|
[Benchmark(Description = "Clipr")]
|
||||||
|
public void ExecuteWithClipr() =>
|
||||||
|
clipr.CliParser.Parse<CliprCommand>(Arguments).Execute();
|
||||||
|
|
||||||
|
[Benchmark(Description = "Cocona")]
|
||||||
|
public void ExecuteWithCocona() =>
|
||||||
|
Cocona.CoconaApp.Run<CoconaCommand>(Arguments);
|
||||||
|
|
||||||
|
public static void Main() =>
|
||||||
|
BenchmarkRunner.Run<Benchmarks>(DefaultConfig.Instance.With(ConfigOptions.DisableOptimizationsValidator));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,15 +1,17 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
<Import Project="../CliFx.props" />
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<OutputType>Exe</OutputType>
|
<OutputType>Exe</OutputType>
|
||||||
<TargetFramework>netcoreapp2.2</TargetFramework>
|
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||||
<LangVersion>latest</LangVersion>
|
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="BenchmarkDotNet" Version="0.11.5" />
|
<PackageReference Include="BenchmarkDotNet" Version="0.12.0" />
|
||||||
<PackageReference Include="CommandLineParser" Version="2.6.0" />
|
<PackageReference Include="clipr" Version="1.6.1" />
|
||||||
<PackageReference Include="McMaster.Extensions.CommandLineUtils" Version="2.3.4" />
|
<PackageReference Include="Cocona" Version="1.5.0" />
|
||||||
|
<PackageReference Include="CommandLineParser" Version="2.8.0" />
|
||||||
|
<PackageReference Include="McMaster.Extensions.CommandLineUtils" Version="3.0.0" />
|
||||||
<PackageReference Include="PowerArgs" Version="3.6.0" />
|
<PackageReference Include="PowerArgs" Version="3.6.0" />
|
||||||
<PackageReference Include="System.CommandLine.Experimental" Version="0.3.0-alpha.19317.1" />
|
<PackageReference Include="System.CommandLine.Experimental" Version="0.3.0-alpha.19317.1" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using CliFx.Attributes;
|
using CliFx.Attributes;
|
||||||
using CliFx.Services;
|
|
||||||
|
|
||||||
namespace CliFx.Benchmarks.Commands
|
namespace CliFx.Benchmarks.Commands
|
||||||
{
|
{
|
||||||
@@ -8,7 +7,7 @@ namespace CliFx.Benchmarks.Commands
|
|||||||
public class CliFxCommand : ICommand
|
public class CliFxCommand : ICommand
|
||||||
{
|
{
|
||||||
[CommandOption("str", 's')]
|
[CommandOption("str", 's')]
|
||||||
public string StrOption { get; set; }
|
public string? StrOption { get; set; }
|
||||||
|
|
||||||
[CommandOption("int", 'i')]
|
[CommandOption("int", 'i')]
|
||||||
public int IntOption { get; set; }
|
public int IntOption { get; set; }
|
||||||
@@ -16,6 +15,6 @@ namespace CliFx.Benchmarks.Commands
|
|||||||
[CommandOption("bool", 'b')]
|
[CommandOption("bool", 'b')]
|
||||||
public bool BoolOption { get; set; }
|
public bool BoolOption { get; set; }
|
||||||
|
|
||||||
public Task ExecuteAsync(IConsole console) => Task.CompletedTask;
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
20
CliFx.Benchmarks/Commands/CliprCommand.cs
Normal file
20
CliFx.Benchmarks/Commands/CliprCommand.cs
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
using clipr;
|
||||||
|
|
||||||
|
namespace CliFx.Benchmarks.Commands
|
||||||
|
{
|
||||||
|
public class CliprCommand
|
||||||
|
{
|
||||||
|
[NamedArgument('s', "str")]
|
||||||
|
public string? StrOption { get; set; }
|
||||||
|
|
||||||
|
[NamedArgument('i', "int")]
|
||||||
|
public int IntOption { get; set; }
|
||||||
|
|
||||||
|
[NamedArgument('b', "bool", Constraint = NumArgsConstraint.Optional, Const = true)]
|
||||||
|
public bool BoolOption { get; set; }
|
||||||
|
|
||||||
|
public void Execute()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
17
CliFx.Benchmarks/Commands/CoconaCommand.cs
Normal file
17
CliFx.Benchmarks/Commands/CoconaCommand.cs
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
using Cocona;
|
||||||
|
|
||||||
|
namespace CliFx.Benchmarks.Commands
|
||||||
|
{
|
||||||
|
public class CoconaCommand
|
||||||
|
{
|
||||||
|
public void Execute(
|
||||||
|
[Option("str", new []{'s'})]
|
||||||
|
string? strOption,
|
||||||
|
[Option("int", new []{'i'})]
|
||||||
|
int intOption,
|
||||||
|
[Option("bool", new []{'b'})]
|
||||||
|
bool boolOption)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,7 +5,7 @@ namespace CliFx.Benchmarks.Commands
|
|||||||
public class CommandLineParserCommand
|
public class CommandLineParserCommand
|
||||||
{
|
{
|
||||||
[Option('s', "str")]
|
[Option('s', "str")]
|
||||||
public string StrOption { get; set; }
|
public string? StrOption { get; set; }
|
||||||
|
|
||||||
[Option('i', "int")]
|
[Option('i', "int")]
|
||||||
public int IntOption { get; set; }
|
public int IntOption { get; set; }
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ namespace CliFx.Benchmarks.Commands
|
|||||||
public class McMasterCommand
|
public class McMasterCommand
|
||||||
{
|
{
|
||||||
[Option("--str|-s")]
|
[Option("--str|-s")]
|
||||||
public string StrOption { get; set; }
|
public string? StrOption { get; set; }
|
||||||
|
|
||||||
[Option("--int|-i")]
|
[Option("--int|-i")]
|
||||||
public int IntOption { get; set; }
|
public int IntOption { get; set; }
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ namespace CliFx.Benchmarks.Commands
|
|||||||
public class PowerArgsCommand
|
public class PowerArgsCommand
|
||||||
{
|
{
|
||||||
[ArgShortcut("--str"), ArgShortcut("-s")]
|
[ArgShortcut("--str"), ArgShortcut("-s")]
|
||||||
public string StrOption { get; set; }
|
public string? StrOption { get; set; }
|
||||||
|
|
||||||
[ArgShortcut("--int"), ArgShortcut("-i")]
|
[ArgShortcut("--int"), ArgShortcut("-i")]
|
||||||
public int IntOption { get; set; }
|
public int IntOption { get; set; }
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ namespace CliFx.Benchmarks.Commands
|
|||||||
{
|
{
|
||||||
new Option(new[] {"--str", "-s"})
|
new Option(new[] {"--str", "-s"})
|
||||||
{
|
{
|
||||||
Argument = new Argument<string>()
|
Argument = new Argument<string?>()
|
||||||
},
|
},
|
||||||
new Option(new[] {"--int", "-i"})
|
new Option(new[] {"--int", "-i"})
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -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));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,18 +1,19 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
<Import Project="../CliFx.props" />
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<OutputType>Exe</OutputType>
|
<OutputType>Exe</OutputType>
|
||||||
<TargetFramework>netcoreapp2.2</TargetFramework>
|
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||||
<LangVersion>latest</LangVersion>
|
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.2.0" />
|
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.8" />
|
||||||
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
|
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\CliFx\CliFx.csproj" />
|
<ProjectReference Include="..\CliFx\CliFx.csproj" />
|
||||||
|
<ProjectReference Include="..\CliFx.Analyzers\CliFx.Analyzers.csproj" ReferenceOutputAssembly="false" OutputItemType="Analyzer" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
@@ -5,7 +5,6 @@ using CliFx.Demo.Internal;
|
|||||||
using CliFx.Demo.Models;
|
using CliFx.Demo.Models;
|
||||||
using CliFx.Demo.Services;
|
using CliFx.Demo.Services;
|
||||||
using CliFx.Exceptions;
|
using CliFx.Exceptions;
|
||||||
using CliFx.Services;
|
|
||||||
|
|
||||||
namespace CliFx.Demo.Commands
|
namespace CliFx.Demo.Commands
|
||||||
{
|
{
|
||||||
@@ -14,31 +13,25 @@ namespace CliFx.Demo.Commands
|
|||||||
{
|
{
|
||||||
private readonly LibraryService _libraryService;
|
private readonly LibraryService _libraryService;
|
||||||
|
|
||||||
[CommandOption("title", 't', IsRequired = true, Description = "Book title.")]
|
[CommandParameter(0, Name = "title", Description = "Book title.")]
|
||||||
public string Title { get; set; }
|
public string Title { get; set; } = "";
|
||||||
|
|
||||||
[CommandOption("author", 'a', IsRequired = true, Description = "Book author.")]
|
[CommandOption("author", 'a', IsRequired = true, Description = "Book author.")]
|
||||||
public string Author { get; set; }
|
public string Author { get; set; } = "";
|
||||||
|
|
||||||
[CommandOption("published", 'p', Description = "Book publish date.")]
|
[CommandOption("published", 'p', Description = "Book publish date.")]
|
||||||
public DateTimeOffset Published { get; set; }
|
public DateTimeOffset Published { get; set; } = CreateRandomDate();
|
||||||
|
|
||||||
[CommandOption("isbn", 'n', Description = "Book ISBN.")]
|
[CommandOption("isbn", 'n', Description = "Book ISBN.")]
|
||||||
public Isbn Isbn { get; set; }
|
public Isbn Isbn { get; set; } = CreateRandomIsbn();
|
||||||
|
|
||||||
public BookAddCommand(LibraryService libraryService)
|
public BookAddCommand(LibraryService libraryService)
|
||||||
{
|
{
|
||||||
_libraryService = libraryService;
|
_libraryService = libraryService;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task ExecuteAsync(IConsole console)
|
public ValueTask ExecuteAsync(IConsole console)
|
||||||
{
|
{
|
||||||
// To make the demo simpler, we will just generate random publish date and ISBN if they were not set
|
|
||||||
if (Published == default)
|
|
||||||
Published = CreateRandomDate();
|
|
||||||
if (Isbn == default)
|
|
||||||
Isbn = CreateRandomIsbn();
|
|
||||||
|
|
||||||
if (_libraryService.GetBook(Title) != null)
|
if (_libraryService.GetBook(Title) != null)
|
||||||
throw new CommandException("Book already exists.", 1);
|
throw new CommandException("Book already exists.", 1);
|
||||||
|
|
||||||
@@ -48,7 +41,7 @@ namespace CliFx.Demo.Commands
|
|||||||
console.Output.WriteLine("Book added.");
|
console.Output.WriteLine("Book added.");
|
||||||
console.RenderBook(book);
|
console.RenderBook(book);
|
||||||
|
|
||||||
return Task.CompletedTask;
|
return default;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -65,7 +58,7 @@ namespace CliFx.Demo.Commands
|
|||||||
Random.Next(1, 59),
|
Random.Next(1, 59),
|
||||||
TimeSpan.Zero);
|
TimeSpan.Zero);
|
||||||
|
|
||||||
public static Isbn CreateRandomIsbn() => new Isbn(
|
private static Isbn CreateRandomIsbn() => new Isbn(
|
||||||
Random.Next(0, 999),
|
Random.Next(0, 999),
|
||||||
Random.Next(0, 99),
|
Random.Next(0, 99),
|
||||||
Random.Next(0, 99999),
|
Random.Next(0, 99999),
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ using CliFx.Attributes;
|
|||||||
using CliFx.Demo.Internal;
|
using CliFx.Demo.Internal;
|
||||||
using CliFx.Demo.Services;
|
using CliFx.Demo.Services;
|
||||||
using CliFx.Exceptions;
|
using CliFx.Exceptions;
|
||||||
using CliFx.Services;
|
|
||||||
|
|
||||||
namespace CliFx.Demo.Commands
|
namespace CliFx.Demo.Commands
|
||||||
{
|
{
|
||||||
@@ -12,15 +11,15 @@ namespace CliFx.Demo.Commands
|
|||||||
{
|
{
|
||||||
private readonly LibraryService _libraryService;
|
private readonly LibraryService _libraryService;
|
||||||
|
|
||||||
[CommandOption("title", 't', IsRequired = true, Description = "Book title.")]
|
[CommandParameter(0, Name = "title", Description = "Book title.")]
|
||||||
public string Title { get; set; }
|
public string Title { get; set; } = "";
|
||||||
|
|
||||||
public BookCommand(LibraryService libraryService)
|
public BookCommand(LibraryService libraryService)
|
||||||
{
|
{
|
||||||
_libraryService = libraryService;
|
_libraryService = libraryService;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task ExecuteAsync(IConsole console)
|
public ValueTask ExecuteAsync(IConsole console)
|
||||||
{
|
{
|
||||||
var book = _libraryService.GetBook(Title);
|
var book = _libraryService.GetBook(Title);
|
||||||
|
|
||||||
@@ -29,7 +28,7 @@ namespace CliFx.Demo.Commands
|
|||||||
|
|
||||||
console.RenderBook(book);
|
console.RenderBook(book);
|
||||||
|
|
||||||
return Task.CompletedTask;
|
return default;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2,7 +2,6 @@
|
|||||||
using CliFx.Attributes;
|
using CliFx.Attributes;
|
||||||
using CliFx.Demo.Internal;
|
using CliFx.Demo.Internal;
|
||||||
using CliFx.Demo.Services;
|
using CliFx.Demo.Services;
|
||||||
using CliFx.Services;
|
|
||||||
|
|
||||||
namespace CliFx.Demo.Commands
|
namespace CliFx.Demo.Commands
|
||||||
{
|
{
|
||||||
@@ -16,7 +15,7 @@ namespace CliFx.Demo.Commands
|
|||||||
_libraryService = libraryService;
|
_libraryService = libraryService;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task ExecuteAsync(IConsole console)
|
public ValueTask ExecuteAsync(IConsole console)
|
||||||
{
|
{
|
||||||
var library = _libraryService.GetLibrary();
|
var library = _libraryService.GetLibrary();
|
||||||
|
|
||||||
@@ -32,7 +31,7 @@ namespace CliFx.Demo.Commands
|
|||||||
console.RenderBook(book);
|
console.RenderBook(book);
|
||||||
}
|
}
|
||||||
|
|
||||||
return Task.CompletedTask;
|
return default;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2,7 +2,6 @@
|
|||||||
using CliFx.Attributes;
|
using CliFx.Attributes;
|
||||||
using CliFx.Demo.Services;
|
using CliFx.Demo.Services;
|
||||||
using CliFx.Exceptions;
|
using CliFx.Exceptions;
|
||||||
using CliFx.Services;
|
|
||||||
|
|
||||||
namespace CliFx.Demo.Commands
|
namespace CliFx.Demo.Commands
|
||||||
{
|
{
|
||||||
@@ -11,15 +10,15 @@ namespace CliFx.Demo.Commands
|
|||||||
{
|
{
|
||||||
private readonly LibraryService _libraryService;
|
private readonly LibraryService _libraryService;
|
||||||
|
|
||||||
[CommandOption("title", 't', IsRequired = true, Description = "Book title.")]
|
[CommandParameter(0, Name = "title", Description = "Book title.")]
|
||||||
public string Title { get; set; }
|
public string Title { get; set; } = "";
|
||||||
|
|
||||||
public BookRemoveCommand(LibraryService libraryService)
|
public BookRemoveCommand(LibraryService libraryService)
|
||||||
{
|
{
|
||||||
_libraryService = libraryService;
|
_libraryService = libraryService;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task ExecuteAsync(IConsole console)
|
public ValueTask ExecuteAsync(IConsole console)
|
||||||
{
|
{
|
||||||
var book = _libraryService.GetBook(Title);
|
var book = _libraryService.GetBook(Title);
|
||||||
|
|
||||||
@@ -30,7 +29,7 @@ namespace CliFx.Demo.Commands
|
|||||||
|
|
||||||
console.Output.WriteLine($"Book {Title} removed.");
|
console.Output.WriteLine($"Book {Title} removed.");
|
||||||
|
|
||||||
return Task.CompletedTask;
|
return default;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
using CliFx.Demo.Models;
|
using CliFx.Demo.Models;
|
||||||
using CliFx.Services;
|
|
||||||
|
|
||||||
namespace CliFx.Demo.Internal
|
namespace CliFx.Demo.Internal
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Globalization;
|
|
||||||
|
|
||||||
namespace CliFx.Demo.Models
|
namespace CliFx.Demo.Models
|
||||||
{
|
{
|
||||||
@@ -24,21 +23,23 @@ namespace CliFx.Demo.Models
|
|||||||
CheckDigit = checkDigit;
|
CheckDigit = checkDigit;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override string ToString() => $"{EanPrefix:000}-{RegistrationGroup:00}-{Registrant:00000}-{Publication:00}-{CheckDigit:0}";
|
public override string ToString() =>
|
||||||
|
$"{EanPrefix:000}-{RegistrationGroup:00}-{Registrant:00000}-{Publication:00}-{CheckDigit:0}";
|
||||||
}
|
}
|
||||||
|
|
||||||
public partial class Isbn
|
public partial class Isbn
|
||||||
{
|
{
|
||||||
public static Isbn Parse(string value)
|
public static Isbn Parse(string value, IFormatProvider formatProvider)
|
||||||
{
|
{
|
||||||
var components = value.Split('-', 5, StringSplitOptions.RemoveEmptyEntries);
|
var components = value.Split('-', 5, StringSplitOptions.RemoveEmptyEntries);
|
||||||
|
|
||||||
return new Isbn(
|
return new Isbn(
|
||||||
int.Parse(components[0], CultureInfo.InvariantCulture),
|
int.Parse(components[0], formatProvider),
|
||||||
int.Parse(components[1], CultureInfo.InvariantCulture),
|
int.Parse(components[1], formatProvider),
|
||||||
int.Parse(components[2], CultureInfo.InvariantCulture),
|
int.Parse(components[2], formatProvider),
|
||||||
int.Parse(components[3], CultureInfo.InvariantCulture),
|
int.Parse(components[3], formatProvider),
|
||||||
int.Parse(components[4], CultureInfo.InvariantCulture));
|
int.Parse(components[4], formatProvider)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
using System.Threading.Tasks;
|
using System;
|
||||||
|
using System.Threading.Tasks;
|
||||||
using CliFx.Demo.Commands;
|
using CliFx.Demo.Commands;
|
||||||
using CliFx.Demo.Services;
|
using CliFx.Demo.Services;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
@@ -7,7 +8,7 @@ namespace CliFx.Demo
|
|||||||
{
|
{
|
||||||
public static class Program
|
public static class Program
|
||||||
{
|
{
|
||||||
public static Task<int> Main(string[] args)
|
private static IServiceProvider GetServiceProvider()
|
||||||
{
|
{
|
||||||
// We use Microsoft.Extensions.DependencyInjection for injecting dependencies in commands
|
// We use Microsoft.Extensions.DependencyInjection for injecting dependencies in commands
|
||||||
var services = new ServiceCollection();
|
var services = new ServiceCollection();
|
||||||
@@ -21,13 +22,14 @@ namespace CliFx.Demo
|
|||||||
services.AddTransient<BookRemoveCommand>();
|
services.AddTransient<BookRemoveCommand>();
|
||||||
services.AddTransient<BookListCommand>();
|
services.AddTransient<BookListCommand>();
|
||||||
|
|
||||||
var serviceProvider = services.BuildServiceProvider();
|
return services.BuildServiceProvider();
|
||||||
|
}
|
||||||
|
|
||||||
return new CliApplicationBuilder()
|
public static async Task<int> Main() =>
|
||||||
|
await new CliApplicationBuilder()
|
||||||
.AddCommandsFromThisAssembly()
|
.AddCommandsFromThisAssembly()
|
||||||
.UseCommandFactory(schema => (ICommand) serviceProvider.GetRequiredService(schema.Type))
|
.UseTypeActivator(GetServiceProvider().GetService)
|
||||||
.Build()
|
.Build()
|
||||||
.RunAsync(args);
|
.RunAsync();
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2,6 +2,6 @@
|
|||||||
|
|
||||||
Sample command line interface for managing a library of books.
|
Sample command line interface for managing a library of books.
|
||||||
|
|
||||||
This demo project shows basic CliFx functionality such as command routing, option parsing, autogenerated help text, and some other things.
|
This demo project shows basic CliFx functionality such as command routing, argument parsing, autogenerated help text, and some other things.
|
||||||
|
|
||||||
You can get a list of available commands by running `CliFx.Demo --help`.
|
You can get a list of available commands by running `CliFx.Demo --help`.
|
||||||
@@ -25,7 +25,7 @@ namespace CliFx.Demo.Services
|
|||||||
return JsonConvert.DeserializeObject<Library>(data);
|
return JsonConvert.DeserializeObject<Library>(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Book GetBook(string title) => GetLibrary().Books.FirstOrDefault(b => b.Title == title);
|
public Book? GetBook(string title) => GetLibrary().Books.FirstOrDefault(b => b.Title == title);
|
||||||
|
|
||||||
public void AddBook(Book book)
|
public void AddBook(Book book)
|
||||||
{
|
{
|
||||||
|
|||||||
14
CliFx.Tests.Dummy/CliFx.Tests.Dummy.csproj
Normal file
14
CliFx.Tests.Dummy/CliFx.Tests.Dummy.csproj
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
<Import Project="../CliFx.props" />
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
19
CliFx.Tests.Dummy/Commands/HelloWorldCommand.cs
Normal file
19
CliFx.Tests.Dummy/Commands/HelloWorldCommand.cs
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
using System.Threading.Tasks;
|
||||||
|
using CliFx.Attributes;
|
||||||
|
|
||||||
|
namespace CliFx.Tests.Dummy.Commands
|
||||||
|
{
|
||||||
|
[Command]
|
||||||
|
public class HelloWorldCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandOption("target", EnvironmentVariableName = "ENV_TARGET")]
|
||||||
|
public string Target { get; set; } = "World";
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console)
|
||||||
|
{
|
||||||
|
console.Output.WriteLine($"Hello {Target}!");
|
||||||
|
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
21
CliFx.Tests.Dummy/Program.cs
Normal file
21
CliFx.Tests.Dummy/Program.cs
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
using System.Reflection;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace CliFx.Tests.Dummy
|
||||||
|
{
|
||||||
|
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()
|
||||||
|
.AddCommandsFromThisAssembly()
|
||||||
|
.Build()
|
||||||
|
.RunAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
415
CliFx.Tests/ApplicationSpecs.cs
Normal file
415
CliFx.Tests/ApplicationSpecs.cs
Normal file
@@ -0,0 +1,415 @@
|
|||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using CliFx.Tests.Commands;
|
||||||
|
using CliFx.Tests.Commands.Invalid;
|
||||||
|
using FluentAssertions;
|
||||||
|
using Xunit;
|
||||||
|
using Xunit.Abstractions;
|
||||||
|
|
||||||
|
namespace CliFx.Tests
|
||||||
|
{
|
||||||
|
public 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<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 async Task At_least_one_command_must_be_defined_in_an_application()
|
||||||
|
{
|
||||||
|
var (console, _, stdErr) = VirtualConsole.CreateBuffered();
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var exitCode = await application.RunAsync(Array.Empty<string>());
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
exitCode.Should().NotBe(0);
|
||||||
|
stdErr.GetString().Should().NotBeNullOrWhiteSpace();
|
||||||
|
|
||||||
|
_output.WriteLine(stdErr.GetString());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Commands_must_implement_the_corresponding_interface()
|
||||||
|
{
|
||||||
|
var (console, _, stdErr) = VirtualConsole.CreateBuffered();
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand(typeof(NonImplementedCommand))
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var exitCode = await application.RunAsync(Array.Empty<string>());
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
exitCode.Should().NotBe(0);
|
||||||
|
stdErr.GetString().Should().NotBeNullOrWhiteSpace();
|
||||||
|
|
||||||
|
_output.WriteLine(stdErr.GetString());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Commands_must_be_annotated_by_an_attribute()
|
||||||
|
{
|
||||||
|
var (console, _, stdErr) = VirtualConsole.CreateBuffered();
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand<NonAnnotatedCommand>()
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var exitCode = await application.RunAsync(Array.Empty<string>());
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
exitCode.Should().NotBe(0);
|
||||||
|
stdErr.GetString().Should().NotBeNullOrWhiteSpace();
|
||||||
|
|
||||||
|
_output.WriteLine(stdErr.GetString());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Commands_must_have_unique_names()
|
||||||
|
{
|
||||||
|
var (console, _, stdErr) = VirtualConsole.CreateBuffered();
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand<GenericExceptionCommand>()
|
||||||
|
.AddCommand<CommandExceptionCommand>()
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var exitCode = await application.RunAsync(Array.Empty<string>());
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
exitCode.Should().NotBe(0);
|
||||||
|
stdErr.GetString().Should().NotBeNullOrWhiteSpace();
|
||||||
|
|
||||||
|
_output.WriteLine(stdErr.GetString());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Command_can_be_default_but_only_if_it_is_the_only_such_command()
|
||||||
|
{
|
||||||
|
var (console, _, stdErr) = VirtualConsole.CreateBuffered();
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand<DefaultCommand>()
|
||||||
|
.AddCommand<OtherDefaultCommand>()
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var exitCode = await application.RunAsync(Array.Empty<string>());
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
exitCode.Should().NotBe(0);
|
||||||
|
stdErr.GetString().Should().NotBeNullOrWhiteSpace();
|
||||||
|
|
||||||
|
_output.WriteLine(stdErr.GetString());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Command_parameters_must_have_unique_order()
|
||||||
|
{
|
||||||
|
var (console, _, stdErr) = VirtualConsole.CreateBuffered();
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand<DuplicateParameterOrderCommand>()
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var exitCode = await application.RunAsync(Array.Empty<string>());
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
exitCode.Should().NotBe(0);
|
||||||
|
stdErr.GetString().Should().NotBeNullOrWhiteSpace();
|
||||||
|
|
||||||
|
_output.WriteLine(stdErr.GetString());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Command_parameters_must_have_unique_names()
|
||||||
|
{
|
||||||
|
var (console, _, stdErr) = VirtualConsole.CreateBuffered();
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand<DuplicateParameterNameCommand>()
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var exitCode = await application.RunAsync(Array.Empty<string>());
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
exitCode.Should().NotBe(0);
|
||||||
|
stdErr.GetString().Should().NotBeNullOrWhiteSpace();
|
||||||
|
|
||||||
|
_output.WriteLine(stdErr.GetString());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Command_parameter_can_be_non_scalar_only_if_no_other_such_parameter_is_present()
|
||||||
|
{
|
||||||
|
var (console, _, stdErr) = VirtualConsole.CreateBuffered();
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand<MultipleNonScalarParametersCommand>()
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var exitCode = await application.RunAsync(Array.Empty<string>());
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
exitCode.Should().NotBe(0);
|
||||||
|
stdErr.GetString().Should().NotBeNullOrWhiteSpace();
|
||||||
|
|
||||||
|
_output.WriteLine(stdErr.GetString());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Command_parameter_can_be_non_scalar_only_if_it_is_the_last_in_order()
|
||||||
|
{
|
||||||
|
var (console, _, stdErr) = VirtualConsole.CreateBuffered();
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand<NonLastNonScalarParameterCommand>()
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var exitCode = await application.RunAsync(Array.Empty<string>());
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
exitCode.Should().NotBe(0);
|
||||||
|
stdErr.GetString().Should().NotBeNullOrWhiteSpace();
|
||||||
|
|
||||||
|
_output.WriteLine(stdErr.GetString());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Command_parameter_custom_converter_must_implement_the_corresponding_interface()
|
||||||
|
{
|
||||||
|
var (console, _, stdErr) = VirtualConsole.CreateBuffered();
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand<InvalidCustomConverterParameterCommand>()
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var exitCode = await application.RunAsync(Array.Empty<string>());
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
exitCode.Should().NotBe(0);
|
||||||
|
stdErr.GetString().Should().NotBeNullOrWhiteSpace();
|
||||||
|
|
||||||
|
_output.WriteLine(stdErr.GetString());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Command_options_must_have_names_that_are_not_empty()
|
||||||
|
{
|
||||||
|
var (console, _, stdErr) = VirtualConsole.CreateBuffered();
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand<EmptyOptionNameCommand>()
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var exitCode = await application.RunAsync(Array.Empty<string>());
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
exitCode.Should().NotBe(0);
|
||||||
|
stdErr.GetString().Should().NotBeNullOrWhiteSpace();
|
||||||
|
|
||||||
|
_output.WriteLine(stdErr.GetString());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Command_options_must_have_names_that_are_longer_than_one_character()
|
||||||
|
{
|
||||||
|
var (console, _, stdErr) = VirtualConsole.CreateBuffered();
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand<SingleCharacterOptionNameCommand>()
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var exitCode = await application.RunAsync(Array.Empty<string>());
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
exitCode.Should().NotBe(0);
|
||||||
|
stdErr.GetString().Should().NotBeNullOrWhiteSpace();
|
||||||
|
|
||||||
|
_output.WriteLine(stdErr.GetString());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Command_options_must_have_unique_names()
|
||||||
|
{
|
||||||
|
var (console, _, stdErr) = VirtualConsole.CreateBuffered();
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand<DuplicateOptionNamesCommand>()
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var exitCode = await application.RunAsync(Array.Empty<string>());
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
exitCode.Should().NotBe(0);
|
||||||
|
stdErr.GetString().Should().NotBeNullOrWhiteSpace();
|
||||||
|
|
||||||
|
_output.WriteLine(stdErr.GetString());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Command_options_must_have_unique_short_names()
|
||||||
|
{
|
||||||
|
var (console, _, stdErr) = VirtualConsole.CreateBuffered();
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand<DuplicateOptionShortNamesCommand>()
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var exitCode = await application.RunAsync(Array.Empty<string>());
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
exitCode.Should().NotBe(0);
|
||||||
|
stdErr.GetString().Should().NotBeNullOrWhiteSpace();
|
||||||
|
|
||||||
|
_output.WriteLine(stdErr.GetString());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Command_options_must_have_unique_environment_variable_names()
|
||||||
|
{
|
||||||
|
var (console, _, stdErr) = VirtualConsole.CreateBuffered();
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand<DuplicateOptionEnvironmentVariableNamesCommand>()
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var exitCode = await application.RunAsync(Array.Empty<string>());
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
exitCode.Should().NotBe(0);
|
||||||
|
stdErr.GetString().Should().NotBeNullOrWhiteSpace();
|
||||||
|
|
||||||
|
_output.WriteLine(stdErr.GetString());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Command_options_must_not_have_conflicts_with_the_implicit_help_option()
|
||||||
|
{
|
||||||
|
var (console, _, stdErr) = VirtualConsole.CreateBuffered();
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand<ConflictWithHelpOptionCommand>()
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var exitCode = await application.RunAsync(Array.Empty<string>());
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
exitCode.Should().NotBe(0);
|
||||||
|
stdErr.GetString().Should().NotBeNullOrWhiteSpace();
|
||||||
|
|
||||||
|
_output.WriteLine(stdErr.GetString());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Command_options_must_not_have_conflicts_with_the_implicit_version_option()
|
||||||
|
{
|
||||||
|
var (console, _, stdErr) = VirtualConsole.CreateBuffered();
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand<ConflictWithVersionOptionCommand>()
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var exitCode = await application.RunAsync(Array.Empty<string>());
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
exitCode.Should().NotBe(0);
|
||||||
|
stdErr.GetString().Should().NotBeNullOrWhiteSpace();
|
||||||
|
|
||||||
|
_output.WriteLine(stdErr.GetString());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Command_option_custom_converter_must_implement_the_corresponding_interface()
|
||||||
|
{
|
||||||
|
var (console, _, stdErr) = VirtualConsole.CreateBuffered();
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand<InvalidCustomConverterOptionCommand>()
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var exitCode = await application.RunAsync(Array.Empty<string>());
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
exitCode.Should().NotBe(0);
|
||||||
|
stdErr.GetString().Should().NotBeNullOrWhiteSpace();
|
||||||
|
|
||||||
|
_output.WriteLine(stdErr.GetString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
242
CliFx.Tests/ArgumentBindingSpecs.cs
Normal file
242
CliFx.Tests/ArgumentBindingSpecs.cs
Normal file
@@ -0,0 +1,242 @@
|
|||||||
|
using System.Threading.Tasks;
|
||||||
|
using CliFx.Tests.Commands;
|
||||||
|
using CliFx.Tests.Internal;
|
||||||
|
using FluentAssertions;
|
||||||
|
using Xunit;
|
||||||
|
using Xunit.Abstractions;
|
||||||
|
|
||||||
|
namespace CliFx.Tests
|
||||||
|
{
|
||||||
|
public class ArgumentBindingSpecs
|
||||||
|
{
|
||||||
|
private readonly ITestOutputHelper _output;
|
||||||
|
|
||||||
|
public ArgumentBindingSpecs(ITestOutputHelper output) => _output = output;
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Property_annotated_as_an_option_can_be_bound_from_multiple_values_even_if_the_inputs_use_mixed_naming()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var (console, stdOut, _) = VirtualConsole.CreateBuffered();
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand<WithStringArrayOptionCommand>()
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var exitCode = await application.RunAsync(new[]
|
||||||
|
{
|
||||||
|
"cmd", "--opt", "foo", "-o", "bar", "--opt", "baz"
|
||||||
|
});
|
||||||
|
|
||||||
|
var commandInstance = stdOut.GetString().DeserializeJson<WithStringArrayOptionCommand>();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
exitCode.Should().Be(0);
|
||||||
|
|
||||||
|
commandInstance.Should().BeEquivalentTo(new WithStringArrayOptionCommand
|
||||||
|
{
|
||||||
|
Opt = new[] {"foo", "bar", "baz"}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Property_annotated_as_a_required_option_must_always_be_set()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var (console, _, stdErr) = VirtualConsole.CreateBuffered();
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand<WithSingleRequiredOptionCommand>()
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var exitCode = await application.RunAsync(new[]
|
||||||
|
{
|
||||||
|
"cmd", "--opt-a", "foo"
|
||||||
|
});
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
exitCode.Should().NotBe(0);
|
||||||
|
stdErr.GetString().Should().NotBeNullOrWhiteSpace();
|
||||||
|
|
||||||
|
_output.WriteLine(stdErr.GetString());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Property_annotated_as_a_required_option_must_always_be_bound_to_some_value()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var (console, _, stdErr) = VirtualConsole.CreateBuffered();
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand<WithSingleRequiredOptionCommand>()
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var exitCode = await application.RunAsync(new[]
|
||||||
|
{
|
||||||
|
"cmd", "--opt-a"
|
||||||
|
});
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
exitCode.Should().NotBe(0);
|
||||||
|
stdErr.GetString().Should().NotBeNullOrWhiteSpace();
|
||||||
|
|
||||||
|
_output.WriteLine(stdErr.GetString());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Property_annotated_as_a_required_option_must_always_be_bound_to_at_least_one_value_if_it_expects_multiple_values()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var (console, _, stdErr) = VirtualConsole.CreateBuffered();
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand<WithRequiredOptionsCommand>()
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var exitCode = await application.RunAsync(new[]
|
||||||
|
{
|
||||||
|
"cmd", "--opt-a", "foo"
|
||||||
|
});
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
exitCode.Should().NotBe(0);
|
||||||
|
stdErr.GetString().Should().NotBeNullOrWhiteSpace();
|
||||||
|
|
||||||
|
_output.WriteLine(stdErr.GetString());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Property_annotated_as_parameter_is_bound_directly_from_argument_value_according_to_the_order()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var (console, stdOut, _) = VirtualConsole.CreateBuffered();
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand<WithParametersCommand>()
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var exitCode = await application.RunAsync(new[]
|
||||||
|
{
|
||||||
|
"cmd", "foo", "13", "bar", "baz"
|
||||||
|
});
|
||||||
|
|
||||||
|
var commandInstance = stdOut.GetString().DeserializeJson<WithParametersCommand>();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
exitCode.Should().Be(0);
|
||||||
|
|
||||||
|
commandInstance.Should().BeEquivalentTo(new WithParametersCommand
|
||||||
|
{
|
||||||
|
ParamA = "foo",
|
||||||
|
ParamB = 13,
|
||||||
|
ParamC = new[] {"bar", "baz"}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Property_annotated_as_parameter_must_always_be_bound_to_some_value()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var (console, _, stdErr) = VirtualConsole.CreateBuffered();
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand<WithSingleParameterCommand>()
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var exitCode = await application.RunAsync(new[]
|
||||||
|
{
|
||||||
|
"cmd"
|
||||||
|
});
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
exitCode.Should().NotBe(0);
|
||||||
|
stdErr.GetString().Should().NotBeNullOrWhiteSpace();
|
||||||
|
|
||||||
|
_output.WriteLine(stdErr.GetString());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Property_annotated_as_parameter_must_always_be_bound_to_at_least_one_value_if_it_expects_multiple_values()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var (console, _, stdErr) = VirtualConsole.CreateBuffered();
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand<WithParametersCommand>()
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var exitCode = await application.RunAsync(new[]
|
||||||
|
{
|
||||||
|
"cmd", "foo", "13"
|
||||||
|
});
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
exitCode.Should().NotBe(0);
|
||||||
|
stdErr.GetString().Should().NotBeNullOrWhiteSpace();
|
||||||
|
|
||||||
|
_output.WriteLine(stdErr.GetString());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task All_provided_option_arguments_must_be_bound_to_corresponding_properties()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var (console, _, stdErr) = VirtualConsole.CreateBuffered();
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand<SupportedArgumentTypesCommand>()
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var exitCode = await application.RunAsync(new[]
|
||||||
|
{
|
||||||
|
"cmd", "--non-existing-option", "13"
|
||||||
|
});
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
exitCode.Should().NotBe(0);
|
||||||
|
stdErr.GetString().Should().NotBeNullOrWhiteSpace();
|
||||||
|
|
||||||
|
_output.WriteLine(stdErr.GetString());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task All_provided_parameter_arguments_must_be_bound_to_corresponding_properties()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var (console, _, stdErr) = VirtualConsole.CreateBuffered();
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand<SupportedArgumentTypesCommand>()
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var exitCode = await application.RunAsync(new[]
|
||||||
|
{
|
||||||
|
"cnd", "non-existing-parameter"
|
||||||
|
});
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
exitCode.Should().NotBe(0);
|
||||||
|
stdErr.GetString().Should().NotBeNullOrWhiteSpace();
|
||||||
|
|
||||||
|
_output.WriteLine(stdErr.GetString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
1441
CliFx.Tests/ArgumentConversionSpecs.cs
Normal file
1441
CliFx.Tests/ArgumentConversionSpecs.cs
Normal file
File diff suppressed because it is too large
Load Diff
36
CliFx.Tests/CancellationSpecs.cs
Normal file
36
CliFx.Tests/CancellationSpecs.cs
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
using System;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using CliFx.Tests.Commands;
|
||||||
|
using FluentAssertions;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace CliFx.Tests
|
||||||
|
{
|
||||||
|
public 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();
|
||||||
|
var (console, stdOut, _) = VirtualConsole.CreateBuffered(cts.Token);
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand<CancellableCommand>()
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
cts.CancelAfter(TimeSpan.FromSeconds(0.2));
|
||||||
|
|
||||||
|
var exitCode = await application.RunAsync(new[] {"cmd"});
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
exitCode.Should().NotBe(0);
|
||||||
|
stdOut.GetString().Trim().Should().Be(CancellableCommand.CancellationOutputText);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,48 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.IO;
|
|
||||||
using CliFx.Services;
|
|
||||||
using CliFx.Tests.TestCommands;
|
|
||||||
using NUnit.Framework;
|
|
||||||
|
|
||||||
namespace CliFx.Tests
|
|
||||||
{
|
|
||||||
[TestFixture]
|
|
||||||
public class CliApplicationBuilderTests
|
|
||||||
{
|
|
||||||
// Make sure all builder methods work
|
|
||||||
[Test]
|
|
||||||
public void All_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))
|
|
||||||
.UseCommandFactory(schema => (ICommand) Activator.CreateInstance(schema.Type))
|
|
||||||
.Build();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Make sure builder can produce an application with no parameters specified
|
|
||||||
[Test]
|
|
||||||
public void Build_Test()
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var builder = new CliApplicationBuilder();
|
|
||||||
|
|
||||||
// Act
|
|
||||||
builder.Build();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,230 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.IO;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using CliFx.Services;
|
|
||||||
using CliFx.Tests.TestCommands;
|
|
||||||
using FluentAssertions;
|
|
||||||
using NUnit.Framework;
|
|
||||||
|
|
||||||
namespace CliFx.Tests
|
|
||||||
{
|
|
||||||
[TestFixture]
|
|
||||||
public class CliApplicationTests
|
|
||||||
{
|
|
||||||
private const string TestVersionText = "v1.0";
|
|
||||||
|
|
||||||
private static IEnumerable<TestCaseData> GetTestCases_RunAsync()
|
|
||||||
{
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(HelloWorldDefaultCommand)},
|
|
||||||
new string[0],
|
|
||||||
"Hello world."
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(ConcatCommand)},
|
|
||||||
new[] {"concat", "-i", "foo", "-i", "bar", "-s", " "},
|
|
||||||
"foo bar"
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(ConcatCommand)},
|
|
||||||
new[] {"concat", "-i", "one", "two", "three", "-s", ", "},
|
|
||||||
"one, two, three"
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(DivideCommand)},
|
|
||||||
new[] {"div", "-D", "24", "-d", "8"},
|
|
||||||
"3"
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(HelloWorldDefaultCommand)},
|
|
||||||
new[] {"--version"},
|
|
||||||
TestVersionText
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(ConcatCommand)},
|
|
||||||
new[] {"--version"},
|
|
||||||
TestVersionText
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(HelloWorldDefaultCommand)},
|
|
||||||
new[] {"-h"},
|
|
||||||
null
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(HelloWorldDefaultCommand)},
|
|
||||||
new[] {"--help"},
|
|
||||||
null
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(ConcatCommand)},
|
|
||||||
new string[0],
|
|
||||||
null
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(ConcatCommand)},
|
|
||||||
new[] {"-h"},
|
|
||||||
null
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(ConcatCommand)},
|
|
||||||
new[] {"--help"},
|
|
||||||
null
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(ConcatCommand)},
|
|
||||||
new[] {"concat", "-h"},
|
|
||||||
null
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(ExceptionCommand)},
|
|
||||||
new[] {"exc", "-h"},
|
|
||||||
null
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(CommandExceptionCommand)},
|
|
||||||
new[] {"exc", "-h"},
|
|
||||||
null
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(ConcatCommand)},
|
|
||||||
new[] {"[preview]"},
|
|
||||||
null
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(ExceptionCommand)},
|
|
||||||
new[] {"exc", "[preview]"},
|
|
||||||
null
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(ConcatCommand)},
|
|
||||||
new[] {"concat", "[preview]", "-o", "value"},
|
|
||||||
null
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static IEnumerable<TestCaseData> GetTestCases_RunAsync_Negative()
|
|
||||||
{
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new Type[0],
|
|
||||||
new string[0],
|
|
||||||
null, null
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(ConcatCommand)},
|
|
||||||
new[] {"non-existing"},
|
|
||||||
null, null
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(ExceptionCommand)},
|
|
||||||
new[] {"exc"},
|
|
||||||
null, null
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(CommandExceptionCommand)},
|
|
||||||
new[] {"exc"},
|
|
||||||
null, null
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(CommandExceptionCommand)},
|
|
||||||
new[] {"exc"},
|
|
||||||
null, null
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(CommandExceptionCommand)},
|
|
||||||
new[] {"exc", "-m", "foo bar"},
|
|
||||||
"foo bar", null
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(CommandExceptionCommand)},
|
|
||||||
new[] {"exc", "-m", "foo bar", "-c", "666"},
|
|
||||||
"foo bar", 666
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Test]
|
|
||||||
[TestCaseSource(nameof(GetTestCases_RunAsync))]
|
|
||||||
public async Task RunAsync_Test(IReadOnlyList<Type> commandTypes, IReadOnlyList<string> commandLineArguments,
|
|
||||||
string expectedStdOut = null)
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
using (var stdoutStream = new StringWriter())
|
|
||||||
{
|
|
||||||
var console = new VirtualConsole(stdoutStream);
|
|
||||||
|
|
||||||
var application = new CliApplicationBuilder()
|
|
||||||
.AddCommands(commandTypes)
|
|
||||||
.UseVersionText(TestVersionText)
|
|
||||||
.UseConsole(console)
|
|
||||||
.Build();
|
|
||||||
|
|
||||||
// Act
|
|
||||||
var exitCode = await application.RunAsync(commandLineArguments);
|
|
||||||
var stdOut = stdoutStream.ToString().Trim();
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
exitCode.Should().Be(0);
|
|
||||||
|
|
||||||
if (expectedStdOut != null)
|
|
||||||
stdOut.Should().Be(expectedStdOut);
|
|
||||||
else
|
|
||||||
stdOut.Should().NotBeNullOrWhiteSpace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[Test]
|
|
||||||
[TestCaseSource(nameof(GetTestCases_RunAsync_Negative))]
|
|
||||||
public async Task RunAsync_Negative_Test(IReadOnlyList<Type> commandTypes, IReadOnlyList<string> commandLineArguments,
|
|
||||||
string expectedStdErr = null, int? expectedExitCode = null)
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
using (var stderrStream = new StringWriter())
|
|
||||||
{
|
|
||||||
var console = new VirtualConsole(TextWriter.Null, stderrStream);
|
|
||||||
|
|
||||||
var application = new CliApplicationBuilder()
|
|
||||||
.AddCommands(commandTypes)
|
|
||||||
.UseVersionText(TestVersionText)
|
|
||||||
.UseConsole(console)
|
|
||||||
.Build();
|
|
||||||
|
|
||||||
// Act
|
|
||||||
var exitCode = await application.RunAsync(commandLineArguments);
|
|
||||||
var stderr = stderrStream.ToString().Trim();
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
if (expectedExitCode != null)
|
|
||||||
exitCode.Should().Be(expectedExitCode);
|
|
||||||
else
|
|
||||||
exitCode.Should().NotBe(0);
|
|
||||||
|
|
||||||
if (expectedStdErr != null)
|
|
||||||
stderr.Should().Be(expectedStdErr);
|
|
||||||
else
|
|
||||||
stderr.Should().NotBeNullOrWhiteSpace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,28 +1,41 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
<Import Project="../CliFx.props" />
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>net46</TargetFramework>
|
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||||
<IsPackable>false</IsPackable>
|
<IsPackable>false</IsPackable>
|
||||||
<IsTestProject>true</IsTestProject>
|
<IsTestProject>true</IsTestProject>
|
||||||
<CollectCoverage>true</CollectCoverage>
|
<CollectCoverage>true</CollectCoverage>
|
||||||
<CoverletOutputFormat>opencover</CoverletOutputFormat>
|
<CoverletOutputFormat>opencover</CoverletOutputFormat>
|
||||||
<CoverletOutput>bin/$(Configuration)/Coverage.xml</CoverletOutput>
|
<CoverletOutput>bin/$(Configuration)/Coverage.xml</CoverletOutput>
|
||||||
<LangVersion>latest</LangVersion>
|
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="FluentAssertions" Version="5.8.0" />
|
<Content Include="xunit.runner.json" CopyToOutputDirectory="PreserveNewest" />
|
||||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
|
|
||||||
<PackageReference Include="NUnit" Version="3.12.0" />
|
|
||||||
<PackageReference Include="NUnit3TestAdapter" Version="3.14.0" />
|
|
||||||
<PackageReference Include="coverlet.msbuild" Version="2.6.3">
|
|
||||||
<PrivateAssets>all</PrivateAssets>
|
|
||||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
|
||||||
</PackageReference>
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="CliWrap" Version="3.2.0" />
|
||||||
|
<PackageReference Include="FluentAssertions" Version="5.10.3" />
|
||||||
|
<PackageReference Include="GitHubActionsTestLogger" Version="1.1.1" />
|
||||||
|
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
|
||||||
|
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
|
||||||
|
<PackageReference Include="xunit" Version="2.4.1" />
|
||||||
|
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" PrivateAssets="all" />
|
||||||
|
<PackageReference Include="coverlet.msbuild" Version="2.9.0" PrivateAssets="all" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\CliFx.Tests.Dummy\CliFx.Tests.Dummy.csproj" />
|
||||||
<ProjectReference Include="..\CliFx\CliFx.csproj" />
|
<ProjectReference Include="..\CliFx\CliFx.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<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>
|
</Project>
|
||||||
31
CliFx.Tests/Commands/CancellableCommand.cs
Normal file
31
CliFx.Tests/Commands/CancellableCommand.cs
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
using System;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using CliFx.Attributes;
|
||||||
|
|
||||||
|
namespace CliFx.Tests.Commands
|
||||||
|
{
|
||||||
|
[Command("cmd")]
|
||||||
|
public class CancellableCommand : ICommand
|
||||||
|
{
|
||||||
|
public const string CompletionOutputText = "Finished";
|
||||||
|
public const string CancellationOutputText = "Canceled";
|
||||||
|
|
||||||
|
public async ValueTask ExecuteAsync(IConsole console)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await Task.Delay(
|
||||||
|
TimeSpan.FromSeconds(3),
|
||||||
|
console.GetCancellationToken()
|
||||||
|
);
|
||||||
|
|
||||||
|
console.Output.WriteLine(CompletionOutputText);
|
||||||
|
}
|
||||||
|
catch (OperationCanceledException)
|
||||||
|
{
|
||||||
|
console.Output.WriteLine(CancellationOutputText);
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
21
CliFx.Tests/Commands/CommandExceptionCommand.cs
Normal file
21
CliFx.Tests/Commands/CommandExceptionCommand.cs
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
using System.Threading.Tasks;
|
||||||
|
using CliFx.Attributes;
|
||||||
|
using CliFx.Exceptions;
|
||||||
|
|
||||||
|
namespace CliFx.Tests.Commands
|
||||||
|
{
|
||||||
|
[Command("cmd")]
|
||||||
|
public 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
17
CliFx.Tests/Commands/DefaultCommand.cs
Normal file
17
CliFx.Tests/Commands/DefaultCommand.cs
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
using System.Threading.Tasks;
|
||||||
|
using CliFx.Attributes;
|
||||||
|
|
||||||
|
namespace CliFx.Tests.Commands
|
||||||
|
{
|
||||||
|
[Command(Description = "Default command description")]
|
||||||
|
public class DefaultCommand : ICommand
|
||||||
|
{
|
||||||
|
public const string ExpectedOutputText = nameof(DefaultCommand);
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console)
|
||||||
|
{
|
||||||
|
console.Output.WriteLine(ExpectedOutputText);
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
15
CliFx.Tests/Commands/GenericExceptionCommand.cs
Normal file
15
CliFx.Tests/Commands/GenericExceptionCommand.cs
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
using System;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using CliFx.Attributes;
|
||||||
|
|
||||||
|
namespace CliFx.Tests.Commands
|
||||||
|
{
|
||||||
|
[Command("cmd")]
|
||||||
|
public class GenericExceptionCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandOption("msg", 'm')]
|
||||||
|
public string? Message { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => throw new Exception(Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
19
CliFx.Tests/Commands/GenericInnerExceptionCommand.cs
Normal file
19
CliFx.Tests/Commands/GenericInnerExceptionCommand.cs
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
using System;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using CliFx.Attributes;
|
||||||
|
|
||||||
|
namespace CliFx.Tests.Commands
|
||||||
|
{
|
||||||
|
[Command("cmd")]
|
||||||
|
public class GenericInnerExceptionCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandOption("msg", 'm')]
|
||||||
|
public string? Message { get; set; }
|
||||||
|
|
||||||
|
[CommandOption("inner-msg", 'i')]
|
||||||
|
public string? InnerMessage { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) =>
|
||||||
|
throw new Exception(Message, new Exception(InnerMessage));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
using CliFx.Attributes;
|
||||||
|
|
||||||
|
namespace CliFx.Tests.Commands.Invalid
|
||||||
|
{
|
||||||
|
[Command("cmd")]
|
||||||
|
public class ConflictWithHelpOptionCommand : SelfSerializeCommandBase
|
||||||
|
{
|
||||||
|
[CommandOption("option-h", 'h')]
|
||||||
|
public string? OptionH { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
using CliFx.Attributes;
|
||||||
|
|
||||||
|
namespace CliFx.Tests.Commands.Invalid
|
||||||
|
{
|
||||||
|
// Must be default because version option is available only on default commands
|
||||||
|
[Command]
|
||||||
|
public class ConflictWithVersionOptionCommand : SelfSerializeCommandBase
|
||||||
|
{
|
||||||
|
[CommandOption("version")]
|
||||||
|
public string? Version { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
using CliFx.Attributes;
|
||||||
|
|
||||||
|
namespace CliFx.Tests.Commands.Invalid
|
||||||
|
{
|
||||||
|
[Command("cmd")]
|
||||||
|
public class DuplicateOptionEnvironmentVariableNamesCommand : SelfSerializeCommandBase
|
||||||
|
{
|
||||||
|
[CommandOption("option-a", EnvironmentVariableName = "ENV_VAR")]
|
||||||
|
public string? OptionA { get; set; }
|
||||||
|
|
||||||
|
[CommandOption("option-b", EnvironmentVariableName = "ENV_VAR")]
|
||||||
|
public string? OptionB { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
14
CliFx.Tests/Commands/Invalid/DuplicateOptionNamesCommand.cs
Normal file
14
CliFx.Tests/Commands/Invalid/DuplicateOptionNamesCommand.cs
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
using CliFx.Attributes;
|
||||||
|
|
||||||
|
namespace CliFx.Tests.Commands.Invalid
|
||||||
|
{
|
||||||
|
[Command("cmd")]
|
||||||
|
public class DuplicateOptionNamesCommand : SelfSerializeCommandBase
|
||||||
|
{
|
||||||
|
[CommandOption("fruits")]
|
||||||
|
public string? Apples { get; set; }
|
||||||
|
|
||||||
|
[CommandOption("fruits")]
|
||||||
|
public string? Oranges { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
using CliFx.Attributes;
|
||||||
|
|
||||||
|
namespace CliFx.Tests.Commands.Invalid
|
||||||
|
{
|
||||||
|
[Command("cmd")]
|
||||||
|
public class DuplicateOptionShortNamesCommand : SelfSerializeCommandBase
|
||||||
|
{
|
||||||
|
[CommandOption('x')]
|
||||||
|
public string? OptionA { get; set; }
|
||||||
|
|
||||||
|
[CommandOption('x')]
|
||||||
|
public string? OptionB { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
using CliFx.Attributes;
|
||||||
|
|
||||||
|
namespace CliFx.Tests.Commands.Invalid
|
||||||
|
{
|
||||||
|
[Command("cmd")]
|
||||||
|
public class DuplicateParameterNameCommand : SelfSerializeCommandBase
|
||||||
|
{
|
||||||
|
[CommandParameter(0, Name = "param")]
|
||||||
|
public string? ParamA { get; set; }
|
||||||
|
|
||||||
|
[CommandParameter(1, Name = "param")]
|
||||||
|
public string? ParamB { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
using CliFx.Attributes;
|
||||||
|
|
||||||
|
namespace CliFx.Tests.Commands.Invalid
|
||||||
|
{
|
||||||
|
[Command("cmd")]
|
||||||
|
public class DuplicateParameterOrderCommand : SelfSerializeCommandBase
|
||||||
|
{
|
||||||
|
[CommandParameter(13)]
|
||||||
|
public string? ParamA { get; set; }
|
||||||
|
|
||||||
|
[CommandParameter(13)]
|
||||||
|
public string? ParamB { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
11
CliFx.Tests/Commands/Invalid/EmptyOptionNameCommand.cs
Normal file
11
CliFx.Tests/Commands/Invalid/EmptyOptionNameCommand.cs
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
using CliFx.Attributes;
|
||||||
|
|
||||||
|
namespace CliFx.Tests.Commands.Invalid
|
||||||
|
{
|
||||||
|
[Command("cmd")]
|
||||||
|
public class EmptyOptionNameCommand : SelfSerializeCommandBase
|
||||||
|
{
|
||||||
|
[CommandOption("")]
|
||||||
|
public string? Apples { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
using CliFx.Attributes;
|
||||||
|
|
||||||
|
namespace CliFx.Tests.Commands.Invalid
|
||||||
|
{
|
||||||
|
[Command]
|
||||||
|
public class InvalidCustomConverterOptionCommand : SelfSerializeCommandBase
|
||||||
|
{
|
||||||
|
[CommandOption('f', Converter = typeof(Converter))]
|
||||||
|
public string? Option { get; set; }
|
||||||
|
|
||||||
|
public class Converter
|
||||||
|
{
|
||||||
|
public object ConvertFrom(string value) => value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
using CliFx.Attributes;
|
||||||
|
|
||||||
|
namespace CliFx.Tests.Commands.Invalid
|
||||||
|
{
|
||||||
|
[Command]
|
||||||
|
public class InvalidCustomConverterParameterCommand : SelfSerializeCommandBase
|
||||||
|
{
|
||||||
|
[CommandParameter(0, Converter = typeof(Converter))]
|
||||||
|
public string? Param { get; set; }
|
||||||
|
|
||||||
|
public class Converter
|
||||||
|
{
|
||||||
|
public object ConvertFrom(string value) => value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using CliFx.Attributes;
|
||||||
|
|
||||||
|
namespace CliFx.Tests.Commands.Invalid
|
||||||
|
{
|
||||||
|
[Command("cmd")]
|
||||||
|
public class MultipleNonScalarParametersCommand : SelfSerializeCommandBase
|
||||||
|
{
|
||||||
|
[CommandParameter(0)]
|
||||||
|
public IReadOnlyList<string>? ParamA { get; set; }
|
||||||
|
|
||||||
|
[CommandParameter(1)]
|
||||||
|
public IReadOnlyList<string>? ParamB { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
6
CliFx.Tests/Commands/Invalid/NonAnnotatedCommand.cs
Normal file
6
CliFx.Tests/Commands/Invalid/NonAnnotatedCommand.cs
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
namespace CliFx.Tests.Commands.Invalid
|
||||||
|
{
|
||||||
|
public class NonAnnotatedCommand : SelfSerializeCommandBase
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
using CliFx.Attributes;
|
using CliFx.Attributes;
|
||||||
|
|
||||||
namespace CliFx.Tests.TestCommands
|
namespace CliFx.Tests.Commands.Invalid
|
||||||
{
|
{
|
||||||
[Command]
|
[Command]
|
||||||
public class NonImplementedCommand
|
public class NonImplementedCommand
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using CliFx.Attributes;
|
||||||
|
|
||||||
|
namespace CliFx.Tests.Commands.Invalid
|
||||||
|
{
|
||||||
|
[Command("cmd")]
|
||||||
|
public class NonLastNonScalarParameterCommand : SelfSerializeCommandBase
|
||||||
|
{
|
||||||
|
[CommandParameter(0)]
|
||||||
|
public IReadOnlyList<string>? ParamA { get; set; }
|
||||||
|
|
||||||
|
[CommandParameter(1)]
|
||||||
|
public string? ParamB { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
9
CliFx.Tests/Commands/Invalid/OtherDefaultCommand.cs
Normal file
9
CliFx.Tests/Commands/Invalid/OtherDefaultCommand.cs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
using CliFx.Attributes;
|
||||||
|
|
||||||
|
namespace CliFx.Tests.Commands.Invalid
|
||||||
|
{
|
||||||
|
[Command]
|
||||||
|
public class OtherDefaultCommand : SelfSerializeCommandBase
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
using CliFx.Attributes;
|
||||||
|
|
||||||
|
namespace CliFx.Tests.Commands.Invalid
|
||||||
|
{
|
||||||
|
[Command("cmd")]
|
||||||
|
public class SingleCharacterOptionNameCommand : SelfSerializeCommandBase
|
||||||
|
{
|
||||||
|
[CommandOption("a")]
|
||||||
|
public string? Apples { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
17
CliFx.Tests/Commands/NamedCommand.cs
Normal file
17
CliFx.Tests/Commands/NamedCommand.cs
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
using System.Threading.Tasks;
|
||||||
|
using CliFx.Attributes;
|
||||||
|
|
||||||
|
namespace CliFx.Tests.Commands
|
||||||
|
{
|
||||||
|
[Command("named", Description = "Named command description")]
|
||||||
|
public class NamedCommand : ICommand
|
||||||
|
{
|
||||||
|
public const string ExpectedOutputText = nameof(NamedCommand);
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console)
|
||||||
|
{
|
||||||
|
console.Output.WriteLine(ExpectedOutputText);
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
17
CliFx.Tests/Commands/NamedSubCommand.cs
Normal file
17
CliFx.Tests/Commands/NamedSubCommand.cs
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
using System.Threading.Tasks;
|
||||||
|
using CliFx.Attributes;
|
||||||
|
|
||||||
|
namespace CliFx.Tests.Commands
|
||||||
|
{
|
||||||
|
[Command("named sub", Description = "Named sub command description")]
|
||||||
|
public class NamedSubCommand : ICommand
|
||||||
|
{
|
||||||
|
public const string ExpectedOutputText = nameof(NamedSubCommand);
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console)
|
||||||
|
{
|
||||||
|
console.Output.WriteLine(ExpectedOutputText);
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
14
CliFx.Tests/Commands/SelfSerializeCommandBase.cs
Normal file
14
CliFx.Tests/Commands/SelfSerializeCommandBase.cs
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
using System.Threading.Tasks;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
|
namespace CliFx.Tests.Commands
|
||||||
|
{
|
||||||
|
public abstract class SelfSerializeCommandBase : ICommand
|
||||||
|
{
|
||||||
|
public ValueTask ExecuteAsync(IConsole console)
|
||||||
|
{
|
||||||
|
console.Output.WriteLine(JsonConvert.SerializeObject(this));
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
174
CliFx.Tests/Commands/SupportedArgumentTypesCommand.cs
Normal file
174
CliFx.Tests/Commands/SupportedArgumentTypesCommand.cs
Normal file
@@ -0,0 +1,174 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Globalization;
|
||||||
|
using CliFx.Attributes;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
|
namespace CliFx.Tests.Commands
|
||||||
|
{
|
||||||
|
[Command("cmd")]
|
||||||
|
public partial class SupportedArgumentTypesCommand : SelfSerializeCommandBase
|
||||||
|
{
|
||||||
|
[CommandOption("obj")]
|
||||||
|
public object? Object { get; set; } = 42;
|
||||||
|
|
||||||
|
[CommandOption("str")]
|
||||||
|
public string? String { get; set; } = "foo bar";
|
||||||
|
|
||||||
|
[CommandOption("bool")]
|
||||||
|
public bool Bool { get; set; }
|
||||||
|
|
||||||
|
[CommandOption("char")]
|
||||||
|
public char Char { get; set; }
|
||||||
|
|
||||||
|
[CommandOption("sbyte")]
|
||||||
|
public sbyte Sbyte { get; set; }
|
||||||
|
|
||||||
|
[CommandOption("byte")]
|
||||||
|
public byte Byte { get; set; }
|
||||||
|
|
||||||
|
[CommandOption("short")]
|
||||||
|
public short Short { get; set; }
|
||||||
|
|
||||||
|
[CommandOption("ushort")]
|
||||||
|
public ushort Ushort { get; set; }
|
||||||
|
|
||||||
|
[CommandOption("int")]
|
||||||
|
public int Int { get; set; }
|
||||||
|
|
||||||
|
[CommandOption("uint")]
|
||||||
|
public uint Uint { get; set; }
|
||||||
|
|
||||||
|
[CommandOption("long")]
|
||||||
|
public long Long { get; set; }
|
||||||
|
|
||||||
|
[CommandOption("ulong")]
|
||||||
|
public ulong Ulong { get; set; }
|
||||||
|
|
||||||
|
[CommandOption("float")]
|
||||||
|
public float Float { get; set; }
|
||||||
|
|
||||||
|
[CommandOption("double")]
|
||||||
|
public double Double { get; set; }
|
||||||
|
|
||||||
|
[CommandOption("decimal")]
|
||||||
|
public decimal Decimal { get; set; }
|
||||||
|
|
||||||
|
[CommandOption("datetime")]
|
||||||
|
public DateTime DateTime { get; set; }
|
||||||
|
|
||||||
|
[CommandOption("datetime-offset")]
|
||||||
|
public DateTimeOffset DateTimeOffset { get; set; }
|
||||||
|
|
||||||
|
[CommandOption("timespan")]
|
||||||
|
public TimeSpan TimeSpan { get; set; }
|
||||||
|
|
||||||
|
[CommandOption("enum")]
|
||||||
|
public CustomEnum Enum { get; set; }
|
||||||
|
|
||||||
|
[CommandOption("int-nullable")]
|
||||||
|
public int? IntNullable { get; set; }
|
||||||
|
|
||||||
|
[CommandOption("enum-nullable")]
|
||||||
|
public CustomEnum? EnumNullable { get; set; }
|
||||||
|
|
||||||
|
[CommandOption("timespan-nullable")]
|
||||||
|
public TimeSpan? TimeSpanNullable { get; set; }
|
||||||
|
|
||||||
|
[CommandOption("str-constructible")]
|
||||||
|
public CustomStringConstructible? StringConstructible { get; set; }
|
||||||
|
|
||||||
|
[CommandOption("str-parseable")]
|
||||||
|
public CustomStringParseable? StringParseable { get; set; }
|
||||||
|
|
||||||
|
[CommandOption("str-parseable-format")]
|
||||||
|
public CustomStringParseableWithFormatProvider? StringParseableWithFormatProvider { get; set; }
|
||||||
|
|
||||||
|
[CommandOption("convertible", Converter = typeof(CustomConvertibleConverter))]
|
||||||
|
public CustomConvertible? Convertible { get; set; }
|
||||||
|
|
||||||
|
[CommandOption("obj-array")]
|
||||||
|
public object[]? ObjectArray { get; set; }
|
||||||
|
|
||||||
|
[CommandOption("str-array")]
|
||||||
|
public string[]? StringArray { get; set; }
|
||||||
|
|
||||||
|
[CommandOption("int-array")]
|
||||||
|
public int[]? IntArray { get; set; }
|
||||||
|
|
||||||
|
[CommandOption("enum-array")]
|
||||||
|
public CustomEnum[]? EnumArray { get; set; }
|
||||||
|
|
||||||
|
[CommandOption("int-nullable-array")]
|
||||||
|
public int?[]? IntNullableArray { get; set; }
|
||||||
|
|
||||||
|
[CommandOption("str-constructible-array")]
|
||||||
|
public CustomStringConstructible[]? StringConstructibleArray { get; set; }
|
||||||
|
|
||||||
|
[CommandOption("convertible-array", Converter = typeof(CustomConvertibleConverter))]
|
||||||
|
public CustomConvertible[]? ConvertibleArray { get; set; }
|
||||||
|
|
||||||
|
[CommandOption("str-enumerable")]
|
||||||
|
public IEnumerable<string>? StringEnumerable { get; set; }
|
||||||
|
|
||||||
|
[CommandOption("str-read-only-list")]
|
||||||
|
public IReadOnlyList<string>? StringReadOnlyList { get; set; }
|
||||||
|
|
||||||
|
[CommandOption("str-list")]
|
||||||
|
public List<string>? StringList { get; set; }
|
||||||
|
|
||||||
|
[CommandOption("str-set")]
|
||||||
|
public HashSet<string>? StringHashSet { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public partial class SupportedArgumentTypesCommand
|
||||||
|
{
|
||||||
|
public enum CustomEnum
|
||||||
|
{
|
||||||
|
Value1 = 1,
|
||||||
|
Value2 = 2,
|
||||||
|
Value3 = 3
|
||||||
|
}
|
||||||
|
|
||||||
|
public class CustomStringConstructible
|
||||||
|
{
|
||||||
|
public string Value { get; }
|
||||||
|
|
||||||
|
public CustomStringConstructible(string value) => Value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public class CustomStringParseable
|
||||||
|
{
|
||||||
|
public string Value { get; }
|
||||||
|
|
||||||
|
[JsonConstructor]
|
||||||
|
private CustomStringParseable(string value) => Value = value;
|
||||||
|
|
||||||
|
public static CustomStringParseable Parse(string value) => new CustomStringParseable(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public class CustomStringParseableWithFormatProvider
|
||||||
|
{
|
||||||
|
public string Value { get; }
|
||||||
|
|
||||||
|
[JsonConstructor]
|
||||||
|
private CustomStringParseableWithFormatProvider(string value) => Value = value;
|
||||||
|
|
||||||
|
public static CustomStringParseableWithFormatProvider Parse(string value, IFormatProvider formatProvider) =>
|
||||||
|
new CustomStringParseableWithFormatProvider(value + " " + formatProvider);
|
||||||
|
}
|
||||||
|
|
||||||
|
public class CustomConvertible
|
||||||
|
{
|
||||||
|
public int Value { get; }
|
||||||
|
|
||||||
|
public CustomConvertible(int value) => Value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public class CustomConvertibleConverter : IArgumentValueConverter
|
||||||
|
{
|
||||||
|
public object ConvertFrom(string value) =>
|
||||||
|
new CustomConvertible(int.Parse(value, CultureInfo.InvariantCulture));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
31
CliFx.Tests/Commands/UnsupportedArgumentTypesCommand.cs
Normal file
31
CliFx.Tests/Commands/UnsupportedArgumentTypesCommand.cs
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using CliFx.Attributes;
|
||||||
|
|
||||||
|
namespace CliFx.Tests.Commands
|
||||||
|
{
|
||||||
|
[Command("cmd")]
|
||||||
|
public partial class UnsupportedArgumentTypesCommand : SelfSerializeCommandBase
|
||||||
|
{
|
||||||
|
[CommandOption("custom")]
|
||||||
|
public CustomType? CustomNonConvertible { get; set; }
|
||||||
|
|
||||||
|
[CommandOption("custom-enumerable")]
|
||||||
|
public CustomEnumerable<string>? CustomEnumerableNonConvertible { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public partial class UnsupportedArgumentTypesCommand
|
||||||
|
{
|
||||||
|
public class CustomType
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public class CustomEnumerable<T> : IEnumerable<T>
|
||||||
|
{
|
||||||
|
public IEnumerator<T> GetEnumerator() => ((IEnumerable<T>) Array.Empty<T>()).GetEnumerator();
|
||||||
|
|
||||||
|
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
44
CliFx.Tests/Commands/WithDefaultValuesCommand.cs
Normal file
44
CliFx.Tests/Commands/WithDefaultValuesCommand.cs
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
using System;
|
||||||
|
using CliFx.Attributes;
|
||||||
|
|
||||||
|
namespace CliFx.Tests.Commands
|
||||||
|
{
|
||||||
|
[Command("cmd")]
|
||||||
|
public class WithDefaultValuesCommand : SelfSerializeCommandBase
|
||||||
|
{
|
||||||
|
public enum CustomEnum { Value1, Value2, Value3 };
|
||||||
|
|
||||||
|
[CommandOption("obj")]
|
||||||
|
public object? Object { get; set; } = 42;
|
||||||
|
|
||||||
|
[CommandOption("str")]
|
||||||
|
public string? String { get; set; } = "foo";
|
||||||
|
|
||||||
|
[CommandOption("str-empty")]
|
||||||
|
public string StringEmpty { get; set; } = "";
|
||||||
|
|
||||||
|
[CommandOption("str-array")]
|
||||||
|
public string[]? StringArray { get; set; } = { "foo", "bar", "baz" };
|
||||||
|
|
||||||
|
[CommandOption("bool")]
|
||||||
|
public bool Bool { get; set; } = true;
|
||||||
|
|
||||||
|
[CommandOption("char")]
|
||||||
|
public char Char { get; set; } = 't';
|
||||||
|
|
||||||
|
[CommandOption("int")]
|
||||||
|
public int Int { get; set; } = 1337;
|
||||||
|
|
||||||
|
[CommandOption("int-nullable")]
|
||||||
|
public int? IntNullable { get; set; } = 1337;
|
||||||
|
|
||||||
|
[CommandOption("int-array")]
|
||||||
|
public int[]? IntArray { get; set; } = { 1, 2, 3 };
|
||||||
|
|
||||||
|
[CommandOption("timespan")]
|
||||||
|
public TimeSpan TimeSpan { get; set; } = TimeSpan.FromMinutes(123);
|
||||||
|
|
||||||
|
[CommandOption("enum")]
|
||||||
|
public CustomEnum Enum { get; set; } = CustomEnum.Value2;
|
||||||
|
}
|
||||||
|
}
|
||||||
28
CliFx.Tests/Commands/WithDependenciesCommand.cs
Normal file
28
CliFx.Tests/Commands/WithDependenciesCommand.cs
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
using System.Threading.Tasks;
|
||||||
|
using CliFx.Attributes;
|
||||||
|
|
||||||
|
namespace CliFx.Tests.Commands
|
||||||
|
{
|
||||||
|
[Command("cmd")]
|
||||||
|
public class WithDependenciesCommand : ICommand
|
||||||
|
{
|
||||||
|
public class DependencyA
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public class DependencyB
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
private readonly DependencyA _dependencyA;
|
||||||
|
private readonly DependencyB _dependencyB;
|
||||||
|
|
||||||
|
public WithDependenciesCommand(DependencyA dependencyA, DependencyB dependencyB)
|
||||||
|
{
|
||||||
|
_dependencyA = dependencyA;
|
||||||
|
_dependencyB = dependencyB;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}
|
||||||
|
}
|
||||||
19
CliFx.Tests/Commands/WithEnumArgumentsCommand.cs
Normal file
19
CliFx.Tests/Commands/WithEnumArgumentsCommand.cs
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
using CliFx.Attributes;
|
||||||
|
|
||||||
|
namespace CliFx.Tests.Commands
|
||||||
|
{
|
||||||
|
[Command("cmd")]
|
||||||
|
public class WithEnumArgumentsCommand : SelfSerializeCommandBase
|
||||||
|
{
|
||||||
|
public enum CustomEnum { Value1, Value2, Value3 };
|
||||||
|
|
||||||
|
[CommandParameter(0, Name = "enum")]
|
||||||
|
public CustomEnum EnumParameter { get; set; }
|
||||||
|
|
||||||
|
[CommandOption("enum")]
|
||||||
|
public CustomEnum? EnumOption { get; set; }
|
||||||
|
|
||||||
|
[CommandOption("required-enum", IsRequired = true)]
|
||||||
|
public CustomEnum RequiredEnumOption { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
15
CliFx.Tests/Commands/WithEnvironmentVariablesCommand.cs
Normal file
15
CliFx.Tests/Commands/WithEnvironmentVariablesCommand.cs
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using CliFx.Attributes;
|
||||||
|
|
||||||
|
namespace CliFx.Tests.Commands
|
||||||
|
{
|
||||||
|
[Command("cmd")]
|
||||||
|
public class WithEnvironmentVariablesCommand : SelfSerializeCommandBase
|
||||||
|
{
|
||||||
|
[CommandOption("opt-a", 'a', EnvironmentVariableName = "ENV_OPT_A")]
|
||||||
|
public string? OptA { get; set; }
|
||||||
|
|
||||||
|
[CommandOption("opt-b", 'b', EnvironmentVariableName = "ENV_OPT_B")]
|
||||||
|
public IReadOnlyList<string>? OptB { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
18
CliFx.Tests/Commands/WithParametersCommand.cs
Normal file
18
CliFx.Tests/Commands/WithParametersCommand.cs
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using CliFx.Attributes;
|
||||||
|
|
||||||
|
namespace CliFx.Tests.Commands
|
||||||
|
{
|
||||||
|
[Command("cmd")]
|
||||||
|
public class WithParametersCommand : SelfSerializeCommandBase
|
||||||
|
{
|
||||||
|
[CommandParameter(0)]
|
||||||
|
public string? ParamA { get; set; }
|
||||||
|
|
||||||
|
[CommandParameter(1)]
|
||||||
|
public int? ParamB { get; set; }
|
||||||
|
|
||||||
|
[CommandParameter(2)]
|
||||||
|
public IReadOnlyList<string>? ParamC { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
18
CliFx.Tests/Commands/WithRequiredOptionsCommand.cs
Normal file
18
CliFx.Tests/Commands/WithRequiredOptionsCommand.cs
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using CliFx.Attributes;
|
||||||
|
|
||||||
|
namespace CliFx.Tests.Commands
|
||||||
|
{
|
||||||
|
[Command("cmd")]
|
||||||
|
public class WithRequiredOptionsCommand : SelfSerializeCommandBase
|
||||||
|
{
|
||||||
|
[CommandOption("opt-a", 'a', IsRequired = true)]
|
||||||
|
public string? OptA { get; set; }
|
||||||
|
|
||||||
|
[CommandOption("opt-b", 'b')]
|
||||||
|
public int? OptB { get; set; }
|
||||||
|
|
||||||
|
[CommandOption("opt-c", 'c', IsRequired = true)]
|
||||||
|
public IReadOnlyList<char>? OptC { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
11
CliFx.Tests/Commands/WithSingleParameterCommand.cs
Normal file
11
CliFx.Tests/Commands/WithSingleParameterCommand.cs
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
using CliFx.Attributes;
|
||||||
|
|
||||||
|
namespace CliFx.Tests.Commands
|
||||||
|
{
|
||||||
|
[Command("cmd")]
|
||||||
|
public class WithSingleParameterCommand : SelfSerializeCommandBase
|
||||||
|
{
|
||||||
|
[CommandParameter(0)]
|
||||||
|
public string? ParamA { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
14
CliFx.Tests/Commands/WithSingleRequiredOptionCommand.cs
Normal file
14
CliFx.Tests/Commands/WithSingleRequiredOptionCommand.cs
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
using CliFx.Attributes;
|
||||||
|
|
||||||
|
namespace CliFx.Tests.Commands
|
||||||
|
{
|
||||||
|
[Command("cmd")]
|
||||||
|
public class WithSingleRequiredOptionCommand : SelfSerializeCommandBase
|
||||||
|
{
|
||||||
|
[CommandOption("opt-a")]
|
||||||
|
public string? OptA { get; set; }
|
||||||
|
|
||||||
|
[CommandOption("opt-b", IsRequired = true)]
|
||||||
|
public string? OptB { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
12
CliFx.Tests/Commands/WithStringArrayOptionCommand.cs
Normal file
12
CliFx.Tests/Commands/WithStringArrayOptionCommand.cs
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using CliFx.Attributes;
|
||||||
|
|
||||||
|
namespace CliFx.Tests.Commands
|
||||||
|
{
|
||||||
|
[Command("cmd")]
|
||||||
|
public class WithStringArrayOptionCommand : SelfSerializeCommandBase
|
||||||
|
{
|
||||||
|
[CommandOption("opt", 'o')]
|
||||||
|
public IReadOnlyList<string>? Opt { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
75
CliFx.Tests/ConsoleSpecs.cs
Normal file
75
CliFx.Tests/ConsoleSpecs.cs
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
67
CliFx.Tests/DependencyInjectionSpecs.cs
Normal file
67
CliFx.Tests/DependencyInjectionSpecs.cs
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
using CliFx.Exceptions;
|
||||||
|
using CliFx.Tests.Commands;
|
||||||
|
using FluentAssertions;
|
||||||
|
using Xunit;
|
||||||
|
using Xunit.Abstractions;
|
||||||
|
|
||||||
|
namespace CliFx.Tests
|
||||||
|
{
|
||||||
|
public 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(DefaultCommand));
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
obj.Should().BeOfType<DefaultCommand>();
|
||||||
|
}
|
||||||
|
|
||||||
|
[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 WithDependenciesCommand.DependencyA(),
|
||||||
|
new WithDependenciesCommand.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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
44
CliFx.Tests/DirectivesSpecs.cs
Normal file
44
CliFx.Tests/DirectivesSpecs.cs
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using CliFx.Tests.Commands;
|
||||||
|
using FluentAssertions;
|
||||||
|
using Xunit;
|
||||||
|
using Xunit.Abstractions;
|
||||||
|
|
||||||
|
namespace CliFx.Tests
|
||||||
|
{
|
||||||
|
public class DirectivesSpecs
|
||||||
|
{
|
||||||
|
private readonly ITestOutputHelper _output;
|
||||||
|
|
||||||
|
public DirectivesSpecs(ITestOutputHelper output) => _output = output;
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Preview_directive_can_be_specified_to_print_provided_arguments_as_they_were_parsed()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var (console, stdOut, _) = VirtualConsole.CreateBuffered();
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand<NamedCommand>()
|
||||||
|
.UseConsole(console)
|
||||||
|
.AllowPreviewMode()
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var exitCode = await application.RunAsync(
|
||||||
|
new[] {"[preview]", "named", "param", "-abc", "--option", "foo"},
|
||||||
|
new Dictionary<string, string>()
|
||||||
|
);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
exitCode.Should().Be(0);
|
||||||
|
stdOut.GetString().Should().ContainAll(
|
||||||
|
"named", "<param>", "[-a]", "[-b]", "[-c]", "[--option \"foo\"]"
|
||||||
|
);
|
||||||
|
|
||||||
|
_output.WriteLine(stdOut.GetString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
141
CliFx.Tests/EnvironmentVariablesSpecs.cs
Normal file
141
CliFx.Tests/EnvironmentVariablesSpecs.cs
Normal file
@@ -0,0 +1,141 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using CliFx.Tests.Commands;
|
||||||
|
using CliFx.Tests.Internal;
|
||||||
|
using CliWrap;
|
||||||
|
using CliWrap.Buffered;
|
||||||
|
using FluentAssertions;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace CliFx.Tests
|
||||||
|
{
|
||||||
|
public class EnvironmentVariablesSpecs
|
||||||
|
{
|
||||||
|
// This test uses a real application to make sure environment variables are actually read correctly
|
||||||
|
[Fact]
|
||||||
|
public async Task Option_can_use_an_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.Trim().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_an_environment_variable_as_fallback_if_the_value_is_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.Trim().Should().Be("Hello Jupiter!");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Option_only_uses_an_environment_variable_as_fallback_if_the_name_matches_case_sensitively()
|
||||||
|
{
|
||||||
|
var (console, stdOut, _) = VirtualConsole.CreateBuffered();
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand<WithEnvironmentVariablesCommand>()
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var exitCode = await application.RunAsync(
|
||||||
|
new[] {"cmd"},
|
||||||
|
new Dictionary<string, string>
|
||||||
|
{
|
||||||
|
["ENV_opt_A"] = "incorrect",
|
||||||
|
["ENV_OPT_A"] = "correct"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
var commandInstance = stdOut.GetString().DeserializeJson<WithEnvironmentVariablesCommand>();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
exitCode.Should().Be(0);
|
||||||
|
commandInstance.Should().BeEquivalentTo(new WithEnvironmentVariablesCommand
|
||||||
|
{
|
||||||
|
OptA = "correct"
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Option_of_non_scalar_type_can_use_an_environment_variable_as_fallback_and_extract_multiple_values()
|
||||||
|
{
|
||||||
|
var (console, stdOut, _) = VirtualConsole.CreateBuffered();
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand<WithEnvironmentVariablesCommand>()
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var exitCode = await application.RunAsync(
|
||||||
|
new[] {"cmd"},
|
||||||
|
new Dictionary<string, string>
|
||||||
|
{
|
||||||
|
["ENV_OPT_B"] = $"foo{Path.PathSeparator}bar"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
var commandInstance = stdOut.GetString().DeserializeJson<WithEnvironmentVariablesCommand>();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
exitCode.Should().Be(0);
|
||||||
|
commandInstance.Should().BeEquivalentTo(new WithEnvironmentVariablesCommand
|
||||||
|
{
|
||||||
|
OptB = new[] {"foo", "bar"}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Option_of_scalar_type_can_use_an_environment_variable_as_fallback_regardless_of_separators()
|
||||||
|
{
|
||||||
|
var (console, stdOut, _) = VirtualConsole.CreateBuffered();
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand<WithEnvironmentVariablesCommand>()
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var exitCode = await application.RunAsync(
|
||||||
|
new[] {"cmd"},
|
||||||
|
new Dictionary<string, string>
|
||||||
|
{
|
||||||
|
["ENV_OPT_A"] = $"foo{Path.PathSeparator}bar"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
var commandInstance = stdOut.GetString().DeserializeJson<WithEnvironmentVariablesCommand>();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
exitCode.Should().Be(0);
|
||||||
|
commandInstance.Should().BeEquivalentTo(new WithEnvironmentVariablesCommand
|
||||||
|
{
|
||||||
|
OptA = $"foo{Path.PathSeparator}bar"
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
174
CliFx.Tests/ErrorReportingSpecs.cs
Normal file
174
CliFx.Tests/ErrorReportingSpecs.cs
Normal file
@@ -0,0 +1,174 @@
|
|||||||
|
using System.Threading.Tasks;
|
||||||
|
using CliFx.Tests.Commands;
|
||||||
|
using FluentAssertions;
|
||||||
|
using Xunit;
|
||||||
|
using Xunit.Abstractions;
|
||||||
|
|
||||||
|
namespace CliFx.Tests
|
||||||
|
{
|
||||||
|
public 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
|
||||||
|
var (console, stdOut, stdErr) = VirtualConsole.CreateBuffered();
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand<GenericExceptionCommand>()
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var exitCode = await application.RunAsync(new[] {"cmd", "-m", "Kaput"});
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
exitCode.Should().NotBe(0);
|
||||||
|
stdOut.GetString().Should().BeEmpty();
|
||||||
|
stdErr.GetString().Should().ContainAll(
|
||||||
|
"System.Exception:",
|
||||||
|
"Kaput", "at",
|
||||||
|
"CliFx.Tests"
|
||||||
|
);
|
||||||
|
|
||||||
|
_output.WriteLine(stdOut.GetString());
|
||||||
|
_output.WriteLine(stdErr.GetString());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Command_may_throw_a_generic_exception_with_inner_exception_which_exits_and_prints_error_message_and_stack_trace()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var (console, stdOut, stdErr) = VirtualConsole.CreateBuffered();
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand<GenericInnerExceptionCommand>()
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var exitCode = await application.RunAsync(new[] {"cmd", "-m", "Kaput", "-i", "FooBar"});
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
exitCode.Should().NotBe(0);
|
||||||
|
stdOut.GetString().Should().BeEmpty();
|
||||||
|
stdErr.GetString().Should().ContainAll(
|
||||||
|
"System.Exception:",
|
||||||
|
"FooBar",
|
||||||
|
"Kaput", "at",
|
||||||
|
"CliFx.Tests"
|
||||||
|
);
|
||||||
|
|
||||||
|
_output.WriteLine(stdOut.GetString());
|
||||||
|
_output.WriteLine(stdErr.GetString());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Command_may_throw_a_specialized_exception_which_exits_with_custom_code_and_prints_minimal_error_details()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var (console, stdOut, stdErr) = VirtualConsole.CreateBuffered();
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand<CommandExceptionCommand>()
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var exitCode = await application.RunAsync(new[] {"cmd", "-m", "Kaput", "-c", "69"});
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
exitCode.Should().Be(69);
|
||||||
|
stdOut.GetString().Should().BeEmpty();
|
||||||
|
stdErr.GetString().Trim().Should().Be("Kaput");
|
||||||
|
|
||||||
|
_output.WriteLine(stdOut.GetString());
|
||||||
|
_output.WriteLine(stdErr.GetString());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Command_may_throw_a_specialized_exception_without_error_message_which_exits_and_prints_full_error_details()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var (console, stdOut, stdErr) = VirtualConsole.CreateBuffered();
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand<CommandExceptionCommand>()
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var exitCode = await application.RunAsync(new[] {"cmd"});
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
exitCode.Should().NotBe(0);
|
||||||
|
stdOut.GetString().Should().BeEmpty();
|
||||||
|
stdErr.GetString().Should().ContainAll(
|
||||||
|
"CliFx.Exceptions.CommandException:",
|
||||||
|
"at",
|
||||||
|
"CliFx.Tests"
|
||||||
|
);
|
||||||
|
|
||||||
|
_output.WriteLine(stdOut.GetString());
|
||||||
|
_output.WriteLine(stdErr.GetString());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Command_may_throw_a_specialized_exception_which_exits_and_prints_help_text()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var (console, stdOut, stdErr) = VirtualConsole.CreateBuffered();
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand<CommandExceptionCommand>()
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var exitCode = await application.RunAsync(new[] {"cmd", "-m", "Kaput", "--show-help"});
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
exitCode.Should().NotBe(0);
|
||||||
|
stdOut.GetString().Should().ContainAll(
|
||||||
|
"Usage",
|
||||||
|
"Options",
|
||||||
|
"-h|--help"
|
||||||
|
);
|
||||||
|
stdErr.GetString().Trim().Should().Be("Kaput");
|
||||||
|
|
||||||
|
_output.WriteLine(stdOut.GetString());
|
||||||
|
_output.WriteLine(stdErr.GetString());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Command_shows_help_text_on_invalid_user_input()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var (console, stdOut, stdErr) = VirtualConsole.CreateBuffered();
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand<DefaultCommand>()
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var exitCode = await application.RunAsync(new[] {"not-a-valid-command", "-r", "foo"});
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
exitCode.Should().NotBe(0);
|
||||||
|
stdOut.GetString().Should().ContainAll(
|
||||||
|
"Usage",
|
||||||
|
"Options",
|
||||||
|
"-h|--help"
|
||||||
|
);
|
||||||
|
stdErr.GetString().Should().NotBeNullOrWhiteSpace();
|
||||||
|
|
||||||
|
_output.WriteLine(stdOut.GetString());
|
||||||
|
_output.WriteLine(stdErr.GetString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
180
CliFx.Tests/HelpTextSpecs.cs
Normal file
180
CliFx.Tests/HelpTextSpecs.cs
Normal file
@@ -0,0 +1,180 @@
|
|||||||
|
using System.Threading.Tasks;
|
||||||
|
using CliFx.Tests.Commands;
|
||||||
|
using FluentAssertions;
|
||||||
|
using Xunit;
|
||||||
|
using Xunit.Abstractions;
|
||||||
|
|
||||||
|
namespace CliFx.Tests
|
||||||
|
{
|
||||||
|
public class HelpTextSpecs
|
||||||
|
{
|
||||||
|
private readonly ITestOutputHelper _output;
|
||||||
|
|
||||||
|
public HelpTextSpecs(ITestOutputHelper output) => _output = output;
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Help_text_shows_usage_format_which_lists_all_parameters()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var (console, stdOut, _) = VirtualConsole.CreateBuffered();
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand<WithParametersCommand>()
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var exitCode = await application.RunAsync(new[] {"cmd", "--help"});
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
exitCode.Should().Be(0);
|
||||||
|
stdOut.GetString().Should().ContainAll(
|
||||||
|
"Usage",
|
||||||
|
"cmd", "<parama>", "<paramb>", "<paramc...>"
|
||||||
|
);
|
||||||
|
|
||||||
|
_output.WriteLine(stdOut.GetString());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Help_text_shows_usage_format_which_lists_all_required_options()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var (console, stdOut, _) = VirtualConsole.CreateBuffered();
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand<WithRequiredOptionsCommand>()
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var exitCode = await application.RunAsync(new[] {"cmd", "--help"});
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
exitCode.Should().Be(0);
|
||||||
|
stdOut.GetString().Should().ContainAll(
|
||||||
|
"Usage",
|
||||||
|
"cmd", "--opt-a <value>", "--opt-c <values...>", "[options]",
|
||||||
|
"Options",
|
||||||
|
"* -a|--opt-a",
|
||||||
|
"-b|--opt-b",
|
||||||
|
"* -c|--opt-c"
|
||||||
|
);
|
||||||
|
|
||||||
|
_output.WriteLine(stdOut.GetString());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Help_text_shows_usage_format_which_lists_available_sub_commands()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var (console, stdOut, _) = VirtualConsole.CreateBuffered();
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand<DefaultCommand>()
|
||||||
|
.AddCommand<NamedCommand>()
|
||||||
|
.AddCommand<NamedSubCommand>()
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var exitCode = await application.RunAsync(new[] {"--help"});
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
exitCode.Should().Be(0);
|
||||||
|
stdOut.GetString().Should().ContainAll(
|
||||||
|
"Usage",
|
||||||
|
"... named",
|
||||||
|
"... named sub"
|
||||||
|
);
|
||||||
|
|
||||||
|
_output.WriteLine(stdOut.GetString());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Help_text_shows_all_valid_values_for_enum_arguments()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var (console, stdOut, _) = VirtualConsole.CreateBuffered();
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand<WithEnumArgumentsCommand>()
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var exitCode = await application.RunAsync(new[] {"cmd", "--help"});
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
exitCode.Should().Be(0);
|
||||||
|
stdOut.GetString().Should().ContainAll(
|
||||||
|
"Parameters",
|
||||||
|
"enum", "Valid values: \"Value1\", \"Value2\", \"Value3\".",
|
||||||
|
"Options",
|
||||||
|
"--enum", "Valid values: \"Value1\", \"Value2\", \"Value3\".",
|
||||||
|
"* --required-enum", "Valid values: \"Value1\", \"Value2\", \"Value3\"."
|
||||||
|
);
|
||||||
|
|
||||||
|
_output.WriteLine(stdOut.GetString());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Help_text_shows_environment_variable_names_for_options_that_have_them_defined()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var (console, stdOut, _) = VirtualConsole.CreateBuffered();
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand<WithEnvironmentVariablesCommand>()
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var exitCode = await application.RunAsync(new[] {"cmd", "--help"});
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
exitCode.Should().Be(0);
|
||||||
|
stdOut.GetString().Should().ContainAll(
|
||||||
|
"Options",
|
||||||
|
"-a|--opt-a", "Environment variable:", "ENV_OPT_A",
|
||||||
|
"-b|--opt-b", "Environment variable:", "ENV_OPT_B"
|
||||||
|
);
|
||||||
|
|
||||||
|
_output.WriteLine(stdOut.GetString());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Help_text_shows_default_values_for_non_required_options()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var (console, stdOut, _) = VirtualConsole.CreateBuffered();
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand<WithDefaultValuesCommand>()
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var exitCode = await application.RunAsync(new[] {"cmd", "--help"});
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
exitCode.Should().Be(0);
|
||||||
|
stdOut.GetString().Should().ContainAll(
|
||||||
|
"Options",
|
||||||
|
"--obj", "Default: \"42\"",
|
||||||
|
"--str", "Default: \"foo\"",
|
||||||
|
"--str-empty", "Default: \"\"",
|
||||||
|
"--str-array", "Default: \"foo\" \"bar\" \"baz\"",
|
||||||
|
"--bool", "Default: \"True\"",
|
||||||
|
"--char", "Default: \"t\"",
|
||||||
|
"--int", "Default: \"1337\"",
|
||||||
|
"--int-nullable", "Default: \"1337\"",
|
||||||
|
"--int-array", "Default: \"1\" \"2\" \"3\"",
|
||||||
|
"--timespan", "Default: \"02:03:00\"",
|
||||||
|
"--enum", "Default: \"Value2\""
|
||||||
|
);
|
||||||
|
|
||||||
|
_output.WriteLine(stdOut.GetString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
10
CliFx.Tests/Internal/JsonExtensions.cs
Normal file
10
CliFx.Tests/Internal/JsonExtensions.cs
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
|
namespace CliFx.Tests.Internal
|
||||||
|
{
|
||||||
|
internal static class JsonExtensions
|
||||||
|
{
|
||||||
|
public static T DeserializeJson<T>(this string json) =>
|
||||||
|
JsonConvert.DeserializeObject<T>(json);
|
||||||
|
}
|
||||||
|
}
|
||||||
235
CliFx.Tests/RoutingSpecs.cs
Normal file
235
CliFx.Tests/RoutingSpecs.cs
Normal file
@@ -0,0 +1,235 @@
|
|||||||
|
using System;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using CliFx.Tests.Commands;
|
||||||
|
using FluentAssertions;
|
||||||
|
using Xunit;
|
||||||
|
using Xunit.Abstractions;
|
||||||
|
|
||||||
|
namespace CliFx.Tests
|
||||||
|
{
|
||||||
|
public class RoutingSpecs
|
||||||
|
{
|
||||||
|
private readonly ITestOutputHelper _output;
|
||||||
|
|
||||||
|
public RoutingSpecs(ITestOutputHelper testOutput) => _output = testOutput;
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Default_command_is_executed_if_provided_arguments_do_not_match_any_named_command()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var (console, stdOut, _) = VirtualConsole.CreateBuffered();
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand<DefaultCommand>()
|
||||||
|
.AddCommand<NamedCommand>()
|
||||||
|
.AddCommand<NamedSubCommand>()
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var exitCode = await application.RunAsync(Array.Empty<string>());
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
exitCode.Should().Be(0);
|
||||||
|
stdOut.GetString().Trim().Should().Be(DefaultCommand.ExpectedOutputText);
|
||||||
|
|
||||||
|
_output.WriteLine(stdOut.GetString());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Specific_named_command_is_executed_if_provided_arguments_match_its_name()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var (console, stdOut, _) = VirtualConsole.CreateBuffered();
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand<DefaultCommand>()
|
||||||
|
.AddCommand<NamedCommand>()
|
||||||
|
.AddCommand<NamedSubCommand>()
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var exitCode = await application.RunAsync(new[] {"named"});
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
exitCode.Should().Be(0);
|
||||||
|
stdOut.GetString().Trim().Should().Be(NamedCommand.ExpectedOutputText);
|
||||||
|
|
||||||
|
_output.WriteLine(stdOut.GetString());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Specific_named_sub_command_is_executed_if_provided_arguments_match_its_name()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var (console, stdOut, _) = VirtualConsole.CreateBuffered();
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand<DefaultCommand>()
|
||||||
|
.AddCommand<NamedCommand>()
|
||||||
|
.AddCommand<NamedSubCommand>()
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var exitCode = await application.RunAsync(new[] {"named", "sub"});
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
exitCode.Should().Be(0);
|
||||||
|
stdOut.GetString().Trim().Should().Be(NamedSubCommand.ExpectedOutputText);
|
||||||
|
|
||||||
|
_output.WriteLine(stdOut.GetString());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Help_text_is_printed_if_no_arguments_were_provided_and_default_command_is_not_defined()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var (console, stdOut, _) = VirtualConsole.CreateBuffered();
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand<NamedCommand>()
|
||||||
|
.AddCommand<NamedSubCommand>()
|
||||||
|
.UseConsole(console)
|
||||||
|
.UseDescription("This will be visible in help")
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var exitCode = await application.RunAsync(Array.Empty<string>());
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
exitCode.Should().Be(0);
|
||||||
|
stdOut.GetString().Should().Contain("This will be visible in help");
|
||||||
|
|
||||||
|
_output.WriteLine(stdOut.GetString());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Help_text_is_printed_if_provided_arguments_contain_the_help_option()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var (console, stdOut, _) = VirtualConsole.CreateBuffered();
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand<DefaultCommand>()
|
||||||
|
.AddCommand<NamedCommand>()
|
||||||
|
.AddCommand<NamedSubCommand>()
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var exitCode = await application.RunAsync(new[] {"--help"});
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
exitCode.Should().Be(0);
|
||||||
|
stdOut.GetString().Should().ContainAll(
|
||||||
|
"Default command description",
|
||||||
|
"Usage"
|
||||||
|
);
|
||||||
|
|
||||||
|
_output.WriteLine(stdOut.GetString());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Help_text_is_printed_if_provided_arguments_contain_the_help_option_even_if_default_command_is_not_defined()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var (console, stdOut, _) = VirtualConsole.CreateBuffered();
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand<NamedCommand>()
|
||||||
|
.AddCommand<NamedSubCommand>()
|
||||||
|
.UseDescription("This will be visible in help")
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var exitCode = await application.RunAsync(new[] {"--help"});
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
exitCode.Should().Be(0);
|
||||||
|
stdOut.GetString().Should().Contain("This will be visible in help");
|
||||||
|
|
||||||
|
_output.WriteLine(stdOut.GetString());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Help_text_for_a_specific_named_command_is_printed_if_provided_arguments_match_its_name_and_contain_the_help_option()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var (console, stdOut, _) = VirtualConsole.CreateBuffered();
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand<DefaultCommand>()
|
||||||
|
.AddCommand<NamedCommand>()
|
||||||
|
.AddCommand<NamedSubCommand>()
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var exitCode = await application.RunAsync(new[] {"named", "--help"});
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
exitCode.Should().Be(0);
|
||||||
|
stdOut.GetString().Should().ContainAll(
|
||||||
|
"Named command description",
|
||||||
|
"Usage",
|
||||||
|
"named"
|
||||||
|
);
|
||||||
|
|
||||||
|
_output.WriteLine(stdOut.GetString());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Help_text_for_a_specific_named_sub_command_is_printed_if_provided_arguments_match_its_name_and_contain_the_help_option()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var (console, stdOut, _) = VirtualConsole.CreateBuffered();
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand<DefaultCommand>()
|
||||||
|
.AddCommand<NamedCommand>()
|
||||||
|
.AddCommand<NamedSubCommand>()
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var exitCode = await application.RunAsync(new[] {"named", "sub", "--help"});
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
exitCode.Should().Be(0);
|
||||||
|
stdOut.GetString().Should().ContainAll(
|
||||||
|
"Named sub command description",
|
||||||
|
"Usage",
|
||||||
|
"named", "sub"
|
||||||
|
);
|
||||||
|
|
||||||
|
_output.WriteLine(stdOut.GetString());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Version_is_printed_if_the_only_provided_argument_is_the_version_option()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var (console, stdOut, _) = VirtualConsole.CreateBuffered();
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand<DefaultCommand>()
|
||||||
|
.AddCommand<NamedCommand>()
|
||||||
|
.AddCommand<NamedSubCommand>()
|
||||||
|
.UseVersionText("v6.9")
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var exitCode = await application.RunAsync(new[] {"--version"});
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
exitCode.Should().Be(0);
|
||||||
|
stdOut.GetString().Trim().Should().Be("v6.9");
|
||||||
|
|
||||||
|
_output.WriteLine(stdOut.GetString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,37 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using CliFx.Models;
|
|
||||||
using CliFx.Services;
|
|
||||||
using CliFx.Tests.TestCommands;
|
|
||||||
using FluentAssertions;
|
|
||||||
using NUnit.Framework;
|
|
||||||
|
|
||||||
namespace CliFx.Tests.Services
|
|
||||||
{
|
|
||||||
[TestFixture]
|
|
||||||
public class CommandFactoryTests
|
|
||||||
{
|
|
||||||
private static CommandSchema GetCommandSchema(Type commandType) =>
|
|
||||||
new CommandSchemaResolver().GetCommandSchemas(new[] {commandType}).Single();
|
|
||||||
|
|
||||||
private static IEnumerable<TestCaseData> GetTestCases_CreateCommand()
|
|
||||||
{
|
|
||||||
yield return new TestCaseData(GetCommandSchema(typeof(HelloWorldDefaultCommand)));
|
|
||||||
}
|
|
||||||
|
|
||||||
[Test]
|
|
||||||
[TestCaseSource(nameof(GetTestCases_CreateCommand))]
|
|
||||||
public void CreateCommand_Test(CommandSchema commandSchema)
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var factory = new CommandFactory();
|
|
||||||
|
|
||||||
// Act
|
|
||||||
var command = factory.CreateCommand(commandSchema);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
command.Should().BeOfType(commandSchema.Type);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,136 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using CliFx.Exceptions;
|
|
||||||
using CliFx.Models;
|
|
||||||
using CliFx.Services;
|
|
||||||
using CliFx.Tests.TestCommands;
|
|
||||||
using FluentAssertions;
|
|
||||||
using NUnit.Framework;
|
|
||||||
|
|
||||||
namespace CliFx.Tests.Services
|
|
||||||
{
|
|
||||||
[TestFixture]
|
|
||||||
public class CommandInitializerTests
|
|
||||||
{
|
|
||||||
private static CommandSchema GetCommandSchema(Type commandType) =>
|
|
||||||
new CommandSchemaResolver().GetCommandSchemas(new[] {commandType}).Single();
|
|
||||||
|
|
||||||
private static IEnumerable<TestCaseData> GetTestCases_InitializeCommand()
|
|
||||||
{
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new DivideCommand(),
|
|
||||||
GetCommandSchema(typeof(DivideCommand)),
|
|
||||||
new CommandInput("div", new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput("dividend", "13"),
|
|
||||||
new CommandOptionInput("divisor", "8")
|
|
||||||
}),
|
|
||||||
new DivideCommand {Dividend = 13, Divisor = 8}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new DivideCommand(),
|
|
||||||
GetCommandSchema(typeof(DivideCommand)),
|
|
||||||
new CommandInput("div", new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput("dividend", "13"),
|
|
||||||
new CommandOptionInput("d", "8")
|
|
||||||
}),
|
|
||||||
new DivideCommand {Dividend = 13, Divisor = 8}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new DivideCommand(),
|
|
||||||
GetCommandSchema(typeof(DivideCommand)),
|
|
||||||
new CommandInput("div", new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput("D", "13"),
|
|
||||||
new CommandOptionInput("d", "8")
|
|
||||||
}),
|
|
||||||
new DivideCommand {Dividend = 13, Divisor = 8}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new ConcatCommand(),
|
|
||||||
GetCommandSchema(typeof(ConcatCommand)),
|
|
||||||
new CommandInput("concat", new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput("i", new[] {"foo", " ", "bar"})
|
|
||||||
}),
|
|
||||||
new ConcatCommand {Inputs = new[] {"foo", " ", "bar"}}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new ConcatCommand(),
|
|
||||||
GetCommandSchema(typeof(ConcatCommand)),
|
|
||||||
new CommandInput("concat", new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput("i", new[] {"foo", "bar"}),
|
|
||||||
new CommandOptionInput("s", " ")
|
|
||||||
}),
|
|
||||||
new ConcatCommand {Inputs = new[] {"foo", "bar"}, Separator = " "}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static IEnumerable<TestCaseData> GetTestCases_InitializeCommand_Negative()
|
|
||||||
{
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new DivideCommand(),
|
|
||||||
GetCommandSchema(typeof(DivideCommand)),
|
|
||||||
new CommandInput("div")
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new DivideCommand(),
|
|
||||||
GetCommandSchema(typeof(DivideCommand)),
|
|
||||||
new CommandInput("div", new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput("D", "13")
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new ConcatCommand(),
|
|
||||||
GetCommandSchema(typeof(ConcatCommand)),
|
|
||||||
new CommandInput("concat")
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new ConcatCommand(),
|
|
||||||
GetCommandSchema(typeof(ConcatCommand)),
|
|
||||||
new CommandInput("concat", new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput("s", "_")
|
|
||||||
})
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Test]
|
|
||||||
[TestCaseSource(nameof(GetTestCases_InitializeCommand))]
|
|
||||||
public void InitializeCommand_Test(ICommand command, CommandSchema commandSchema, CommandInput commandInput,
|
|
||||||
ICommand expectedCommand)
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var initializer = new CommandInitializer();
|
|
||||||
|
|
||||||
// Act
|
|
||||||
initializer.InitializeCommand(command, commandSchema, commandInput);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
command.Should().BeEquivalentTo(expectedCommand, o => o.RespectingRuntimeTypes());
|
|
||||||
}
|
|
||||||
|
|
||||||
[Test]
|
|
||||||
[TestCaseSource(nameof(GetTestCases_InitializeCommand_Negative))]
|
|
||||||
public void InitializeCommand_Negative_Test(ICommand command, CommandSchema commandSchema, CommandInput commandInput)
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var initializer = new CommandInitializer();
|
|
||||||
|
|
||||||
// Act & Assert
|
|
||||||
initializer.Invoking(i => i.InitializeCommand(command, commandSchema, commandInput))
|
|
||||||
.Should().ThrowExactly<CliFxException>();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,219 +0,0 @@
|
|||||||
using System.Collections.Generic;
|
|
||||||
using CliFx.Models;
|
|
||||||
using CliFx.Services;
|
|
||||||
using FluentAssertions;
|
|
||||||
using NUnit.Framework;
|
|
||||||
|
|
||||||
namespace CliFx.Tests.Services
|
|
||||||
{
|
|
||||||
[TestFixture]
|
|
||||||
public class CommandInputParserTests
|
|
||||||
{
|
|
||||||
private static IEnumerable<TestCaseData> GetTestCases_ParseCommandInput()
|
|
||||||
{
|
|
||||||
yield return new TestCaseData(new string[0], CommandInput.Empty);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {"--option", "value"},
|
|
||||||
new CommandInput(new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput("option", "value")
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {"--option1", "value1", "--option2", "value2"},
|
|
||||||
new CommandInput(new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput("option1", "value1"),
|
|
||||||
new CommandOptionInput("option2", "value2")
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {"--option", "value1", "value2"},
|
|
||||||
new CommandInput(new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput("option", new[] {"value1", "value2"})
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {"--option", "value1", "--option", "value2"},
|
|
||||||
new CommandInput(new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput("option", new[] {"value1", "value2"})
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {"-a", "value"},
|
|
||||||
new CommandInput(new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput("a", "value")
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {"-a", "value1", "-b", "value2"},
|
|
||||||
new CommandInput(new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput("a", "value1"),
|
|
||||||
new CommandOptionInput("b", "value2")
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {"-a", "value1", "value2"},
|
|
||||||
new CommandInput(new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput("a", new[] {"value1", "value2"})
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {"-a", "value1", "-a", "value2"},
|
|
||||||
new CommandInput(new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput("a", new[] {"value1", "value2"})
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {"--option1", "value1", "-b", "value2"},
|
|
||||||
new CommandInput(new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput("option1", "value1"),
|
|
||||||
new CommandOptionInput("b", "value2")
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {"--switch"},
|
|
||||||
new CommandInput(new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput("switch")
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {"--switch1", "--switch2"},
|
|
||||||
new CommandInput(new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput("switch1"),
|
|
||||||
new CommandOptionInput("switch2")
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {"-s"},
|
|
||||||
new CommandInput(new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput("s")
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {"-a", "-b"},
|
|
||||||
new CommandInput(new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput("a"),
|
|
||||||
new CommandOptionInput("b")
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {"-ab"},
|
|
||||||
new CommandInput(new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput("a"),
|
|
||||||
new CommandOptionInput("b")
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {"-ab", "value"},
|
|
||||||
new CommandInput(new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput("a"),
|
|
||||||
new CommandOptionInput("b", "value")
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {"command"},
|
|
||||||
new CommandInput("command")
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {"command", "--option", "value"},
|
|
||||||
new CommandInput("command", new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput("option", "value")
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {"long", "command", "name"},
|
|
||||||
new CommandInput("long command name")
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {"long", "command", "name", "--option", "value"},
|
|
||||||
new CommandInput("long command name", new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput("option", "value")
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {"[debug]"},
|
|
||||||
new CommandInput(null,
|
|
||||||
new[] {"debug"},
|
|
||||||
new CommandOptionInput[0])
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {"[debug]", "[preview]"},
|
|
||||||
new CommandInput(null,
|
|
||||||
new[] {"debug", "preview"},
|
|
||||||
new CommandOptionInput[0])
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {"[debug]", "[preview]", "-o", "value"},
|
|
||||||
new CommandInput(null,
|
|
||||||
new[] {"debug", "preview"},
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput("o", "value")
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {"command", "[debug]", "[preview]", "-o", "value"},
|
|
||||||
new CommandInput("command",
|
|
||||||
new[] {"debug", "preview"},
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput("o", "value")
|
|
||||||
})
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Test]
|
|
||||||
[TestCaseSource(nameof(GetTestCases_ParseCommandInput))]
|
|
||||||
public void ParseCommandInput_Test(IReadOnlyList<string> commandLineArguments,
|
|
||||||
CommandInput expectedCommandInput)
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var parser = new CommandInputParser();
|
|
||||||
|
|
||||||
// Act
|
|
||||||
var commandInput = parser.ParseCommandInput(commandLineArguments);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
commandInput.Should().BeEquivalentTo(expectedCommandInput);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,307 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Globalization;
|
|
||||||
using CliFx.Exceptions;
|
|
||||||
using CliFx.Models;
|
|
||||||
using CliFx.Services;
|
|
||||||
using CliFx.Tests.TestCustomTypes;
|
|
||||||
using FluentAssertions;
|
|
||||||
using NUnit.Framework;
|
|
||||||
|
|
||||||
namespace CliFx.Tests.Services
|
|
||||||
{
|
|
||||||
[TestFixture]
|
|
||||||
public class CommandOptionInputConverterTests
|
|
||||||
{
|
|
||||||
private static IEnumerable<TestCaseData> GetTestCases_ConvertOptionInput()
|
|
||||||
{
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new CommandOptionInput("option", "value"),
|
|
||||||
typeof(string),
|
|
||||||
"value"
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new CommandOptionInput("option", "value"),
|
|
||||||
typeof(object),
|
|
||||||
"value"
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new CommandOptionInput("option", "true"),
|
|
||||||
typeof(bool),
|
|
||||||
true
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new CommandOptionInput("option", "false"),
|
|
||||||
typeof(bool),
|
|
||||||
false
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new CommandOptionInput("option"),
|
|
||||||
typeof(bool),
|
|
||||||
true
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new CommandOptionInput("option", "a"),
|
|
||||||
typeof(char),
|
|
||||||
'a'
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new CommandOptionInput("option", "15"),
|
|
||||||
typeof(sbyte),
|
|
||||||
(sbyte) 15
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new CommandOptionInput("option", "15"),
|
|
||||||
typeof(byte),
|
|
||||||
(byte) 15
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new CommandOptionInput("option", "15"),
|
|
||||||
typeof(short),
|
|
||||||
(short) 15
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new CommandOptionInput("option", "15"),
|
|
||||||
typeof(ushort),
|
|
||||||
(ushort) 15
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new CommandOptionInput("option", "123"),
|
|
||||||
typeof(int),
|
|
||||||
123
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new CommandOptionInput("option", "123"),
|
|
||||||
typeof(uint),
|
|
||||||
123u
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new CommandOptionInput("option", "123"),
|
|
||||||
typeof(long),
|
|
||||||
123L
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new CommandOptionInput("option", "123"),
|
|
||||||
typeof(ulong),
|
|
||||||
123UL
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new CommandOptionInput("option", "123.45"),
|
|
||||||
typeof(float),
|
|
||||||
123.45f
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new CommandOptionInput("option", "123.45"),
|
|
||||||
typeof(double),
|
|
||||||
123.45
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new CommandOptionInput("option", "123.45"),
|
|
||||||
typeof(decimal),
|
|
||||||
123.45m
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new CommandOptionInput("option", "28 Apr 1995"),
|
|
||||||
typeof(DateTime),
|
|
||||||
new DateTime(1995, 04, 28)
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new CommandOptionInput("option", "28 Apr 1995"),
|
|
||||||
typeof(DateTimeOffset),
|
|
||||||
new DateTimeOffset(new DateTime(1995, 04, 28))
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new CommandOptionInput("option", "00:14:59"),
|
|
||||||
typeof(TimeSpan),
|
|
||||||
new TimeSpan(00, 14, 59)
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new CommandOptionInput("option", "value2"),
|
|
||||||
typeof(TestEnum),
|
|
||||||
TestEnum.Value2
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new CommandOptionInput("option", "666"),
|
|
||||||
typeof(int?),
|
|
||||||
666
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new CommandOptionInput("option"),
|
|
||||||
typeof(int?),
|
|
||||||
null
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new CommandOptionInput("option", "value3"),
|
|
||||||
typeof(TestEnum?),
|
|
||||||
TestEnum.Value3
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new CommandOptionInput("option"),
|
|
||||||
typeof(TestEnum?),
|
|
||||||
null
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new CommandOptionInput("option", "01:00:00"),
|
|
||||||
typeof(TimeSpan?),
|
|
||||||
new TimeSpan(01, 00, 00)
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new CommandOptionInput("option"),
|
|
||||||
typeof(TimeSpan?),
|
|
||||||
null
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new CommandOptionInput("option", "value"),
|
|
||||||
typeof(TestStringConstructable),
|
|
||||||
new TestStringConstructable("value")
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new CommandOptionInput("option", "value"),
|
|
||||||
typeof(TestStringParseable),
|
|
||||||
TestStringParseable.Parse("value")
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new CommandOptionInput("option", "value"),
|
|
||||||
typeof(TestStringParseableWithFormatProvider),
|
|
||||||
TestStringParseableWithFormatProvider.Parse("value", CultureInfo.InvariantCulture)
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new CommandOptionInput("option", new[] {"value1", "value2"}),
|
|
||||||
typeof(string[]),
|
|
||||||
new[] {"value1", "value2"}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new CommandOptionInput("option", new[] {"value1", "value2"}),
|
|
||||||
typeof(object[]),
|
|
||||||
new[] {"value1", "value2"}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new CommandOptionInput("option", new[] {"47", "69"}),
|
|
||||||
typeof(int[]),
|
|
||||||
new[] {47, 69}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new CommandOptionInput("option", new[] {"value1", "value3"}),
|
|
||||||
typeof(TestEnum[]),
|
|
||||||
new[] {TestEnum.Value1, TestEnum.Value3}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new CommandOptionInput("option", new[] {"1337", "2441"}),
|
|
||||||
typeof(int?[]),
|
|
||||||
new int?[] {1337, 2441}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new CommandOptionInput("option", new[] {"value1", "value2"}),
|
|
||||||
typeof(TestStringConstructable[]),
|
|
||||||
new[] {new TestStringConstructable("value1"), new TestStringConstructable("value2")}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new CommandOptionInput("option", new[] {"value1", "value2"}),
|
|
||||||
typeof(IEnumerable),
|
|
||||||
new[] {"value1", "value2"}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new CommandOptionInput("option", new[] {"value1", "value2"}),
|
|
||||||
typeof(IEnumerable<string>),
|
|
||||||
new[] {"value1", "value2"}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new CommandOptionInput("option", new[] {"value1", "value2"}),
|
|
||||||
typeof(IReadOnlyList<string>),
|
|
||||||
new[] {"value1", "value2"}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new CommandOptionInput("option", new[] {"value1", "value2"}),
|
|
||||||
typeof(List<string>),
|
|
||||||
new List<string> {"value1", "value2"}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new CommandOptionInput("option", new[] {"value1", "value2"}),
|
|
||||||
typeof(HashSet<string>),
|
|
||||||
new HashSet<string> {"value1", "value2"}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static IEnumerable<TestCaseData> GetTestCases_ConvertOptionInput_Negative()
|
|
||||||
{
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new CommandOptionInput("option", "1234.5"),
|
|
||||||
typeof(int)
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new CommandOptionInput("option", "123"),
|
|
||||||
typeof(TestNonStringParseable)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Test]
|
|
||||||
[TestCaseSource(nameof(GetTestCases_ConvertOptionInput))]
|
|
||||||
public void ConvertOptionInput_Test(CommandOptionInput optionInput, Type targetType,
|
|
||||||
object expectedConvertedValue)
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var converter = new CommandOptionInputConverter();
|
|
||||||
|
|
||||||
// Act
|
|
||||||
var convertedValue = converter.ConvertOptionInput(optionInput, targetType);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
convertedValue.Should().BeEquivalentTo(expectedConvertedValue);
|
|
||||||
convertedValue?.Should().BeAssignableTo(targetType);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Test]
|
|
||||||
[TestCaseSource(nameof(GetTestCases_ConvertOptionInput_Negative))]
|
|
||||||
public void ConvertOptionInput_Negative_Test(CommandOptionInput optionInput, Type targetType)
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var converter = new CommandOptionInputConverter();
|
|
||||||
|
|
||||||
// Act & Assert
|
|
||||||
converter.Invoking(c => c.ConvertOptionInput(optionInput, targetType))
|
|
||||||
.Should().ThrowExactly<CliFxException>();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,109 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using CliFx.Exceptions;
|
|
||||||
using CliFx.Models;
|
|
||||||
using CliFx.Services;
|
|
||||||
using CliFx.Tests.TestCommands;
|
|
||||||
using FluentAssertions;
|
|
||||||
using NUnit.Framework;
|
|
||||||
|
|
||||||
namespace CliFx.Tests.Services
|
|
||||||
{
|
|
||||||
[TestFixture]
|
|
||||||
public class CommandSchemaResolverTests
|
|
||||||
{
|
|
||||||
private static IEnumerable<TestCaseData> GetTestCases_GetCommandSchemas()
|
|
||||||
{
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(DivideCommand), typeof(ConcatCommand)},
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandSchema(typeof(DivideCommand), "div", "Divide one number by another.",
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandOptionSchema(typeof(DivideCommand).GetProperty(nameof(DivideCommand.Dividend)),
|
|
||||||
"dividend", 'D', true, "The number to divide."),
|
|
||||||
new CommandOptionSchema(typeof(DivideCommand).GetProperty(nameof(DivideCommand.Divisor)),
|
|
||||||
"divisor", 'd', true, "The number to divide by.")
|
|
||||||
}),
|
|
||||||
new CommandSchema(typeof(ConcatCommand), "concat", "Concatenate strings.",
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandOptionSchema(typeof(ConcatCommand).GetProperty(nameof(ConcatCommand.Inputs)),
|
|
||||||
null, 'i', true, "Input strings."),
|
|
||||||
new CommandOptionSchema(typeof(ConcatCommand).GetProperty(nameof(ConcatCommand.Separator)),
|
|
||||||
null, 's', false, "String separator.")
|
|
||||||
})
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(HelloWorldDefaultCommand)},
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandSchema(typeof(HelloWorldDefaultCommand), null, null, new CommandOptionSchema[0])
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static IEnumerable<TestCaseData> GetTestCases_GetCommandSchemas_Negative()
|
|
||||||
{
|
|
||||||
yield return new TestCaseData(new object[]
|
|
||||||
{
|
|
||||||
new Type[0]
|
|
||||||
});
|
|
||||||
|
|
||||||
yield return new TestCaseData(new object[]
|
|
||||||
{
|
|
||||||
new[] {typeof(NonImplementedCommand)}
|
|
||||||
});
|
|
||||||
|
|
||||||
yield return new TestCaseData(new object[]
|
|
||||||
{
|
|
||||||
new[] {typeof(NonAnnotatedCommand)}
|
|
||||||
});
|
|
||||||
|
|
||||||
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(ExceptionCommand), typeof(CommandExceptionCommand)}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
[Test]
|
|
||||||
[TestCaseSource(nameof(GetTestCases_GetCommandSchemas))]
|
|
||||||
public void GetCommandSchemas_Test(IReadOnlyList<Type> commandTypes,
|
|
||||||
IReadOnlyList<CommandSchema> expectedCommandSchemas)
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var commandSchemaResolver = new CommandSchemaResolver();
|
|
||||||
|
|
||||||
// Act
|
|
||||||
var commandSchemas = commandSchemaResolver.GetCommandSchemas(commandTypes);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
commandSchemas.Should().BeEquivalentTo(expectedCommandSchemas);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Test]
|
|
||||||
[TestCaseSource(nameof(GetTestCases_GetCommandSchemas_Negative))]
|
|
||||||
public void GetCommandSchemas_Negative_Test(IReadOnlyList<Type> commandTypes)
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var resolver = new CommandSchemaResolver();
|
|
||||||
|
|
||||||
// Act & Assert
|
|
||||||
resolver.Invoking(r => r.GetCommandSchemas(commandTypes))
|
|
||||||
.Should().ThrowExactly<CliFxException>();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,40 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using CliFx.Models;
|
|
||||||
using CliFx.Services;
|
|
||||||
using CliFx.Tests.TestCommands;
|
|
||||||
using FluentAssertions;
|
|
||||||
using NUnit.Framework;
|
|
||||||
|
|
||||||
namespace CliFx.Tests.Services
|
|
||||||
{
|
|
||||||
[TestFixture]
|
|
||||||
public class DelegateCommandFactoryTests
|
|
||||||
{
|
|
||||||
private static CommandSchema GetCommandSchema(Type commandType) =>
|
|
||||||
new CommandSchemaResolver().GetCommandSchemas(new[] {commandType}).Single();
|
|
||||||
|
|
||||||
private static IEnumerable<TestCaseData> GetTestCases_CreateCommand()
|
|
||||||
{
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new Func<CommandSchema, ICommand>(schema => (ICommand) Activator.CreateInstance(schema.Type)),
|
|
||||||
GetCommandSchema(typeof(HelloWorldDefaultCommand))
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Test]
|
|
||||||
[TestCaseSource(nameof(GetTestCases_CreateCommand))]
|
|
||||||
public void CreateCommand_Test(Func<CommandSchema, ICommand> factoryMethod, CommandSchema commandSchema)
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var factory = new DelegateCommandFactory(factoryMethod);
|
|
||||||
|
|
||||||
// Act
|
|
||||||
var command = factory.CreateCommand(commandSchema);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
command.Should().BeOfType(commandSchema.Type);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,109 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.IO;
|
|
||||||
using System.Linq;
|
|
||||||
using CliFx.Models;
|
|
||||||
using CliFx.Services;
|
|
||||||
using CliFx.Tests.TestCommands;
|
|
||||||
using FluentAssertions;
|
|
||||||
using NUnit.Framework;
|
|
||||||
|
|
||||||
namespace CliFx.Tests.Services
|
|
||||||
{
|
|
||||||
[TestFixture]
|
|
||||||
public class HelpTextRendererTests
|
|
||||||
{
|
|
||||||
private static HelpTextSource CreateHelpTextSource(IReadOnlyList<Type> availableCommandTypes, Type targetCommandType)
|
|
||||||
{
|
|
||||||
var commandSchemaResolver = new CommandSchemaResolver();
|
|
||||||
|
|
||||||
var applicationMetadata = new ApplicationMetadata("TestApp", "testapp", "1.0", null);
|
|
||||||
var availableCommandSchemas = commandSchemaResolver.GetCommandSchemas(availableCommandTypes);
|
|
||||||
var targetCommandSchema = availableCommandSchemas.Single(s => s.Type == targetCommandType);
|
|
||||||
|
|
||||||
return new HelpTextSource(applicationMetadata, availableCommandSchemas, targetCommandSchema);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static IEnumerable<TestCaseData> GetTestCases_RenderHelpText()
|
|
||||||
{
|
|
||||||
yield return new TestCaseData(
|
|
||||||
CreateHelpTextSource(
|
|
||||||
new[] {typeof(HelpDefaultCommand), typeof(HelpNamedCommand), typeof(HelpSubCommand)},
|
|
||||||
typeof(HelpDefaultCommand)),
|
|
||||||
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
"Description",
|
|
||||||
"HelpDefaultCommand description.",
|
|
||||||
"Usage",
|
|
||||||
"[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(
|
|
||||||
CreateHelpTextSource(
|
|
||||||
new[] {typeof(HelpDefaultCommand), typeof(HelpNamedCommand), typeof(HelpSubCommand)},
|
|
||||||
typeof(HelpNamedCommand)),
|
|
||||||
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
"Description",
|
|
||||||
"HelpNamedCommand description.",
|
|
||||||
"Usage",
|
|
||||||
"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(
|
|
||||||
CreateHelpTextSource(
|
|
||||||
new[] {typeof(HelpDefaultCommand), typeof(HelpNamedCommand), typeof(HelpSubCommand)},
|
|
||||||
typeof(HelpSubCommand)),
|
|
||||||
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
"Description",
|
|
||||||
"HelpSubCommand description.",
|
|
||||||
"Usage",
|
|
||||||
"cmd sub", "[options]",
|
|
||||||
"Options",
|
|
||||||
"-e|--option-e", "OptionE description.",
|
|
||||||
"-h|--help", "Shows help text."
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Test]
|
|
||||||
[TestCaseSource(nameof(GetTestCases_RenderHelpText))]
|
|
||||||
public void RenderHelpText_Test(HelpTextSource source,
|
|
||||||
IReadOnlyList<string> expectedSubstrings)
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
using (var stdout = new StringWriter())
|
|
||||||
{
|
|
||||||
var console = new VirtualConsole(stdout);
|
|
||||||
var renderer = new HelpTextRenderer();
|
|
||||||
|
|
||||||
// Act
|
|
||||||
renderer.RenderHelpText(console, source);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
stdout.ToString().Should().ContainAll(expectedSubstrings);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,41 +0,0 @@
|
|||||||
using System;
|
|
||||||
using CliFx.Services;
|
|
||||||
using FluentAssertions;
|
|
||||||
using NUnit.Framework;
|
|
||||||
|
|
||||||
namespace CliFx.Tests.Services
|
|
||||||
{
|
|
||||||
[TestFixture]
|
|
||||||
public class SystemConsoleTests
|
|
||||||
{
|
|
||||||
[TearDown]
|
|
||||||
public void TearDown()
|
|
||||||
{
|
|
||||||
// Reset console color so it doesn't carry on into next tests
|
|
||||||
Console.ResetColor();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Make sure console correctly wraps around System.Console
|
|
||||||
[Test]
|
|
||||||
public void All_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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user