-
Notifications
You must be signed in to change notification settings - Fork 867
/
ProcessUtilL0.cs
96 lines (80 loc) · 3.35 KB
/
ProcessUtilL0.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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using Microsoft.VisualStudio.Services.Agent.Util;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Xunit;
namespace Microsoft.VisualStudio.Services.Agent.Tests.L0.Util
{
public sealed class WindowsProcessUtilL0
{
[Theory]
[InlineData(false)]
[InlineData(true)]
[Trait("Level", "L0")]
[Trait("Category", "Common")]
[Trait("SkipOn", "darwin")]
[Trait("SkipOn", "linux")]
public void Test_GetProcessList(bool useInteropToFindParentProcess)
{
using TestHostContext hc = new TestHostContext(this);
Tracing trace = hc.GetTrace();
// Arrange: This test is based on the current process.
Process currentProcess = Process.GetCurrentProcess();
// The first three processes in the list.
// We do not take other processes since they may differ.
string[] expectedProcessNames = { currentProcess.ProcessName, "dotnet", "dotnet" };
// Since VS has a different process list, we have to handle it separately.
string[] vsExpectedProcessNames = { currentProcess.ProcessName, "vstest.console", "ServiceHub.TestWindowStoreHost" };
// Act.
(List<Process> processes, Dictionary<string, string> telemetryErrors) =
WindowsProcessUtil.GetProcessList(currentProcess, useInteropToFindParentProcess);
string[] actualProcessNames = processes.Take(expectedProcessNames.Length)
.Select(process => process.ProcessName)
.ToArray();
// Assert.
if (actualProcessNames[1] == "vstest.console")
{
Assert.Equal(vsExpectedProcessNames, actualProcessNames);
}
else
{
Assert.Equal(expectedProcessNames, actualProcessNames);
}
}
[Fact]
[Trait("Level", "L0")]
[Trait("Category", "Common")]
[Trait("SkipOn", "darwin")]
[Trait("SkipOn", "linux")]
public void Test_GetParentProcessId_ViaInterop()
{
using TestHostContext hc = new TestHostContext(this);
Tracing trace = hc.GetTrace();
// Arrange: This test is based on the current process.
Process currentProcess = Process.GetCurrentProcess();
// Act.
int? parentProcessId = WindowsProcessUtil.GetParentProcessId(currentProcess.Handle);
// Assert.
Assert.NotNull(parentProcessId);
}
[Fact]
[Trait("Level", "L0")]
[Trait("Category", "Common")]
[Trait("SkipOn", "darwin")]
[Trait("SkipOn", "linux")]
public void Test_GetParentProcess_ViaInterop()
{
using TestHostContext hc = new TestHostContext(this);
Tracing trace = hc.GetTrace();
// Arrange: This test is based on the current process.
Process currentProcess = Process.GetCurrentProcess();
// Act.
(Process parentProcess, Dictionary<string, string> telemetryErrors) = WindowsProcessUtil.GetParentProcess(currentProcess);
// Assert.
Assert.NotNull(parentProcess);
Assert.Equal(0, telemetryErrors.Count);
}
}
}