[analysis_server] Enable line ending normalization in completion tests

See https://github.com/dart-lang/sdk/issues/60234

Change-Id: I8d0bf709c05dfbc54f94e939599b75aebd0f09ca
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/453520
Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
Auto-Submit: Danny Tuppeny <danny@tuppeny.com>
Reviewed-by: Keerti Parthasarathy <keertip@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
diff --git a/pkg/analysis_server/test/client/completion_driver_test.dart b/pkg/analysis_server/test/client/completion_driver_test.dart
index 0adf650..ca0cd45 100644
--- a/pkg/analysis_server/test/client/completion_driver_test.dart
+++ b/pkg/analysis_server/test/client/completion_driver_test.dart
@@ -116,6 +116,11 @@
   /// the name(s) is(are) invalid to [identifierRegExp], add the expected name
   /// to [allowedIdentifiers].
   void assertResponse(String expected, {String where = ''}) {
+    // The generated response will always be using the EOLs being tested so
+    // normalize the expected test to the same to ensure that all actual output
+    // uses the expected line endings.
+    expected = normalizeNewlinesForPlatform(expected);
+
     var buffer = StringBuffer();
     printer.CompletionResponsePrinter(
       buffer: buffer,
@@ -134,13 +139,19 @@
           where = ' (containingNode = $containingNode, entity = $entity)';
         }
       }
-      TextExpectationsCollector.add(actual);
+
+      // The source code in our test files will always be written with only `\n`
+      // (due to git settings) so strip any `\r` that might be in the actual
+      // result for any output intended to go into source files.
+      var actualForSourceCode = actual.replaceAll('\r', '');
+
+      TextExpectationsCollector.add(actualForSourceCode);
       fail('''
 The actual suggestions do not match the expected suggestions$where.
 
 To accept the current state change the expectation to
 \r${'-' * 64}
-\r${actual.trimRight().split('\n').join('\n\r')}
+\r${actualForSourceCode.trimRight().split('\n').join('\n\r')}
 \r${'-' * 64}
 ''');
     }
@@ -192,8 +203,6 @@
 
   @override
   Future<void> setUp() async {
-    useLineEndingsForPlatform = false;
-
     super.setUp();
 
     writeTestPackagePubspecYamlFile(r'''
diff --git a/pkg/analysis_server/test/completion_test_support.dart b/pkg/analysis_server/test/completion_test_support.dart
index 90108f8..0db77aa 100644
--- a/pkg/analysis_server/test/completion_test_support.dart
+++ b/pkg/analysis_server/test/completion_test_support.dart
@@ -123,16 +123,6 @@
       await super.tearDown();
     }
   }
-
-  @override
-  Future<void> setUp() async {
-    // TODO(dantup): All tests that use this base class support normalization
-    //  however the base class of this (`AbstractCompletionDomainTest`) has a
-    //  lot of other tests classes that do not currently.
-    useLineEndingsForPlatform = true;
-
-    await super.setUp();
-  }
 }
 
 /// A specification of the completion results expected at a given location.
diff --git a/pkg/analysis_server/test/domain_completion_test.dart b/pkg/analysis_server/test/domain_completion_test.dart
index d1b36dd..8d63db6 100644
--- a/pkg/analysis_server/test/domain_completion_test.dart
+++ b/pkg/analysis_server/test/domain_completion_test.dart
@@ -272,6 +272,11 @@
     String expected, {
     bool printIfFailed = true,
   }) {
+    // The generated response will always be using the EOLs being tested so
+    // normalize the expected test to the same to ensure that all actual output
+    // uses the expected line endings.
+    expected = normalizeNewlinesForPlatform(expected);
+
     var buffer = StringBuffer();
     printer.CompletionResponsePrinter(
       buffer: buffer,
@@ -280,11 +285,16 @@
     ).writeResponse();
     var actual = buffer.toString();
 
+    // The source code in our test files will always be written with only `\n`
+    // (due to git settings) so strip any `\r` that might be in the actual
+    // result for any output intended to go into source files.
+    var actualForSourceCode = actual.replaceAll('\r', '');
+
     if (actual != expected) {
       if (printIfFailed) {
-        print(actual);
+        print(actualForSourceCode);
       }
-      TextExpectationsCollector.add(actual);
+      TextExpectationsCollector.add(actualForSourceCode);
     }
     expect(actual, expected);
   }
diff --git a/pkg/analysis_server/test/services/completion/dart/completion_printer.dart b/pkg/analysis_server/test/services/completion/dart/completion_printer.dart
index c2ce010..1cc6c79 100644
--- a/pkg/analysis_server/test/services/completion/dart/completion_printer.dart
+++ b/pkg/analysis_server/test/services/completion/dart/completion_printer.dart
@@ -3,6 +3,7 @@
 // BSD-style license that can be found in the LICENSE file.
 
 import 'package:analysis_server/src/protocol_server.dart';
+import 'package:analyzer/src/test_utilities/platform.dart';
 import 'package:collection/collection.dart';
 
 import 'completion_check.dart';
@@ -285,7 +286,12 @@
 
   void _writelnWithIndent(String line) {
     buffer.write(_indent);
-    buffer.writeln(line);
+    buffer.write(line);
+    // Always write the EOLs being assumed in this test run because any
+    // multiline content will (if no bugs) be written with these, and we don't
+    // want to perform any normalization on the actual results because that
+    // could mask bugs.
+    buffer.write(testEol);
   }
 
   void _writeLocation() {