-
-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathDiffRunnerTests.cs
More file actions
311 lines (281 loc) · 11.2 KB
/
Copy pathDiffRunnerTests.cs
File metadata and controls
311 lines (281 loc) · 11.2 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
#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;
// Launching registers a real pending move of file1 over file2 with whatever owns the queue on
// this machine, usually the developer's tray. Accepting it moves file1, and discarding it
// deletes file1 and then its directory. Run against the source directory that quietly destroys
// the checked in fixtures, so the tests get copies instead.
// One directory for the whole class, because IsRunning matches the exact command string and
// WaitForRunning is also used at test start to wait out the previous test's kill, both of which
// want the same paths across tests. Fresh per run, so a FakeDiffTool left behind by a crashed
// earlier run cannot match this run's command.
static string TempDirectory { get; } = Path.Combine(
Path.GetTempPath(),
"DiffEngine.DiffRunnerTests",
Guid.NewGuid().ToString("N"));
[After(Class)]
public static void DeleteTempDirectory()
{
if (Directory.Exists(TempDirectory))
{
Directory.Delete(TempDirectory, true);
}
}
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 = DiffRunner.Launch(file1, "fake.txt");
await Task.Delay(300);
await Assert.That(result).IsEqualTo(LaunchResult.StartedNewInstance);
ProcessCleanup.Refresh();
result = DiffRunner.Launch(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 = DiffRunner.Launch(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 = DiffRunner.Launch(file1, file2);
await Assert.That(result).IsEqualTo(LaunchResult.StartedNewInstance);
await WaitForRunning(true);
await Assert.That(IsRunning()).IsTrue();
await Assert.That(ProcessCleanup.IsRunning(command)).IsTrue();
using var launched = OpenLaunched();
DiffRunner.Kill(file1, file2);
await WaitForRunning(false);
await Assert.That(IsRunning()).IsFalse();
await Assert.That(ProcessCleanup.IsRunning(command)).IsFalse();
await AssertTerminated(launched);
}
[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();
using var launched = OpenLaunched();
DiffRunner.Kill(file1, file2);
await WaitForRunning(false);
await Assert.That(IsRunning()).IsFalse();
await Assert.That(ProcessCleanup.IsRunning(command)).IsFalse();
await AssertTerminated(launched);
}
/// <summary>
/// Opens a handle on the process the launch just started, before anything can kill it.
/// <para>
/// Process.GetProcessById holds no OS handle of its own, and a handle opened after the process
/// has gone cannot report how it went. Touching Handle here is what makes the exit code
/// readable afterwards.
/// </para>
/// </summary>
Process OpenLaunched()
{
var match = ProcessCleanup.FindAll().Single(_ => _.Command == Expected);
var process = Process.GetProcessById(match.Process);
_ = process.Handle;
return process;
}
/// <summary>
/// That the process was killed, rather than that it is merely gone.
/// <para>
/// The distinction is the whole point of this assertion. FakeDiffTool sleeps for five seconds
/// and then exits on its own, and WaitForRunning polls for ten, so "no longer running" is
/// true whether the kill worked or did nothing at all - these tests passed with
/// ProcessCleanup.Kill short circuited to a bare return. The exit code tells them apart:
/// WindowsProcess.TryTerminateProcess passes -1 to TerminateProcess, and a FakeDiffTool that
/// ran out its sleep returns 0.
/// </para>
/// </summary>
static async Task AssertTerminated(Process process)
{
await Assert.That(process.WaitForExit(5000)).IsTrue();
await Assert.That(process.ExitCode).IsNotEqualTo(0);
}
// 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.
string Expected =>
RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
? command
: command.Replace("\"", "");
bool IsRunning() =>
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 = CopyFixture("DiffRunner.file1.txt");
file2 = CopyFixture("DiffRunner.file2.txt");
command = Tool.BuildCommand(file1, file2);
}
// Per test rather than per class: a discard deletes the temp file and its directory, so the
// copies have to be put back for the test that follows.
static string CopyFixture(string name)
{
Directory.CreateDirectory(TempDirectory);
var target = Path.Combine(TempDirectory, name);
File.Copy(Path.Combine(SourceDirectory, name), target, true);
return target;
}
// Resolved on first use rather than in a type initializer. [After(Class)] runs even when every
// test here is skipped for the OS, so initializing the type has to be safe everywhere, and this
// reaches FakeDiffTool, which is only built for Windows and macOS.
static ResolvedTool Tool =>
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