Skip to content

Commit 59587b0

Browse files
DanTupCommit Queue
authored andcommitted
[analysis_server] Enable test newline normalization by default
+ fix several failing tests with this enabled. See #60234 Change-Id: Ie71e1655bf2ab8ac651bfcf803884741863b42c1 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/423801 Reviewed-by: Samuel Rawlins <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent 226ec84 commit 59587b0

File tree

15 files changed

+75
-46
lines changed

15 files changed

+75
-46
lines changed

pkg/analysis_server/test/abstract_context.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class AbstractContextTest
3434
static final ByteStore _byteStore = MemoryByteStore();
3535

3636
/// Whether to rewrite line endings in test code based on platform.
37-
bool useLineEndingsForPlatform = false;
37+
bool useLineEndingsForPlatform = true;
3838

3939
final Map<String, String> _declaredVariables = {};
4040
AnalysisContextCollectionImpl? _analysisContextCollection;

pkg/analysis_server/test/lsp/source_edits_test.dart

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'dart:convert';
6+
import 'dart:io';
67

78
import 'package:analysis_server/lsp_protocol/protocol.dart';
89
import 'package:analysis_server/src/lsp/error_or.dart';
910
import 'package:analysis_server/src/lsp/source_edits.dart';
11+
import 'package:analyzer/src/test_utilities/test_code_format.dart';
1012
import 'package:test/test.dart';
1113
import 'package:test_reflective_loader/test_reflective_loader.dart';
1214

