Skip to content

Commit 4db0bdb

Browse files
DanTupCommit Queue
authored andcommitted
[analysis_server] Update remainder of CodeAction tests to use shared interfaces
Some further changes to CodeAction tests to use shared interfaces so that they will be able to run with both LSP and Legacy test base classes. Change-Id: I3383e9aef9ea97d39f28dad42633c1fac74e4efa Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/428040 Reviewed-by: Brian Wilkerson <[email protected]> Reviewed-by: Samuel Rawlins <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]>
1 parent 93f4539 commit 4db0bdb

11 files changed

+143
-98
lines changed

pkg/analysis_server/test/analysis_server_base.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,10 @@ class PubPackageAnalysisServerTest extends ContextResolutionTest
209209
'$testPackageLibPath/test.dart',
210210
);
211211

212+
late String pubspecFilePath = resourceProvider.convertPath(
213+
'$testPackageLibPath/pubspec.yaml',
214+
);
215+
212216
late TestCode parsedTestCode;
213217

214218
/// Return a list of the experiments that are to be enabled for tests in this

pkg/analysis_server/test/integration/lsp/abstract_lsp_over_legacy.dart

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,7 @@ abstract class AbstractLspOverLegacyTest
5353
RequestMessage message,
5454
T Function(R) fromJson,
5555
) async {
56-
var legacyResult = await sendLspHandle(message.toJson());
57-
var lspResponseJson = legacyResult.lspResponse as Map<String, Object?>;
58-
59-
// Unwrap the LSP response.
60-
var lspResponse = ResponseMessage.fromJson(lspResponseJson);
56+
var lspResponse = await sendRequestToServer(message);
6157
var error = lspResponse.error;
6258
if (error != null) {
6359
throw error;
@@ -70,6 +66,15 @@ abstract class AbstractLspOverLegacyTest
7066
}
7167
}
7268

