Skip to content

Commit 6be105b

Browse files
DanTupCommit Queue
authored andcommitted
[analysis_server] Normalize test sources in base class
This adds code to normalize file contents in `PubPackageAnalysisServerTest.newFile()` so all tests using this base class normalize their source newlines by default (this ensures we run with \r\n on Windows, regardless of the git settings always using \n in this repo). A flag `useLineEndingsForPlatform` allows option out of this, and any tests that currently fail in this mode set this in their setUp - with the exception of a few that were just trivial fixes. This will make it easier to fix the remaining tests (by looking at code that does `useLineEndingsForPlatform = false`, deleting it, then fixing those tests). See #60234 Change-Id: If05bc5f7fe3007151c1290831157b6a62d6bf651 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/449880 Commit-Queue: Brian Wilkerson <[email protected]> Reviewed-by: Samuel Rawlins <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent 31f75e7 commit 6be105b

22 files changed

+107
-56
lines changed

pkg/analysis_server/test/analysis/notification_closing_labels_test.dart

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import 'dart:async';
77
import 'package:analysis_server/protocol/protocol.dart';
88
import 'package:analysis_server/protocol/protocol_constants.dart';
99
import 'package:analysis_server/protocol/protocol_generated.dart';
10+
import 'package:analyzer/src/test_utilities/test_code_format.dart';
1011
import 'package:test/test.dart';
1112
import 'package:test_reflective_loader/test_reflective_loader.dart';
1213

