Skip to content

Commit

Permalink
Halve the time it takes to run formatting unit tests (#11173)
Browse files Browse the repository at this point in the history
Turns out the boolean parameter passed in here is read in exactly one
spot in our code, and that line of code is never hit in these tests
because they use mocks.

Yes, this is the equivalent of removing a `Thread.Sleep` that was put in
previously, but I'm taking it regardless :)
  • Loading branch information
davidwengier authored Nov 7, 2024
2 parents e87f30d + 6745d9b commit 390aced
Showing 1 changed file with 6 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ private protected async Task RunFormattingTestAsync(
bool skipFlipLineEndingTest = false)
{
// Run with and without forceRuntimeCodeGeneration
await RunFormattingTestInternalAsync(input, expected, tabSize, insertSpaces, fileKind, tagHelpers, allowDiagnostics, razorLSPOptions, inGlobalNamespace, forceRuntimeCodeGeneration: true);
await RunFormattingTestInternalAsync(input, expected, tabSize, insertSpaces, fileKind, tagHelpers, allowDiagnostics, razorLSPOptions, inGlobalNamespace, forceRuntimeCodeGeneration: false);
await RunFormattingTestInternalAsync(input, expected, tabSize, insertSpaces, fileKind, tagHelpers, allowDiagnostics, razorLSPOptions, inGlobalNamespace);

// some tests are failing, skip for now, tracked by https://github.com/dotnet/razor/issues/10836
if (!skipFlipLineEndingTest)
Expand All @@ -69,12 +68,11 @@ private protected async Task RunFormattingTestAsync(
input = FlipLineEndings(input);
expected = FlipLineEndings(expected);

await RunFormattingTestInternalAsync(input, expected, tabSize, insertSpaces, fileKind, tagHelpers, allowDiagnostics, razorLSPOptions, inGlobalNamespace, forceRuntimeCodeGeneration: true);
await RunFormattingTestInternalAsync(input, expected, tabSize, insertSpaces, fileKind, tagHelpers, allowDiagnostics, razorLSPOptions, inGlobalNamespace, forceRuntimeCodeGeneration: false);
await RunFormattingTestInternalAsync(input, expected, tabSize, insertSpaces, fileKind, tagHelpers, allowDiagnostics, razorLSPOptions, inGlobalNamespace);
}
}

private async Task RunFormattingTestInternalAsync(string input, string expected, int tabSize, bool insertSpaces, string? fileKind, ImmutableArray<TagHelperDescriptor> tagHelpers, bool allowDiagnostics, RazorLSPOptions? razorLSPOptions, bool inGlobalNamespace, bool forceRuntimeCodeGeneration)
private async Task RunFormattingTestInternalAsync(string input, string expected, int tabSize, bool insertSpaces, string? fileKind, ImmutableArray<TagHelperDescriptor> tagHelpers, bool allowDiagnostics, RazorLSPOptions? razorLSPOptions, bool inGlobalNamespace)
{
// Arrange
fileKind ??= FileKinds.Component;
Expand All @@ -89,7 +87,7 @@ private async Task RunFormattingTestInternalAsync(string input, string expected,

var path = "file:///path/to/Document." + fileKind;
var uri = new Uri(path);
var (codeDocument, documentSnapshot) = CreateCodeDocumentAndSnapshot(source, uri.AbsolutePath, tagHelpers, fileKind, allowDiagnostics, inGlobalNamespace, forceRuntimeCodeGeneration);
var (codeDocument, documentSnapshot) = CreateCodeDocumentAndSnapshot(source, uri.AbsolutePath, tagHelpers, fileKind, allowDiagnostics, inGlobalNamespace);
var options = new FormattingOptions()
{
TabSize = tabSize,
Expand Down Expand Up @@ -248,7 +246,7 @@ protected async Task RunCodeActionFormattingTestAsync(
protected static TextEdit Edit(int startLine, int startChar, int endLine, int endChar, string newText)
=> VsLspFactory.CreateTextEdit(startLine, startChar, endLine, endChar, newText);

private static (RazorCodeDocument, IDocumentSnapshot) CreateCodeDocumentAndSnapshot(SourceText text, string path, ImmutableArray<TagHelperDescriptor> tagHelpers = default, string? fileKind = default, bool allowDiagnostics = false, bool inGlobalNamespace = false, bool forceRuntimeCodeGeneration = false)
private static (RazorCodeDocument, IDocumentSnapshot) CreateCodeDocumentAndSnapshot(SourceText text, string path, ImmutableArray<TagHelperDescriptor> tagHelpers = default, string? fileKind = default, bool allowDiagnostics = false, bool inGlobalNamespace = false)
{
fileKind ??= FileKinds.Component;
tagHelpers = tagHelpers.NullToEmpty();
Expand Down Expand Up @@ -291,7 +289,7 @@ @using Microsoft.AspNetCore.Components.Web
new TestRazorProjectItem(importsPath, fileKind: FileKinds.ComponentImport)]);

var projectEngine = RazorProjectEngine.Create(
new RazorConfiguration(RazorLanguageVersion.Latest, "TestConfiguration", Extensions: [], LanguageServerFlags: new LanguageServerFlags(forceRuntimeCodeGeneration)),
new RazorConfiguration(RazorLanguageVersion.Latest, "TestConfiguration", Extensions: []),
projectFileSystem,
builder =>
{
Expand Down

0 comments on commit 390aced

Please sign in to comment.