-
Notifications
You must be signed in to change notification settings - Fork 390
/
Copy pathErrorReportingFunctionalTests.cs
134 lines (107 loc) · 3.58 KB
/
ErrorReportingFunctionalTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
/*
using System.CommandLine.Help;
using System.CommandLine.Invocation;
*/
using System.IO;
using FluentAssertions;
using Xunit;
using System.CommandLine.Tests.Utility;
using System.Linq;
using System.Threading.Tasks;
namespace System.CommandLine.Tests;
public class ErrorReportingFunctionalTests
{
// TODO: these tests depend on help output
/*
[Fact] // https://github.com/dotnet/command-line-api/issues/817
public void Parse_error_reporting_reports_error_when_help_is_used_and_required_subcommand_is_missing()
{
var root = new CliRootCommand
{
new CliCommand("inner"),
new HelpOption()
};
var output = new StringWriter();
var parseResult = root.Parse("", new CliConfiguration(root)
{
Output = output,
});
parseResult.Errors.Should().NotBeEmpty();
var result = parseResult.Invoke();
result.Should().Be(1);
output.ToString().Should().ShowHelp();
}
[Fact]
public void Help_display_can_be_disabled()
{
CliRootCommand rootCommand = new()
{
new CliOption<bool>("--verbose")
};
CliConfiguration config = new(rootCommand)
{
Output = new StringWriter()
};
var result = rootCommand.Parse("oops", config);
if (result.Action is CliDiagnosticAction cliDiagnostic)
{
cliDiagnostic.ShowHelp = false;
}
result.Invoke();
var output = config.Output.ToString();
output.Should().NotShowHelp();
}
[Theory] // https://github.com/dotnet/command-line-api/issues/2226
[InlineData(true)]
[InlineData(false)]
public void When_there_are_parse_errors_then_customized_help_action_is_used_if_present(bool useAsyncAction)
{
var wasCalled = false;
CliRootCommand rootCommand = new();
rootCommand.Options.Clear();
CliAction customHelpAction = useAsyncAction
? new AsynchronousTestAction(_ => wasCalled = true)
: new SynchronousTestAction(_ => wasCalled = true);
rootCommand.Add(new HelpOption
{
Action = customHelpAction
});
rootCommand.Parse("oops").Invoke();
wasCalled.Should().BeTrue();
}
[Fact]
public async Task When_there_are_parse_errors_then_customized_help_action_on_ancestor_is_used_if_present()
{
bool rootHelpWasCalled = false;
var rootCommand = new CliRootCommand
{
new CliCommand("child")
{
new CliCommand("grandchild")
}
};
rootCommand.Options.OfType<HelpOption>().Single().Action = new SynchronousTestAction(_ =>
{
rootHelpWasCalled = true;
});
var config = new CliConfiguration(rootCommand);
await config.Parse("child grandchild oops").InvokeAsync();
rootHelpWasCalled.Should().BeTrue();
}
[Fact]
public void When_no_help_option_is_present_then_help_is_not_shown_for_parse_errors()
{
CliRootCommand rootCommand = new();
rootCommand.Options.Clear();
var output = new StringWriter();
CliConfiguration config = new(rootCommand)
{
Output = output
};
config.Parse("oops").Invoke();
output.ToString().Should().NotShowHelp();
}
*/
}