@@ -319,13 +321,14 @@ Delete 1:24-1:26
319321
/// https://github.com/Dart-Code/Dart-Code/issues/5200
320322
Future<void> test_minimalEdits_comment_multiLine_trailingWhitespace() async {
321323
// The initial content has a trailing space on the end of the comment.
322-
const startContent = r'''
324+
var startContent =
325+
TestCode.parse(r'''
323326
/**
324-
* line with trailing whitespace
325-
* line with trailing whitespace
327+
* line with trailing whitespace /**/
328+
* line with trailing whitespace /**/
326329
*/
327330
int? a;
328-
''';
331+
''').code;
329332
// We expect the trailing spaces to be removed.
330333
const endContent = r'''
331334
/**
@@ -619,6 +622,9 @@ void g() {
619622
String? expectedFormatResult,
620623
Range? range,
621624
}) async {
625+
start = normalizeSource(start);
626+
end = normalizeSource(end);
627+
622628
await parseTestCode(start);
623629
var edits =
624630
generateEditsForFormatting(testParsedResult, range: range).result!;
@@ -639,10 +645,26 @@ void g() {
639645
String? expectedFormatResult,
640646
Range? range,
641647
}) async {
642-
start = start.trim();
643-
end = end.trim();
648+
start = normalizeSource(start.trim());
649+
end = normalizeSource(end.trim());
650+
if (expectedFormatResult != null) {
651+
expectedFormatResult = normalizeSource(expectedFormatResult.trim());
652+
}
644653
expected = expected.trim();
645-
expectedFormatResult = expectedFormatResult?.trim();
654+
655+
// Expectation descriptions are always written with literal newlines as '\n'
656+
// so on Windows we will need to update the expectation to '\r\n' before
657+
// comparing.
658+
expect(
659+
expected,
660+
isNot(contains(r'\r')),
661+
reason:
662+
'Expectations should always be written with literal '
663+
r"'\n' to indicate newlines regardless of platform",
664+
);
665+
if (Platform.isWindows) {
666+
expected = expected.replaceAll(r'\n', r'\r\n');
667+
}
646668

647669
await parseTestCode(start);
648670
var edits = generateMinimalEdits(testParsedResult, end, range: range);

pkg/analysis_server/test/services/completion/postfix/postfix_completion_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class PostfixCompletionTest extends AbstractSingleUnitTest {
3737
if (change.edits.isNotEmpty) {
3838
// We use a carat in the expected code to prevent lines containing only
3939
// whitespace from being made empty.
40-
var expectedCode = TestCode.parse(expected);
40+
var expectedCode = TestCode.parse(normalizeSource(expected));
4141
var resultCode = SourceEdit.applySequence(
4242
testCode,
4343
change.edits[0].edits,
@@ -68,7 +68,7 @@ class PostfixCompletionTest extends AbstractSingleUnitTest {
6868
}
6969

7070
Future<void> _prepareProcessor(String key, String code) async {
71-
testCodeCode = TestCode.parse(code);
71+
testCodeCode = TestCode.parse(normalizeSource(code));
7272

7373
verifyNoTestUnitErrors = false;
7474
await resolveTestCode(testCodeCode.code);

pkg/analysis_server/test/services/correction/organize_directives_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1001,7 +1001,7 @@ class NonLibraryAnnotation {
10011001
);
10021002
var edits = organizer.organize();
10031003
var result = SourceEdit.applySequence(testCode, edits);
1004-
expect(result, expectedCode);
1004+
expect(result, normalizeSource(expectedCode));
10051005
}
10061006

10071007
Future<void> _computeUnitAndErrors(String code) async {

pkg/analysis_server/test/services/correction/sort_members_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1793,6 +1793,6 @@ void f() {}
17931793
);
17941794
var edits = sorter.sort();
17951795
var result = SourceEdit.applySequence(testCode, edits);
1796-
expect(result, expectedCode);
1796+
expect(result, normalizeSource(expectedCode));
17971797
}
17981798
}

pkg/analysis_server/test/src/computer/color_computer_test.dart

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,9 @@ class ColorComputerTest extends AbstractContextTest {
137137
Map<String, int> expectedColorValues, {
138138
String? otherCode,
139139
}) async {
140-
dartCode = _withCommonImports(dartCode);
141-
otherCode = otherCode != null ? _withCommonImports(otherCode) : null;
140+
dartCode = _withCommonImportsNormalized(dartCode);
141+
otherCode =
142+
otherCode != null ? _withCommonImportsNormalized(otherCode) : null;
142143

143144
newFile(testPath, dartCode);
144145
if (otherCode != null) {
@@ -460,10 +461,12 @@ final a = [[COLOR]];
460461
await checkAllColors(testCode);
461462
}
462463

463-
String _withCommonImports(String code) => '''
464+
String _withCommonImportsNormalized(String code) {
465+
return normalizeSource('''
464466
import 'package:flutter/cupertino.dart';
465467
import 'package:flutter/painting.dart';
466468
import 'package:flutter/material.dart';
467469
468-
$code''';
470+
$code''');
471+
}
469472
}

pkg/analysis_server/test/src/computer/folding_computer_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1041,7 +1041,7 @@ void f() {}
10411041
}
10421042

10431043
Future<void> _computeRegions(String sourceContent) async {
1044-
code = TestCode.parse(sourceContent);
1044+
code = TestCode.parse(normalizeSource(sourceContent));
10451045
var file = newFile(sourcePath, code.code);
10461046
var result = await getResolvedUnit(file);
10471047
var computer = DartUnitFoldingComputer(result.lineInfo, result.unit);

pkg/analysis_server/test/src/computer/highlights_computer_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ void f() {
110110
String content, {
111111
bool hasErrors = false,
112112
}) async {
113-
this.content = content;
113+
this.content = content = normalizeSource(content);
114114
var file = newFile(sourcePath, content);
115115
var result = await getResolvedUnit(file);
116116

pkg/analysis_server/test/src/services/completion/dart/feature_computer_test.dart

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'package:analysis_server/src/services/completion/dart/feature_computer.dart';
6-
import 'package:analyzer/src/test_utilities/test_code_format.dart';
76
import 'package:analyzer_plugin/src/utilities/completion/completion_target.dart';
87
import 'package:test/test.dart';
98
import 'package:test_reflective_loader/test_reflective_loader.dart';
@@ -1096,11 +1095,8 @@ abstract class FeatureComputerTest extends AbstractSingleUnitTest {
10961095
bool verifyNoTestUnitErrors = false;
10971096

10981097
Future<void> completeIn(String content) async {
1099-
var code = TestCode.parse(content);
1100-
cursorIndex = code.position.offset;
1101-
1102-
content = code.code;
1103-
await resolveTestCode(code.code);
1098+
await resolveTestCode(content);
1099+
cursorIndex = parsedTestCode.position.offset;
11041100
completionTarget = CompletionTarget.forOffset(testUnit, cursorIndex);
11051101
}
11061102
}

pkg/analysis_server/test/src/services/correction/assist/assist_processor.dart

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,13 @@ abstract class AssistProcessorTest extends AbstractSingleUnitTest {
7171
int index = 0,
7272
}) async {
7373
_setPositionOrRange(index);
74-
if (useLineEndingsForPlatform) {
75-
expected = normalizeNewlinesForPlatform(expected);
76-
additionallyChangedFiles = additionallyChangedFiles?.map(
77-
(key, value) =>
78-
MapEntry(key, value.map(normalizeNewlinesForPlatform).toList()),
79-
);
80-
}
74+
75+
expected = normalizeNewlinesForPlatform(expected);
76+
additionallyChangedFiles = additionallyChangedFiles?.map(
77+
(key, value) =>
78+
MapEntry(key, value.map(normalizeNewlinesForPlatform).toList()),
79+
);
80+
8181
// Remove any marker in the expected code. We allow markers to prevent an
8282
// otherwise empty line from having the leading whitespace be removed.
8383
expected = TestCode.parse(expected).code;
@@ -139,12 +139,6 @@ abstract class AssistProcessorTest extends AbstractSingleUnitTest {
139139
}).toList();
140140
}
141141

142-
@override
143-
void setUp() {
144-
super.setUp();
145-
useLineEndingsForPlatform = true;
146-
}
147-
148142
/// Computes assists and verifies that there is an assist of the given kind.
149143
Future<Assist> _assertHasAssist() async {
150144
var assists = await _computeAssists();

0 commit comments

Comments
 (0)