-
Notifications
You must be signed in to change notification settings - Fork 417
Expand file tree
/
Copy pathInvocationTests.cs
More file actions
549 lines (444 loc) · 17.1 KB
/
InvocationTests.cs
File metadata and controls
549 lines (444 loc) · 17.1 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
// 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.Parsing;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using FluentAssertions;
using FluentAssertions.Execution;
using Xunit;
namespace System.CommandLine.Tests.Invocation
{
public class InvocationTests
{
[Fact]
public async Task Command_InvokeAsync_displays_help_when_HelpOption_is_present()
{
var command = new Command("the-command")
{
new HelpOption()
};
var theHelpText = "the help text";
command.Description = theHelpText;
StringWriter output = new();
await command.Parse("-h").InvokeAsync(new() { Output = output }, CancellationToken.None);
output.ToString()
.Should()
.Contain(theHelpText);
}
[Fact]
public void Command_Invoke_displays_help_when_HelpOption_is_present()
{
var command = new Command("the-command")
{
new HelpOption()
};
var theHelpText = "the help text";
command.Description = theHelpText;
StringWriter output = new ();
command.Parse("-h").Invoke(new() { Output = output });
output.ToString()
.Should()
.Contain(theHelpText);
}
[Fact]
public async Task RootCommand_InvokeAsync_returns_0_when_handler_is_successful()
{
var wasCalled = false;
var rootCommand = new RootCommand();
rootCommand.SetAction((_) => wasCalled = true);
var result = await rootCommand.Parse("").InvokeAsync();
wasCalled.Should().BeTrue();
result.Should().Be(0);
}
[Fact]
public void RootCommand_Invoke_returns_0_when_handler_is_successful()
{
var wasCalled = false;
var rootCommand = new RootCommand();
rootCommand.SetAction(_ => wasCalled = true);
int result = rootCommand.Parse("").Invoke();
wasCalled.Should().BeTrue();
result.Should().Be(0);
}
[Fact]
public async Task RootCommand_InvokeAsync_returns_1_when_handler_throws()
{
var wasCalled = false;
var rootCommand = new RootCommand();
rootCommand.SetAction((_, _) =>
{
wasCalled = true;
return Task.FromException(new Exception("oops!"));
});
var resultCode = await rootCommand.Parse("").InvokeAsync();
wasCalled.Should().BeTrue();
resultCode.Should().Be(1);
}
[Fact]
public void RootCommand_Invoke_returns_1_when_handler_throws()
{
var wasCalled = false;
var rootCommand = new RootCommand();
rootCommand.SetAction((_, _) =>
{
wasCalled = true;
throw new Exception("oops!");
// Help the compiler pick a CommandHandler.Create overload.
#pragma warning disable CS0162 // Unreachable code detected
return Task.FromResult(0);
#pragma warning restore CS0162
});
var resultCode = rootCommand.Parse("").Invoke();
wasCalled.Should().BeTrue();
resultCode.Should().Be(1);
}
[Fact]
public void Custom_RootCommand_Action_can_set_custom_result_code_via_Invoke()
{
var rootCommand = new RootCommand();
rootCommand.SetAction(_ => 123);
rootCommand.Parse("").Invoke().Should().Be(123);
}
[Fact]
public async Task Custom_RootCommand_Action_can_set_custom_result_code_via_InvokeAsync()
{
var rootCommand = new RootCommand();
rootCommand.SetAction((_, _) => Task.FromResult(456));
(await rootCommand.Parse("").InvokeAsync()).Should().Be(456);
}
[Fact]
public void Anonymous_RootCommand_Task_returning_Action_can_set_custom_result_code_via_Invoke()
{
var rootCommand = new RootCommand();
rootCommand.SetAction((_, _) => Task.FromResult(123));
rootCommand.Parse("").Invoke().Should().Be(123);
}
[Fact]
public async Task Anonymous_RootCommand_Task_returning_Action_can_set_custom_result_code_via_InvokeAsync()
{
var rootCommand = new RootCommand();
rootCommand.SetAction((_, _) => Task.FromResult(123));
(await rootCommand.Parse("").InvokeAsync()).Should().Be(123);
}
[Fact]
public async Task Anonymous_RootCommand_Task_int_returning_Action_can_set_custom_result_code_via_InvokeAsync()
{
var rootCommand = new RootCommand();
rootCommand.SetAction(_ => Task.FromResult(123));
(await rootCommand.Parse("").InvokeAsync()).Should().Be(123);
}
[Fact]
public void Anonymous_RootCommand_int_returning_Action_can_set_custom_result_code_via_Invoke()
{
var rootCommand = new RootCommand();
rootCommand.SetAction(_ => 123);
rootCommand.Parse("").Invoke().Should().Be(123);
}
[Fact]
public async Task Anonymous_RootCommand_int_returning_Action_can_set_custom_result_code_via_InvokeAsync()
{
var rootCommand = new RootCommand();
rootCommand.SetAction(_ => 123);
(await rootCommand.Parse("").InvokeAsync()).Should().Be(123);
}
[Fact] // https://github.com/dotnet/command-line-api/issues/2562
public void Anonymous_async_action_is_not_mapped_into_sync_void_with_fire_and_forget()
{
RootCommand rootCommand = new();
using CancellationTokenSource cts = new();
Task delay = Task.Delay(TimeSpan.FromHours(1), cts.Token);
rootCommand.SetAction(async parseResult =>
{
await delay;
});
Task started = rootCommand.Parse("").InvokeAsync();
// The action is supposed to wait for an hour, so it should not complete immediately.
started.IsCompleted.Should().BeFalse();
cts.Cancel();
}
[Fact]
public void Terminating_option_action_short_circuits_command_action()
{
bool optionActionWasCalled = false;
SynchronousTestAction optionAction = new(_ => optionActionWasCalled = true, terminating: true);
bool commandActionWasCalled = false;
Option<bool> option = new("--test")
{
Action = optionAction
};
Command command = new Command("cmd")
{
option
};
command.SetAction(_ =>
{
commandActionWasCalled = true;
});
ParseResult parseResult = command.Parse("cmd --test");
parseResult.Action.Should().NotBeNull();
optionActionWasCalled.Should().BeFalse();
commandActionWasCalled.Should().BeFalse();
parseResult.Invoke().Should().Be(0);
optionActionWasCalled.Should().BeTrue();
commandActionWasCalled.Should().BeFalse();
}
[Fact]
public void Nonterminating_option_action_does_not_short_circuit_command_action()
{
bool optionActionWasCalled = false;
SynchronousTestAction optionAction = new(_ => optionActionWasCalled = true, terminating: false);
bool commandActionWasCalled = false;
Option<bool> option = new("--test")
{
Action = optionAction
};
Command command = new Command("cmd")
{
option
};
command.SetAction(_ => commandActionWasCalled = true);
ParseResult parseResult = command.Parse("cmd --test");
parseResult.Invoke().Should().Be(0);
optionActionWasCalled.Should().BeTrue();
commandActionWasCalled.Should().BeTrue();
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public async Task When_multiple_options_with_terminating_actions_are_present_then_only_the_last_one_is_invoked(bool invokeAsync)
{
bool optionAction1WasCalled = false;
bool optionAction2WasCalled = false;
bool optionAction3WasCalled = false;
SynchronousTestAction optionAction1 = new(_ =>
{
optionAction1WasCalled = true;
}, terminating: true);
SynchronousTestAction optionAction2 = new(_ =>
{
optionAction2WasCalled = true;
}, terminating: true);
SynchronousTestAction optionAction3 = new(_ =>
{
optionAction3WasCalled = true;
}, terminating: true);
var command = new RootCommand
{
Action = new AsynchronousTestAction(_ => {}),
Options =
{
new Option<bool>("--1") { Action = optionAction1 },
new Option<bool>("--2") { Action = optionAction2 },
new Option<bool>("--3") { Action = optionAction3 },
}
};
ParseResult parseResult = command.Parse("--1 --3 --2");
using var _ = new AssertionScope();
parseResult.Action.Should().Be(optionAction2);
if (invokeAsync)
{
(await parseResult.InvokeAsync()).Should().Be(0);
}
else
{
parseResult.Invoke().Should().Be(0);
}
optionAction1WasCalled.Should().BeFalse();
optionAction2WasCalled.Should().BeTrue();
optionAction3WasCalled.Should().BeFalse();
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public async Task When_multiple_options_with_nonterminating_actions_are_present_then_all_are_invoked(bool invokeAsync)
{
bool optionAction1WasCalled = false;
bool optionAction2WasCalled = false;
bool optionAction3WasCalled = false;
bool commandActionWasCalled = false;
SynchronousTestAction optionAction1 = new(_ =>
{
optionAction1WasCalled = true;
}, terminating: false);
SynchronousTestAction optionAction2 = new(_ =>
{
optionAction2WasCalled = true;
}, terminating: false);
SynchronousTestAction optionAction3 = new(_ =>
{
optionAction3WasCalled = true;
}, terminating: false);
var command = new RootCommand
{
Action = new AsynchronousTestAction(_ => commandActionWasCalled = true),
Options =
{
new Option<bool>("--1") { Action = optionAction1 },
new Option<bool>("--2") { Action = optionAction2 },
new Option<bool>("--3") { Action = optionAction3 },
}
};
ParseResult parseResult = command.Parse("--1 true --3 false --2 true");
using var _ = new AssertionScope();
if (invokeAsync)
{
(await parseResult.InvokeAsync()).Should().Be(0);
}
else
{
parseResult.Invoke().Should().Be(0);
}
optionAction1WasCalled.Should().BeTrue();
optionAction2WasCalled.Should().BeTrue();
optionAction3WasCalled.Should().BeTrue();
commandActionWasCalled.Should().BeTrue();
}
[Fact]
public void Directive_action_takes_precedence_over_option_action()
{
bool optionActionWasCalled = false;
bool directiveActionWasCalled = false;
SynchronousTestAction optionAction = new(_ => optionActionWasCalled = true);
SynchronousTestAction directiveAction = new(_ => directiveActionWasCalled = true);
var directive = new Directive("directive")
{
Action = directiveAction
};
RootCommand command = new("cmd")
{
new Option<bool>("-x") { Action = optionAction },
directive
};
ParseResult parseResult = command.Parse("[directive] cmd -x");
using var _ = new AssertionScope();
parseResult.Action.Should().Be(directiveAction);
parseResult.Invoke().Should().Be(0);
optionActionWasCalled.Should().BeFalse();
directiveActionWasCalled.Should().BeTrue();
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public async Task Nonterminating_option_actions_handle_exceptions_and_return_an_error_return_code(bool invokeAsync)
{
var nonexclusiveAction = new SynchronousTestAction(_ =>
{
throw new Exception("oops!");
}, terminating: false);
var command = new RootCommand
{
new Option<bool>("-x")
{
Action = nonexclusiveAction
}
};
command.SetAction(_ => 0);
int returnCode;
var parseResult = CommandLineParser.Parse(command, "-x");
if (invokeAsync)
{
returnCode = await parseResult.InvokeAsync();
}
else
{
returnCode = parseResult.Invoke();
}
returnCode.Should().Be(1);
}
[Theory] // https://github.com/dotnet/command-line-api/issues/2771
[InlineData(true)]
[InlineData(false)]
public async Task Nonterminating_option_action_is_invoked_when_command_has_no_action(bool invokeAsync)
{
bool optionActionWasCalled = false;
SynchronousTestAction optionAction = new(_ => optionActionWasCalled = true, terminating: false);
Option<bool> option = new("--test")
{
Action = optionAction
};
RootCommand command = new()
{
option
};
ParseResult parseResult = command.Parse("--test");
if (invokeAsync)
{
await parseResult.InvokeAsync();
}
else
{
parseResult.Invoke();
}
optionActionWasCalled.Should().BeTrue();
}
[Theory] // https://github.com/dotnet/command-line-api/issues/2772
[InlineData(true)]
[InlineData(false)]
public async Task Nonterminating_option_action_return_value_is_propagated(bool invokeAsync)
{
SynchronousTestAction optionAction = new(_ => { }, terminating: false, returnValue: 42);
Option<bool> option = new("--test")
{
Action = optionAction
};
RootCommand command = new()
{
option
};
command.SetAction(_ => { });
ParseResult parseResult = command.Parse("--test");
int result;
if (invokeAsync)
{
result = await parseResult.InvokeAsync();
}
else
{
result = parseResult.Invoke();
}
result.Should().Be(42);
}
[Theory] // https://github.com/dotnet/command-line-api/issues/2772
[InlineData(true)]
[InlineData(false)]
public async Task When_preaction_and_command_action_both_return_nonzero_then_preaction_value_wins(bool invokeAsync)
{
SynchronousTestAction optionAction = new(_ => { }, terminating: false, returnValue: 42);
Option<bool> option = new("--test")
{
Action = optionAction
};
RootCommand command = new()
{
option
};
command.SetAction(_ => 99);
ParseResult parseResult = command.Parse("--test");
int result;
if (invokeAsync)
{
result = await parseResult.InvokeAsync();
}
else
{
result = parseResult.Invoke();
}
result.Should().Be(42);
}
[Fact]
public async Task Command_InvokeAsync_with_cancelation_token_invokes_command_handler()
{
using CancellationTokenSource cts = new();
var command = new Command("test");
command.SetAction((_, cancellationToken) =>
{
cancellationToken.Should().Be(cts.Token);
return Task.CompletedTask;
});
cts.Cancel();
await command.Parse("test").InvokeAsync(cancellationToken: cts.Token);
}
}
}