Skip to content

Commit 36f01eb

Browse files
author
Andrew Hall
committed
Working test running locally.
1 parent 0e93552 commit 36f01eb

File tree

5 files changed

+120
-84
lines changed

5 files changed

+120
-84
lines changed

test/lsptoolshost/integrationTests/integrationHelpers.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,19 @@ import testAssetWorkspace from './testAssets/testAssetWorkspace';
1313
import { EOL, platform } from 'os';
1414
import { describe, expect, test } from '@jest/globals';
1515

16-
export async function activateCSharpExtension(): Promise<void> {
16+
export async function activateRazorExtension(): Promise<void> {
17+
// Razor requires statefulness in the extension and server to communicate
18+
// properly with CSharp. That means allowing a restart of the CSharp server
19+
// will improperly break that state. This will be fixed in cohosting and
20+
// could be potentially fixed now but I timeboxed to unblock adding more tests.
21+
// Without this the symptoms in a test will be that all razor files are considered
22+
// in the misc workspace.
23+
await activateCSharpExtension(false);
24+
}
25+
26+
export async function activateCSharpExtension(allowRestart?: boolean): Promise<void> {
27+
allowRestart = allowRestart ?? true;
28+
1729
const csharpExtension = vscode.extensions.getExtension<CSharpExtensionExports>('ms-dotnettools.csharp');
1830
if (!csharpExtension) {
1931
throw new Error('Failed to find installation of ms-dotnettools.csharp');
@@ -50,7 +62,7 @@ export async function activateCSharpExtension(): Promise<void> {
5062
console.log('ms-dotnettools.csharp activated');
5163
console.log(`Extension Log Directory: ${csharpExtension.exports.logDirectory}`);
5264

53-
if (shouldRestart) {
65+
if (shouldRestart && allowRestart) {
5466
await restartLanguageServer();
5567
}
5668
}
@@ -60,15 +72,19 @@ export function usingDevKit(): boolean {
6072
}
6173

6274
export async function openFileInWorkspaceAsync(relativeFilePath: string): Promise<vscode.Uri> {
75+
const uri = getFilePath(relativeFilePath);
76+
await vscode.commands.executeCommand('vscode.open', uri);
77+
return uri;
78+
}
79+
80+
export function getFilePath(relativeFilePath: string): vscode.Uri {
6381
const root = vscode.workspace.workspaceFolders![0].uri.fsPath;
6482
const filePath = path.join(root, relativeFilePath);
6583
if (!existsSync(filePath)) {
6684
throw new Error(`File ${filePath} does not exist`);
6785
}
6886

69-
const uri = vscode.Uri.file(filePath);
70-
await vscode.commands.executeCommand('vscode.open', uri);
71-
return uri;
87+
return vscode.Uri.file(filePath);
7288
}
7389

7490
export async function closeAllEditorsAsync(): Promise<void> {

test/razor/razorIntegrationTests/reference.integration.test.ts

Lines changed: 89 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ integrationHelpers.describeIfWindows(`Razor References ${testAssetWorkspace.desc
1515
return;
1616
}
1717

18-
await integrationHelpers.activateCSharpExtension();
18+
await integrationHelpers.activateRazorExtension();
1919
});
2020

2121
beforeEach(async function () {
@@ -32,22 +32,27 @@ integrationHelpers.describeIfWindows(`Razor References ${testAssetWorkspace.desc
3232
}
3333

3434
const position = new vscode.Position(6, 41);
35-
const locations = <vscode.Location[]>(
36-
await vscode.commands.executeCommand(
37-
'vscode.executeDefinitionProvider',
38-
vscode.window.activeTextEditor!.document.uri,
39-
position
40-
)
41-
);
42-
43-
expect(locations.length).toBe(1);
44-
const definitionLocation = locations[0];
4535

46-
expect(definitionLocation.uri.path).toBe(vscode.window.activeTextEditor!.document.uri.path);
47-
expect(definitionLocation.range.start.line).toBe(11);
48-
expect(definitionLocation.range.start.character).toBe(16);
49-
expect(definitionLocation.range.end.line).toBe(11);
50-
expect(definitionLocation.range.end.character).toBe(28);
36+
await integrationHelpers.waitForExpectedResult<vscode.Location[]>(
37+
async () =>
38+
await vscode.commands.executeCommand(
39+
'vscode.executeDefinitionProvider',
40+
vscode.window.activeTextEditor!.document.uri,
41+
position
42+
),
43+
1000,
44+
100,
45+
(locations) => {
46+
expect(locations.length).toBe(1);
47+
const definitionLocation = locations[0];
48+
49+
expect(definitionLocation.uri.path).toBe(vscode.window.activeTextEditor!.document.uri.path);
50+
expect(definitionLocation.range.start.line).toBe(11);
51+
expect(definitionLocation.range.start.character).toBe(16);
52+
expect(definitionLocation.range.end.line).toBe(11);
53+
expect(definitionLocation.range.end.character).toBe(28);
54+
}
55+
);
5156
});
5257

5358
test('Find All References', async () => {
@@ -56,36 +61,40 @@ integrationHelpers.describeIfWindows(`Razor References ${testAssetWorkspace.desc
5661
}
5762

5863
const position = new vscode.Position(6, 41);
59-
const locations = <vscode.Location[]>(
60-
await vscode.commands.executeCommand(
61-
'vscode.executeReferenceProvider',
62-
vscode.window.activeTextEditor!.document.uri,
63-
position
64-
)
65-
);
64+
await integrationHelpers.waitForExpectedResult<vscode.Location[]>(
65+
async () =>
66+
await vscode.commands.executeCommand(
67+
'vscode.executeReferenceProvider',
68+
vscode.window.activeTextEditor!.document.uri,
69+
position
70+
),
71+
1000,
72+
100,
73+
(locations) => {
74+
expect(locations.length).toBe(3);
6675

67-
expect(locations.length).toBe(3);
68-
69-
let definitionLocation = locations[0];
70-
expect(definitionLocation.uri.path).toBe(vscode.window.activeTextEditor!.document.uri.path);
71-
expect(definitionLocation.range.start.line).toBe(6);
72-
expect(definitionLocation.range.start.character).toBe(33);
73-
expect(definitionLocation.range.end.line).toBe(6);
74-
expect(definitionLocation.range.end.character).toBe(45);
75-
76-
definitionLocation = locations[1];
77-
expect(definitionLocation.uri.path).toBe(vscode.window.activeTextEditor!.document.uri.path);
78-
expect(definitionLocation.range.start.line).toBe(11);
79-
expect(definitionLocation.range.start.character).toBe(16);
80-
expect(definitionLocation.range.end.line).toBe(11);
81-
expect(definitionLocation.range.end.character).toBe(28);
82-
83-
definitionLocation = locations[2];
84-
expect(definitionLocation.uri.path).toBe(vscode.window.activeTextEditor!.document.uri.path);
85-
expect(definitionLocation.range.start.line).toBe(15);
86-
expect(definitionLocation.range.start.character).toBe(8);
87-
expect(definitionLocation.range.end.line).toBe(15);
88-
expect(definitionLocation.range.end.character).toBe(20);
76+
let definitionLocation = locations[0];
77+
expect(definitionLocation.uri.path).toBe(vscode.window.activeTextEditor!.document.uri.path);
78+
expect(definitionLocation.range.start.line).toBe(6);
79+
expect(definitionLocation.range.start.character).toBe(33);
80+
expect(definitionLocation.range.end.line).toBe(6);
81+
expect(definitionLocation.range.end.character).toBe(45);
82+
83+
definitionLocation = locations[1];
84+
expect(definitionLocation.uri.path).toBe(vscode.window.activeTextEditor!.document.uri.path);
85+
expect(definitionLocation.range.start.line).toBe(11);
86+
expect(definitionLocation.range.start.character).toBe(16);
87+
expect(definitionLocation.range.end.line).toBe(11);
88+
expect(definitionLocation.range.end.character).toBe(28);
89+
90+
definitionLocation = locations[2];
91+
expect(definitionLocation.uri.path).toBe(vscode.window.activeTextEditor!.document.uri.path);
92+
expect(definitionLocation.range.start.line).toBe(15);
93+
expect(definitionLocation.range.start.character).toBe(8);
94+
expect(definitionLocation.range.end.line).toBe(15);
95+
expect(definitionLocation.range.end.character).toBe(20);
96+
}
97+
);
8998
});
9099

91100
test('Go To Implementation', async () => {
@@ -94,47 +103,57 @@ integrationHelpers.describeIfWindows(`Razor References ${testAssetWorkspace.desc
94103
}
95104

96105
const position = new vscode.Position(18, 18);
97-
const locations = <vscode.Location[]>(
98-
await vscode.commands.executeCommand(
99-
'vscode.executeImplementationProvider',
100-
vscode.window.activeTextEditor!.document.uri,
101-
position
102-
)
103-
);
104106

105-
expect(locations.length).toBe(1);
106-
const definitionLocation = locations[0];
107-
108-
expect(definitionLocation.uri.path).toBe(vscode.window.activeTextEditor!.document.uri.path);
109-
expect(definitionLocation.range.start.line).toBe(23);
110-
expect(definitionLocation.range.start.character).toBe(10);
111-
expect(definitionLocation.range.end.line).toBe(23);
112-
expect(definitionLocation.range.end.character).toBe(19);
107+
await integrationHelpers.waitForExpectedResult<vscode.Location[]>(
108+
async () =>
109+
await vscode.commands.executeCommand(
110+
'vscode.executeImplementationProvider',
111+
vscode.window.activeTextEditor!.document.uri,
112+
position
113+
),
114+
1000,
115+
100,
116+
(locations) => {
117+
expect(locations.length).toBe(1);
118+
const definitionLocation = locations[0];
119+
120+
expect(definitionLocation.uri.path).toBe(vscode.window.activeTextEditor!.document.uri.path);
121+
expect(definitionLocation.range.start.line).toBe(23);
122+
expect(definitionLocation.range.start.character).toBe(10);
123+
expect(definitionLocation.range.end.line).toBe(23);
124+
expect(definitionLocation.range.end.character).toBe(19);
125+
}
126+
);
113127
});
114128

115129
test('Find All References - CSharp', async () => {
116130
if (!integrationHelpers.isRazorWorkspace(vscode.workspace)) {
117131
return;
118132
}
119-
133+
const position = new vscode.Position(5, 28);
120134
await integrationHelpers.openFileInWorkspaceAsync(path.join('Pages', 'References.razor.cs'));
121135

122-
const position = new vscode.Position(4, 20);
123-
124136
await integrationHelpers.waitForExpectedResult<vscode.Location[]>(
125-
async () =>
126-
await vscode.commands.executeCommand(
137+
async () => {
138+
return await vscode.commands.executeCommand(
127139
'vscode.executeReferenceProvider',
128140
vscode.window.activeTextEditor!.document.uri,
129141
position
130-
),
131-
10000,
142+
);
143+
},
144+
1000,
132145
100,
133146
(locations) => {
134147
expect(locations.length).toBe(3);
135-
expect(locations[0].uri.path).toBe(vscode.window.activeTextEditor!.document.uri.path);
136-
expect(locations[1].uri.path).toBe(vscode.window.activeTextEditor!.document.uri.path);
137-
expect(locations[2].uri.path).toBe(vscode.window.activeTextEditor!.document.uri.path);
148+
149+
const sortedLocations = integrationHelpers.sortLocations(locations);
150+
151+
const razorFile = integrationHelpers.getFilePath(path.join('Pages', 'References.razor'));
152+
const csharpFile = integrationHelpers.getFilePath(path.join('Pages', 'References.razor.cs'));
153+
154+
expect(sortedLocations[0].uri.path).toBe(razorFile.path);
155+
expect(sortedLocations[1].uri.path).toBe(csharpFile.path);
156+
expect(sortedLocations[2].uri.path).toBe(csharpFile.path);
138157
}
139158
);
140159
});

test/razor/razorIntegrationTests/testAssets/RazorApp/Pages/References.razor

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,4 @@
88

99
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
1010

11-
@code {
1211

13-
}
Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
namespace RazorApp.Pages;
2-
3-
public partial class References
1+
namespace RazorApp.Pages
42
{
5-
private int currentCount = 0;
6-
7-
private void IncrementCount()
3+
public partial class References
84
{
9-
currentCount++;
5+
6+
private int currentCount = 0;
7+
8+
private void IncrementCount()
9+
{
10+
currentCount++;
11+
}
1012
}
1113
}

test/razor/razorIntegrationTests/testAssets/RazorApp/RazorApp.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<TargetFramework>net6.0</TargetFramework>
55
<Nullable>enable</Nullable>
66
<ImplicitUsings>enable</ImplicitUsings>
7+
<RootNamespace>RazorApp</RootNamespace>
78
</PropertyGroup>
89

910
</Project>

0 commit comments

Comments
 (0)