-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProcessCleanupTests.cs
More file actions
105 lines (90 loc) · 2.8 KB
/
ProcessCleanupTests.cs
File metadata and controls
105 lines (90 loc) · 2.8 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
public class ProcessCleanupTests
{
[Test]
public async Task GetWordProcessIds_ReturnsHashSet()
{
var pids = Word.GetWordProcessIds();
await Assert.That(pids).IsNotNull();
}
[Test]
public async Task FindNewWordProcess_WhenNoNewProcesses_ReturnsNull()
{
var existingPids = Word.GetWordProcessIds();
var result = Word.FindNewWordProcess(existingPids);
await Assert.That(result).IsNull();
}
[Test]
public void QuitAndKill_WithNullProcess_DoesNotThrow() =>
Word.QuitAndKill((dynamic)new object(), null);
[Test]
public void QuitAndKill_WithExitedProcess_DoesNotThrow()
{
var process = Process.Start(new ProcessStartInfo
{
FileName = "cmd.exe",
Arguments = "/c exit 0",
CreateNoWindow = true,
UseShellExecute = false
})!;
process.WaitForExit();
Word.QuitAndKill((dynamic)new object(), process);
process.Dispose();
}
[Test]
public async Task QuitAndKill_WithRunningProcess_KillsProcess()
{
var process = Process.Start(new ProcessStartInfo
{
FileName = "ping",
Arguments = "-n 60 127.0.0.1",
CreateNoWindow = true,
UseShellExecute = false
})!;
await Assert.That(process.HasExited).IsFalse();
Word.QuitAndKill((dynamic)new object(), process);
process.WaitForExit(5000);
await Assert.That(process.HasExited).IsTrue()
.Because("QuitAndKill should kill running processes");
process.Dispose();
}
[Test]
[Explicit]
public async Task Launch_WithInvalidPath_DoesNotLeaveZombieProcess()
{
var wordType = Type.GetTypeFromProgID("Word.Application");
if (wordType == null)
{
Skip.Test("Microsoft Word is not installed");
}
var beforePids = Word.GetWordProcessIds();
try
{
await Word.Launch(
@"C:\nonexistent\file1.docx",
@"C:\nonexistent\file2.docx");
}
catch
{
// Expected - invalid file paths
}
// Give Word a moment to fully shut down
await Task.Delay(3000);
var afterPids = Word.GetWordProcessIds();
afterPids.ExceptWith(beforePids);
// Clean up any zombie processes (safety net)
foreach (var pid in afterPids)
{
try
{
using var p = Process.GetProcessById(pid);
p.Kill();
}
catch
{
// Process may have already exited
}
}
await Assert.That(afterPids.Count).IsEqualTo(0)
.Because("No zombie Word processes should remain after a failed Launch");
}
}