69+
@override
70+
Future<ResponseMessage> sendRequestToServer(RequestMessage message) async {
71+
var legacyResult = await sendLspHandle(message.toJson());
72+
var lspResponseJson = legacyResult.lspResponse as Map<String, Object?>;
73+
74+
// Unwrap the LSP response.
75+
return ResponseMessage.fromJson(lspResponseJson);
76+
}
77+
7378
@override
7479
void sendResponseToServer(ResponseMessage response) {
7580
throw 'Reverse-requests not currently supported for LSP-over-Legacy integration tests';

pkg/analysis_server/test/lsp/code_actions_assists_test.dart

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
import 'dart:async';
56
import 'dart:convert';
67

78
import 'package:analysis_server/lsp_protocol/protocol.dart';
@@ -15,10 +16,10 @@ import 'package:analyzer_plugin/protocol/protocol_generated.dart' as plugin;
1516
import 'package:test/test.dart';
1617
import 'package:test_reflective_loader/test_reflective_loader.dart';
1718

19+
import '../lsp/code_actions_mixin.dart';
20+
import '../lsp/server_abstract.dart';
1821
import '../utils/lsp_protocol_extensions.dart';
1922
import '../utils/test_code_extensions.dart';
20-
import 'code_actions_mixin.dart';
21-
import 'server_abstract.dart';
2223

2324
void main() {
2425
defineReflectiveSuite(() {
@@ -55,6 +56,7 @@ import 'dart:async' show Future;
5556
5657
Future? f;
5758
''';
59+
5860
await verifyCodeActionLiteralEdits(
5961
content,
6062
expectedContent,
@@ -169,8 +171,8 @@ Widget build() {
169171
// indicating this is not a valid (Dart) int.
170172
// https://github.com/dart-lang/sdk/issues/42786
171173

172-
newFile(testFilePath, '');
173-
await initialize();
174+
createFile(testFilePath, '');
175+
await initializeServer();
174176

175177
var request = makeRequest(
176178
Method.textDocument_codeAction,
@@ -254,8 +256,8 @@ Future? f;
254256
Future<void> test_nonDartFile() async {
255257
setSupportedCodeActionKinds([CodeActionKind.Refactor]);
256258

257-
newFile(pubspecFilePath, simplePubspecContent);
258-
await initialize();
259+
createFile(pubspecFilePath, simplePubspecContent);
260+
await initializeServer();
259261

260262
var codeActions = await getCodeActions(
261263
pubspecFileUri,
@@ -329,8 +331,8 @@ bar
329331
request is plugin.EditGetAssistsParams ? pluginResult : null,
330332
);
331333

332-
newFile(testFilePath, code.code);
333-
await initialize();
334+
createFile(testFilePath, code.code);
335+
await initializeServer();
334336

335337
var codeActions = await getCodeActions(
336338
testFileUri,
@@ -512,8 +514,8 @@ import 'package:flutter/widgets.dart';
512514
build() => Contai^ner(child: Container());
513515
''');
514516

515-
newFile(testFilePath, code.code);
516-
await initialize();
517+
createFile(testFilePath, code.code);
518+
await initializeServer();
517519

518520
var codeActions = await getCodeActions(
519521
testFileUri,

pkg/analysis_server/test/lsp/code_actions_fixes_test.dart

Lines changed: 50 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
import 'dart:async';
6+
57
import 'package:analysis_server/lsp_protocol/protocol.dart';
68
import 'package:analysis_server/src/analysis_server.dart';
79
import 'package:analysis_server/src/lsp/extensions/code_action.dart';
@@ -16,9 +18,9 @@ import 'package:linter/src/rules.dart';
1618
import 'package:test/test.dart';
1719
import 'package:test_reflective_loader/test_reflective_loader.dart';
1820

21+
import '../lsp/code_actions_mixin.dart';
22+
import '../lsp/server_abstract.dart';
1923
import '../utils/test_code_extensions.dart';
20-
import 'code_actions_mixin.dart';
21-
import 'server_abstract.dart';
2224

2325
void main() {
2426
defineReflectiveSuite(() {
@@ -123,14 +125,17 @@ bar
123125
}
124126

125127
Future<void> test_addImport_noPreference() async {
126-
newFile(join(projectFolderPath, 'lib', 'class.dart'), 'class MyClass {}');
128+
createFile(
129+
pathContext.join(projectFolderPath, 'lib', 'class.dart'),
130+
'class MyClass {}',
131+
);
127132

128133
var code = TestCode.parse('''
129134
MyCla^ss? a;
130135
''');
131136

132-
newFile(testFilePath, code.code);
133-
await initialize();
137+
createFile(testFilePath, code.code);
138+
await initializeServer();
134139

135140
var codeActions = await getCodeActions(
136141
testFileUri,
@@ -151,14 +156,17 @@ MyCla^ss? a;
151156
Future<void> test_addImport_preferAbsolute() async {
152157
_enableLints(['always_use_package_imports']);
153158

154-
newFile(join(projectFolderPath, 'lib', 'class.dart'), 'class MyClass {}');
159+
createFile(
160+
pathContext.join(projectFolderPath, 'lib', 'class.dart'),
161+
'class MyClass {}',
162+
);
155163

156164
var code = TestCode.parse('''
157165
MyCla^ss? a;
158166
''');
159167

160-
newFile(testFilePath, code.code);
161-
await initialize();
168+
createFile(testFilePath, code.code);
169+
await initializeServer();
162170

163171
var codeActions = await getCodeActions(
164172
testFileUri,
@@ -175,14 +183,17 @@ MyCla^ss? a;
175183
Future<void> test_addImport_preferRelative() async {
176184
_enableLints(['prefer_relative_imports']);
177185

178-
newFile(join(projectFolderPath, 'lib', 'class.dart'), 'class MyClass {}');
186+
createFile(
187+
pathContext.join(projectFolderPath, 'lib', 'class.dart'),
188+
'class MyClass {}',
189+
);
179190

180191
var code = TestCode.parse('''
181192
MyCla^ss? a;
182193
''');
183194

184-
newFile(testFilePath, code.code);
185-
await initialize();
195+
createFile(testFilePath, code.code);
196+
await initializeServer();
186197

187198
var codeActions = await getCodeActions(
188199
testFileUri,
@@ -288,11 +299,11 @@ Future foo;
288299

289300
Future<void> test_createFile() async {
290301
const content = '''
291-
import '[!newfile.dart!]';
302+
import '[!createFile.dart!]';
292303
''';
293304

294305
const expectedContent = '''
295-
>>>>>>>>>> lib/newfile.dart created
306+
>>>>>>>>>> lib/createFile.dart created
296307
// TODO Implement this library.<<<<<<<<<<
297308
''';
298309

@@ -301,7 +312,7 @@ import '[!newfile.dart!]';
301312
content,
302313
expectedContent,
303314
kind: CodeActionKind('quickfix.create.file'),
304-
title: "Create file 'newfile.dart'",
315+
title: "Create file 'createFile.dart'",
305316
);
306317
}
307318

@@ -317,8 +328,8 @@ import 'dart:async';
317328
318329
Future foo;
319330
''');
320-
newFile(testFilePath, code.code);
321-
await initialize();
331+
createFile(testFilePath, code.code);
332+
await initializeServer();
322333

323334
ofKind(CodeActionKind kind) =>
324335
getCodeActions(testFileUri, range: code.range.range, kinds: [kind]);
@@ -358,8 +369,8 @@ var a = [!foo!]();
358369
var b = bar();
359370
''');
360371

361-
newFile(testFilePath, code.code);
362-
await initialize();
372+
createFile(testFilePath, code.code);
373+
await initializeServer();
363374

364375
var allFixes = await getCodeActions(testFileUri, range: code.range.range);
365376

@@ -390,7 +401,7 @@ void f(String a) {
390401
/// https://github.com/dart-lang/sdk/issues/53021
391402
Future<void> test_fixAll_unfixable() async {
392403
registerLintRules();
393-
newFile(analysisOptionsPath, '''
404+
createFile(analysisOptionsPath, '''
394405
linter:
395406
rules:
396407
- non_constant_identifier_names
@@ -446,8 +457,8 @@ void main() {
446457
}
447458
''');
448459

449-
newFile(testFilePath, code.code);
450-
await initialize();
460+
createFile(testFilePath, code.code);
461+
await initializeServer();
451462

452463
var position = code.position.position;
453464
var range = Range(start: position, end: position);
@@ -536,8 +547,8 @@ Future foo;
536547
var code = TestCode.parse('''
537548
[!import!] 'dart:convert';
538549
''');
539-
newFile(testFilePath, code.code);
540-
await initialize();
550+
createFile(testFilePath, code.code);
551+
await initializeServer();
541552

542553
var codeActions = await getCodeActions(
543554
testFileUri,
@@ -560,7 +571,7 @@ Future foo;
560571
/// is the opening brace) and not the whole range of the error.
561572
Future<void> test_multilineError() async {
562573
registerLintRules();
563-
newFile(analysisOptionsPath, '''
574+
createFile(analysisOptionsPath, '''
564575
linter:
565576
rules:
566577
- prefer_expression_function_bodies
@@ -572,8 +583,8 @@ int foo() {
572583
}
573584
''');
574585

575-
newFile(testFilePath, code.code);
576-
await initialize();
586+
createFile(testFilePath, code.code);
587+
await initializeServer();
577588

578589
var codeActions = await getCodeActions(
579590
testFileUri,
@@ -600,8 +611,8 @@ void f() {
600611
}
601612
''');
602613

603-
newFile(testFilePath, code.code);
604-
await initialize();
614+
createFile(testFilePath, code.code);
615+
await initializeServer();
605616

606617
var codeActions = await getCodeActions(
607618
testFileUri,
@@ -628,8 +639,8 @@ void f() {
628639
var a = [Test, Test, Te[!!]st];
629640
''');
630641

631-
newFile(testFilePath, code.code);
632-
await initialize();
642+
createFile(testFilePath, code.code);
643+
await initializeServer();
633644

634645
var codeActions = await getCodeActions(
635646
testFileUri,
@@ -654,8 +665,8 @@ var a = [Test, Test, Te[!!]st];
654665
var a = [Test, Test, Te[!!]st];
655666
''');
656667

657-
newFile(testFilePath, code.code);
658-
await initialize();
668+
createFile(testFilePath, code.code);
669+
await initializeServer();
659670

660671
var codeActions = await getCodeActions(
661672
testFileUri,
@@ -673,7 +684,7 @@ var a = [Test, Test, Te[!!]st];
673684

674685
Future<void> test_organizeImportsFix_namedOrganizeImports() async {
675686
registerLintRules();
676-
newFile(analysisOptionsPath, '''
687+
createFile(analysisOptionsPath, '''
677688
linter:
678689
rules:
679690
- directives_ordering
@@ -705,10 +716,12 @@ ProcessInfo b;
705716
}
706717

707718
Future<void> test_outsideRoot() async {
708-
var otherFilePath = convertPath('/home/otherProject/foo.dart');
719+
var otherFilePath = pathContext.normalize(
720+
pathContext.join(projectFolderPath, '..', 'otherProject', 'foo.dart'),
721+
);
709722
var otherFileUri = pathContext.toUri(otherFilePath);
710-
newFile(otherFilePath, 'bad code to create error');
711-
await initialize();
723+
createFile(otherFilePath, 'bad code to create error');
724+
await initializeServer();
712725

713726
var codeActions = await getCodeActions(
714727
otherFileUri,
@@ -878,7 +891,7 @@ useFunction(int g(a, b)) {}
878891
void _enableLints(List<String> lintNames) {
879892
registerLintRules();
880893
var lintsYaml = lintNames.map((name) => ' - $name\n').join();
881-
newFile(analysisOptionsPath, '''
894+
createFile(analysisOptionsPath, '''
882895
linter:
883896
rules:
884897
$lintsYaml

0 commit comments

Comments
 (0)