forked from microsoft/VSExtensibility
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnitTestRunner.cs
94 lines (81 loc) · 3.79 KB
/
UnitTestRunner.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
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
namespace DocumentSelectorSample;
using System.Threading;
using System.Threading.Tasks;
using Microsoft;
using Microsoft.VisualStudio.Extensibility;
using Microsoft.VisualStudio.Extensibility.Documents;
using Microsoft.VisualStudio.Extensibility.Editor;
/// <summary>
/// A sample demonstrating how to define an editor extension that is only applicable to files matching a file path pattern.
/// This text view event listener monitors users opening/modifying/closing C# files in "tests" folder and emulates running
/// unit tests in these files.
/// </summary>
#pragma warning disable VSEXTPREVIEW_OUTPUTWINDOW // Type is for evaluation purposes only and is subject to change or removal in future updates.
[VisualStudioContribution]
internal class UnitTestRunner : ExtensionPart, ITextViewOpenClosedListener, ITextViewChangedListener
{
private OutputWindow? outputWindow;
/// <summary>
/// Initializes a new instance of the <see cref="UnitTestRunner"/> class.
/// </summary>
/// <param name="extension">Extension instance.</param>
/// <param name="extensibility">Extensibility object.</param>
public UnitTestRunner(Extension extension, VisualStudioExtensibility extensibility)
: base(extension, extensibility)
{
}
/// <inheritdoc />
public TextViewExtensionConfiguration TextViewExtensionConfiguration => new()
{
AppliesTo =
[
DocumentFilter.FromGlobPattern("**/tests/*.cs", relativePath: false),
],
};
/// <inheritdoc />
public Task TextViewChangedAsync(TextViewChangedArgs args, CancellationToken cancellationToken)
{
return this.RunUnitTestsAfterDelayAsync(args.AfterTextView, cancellationToken);
}
/// <inheritdoc />
public Task TextViewOpenedAsync(ITextViewSnapshot textViewSnapshot, CancellationToken cancellationToken)
{
return this.RunUnitTestsAfterDelayAsync(textViewSnapshot, cancellationToken);
}
/// <inheritdoc />
public Task TextViewClosedAsync(ITextViewSnapshot textViewSnapshot, CancellationToken cancellationToken)
{
return this.StopUnitTestsAsync(textViewSnapshot, cancellationToken);
}
/// <inheritdoc />
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
this.outputWindow?.Dispose();
}
private async Task RunUnitTestsAfterDelayAsync(ITextViewSnapshot textViewSnapshot, CancellationToken cancellationToken)
{
await Task.Delay(500, cancellationToken);
await this.WriteToOutputWindowAsync($"Running unit tests in {textViewSnapshot.Document.Uri.LocalPath}", cancellationToken);
}
private async Task StopUnitTestsAsync(ITextViewSnapshot textViewSnapshot, CancellationToken cancellationToken)
{
await this.WriteToOutputWindowAsync($"Stop running unit tests in {textViewSnapshot.Document.Uri.LocalPath}", cancellationToken);
}
private async Task<OutputWindow> GetOutputWindowAsync(CancellationToken cancellationToken)
{
return this.outputWindow ??= await this.Extensibility.Views().Output.GetChannelAsync(
identifier: nameof(DocumentSelectorSample),
displayNameResourceId: nameof(Resources.OutputWindowPaneName),
cancellationToken);
}
private async Task WriteToOutputWindowAsync(string message, CancellationToken cancellationToken)
{
var channel = await this.GetOutputWindowAsync(cancellationToken);
Assumes.NotNull(channel);
await channel.Writer.WriteLineAsync(message);
}
}
#pragma warning restore VSEXTPREVIEW_OUTPUTWINDOW // Type is for evaluation purposes only and is subject to change or removal in future updates.