-
Notifications
You must be signed in to change notification settings - Fork 302
Expand file tree
/
Copy pathPackagedAppTestHostHandle.cs
More file actions
68 lines (57 loc) · 2.28 KB
/
Copy pathPackagedAppTestHostHandle.cs
File metadata and controls
68 lines (57 loc) · 2.28 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using Microsoft.Testing.Platform.Extensions.TestHostControllers;
namespace Microsoft.Testing.Extensions.PackagedApp;
/// <summary>
/// An <see cref="ITestHostHandle"/> over a deployed packaged Windows (UWP/WinUI) test host process
/// that deliberately exposes no identifier, modelling a launch where no local, queryable PID is
/// available.
/// </summary>
internal sealed class PackagedAppTestHostHandle : ITestHostHandle
{
private readonly Process _process;
private readonly string _deploymentDirectory;
public PackagedAppTestHostHandle(Process process, string deploymentDirectory)
{
_process = process;
_deploymentDirectory = deploymentDirectory;
}
// Intentionally null: the platform must not depend on any identifier. A packaged UWP/WinUI
// implementation could surface the AUMID-activated PID (as a string) here instead.
public string? Identifier => null;
public int ExitCode => _process.ExitCode;
public bool HasExited => _process.HasExited;
public Task WaitForExitAsync(CancellationToken cancellationToken) => _process.WaitForExitAsync(cancellationToken);
public void Terminate()
{
try
{
_process.Kill(entireProcessTree: true);
}
catch (InvalidOperationException)
{
// The process has already exited.
}
}
public void Dispose()
{
_process.Dispose();
// The platform disposes the handle once the host has exited, so it is safe to remove the
// staged deployment now. Best-effort: never let cleanup failures surface as run failures.
try
{
if (Directory.Exists(_deploymentDirectory))
{
Directory.Delete(_deploymentDirectory, recursive: true);
}
}
catch (IOException ex)
{
Debug.WriteLine($"Best-effort cleanup of deployment directory '{_deploymentDirectory}' failed: {ex}");
}
catch (UnauthorizedAccessException ex)
{
Debug.WriteLine($"Best-effort cleanup of deployment directory '{_deploymentDirectory}' failed: {ex}");
}
}
}