forked from Tyrrrz/CliWrap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExecutionSpecs.cs
128 lines (104 loc) · 3.53 KB
/
ExecutionSpecs.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
using System;
using System.ComponentModel;
using System.Threading;
using System.Threading.Tasks;
using CliWrap.Tests.Utils;
using FluentAssertions;
using Xunit;
namespace CliWrap.Tests;
public class ExecutionSpecs
{
[Fact(Timeout = 15000)]
public async Task Command_can_be_executed_which_yields_a_result_containing_runtime_information()
{
// Arrange
var cmd = Cli.Wrap("dotnet").WithArguments(Dummy.Program.FilePath);
// Act
var result = await cmd.ExecuteAsync();
// Assert
result.ExitCode.Should().Be(0);
result.RunTime.Should().BeGreaterThan(TimeSpan.Zero);
}
[Fact(Timeout = 15000)]
public async Task Underlying_process_ID_can_be_obtained_while_a_command_is_executing()
{
// Arrange
var cmd = Cli.Wrap("dotnet").WithArguments(Dummy.Program.FilePath);
// Act
var task = cmd.ExecuteAsync();
// Assert
task.ProcessId.Should().NotBe(0);
await task;
}
[Fact(Timeout = 15000)]
public async Task Command_can_be_executed_with_a_configured_awaiter()
{
// Arrange
var cmd = Cli.Wrap("dotnet").WithArguments(Dummy.Program.FilePath);
// Act
var result = await cmd.ExecuteAsync().ConfigureAwait(false);
// Assert
result.ExitCode.Should().Be(0);
}
[Fact(Timeout = 15000)]
public async Task Command_execution_can_be_canceled_immediately()
{
// Arrange
using var cts = new CancellationTokenSource();
cts.Cancel();
var cmd = Cli.Wrap("dotnet")
.WithArguments(a => a
.Add(Dummy.Program.FilePath)
.Add("sleep")
.Add("--duration").Add("00:00:10")
);
// Act
var task = cmd.ExecuteAsync(cts.Token);
// Assert
var ex = await Assert.ThrowsAnyAsync<OperationCanceledException>(() => task);
ProcessEx.IsRunning(task.ProcessId).Should().BeFalse();
ex.CancellationToken.Should().Be(cts.Token);
}
[Fact(Timeout = 15000)]
public async Task Command_execution_can_be_canceled_while_it_is_in_progress()
{
// Arrange
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(0.5));
var cmd = Cli.Wrap("dotnet")
.WithArguments(a => a
.Add(Dummy.Program.FilePath)
.Add("sleep")
.Add("--duration").Add("00:00:10")
);
// Act
var task = cmd.ExecuteAsync(cts.Token);
// Assert
var ex = await Assert.ThrowsAnyAsync<OperationCanceledException>(() => task);
ProcessEx.IsRunning(task.ProcessId).Should().BeFalse();
ex.CancellationToken.Should().Be(cts.Token);
}
[Fact(Timeout = 15000)]
public async Task Command_execution_does_not_deadlock_on_large_stdout_and_stderr()
{
// Arrange
var cmd = Cli.Wrap("dotnet")
.WithArguments(a => a
.Add(Dummy.Program.FilePath)
.Add("generate text")
.Add("--target").Add("all")
.Add("--lines").Add(100_000)
);
// Act
await cmd.ExecuteAsync();
}
[Fact(Timeout = 15000)]
public void Command_execution_throws_if_the_target_file_does_not_exist()
{
// https://github.com/Tyrrrz/CliWrap/issues/139
// Arrange
var cmd = Cli.Wrap("non-existing-target");
// Act & assert
// Should throw synchronously!
Assert.ThrowsAny<Win32Exception>(() => cmd.ExecuteAsync());
}
}