@@ -21,21 +22,21 @@ void main() {
2122
@reflectiveTest
2223
class AnalysisNotificationClosingLabelsTest
2324
extends PubPackageAnalysisServerTest {
24-
static const sampleCode = '''
25+
late final expectedResults = [
26+
_label(sampleCode.ranges[0], 'Row'),
27+
_label(sampleCode.ranges[1], '<Widget>[]'),
28+
];
29+
30+
final sampleCode = TestCode.parseNormalized('''
2531
Widget build(BuildContext context) {
26-
return new Row(
27-
children: <Widget>[
32+
return /*[0*/new Row(
33+
children: /*[1*/<Widget>[
2834
Text('a'),
2935
Text('b'),
30-
],
31-
);
36+
]/*1]*/,
37+
)/*0]*/;
3238
}
33-
''';
34-
35-
static final expectedResults = [
36-
ClosingLabel(46, 77, 'Row'),
37-
ClosingLabel(69, 49, '<Widget>[]'),
38-
];
39+
''');
3940

4041
List<ClosingLabel>? lastLabels;
4142

@@ -72,7 +73,7 @@ Widget build(BuildContext context) {
7273
}
7374

7475
Future<void> test_afterAnalysis() async {
75-
addTestFile(sampleCode);
76+
addTestFile(sampleCode.code);
7677
await waitForTasksFinished();
7778
expect(lastLabels, isNull);
7879

@@ -95,7 +96,7 @@ Widget build(BuildContext context) {
9596
expect(lastLabels, hasLength(0));
9697

9798
// With sample code there will be labels.
98-
await waitForLabels(() async => modifyTestFile(sampleCode));
99+
await waitForLabels(() async => modifyTestFile(sampleCode.code));
99100

100101
expect(lastLabels, expectedResults);
101102
}
@@ -105,4 +106,12 @@ Widget build(BuildContext context) {
105106
await action();
106107
return _labelsReceived.future;
107108
}
109+
110+
ClosingLabel _label(TestCodeRange range, String text) {
111+
return ClosingLabel(
112+
range.sourceRange.offset,
113+
range.sourceRange.length,
114+
text,
115+
);
116+
}
108117
}

pkg/analysis_server/test/analysis/notification_folding_test.dart

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import 'dart:async';
66

77
import 'package:analysis_server/protocol/protocol_constants.dart';
88
import 'package:analysis_server/src/protocol_server.dart';
9+
import 'package:analyzer/src/test_utilities/test_code_format.dart';
910
import 'package:test/test.dart';
1011
import 'package:test_reflective_loader/test_reflective_loader.dart';
1112

@@ -19,17 +20,21 @@ void main() {
1920

2021
@reflectiveTest
2122
class AnalysisNotificationFoldingTest extends PubPackageAnalysisServerTest {
22-
static const sampleCode = '''
23-
import 'dart:async';
24-
import 'dart:core';
23+
final sampleCode = TestCode.parseNormalized('''
24+
import[! 'dart:async';
25+
import 'dart:core';!]
2526
2627
main async() {}
27-
''';
28+
''');
2829

29-
static final expectedResults = [
30+
late final expectedResults = [
3031
// We don't include the first "import" in the region because
3132
// we want that to remain visible (not collapse).
32-
FoldingRegion(FoldingKind.DIRECTIVES, 6, 34),
33+
FoldingRegion(
34+
FoldingKind.DIRECTIVES,
35+
sampleCode.range.sourceRange.offset,
36+
sampleCode.range.sourceRange.length,
37+
),
3338
];
3439

3540
List<FoldingRegion>? lastRegions;
@@ -67,7 +72,7 @@ main async() {}
6772
}
6873

6974
Future<void> test_afterAnalysis() async {
70-
addTestFile(sampleCode);
75+
addTestFile(sampleCode.code);
7176
await waitForTasksFinished();
7277
expect(lastRegions, isNull);
7378

@@ -90,7 +95,7 @@ main async() {}
9095
expect(lastRegions, hasLength(0));
9196

9297
// With sample code there will be folding regions.
93-
await waitForFolding(() async => modifyTestFile(sampleCode));
98+
await waitForFolding(() async => modifyTestFile(sampleCode.code));
9499

95100
expect(lastRegions, expectedResults);
96101
}

pkg/analysis_server/test/analysis/notification_highlights2_test.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2768,6 +2768,8 @@ class HighlightsTestSupport extends PubPackageAnalysisServerTest {
27682768

27692769
@override
27702770
Future<void> setUp() async {
2771+
useLineEndingsForPlatform = false;
2772+
27712773
super.setUp();
27722774
await setRoots(included: [workspaceRootPath], excluded: []);
27732775
}

pkg/analysis_server/test/analysis/notification_navigation_test.dart

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ augment class A {
455455
bar();
456456
}
457457
458-
void bar(){}
458+
void [!bar!](){}
459459
}
460460
''');
461461
newFile(augmentFilePath, '''
@@ -470,7 +470,11 @@ void f() {
470470

471471
await prepareNavigation();
472472
assertHasRegion('bar');
473-
assertHasFileTarget(convertPath(testFilePath), 76, 3);
473+
assertHasFileTarget(
474+
convertPath(testFilePath),
475+
parsedSourceRange.offset,
476+
parsedSourceRange.length,
477+
);
474478
}
475479

476480
@SkippedTest() // TODO(scheglov): implement augmentation

pkg/analysis_server/test/analysis/notification_occurrences_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class AnalysisNotificationOccurrencesTest extends PubPackageAnalysisServerTest {
3232
required ElementKind? kind,
3333
String? elementName,
3434
}) async {
35-
var code = TestCode.parse(content);
35+
var code = TestCode.parseNormalized(content);
3636
addTestFile(code.code);
3737

3838
await prepareOccurrences();

pkg/analysis_server/test/analysis_server_base.dart

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import 'package:analyzer/source/source_range.dart';
1717
import 'package:analyzer/src/dart/analysis/byte_store.dart';
1818
import 'package:analyzer/src/generated/sdk.dart';
1919
import 'package:analyzer/src/test_utilities/mock_sdk.dart';
20+
import 'package:analyzer/src/test_utilities/platform.dart';
2021
import 'package:analyzer/src/test_utilities/test_code_format.dart';
2122
import 'package:analyzer/src/util/file_paths.dart' as file_paths;
2223
import 'package:analyzer_testing/experiments/experiments.dart';
@@ -218,6 +219,9 @@ class PubPackageAnalysisServerTest extends ContextResolutionTest
218219

219220
final String testPackageName = 'test';
220221

222+
/// Whether to automatically normalize line endings for the current platform.
223+
bool useLineEndingsForPlatform = true;
224+
221225
/// Return a list of the experiments that are to be enabled for tests in this
222226
/// class, an empty list if there are no experiments that should be enabled.
223227
List<String> get experiments => experimentsForTests;
@@ -226,6 +230,8 @@ class PubPackageAnalysisServerTest extends ContextResolutionTest
226230
@override
227231
String get packagesRootPath => resourceProvider.convertPath('/packages');
228232

233+
List<TestCodePosition> get parsedPositions => parsedTestCode.positions;
234+
229235
TestCodeRange get parsedRange => parsedTestCode.range;
230236

231237
List<TestCodeRange> get parsedRanges => parsedTestCode.ranges;
@@ -269,7 +275,9 @@ class PubPackageAnalysisServerTest extends ContextResolutionTest
269275

270276
// TODO(scheglov): rename
271277
void addTestFile(String content) {
272-
parsedTestCode = TestCode.parse(content);
278+
parsedTestCode = useLineEndingsForPlatform
279+
? TestCode.parseNormalized(content)
280+
: TestCode.parse(content);
273281
newFile(testFilePath, parsedTestCode.code);
274282
}
275283

@@ -310,6 +318,14 @@ class PubPackageAnalysisServerTest extends ContextResolutionTest
310318
modifyFile2(testFile, content);
311319
}
312320

321+
@override
322+
File newFile(String path, String content) {
323+
if (useLineEndingsForPlatform) {
324+
content = normalizeNewlinesForPlatform(content);
325+
}
326+
return super.newFile(path, content);
327+
}
328+
313329
/// Returns the offset of [search] in [file].
314330
/// Fails if not found.
315331
int offsetInFile(File file, String search) {

pkg/analysis_server/test/client/completion_driver_test.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,8 @@ To accept the current state change the expectation to
191191

192192
@override
193193
Future<void> setUp() async {
194+
useLineEndingsForPlatform = false;
195+
194196
super.setUp();
195197

196198
writeTestPackagePubspecYamlFile(r'''

pkg/analysis_server/test/domain_completion_test.dart

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import 'dart:async';
66

77
import 'package:analysis_server/src/protocol_server.dart';
88
import 'package:analyzer/file_system/file_system.dart';
9+
import 'package:analyzer/src/test_utilities/platform.dart';
910
import 'package:analyzer/src/test_utilities/test_code_format.dart';
1011
import 'package:analyzer/utilities/package_config_file_builder.dart';
1112
import 'package:test/test.dart';
@@ -27,6 +28,9 @@ void main() {
2728
@reflectiveTest
2829
class CompletionDomainHandlerGetSuggestionDetails2Test
2930
extends PubPackageAnalysisServerTest {
31+
String get testEolEscaped =>
32+
testEol.replaceAll('\r', r'\r').replaceAll('\n', r'\n');
33+
3034
void assertDetailsText(
3135
CompletionGetSuggestionDetails2Result result,
3236
String expected, {
@@ -83,13 +87,13 @@ void f() {
8387
libraryUri: 'dart:math',
8488
);
8589

86-
assertDetailsText(details, r'''
90+
assertDetailsText(details, '''
8791
completion: Random
8892
change
8993
testFile
9094
offset: 0
9195
length: 0
92-
replacement: import 'dart:math';\n\n
96+
replacement: import 'dart:math';$testEolEscaped$testEolEscaped
9397
''');
9498
}
9599

@@ -122,13 +126,13 @@ void f() {
122126
libraryUri: 'package:aaa/a.dart',
123127
);
124128

125-
assertDetailsText(details, r'''
129+
assertDetailsText(details, '''
126130
completion: Test
127131
change
128132
testFile
129133
offset: 0
130134
length: 0
131-
replacement: import 'package:aaa/a.dart';\n\n
135+
replacement: import 'package:aaa/a.dart';$testEolEscaped$testEolEscaped
132136
''');
133137
}
134138

@@ -149,13 +153,13 @@ void f() {
149153
libraryUri: 'package:test/a.dart',
150154
);
151155

152-
assertDetailsText(details, r'''
156+
assertDetailsText(details, '''
153157
completion: Test
154158
change
155159
testFile
156160
offset: 0
157161
length: 0
158-
replacement: import 'package:test/a.dart';\n\n
162+
replacement: import 'package:test/a.dart';$testEolEscaped$testEolEscaped
159163
''');
160164
}
161165

@@ -2168,7 +2172,7 @@ suggestions
21682172
required String content,
21692173
int maxResults = 1 << 10,
21702174
}) async {
2171-
var code = TestCode.parse(content);
2175+
var code = TestCode.parseNormalized(content);
21722176
var completionOffset = code.position.offset;
21732177

21742178
newFile(path, code.code);
@@ -2306,7 +2310,9 @@ class _SuggestionDetailsPrinter {
23062310
_writelnWithIndent('offset: ${edit.offset}');
23072311
_writelnWithIndent('length: ${edit.length}');
23082312

2309-
var replacementStr = edit.replacement.replaceAll('\n', r'\n');
2313+
var replacementStr = edit.replacement
2314+
.replaceAll('\n', r'\n')
2315+
.replaceAll('\r', r'\r');
23102316
_writelnWithIndent('replacement: $replacementStr');
23112317
}
23122318

pkg/analysis_server/test/domain_completion_util.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ class AbstractCompletionDomainTest extends PubPackageAnalysisServerTest {
129129

130130
@override
131131
Future<void> setUp() async {
132+
useLineEndingsForPlatform = false;
133+
132134
super.setUp();
133135
await setRoots(included: [workspaceRootPath], excluded: []);
134136
}

pkg/analysis_server/test/edit/assists_test.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import 'package:analysis_server/protocol/protocol_generated.dart';
66
import 'package:analysis_server/src/plugin/plugin_isolate.dart';
77
import 'package:analysis_server/src/services/correction/assist_internal.dart';
88
import 'package:analyzer/instrumentation/service.dart';
9+
import 'package:analyzer/src/test_utilities/platform.dart';
910
import 'package:analyzer_plugin/protocol/protocol_common.dart';
1011
import 'package:analyzer_plugin/protocol/protocol_generated.dart' as plugin;
1112
import 'package:test/test.dart';
@@ -169,7 +170,7 @@ void f() {
169170
testFileContent,
170171
change.edits[0].edits,
171172
);
172-
expect(resultCode, expectedCode);
173+
expect(resultCode, normalizeNewlinesForPlatform(expectedCode));
173174
return;
174175
}
175176
}

0 commit comments

Comments
 (0)