Files
sdk/pkg/analysis_server/test/lsp/code_actions_fixes_test.dart
T
Danny Tuppeny f7e288e3ad Return empty responses instead of errors for non-Dart files in LSP server
Previously we could return "invalid path" responses for any non-Dart files. This allows clients to send requests for other files and just successful (though empty) responses.

Change-Id: I192e83ac2ebd2124c03c22e3e048a066cfd33f17
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/101990
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Danny Tuppeny <dantup@google.com>
2019-05-13 09:04:36 +00:00

109 lines
3.5 KiB
Dart

// Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:analysis_server/lsp_protocol/protocol_generated.dart';
import 'package:test/test.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';
import 'code_actions_abstract.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(FixesCodeActionsTest);
});
}
@reflectiveTest
class FixesCodeActionsTest extends AbstractCodeActionsTest {
test_appliesCorrectEdits_withDocumentChangesSupport() async {
// This code should get a fix to remove the unused import.
const content = '''
import 'dart:async';
[[import]] 'dart:convert';
Future foo;
''';
const expectedContent = '''
import 'dart:async';
Future foo;
''';
await newFile(mainFilePath, content: withoutMarkers(content));
await initialize(
textDocumentCapabilities: withCodeActionKinds(
emptyTextDocumentClientCapabilities, [CodeActionKind.QuickFix]),
workspaceCapabilities:
withDocumentChangesSupport(emptyWorkspaceClientCapabilities),
);
final codeActions = await getCodeActions(mainFileUri.toString(),
range: rangeFromMarkers(content));
final fixAction = findEditAction(
codeActions, CodeActionKind.QuickFix, 'Remove unused import');
// Ensure the edit came back, and using documentChanges.
expect(fixAction, isNotNull);
expect(fixAction.edit.documentChanges, isNotNull);
expect(fixAction.edit.changes, isNull);
// Ensure applying the changes will give us the expected content.
final contents = {
mainFilePath: withoutMarkers(content),
};
applyDocumentChanges(contents, fixAction.edit.documentChanges);
expect(contents[mainFilePath], equals(expectedContent));
}
test_appliesCorrectEdits_withoutDocumentChangesSupport() async {
// This code should get a fix to remove the unused import.
const content = '''
import 'dart:async';
[[import]] 'dart:convert';
Future foo;
''';
const expectedContent = '''
import 'dart:async';
Future foo;
''';
await newFile(mainFilePath, content: withoutMarkers(content));
await initialize(
textDocumentCapabilities: withCodeActionKinds(
emptyTextDocumentClientCapabilities, [CodeActionKind.QuickFix]),
);
final codeActions = await getCodeActions(mainFileUri.toString(),
range: rangeFromMarkers(content));
final fixAction = findEditAction(
codeActions, CodeActionKind.QuickFix, 'Remove unused import');
// Ensure the edit came back, and using changes.
expect(fixAction, isNotNull);
expect(fixAction.edit.changes, isNotNull);
expect(fixAction.edit.documentChanges, isNull);
// Ensure applying the changes will give us the expected content.
final contents = {
mainFilePath: withoutMarkers(content),
};
applyChanges(contents, fixAction.edit.changes);
expect(contents[mainFilePath], equals(expectedContent));
}
test_nonDartFile() async {
await newFile(pubspecFilePath, content: simplePubspecContent);
await initialize(
textDocumentCapabilities: withCodeActionKinds(
emptyTextDocumentClientCapabilities, [CodeActionKind.QuickFix]),
);
final codeActions =
await getCodeActions(pubspecFileUri.toString(), range: startOfDocRange);
expect(codeActions, isEmpty);
}
}