-
-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathDiffRunnerTests.cs
More file actions
238 lines (217 loc) · 7.97 KB
/
Copy pathDiffRunnerTests.cs
File metadata and controls
238 lines (217 loc) · 7.97 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
#if NET10_0
[NotInParallel]
// Needs the Windows only FakeDiffTool.exe, or the Win32 process APIs.
[RunOn(TUnit.Core.Enums.OS.Windows)]
public class DiffRunnerTests
{
static string SourceDirectory { get; } = Path.GetDirectoryName(GetSourceFile())!;
static string GetSourceFile([CallerFilePath] string path = "") => path;
static ResolvedTool tool;
string file2;
string file1;
string command;
[Test]
[Skip("Explicit")]
public async Task MaxInstancesToLaunch()
{
DiffRunner.MaxInstancesToLaunch(1);
try
{
await Task.Delay(500);
ProcessCleanup.Refresh();
var result = await DiffRunner.LaunchAsync(file1, "fake.txt");
await Task.Delay(300);
await Assert.That(result).IsEqualTo(LaunchResult.StartedNewInstance);
ProcessCleanup.Refresh();
result = await DiffRunner.LaunchAsync(file2, "fake.txt");
await Assert.That(result).IsEqualTo(LaunchResult.TooManyRunningDiffTools);
ProcessCleanup.Refresh();
DiffRunner.Kill(file1, "fake.txt");
DiffRunner.Kill(file2, "fake.txt");
}
finally
{
DiffRunner.MaxInstancesToLaunch(5);
}
}
[Test]
[Skip("Explicit")]
public async Task MaxInstancesToLaunchAsync()
{
DiffRunner.MaxInstancesToLaunch(1);
try
{
await Task.Delay(500);
ProcessCleanup.Refresh();
var result = await DiffRunner.LaunchAsync(file1, "fake.txt");
await Task.Delay(300);
await Assert.That(result).IsEqualTo(LaunchResult.StartedNewInstance);
ProcessCleanup.Refresh();
result = await DiffRunner.LaunchAsync(file2, "fake.txt");
await Assert.That(result).IsEqualTo(LaunchResult.TooManyRunningDiffTools);
ProcessCleanup.Refresh();
DiffRunner.Kill(file1, "fake.txt");
DiffRunner.Kill(file2, "fake.txt");
}
finally
{
DiffRunner.MaxInstancesToLaunch(5);
}
}
static async Task Launch()
{
var targetFile = "";
var tempFile = "";
#region DiffRunnerLaunch
await DiffRunner.LaunchAsync(tempFile, targetFile);
#endregion
}
[Test]
[Skip("Explicit")]
public async Task KillAsync()
{
await DiffRunner.LaunchAsync(file1, file2);
ProcessCleanup.Refresh();
#region DiffRunnerKill
DiffRunner.Kill(file1, file2);
#endregion
}
[Test]
public async Task LaunchAndKillDisabled()
{
DiffRunner.Disabled = true;
try
{
await WaitForRunning(false);
await Assert.That(IsRunning()).IsFalse();
await Assert.That(ProcessCleanup.IsRunning(command)).IsFalse();
var result = await DiffRunner.LaunchAsync(file1, file2);
await Assert.That(result).IsEqualTo(LaunchResult.Disabled);
Thread.Sleep(500);
ProcessCleanup.Refresh();
await Assert.That(IsRunning()).IsFalse();
await Assert.That(ProcessCleanup.IsRunning(command)).IsFalse();
DiffRunner.Kill(file1, file2);
Thread.Sleep(500);
ProcessCleanup.Refresh();
await Assert.That(IsRunning()).IsFalse();
await Assert.That(ProcessCleanup.IsRunning(command)).IsFalse();
}
finally
{
DiffRunner.Disabled = false;
}
}
[Test]
public async Task LaunchAndKillDisabledAsync()
{
DiffRunner.Disabled = true;
try
{
await WaitForRunning(false);
await Assert.That(IsRunning()).IsFalse();
await Assert.That(ProcessCleanup.IsRunning(command)).IsFalse();
var result = await DiffRunner.LaunchAsync(file1, file2);
await Assert.That(result).IsEqualTo(LaunchResult.Disabled);
Thread.Sleep(500);
ProcessCleanup.Refresh();
await Assert.That(IsRunning()).IsFalse();
await Assert.That(ProcessCleanup.IsRunning(command)).IsFalse();
DiffRunner.Kill(file1, file2);
Thread.Sleep(500);
ProcessCleanup.Refresh();
await Assert.That(IsRunning()).IsFalse();
await Assert.That(ProcessCleanup.IsRunning(command)).IsFalse();
}
finally
{
DiffRunner.Disabled = false;
}
}
[Test]
public async Task LaunchAndKill()
{
await WaitForRunning(false);
await Assert.That(IsRunning()).IsFalse();
await Assert.That(ProcessCleanup.IsRunning(command)).IsFalse();
var result = await DiffRunner.LaunchAsync(file1, file2);
await Assert.That(result).IsEqualTo(LaunchResult.StartedNewInstance);
await WaitForRunning(true);
await Assert.That(IsRunning()).IsTrue();
await Assert.That(ProcessCleanup.IsRunning(command)).IsTrue();
DiffRunner.Kill(file1, file2);
await WaitForRunning(false);
await Assert.That(IsRunning()).IsFalse();
await Assert.That(ProcessCleanup.IsRunning(command)).IsFalse();
}
[Test]
public async Task LaunchAndKillAsync()
{
await WaitForRunning(false);
await Assert.That(IsRunning()).IsFalse();
await Assert.That(ProcessCleanup.IsRunning(command)).IsFalse();
var result = await DiffRunner.LaunchAsync(file1, file2);
await Assert.That(result).IsEqualTo(LaunchResult.StartedNewInstance);
await WaitForRunning(true);
await Assert.That(IsRunning()).IsTrue();
await Assert.That(ProcessCleanup.IsRunning(command)).IsTrue();
DiffRunner.Kill(file1, file2);
await WaitForRunning(false);
await Assert.That(IsRunning()).IsFalse();
await Assert.That(ProcessCleanup.IsRunning(command)).IsFalse();
}
// Match this test's exact command, not any FakeDiffTool: DiffEngineTray.Tests
// runs concurrently in the same CI job and launches its own FakeDiffTool
// instances, which a machine-wide substring scan would see.
bool IsRunning()
{
var expected = command;
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
expected = expected.Replace("\"", "");
}
return ProcessCleanup
.FindAll()
.Any(_ => _.Command == expected);
}
// Process spawn and kill are asynchronous, so poll instead of guessing with a
// fixed sleep. Also used at test start: the previous test's kill may still be
// completing when the next test begins.
// Both views must agree before returning: IsRunning() scans processes fresh,
// while ProcessCleanup.IsRunning(command) reads the cached list from the last
// Refresh(). A process dying between the two reads would otherwise leave the
// cache stale and fail the cached assert.
async Task WaitForRunning(bool expected)
{
for (var attempt = 0; attempt < 40; attempt++)
{
ProcessCleanup.Refresh();
if (ProcessCleanup.IsRunning(command) == expected &&
IsRunning() == expected)
{
return;
}
await Task.Delay(250);
}
}
public DiffRunnerTests()
{
file1 = Path.Combine(SourceDirectory, "DiffRunner.file1.txt");
file2 = Path.Combine(SourceDirectory, "DiffRunner.file2.txt");
command = tool.BuildCommand(file1, file2);
}
static DiffRunnerTests() =>
tool = DiffTools.AddTool(
name: "FakeDiffTool",
autoRefresh: true,
isMdi: false,
supportsText: true,
useShellExecute: true,
requiresTarget: true,
launchArguments: new(
Left: (tempFile, targetFile) => $"\"{tempFile}\" \"{targetFile}\"",
Right: (tempFile, targetFile) => $"\"{targetFile}\" \"{tempFile}\""),
exePath: FakeDiffTool.Exe,
binaryExtensions: [".knownBin"])!;
}
#endif