Skip to content

Add Razor Diagnostic Test #8266

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
// The profile can be found under /test/csharp-test-profile.
"--profile",
"csharp-test-profile",
"${workspaceRoot}/test/razor/razorIntegrationTests/testAssets/RazorApp/.vscode/RazorApp.code-workspace",
"${workspaceRoot}/test/razor/razorIntegrationTests/testAssets/RazorApp/.vscode/devkit_RazorApp.code-workspace",
"--extensionDevelopmentPath=${workspaceRoot}",
"--extensionTestsPath=${workspaceRoot}/out/test/razor/razorIntegrationTests",
"--log",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import * as path from 'path';
import * as vscode from 'vscode';
import { beforeAll, afterAll, test, expect } from '@jest/globals';
import testAssetWorkspace from './testAssets/testAssetWorkspace';
import * as integrationHelpers from '../../lsptoolshost/integrationTests/integrationHelpers';

integrationHelpers.describeIfDevKit(`Razor Diagnostics ${testAssetWorkspace.description}`, function () {
beforeAll(async function () {
if (!integrationHelpers.isRazorWorkspace(vscode.workspace)) {
return;
}

await integrationHelpers.activateCSharpExtension();
});

afterAll(async () => {
await testAssetWorkspace.cleanupWorkspace();
});

test('CSharp and Razor Diagnostics', async () => {
if (!integrationHelpers.isRazorWorkspace(vscode.workspace)) {
return;
}

let count = 0;
let error;

while (count < 5) {
try {
await integrationHelpers.closeAllEditorsAsync();
await integrationHelpers.openFileInWorkspaceAsync(path.join('Pages', 'Diagnostics.razor'));

await integrationHelpers.waitForExpectedResult<vscode.Diagnostic[]>(
() => vscode.languages.getDiagnostics(vscode.window.activeTextEditor!.document.uri),
5000,
500,
(diagnostics) => {
expect(diagnostics.length).toBe(3);

expect(diagnostics[0].code).toBe('RZ10012');
expect(diagnostics[0].message).toBe(
"Found markup element with unexpected name 'TagHelperDoesNotExist'. If this is intended to be a component, add a @using directive for its namespace."
);
expect(diagnostics[0].severity).toBe(vscode.DiagnosticSeverity.Warning);
expect(diagnostics[0].range).toStrictEqual(new vscode.Range(2, 0, 2, 25));
expect(diagnostics[0].source).toBe('Razor');

expect(diagnostics[1].message).toBe("The name 'Message' does not exist in the current context");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not validate the C# diagnostic code too? Would confirm we're sending it through correctly etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me add that. I wasn't sure how to do it because it's a weird definition but I think I can use toStrictEqual and achieve that

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, it's not the same as the Razor one above? That's interesting...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, it's a code + uri

expect(diagnostics[1].severity).toBe(vscode.DiagnosticSeverity.Error);
expect(diagnostics[1].range).toStrictEqual(new vscode.Range(6, 9, 6, 16));
expect(diagnostics[1].source).toBe(undefined);
const diag1Code = diagnostics[1].code as {
value: string | number;
target: vscode.Uri;
};
expect(diag1Code).toBeDefined();
expect(diag1Code.value).toBe('CS0103');
expect(diag1Code.target.toString()).toBe(
'https://msdn.microsoft.com/query/roslyn.query?appId%3Droslyn%26k%3Dk%28CS0103%29'
);

expect(diagnostics[2].message).toBe(
"The type or namespace name 'TypeDoesNotExist' could not be found (are you missing a using directive or an assembly reference?)"
);
expect(diagnostics[2].severity).toBe(vscode.DiagnosticSeverity.Error);
expect(diagnostics[2].range).toStrictEqual(new vscode.Range(15, 20, 15, 36));
expect(diagnostics[2].source).toBe(undefined);
const diag2Code = diagnostics[2].code as {
value: string | number;
target: vscode.Uri;
};
expect(diag2Code).toBeDefined();
expect(diag2Code.value).toBe('CS0246');
expect(diag2Code.target.toString()).toBe(
'https://msdn.microsoft.com/query/roslyn.query?appId%3Droslyn%26k%3Dk%28CS0246%29'
);
}
);

// If this hits that means the expected results succeeded
// and we can stop the loop
return;
} catch (e) {
error = e;
}

count++;
}

throw error;
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
],
"settings": {
"dotnet.defaultSolution": "RazorApp.sln",
"dotnet.server.useOmnisharp": false,
"omnisharp.enableLspDriver": false,
"razor.server.trace": "Trace",
"dotnet.preferCSharpExtension": true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
],
"settings": {
"dotnet.defaultSolution": "RazorApp.sln",
"dotnet.server.useOmnisharp": false,
"omnisharp.enableLspDriver": false,
"razor.server.trace": "Trace",
"dotnet.preferCSharpExtension": false
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
@page "/diagnostics"

<TagHelperDoesNotExist />

@if (true)
{
<h1>@Message</h1>
}

<div />

@code
{
public void M()
{
var x = new TypeDoesNotExist();
}
}
Loading