Skip to content

Commit 2381352

Browse files
Simplify test hook to avoid file watcher initialization
1 parent 3b9ee1b commit 2381352

File tree

2 files changed

+8
-18
lines changed

2 files changed

+8
-18
lines changed

src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/RazorFileChangeDetector.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,10 @@ public async Task StartAsync(string workspaceDirectory, CancellationToken cancel
146146
}
147147
}
148148

149-
// This is an entry point for testing
150-
OnInitializationFinished();
149+
if (!InitializeFileWatchers)
150+
{
151+
return;
152+
}
151153

152154
if (cancellationToken.IsCancellationRequested)
153155
{
@@ -203,9 +205,7 @@ public void Stop()
203205
}
204206

205207
// Protected virtual for testing
206-
protected virtual void OnInitializationFinished()
207-
{
208-
}
208+
protected virtual bool InitializeFileWatchers => true;
209209

210210
// Protected virtual for testing
211211
protected virtual ImmutableArray<string> GetExistingRazorFiles(string workspaceDirectory)

src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/RazorFileChangeDetectorTest.cs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,12 @@ public async Task StartAsync_NotifiesListenersOfExistingRazorFiles()
3636
.Callback((string filePath, RazorFileChangeKind kind, CancellationToken _) => args2.Add((filePath, kind)));
3737

3838
ImmutableArray<string> existingRazorFiles = ["c:/path/to/index.razor", "c:/other/path/_Host.cshtml"];
39-
using var cts = new CancellationTokenSource();
4039
using var detector = new InitializationSkippingRazorFileChangeDetector(
41-
cts,
4240
[listenerMock1.Object, listenerMock2.Object],
4341
existingRazorFiles);
4442

4543
// Act
46-
await detector.StartAsync("/some/workspacedirectory", cts.Token);
44+
await detector.StartAsync("/some/workspacedirectory", DisposalToken);
4745

4846
// Assert
4947
Assert.Collection(args1,
@@ -122,22 +120,14 @@ private class TestRazorFileChangeDetector(
122120
}
123121

124122
private class InitializationSkippingRazorFileChangeDetector(
125-
CancellationTokenSource initializationTokenSource,
126123
IEnumerable<IRazorFileChangeListener> listeners,
127124
ImmutableArray<string> existingProjectFiles) : RazorFileChangeDetector(listeners)
128125
{
129-
private readonly CancellationTokenSource _initializationTokenSource = initializationTokenSource;
130126
private readonly ImmutableArray<string> _existingProjectFiles = existingProjectFiles;
131127

132-
protected override void OnInitializationFinished()
133-
{
134-
// Once initialization has finished we want to ensure that no file watchers are created so cancel!
135-
_initializationTokenSource.Cancel();
136-
}
128+
protected override bool InitializeFileWatchers => false;
137129

138130
protected override ImmutableArray<string> GetExistingRazorFiles(string workspaceDirectory)
139-
{
140-
return _existingProjectFiles;
141-
}
131+
=> _existingProjectFiles;
142132
}
143133
}

0 commit comments

Comments
 (0)