Skip to content

Commit 3c46092

Browse files
DanTupCommit Queue
authored and
Commit Queue
committed
[analysis_server] Bump yaml_edit, remove ignore_diagnostic workaround
This bumps yaml_edit to get a fix from dart-archive/yaml_edit#66 that corrects line endings on Windows. It removes the workaround that was in the server normalizing these itself. It also updates the tests to use normalized content to ensure consistent line endings on both platforms. Change-Id: I096978500b30ca41d38bc9e78dc9bdf3e44474d1 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/353521 Reviewed-by: Brian Wilkerson <[email protected]> Reviewed-by: Keerti Parthasarathy <[email protected]> Commit-Queue: Keerti Parthasarathy <[email protected]>
1 parent b457490 commit 3c46092

File tree

4 files changed

+54
-51
lines changed

4 files changed

+54
-51
lines changed

pkg/analysis_server/lib/src/services/correction/dart/ignore_diagnostic.dart

-13
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import 'package:analysis_server/src/services/correction/util.dart';
88
import 'package:analyzer/error/error.dart';
99
import 'package:analyzer/file_system/file_system.dart';
1010
import 'package:analyzer/src/ignore_comments/ignore_info.dart';
11-
import 'package:analyzer/src/test_utilities/platform.dart';
1211
import 'package:analyzer/src/workspace/blaze.dart';
1312
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
1413
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
@@ -156,18 +155,6 @@ class IgnoreDiagnosticInAnalysisOptionsFile extends AbstractIgnoreDiagnostic {
156155

157156
var edit = editor.edits.single;
158157
var replacement = edit.replacement;
159-
160-
// TODO(dantup): The YAML editor currently produces inconsistent line
161-
// endings in edits when the source file contains '\r\n'.
162-
// https://github.com/dart-lang/yaml_edit/issues/65
163-
var analysisOptionsEol = content.contains('\r')
164-
? '\r\n'
165-
: content.contains('\n')
166-
? '\n'
167-
: platformEol;
168-
replacement =
169-
replacement.replaceAll('\r', '').replaceAll('\n', analysisOptionsEol);
170-
171158
builder.addSimpleInsertion(edit.offset, replacement);
172159
});
173160
}

pkg/analysis_server/test/abstract_context.dart

+16-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import 'package:analyzer/src/dart/analysis/driver_based_analysis_context.dart';
1515
import 'package:analyzer/src/dart/analysis/experiments.dart';
1616
import 'package:analyzer/src/generated/engine.dart' show AnalysisEngine;
1717
import 'package:analyzer/src/test_utilities/mock_sdk.dart';
18+
import 'package:analyzer/src/test_utilities/platform.dart';
1819
import 'package:analyzer/src/test_utilities/resource_provider_mixin.dart';
1920
import 'package:analyzer/src/util/file_paths.dart' as file_paths;
2021
import 'package:analyzer/src/utilities/extensions/file_system.dart';
@@ -31,6 +32,9 @@ class AbstractContextTest
3132

3233
static final ByteStore _byteStore = MemoryByteStore();
3334

35+
/// Whether to rewrite line endings in test code based on platform.
36+
bool useLineEndingsForPlatform = false;
37+
3438
final Map<String, String> _declaredVariables = {};
3539
AnalysisContextCollectionImpl? _analysisContextCollection;
3640

@@ -161,7 +165,7 @@ class AbstractContextTest
161165
}
162166
}
163167

164-
newFile(analysisOptionsPath, buffer.toString());
168+
writeAnalysisOptionsFile(buffer.toString());
165169
}
166170

167171
/// Returns the existing analysis driver that should be used to analyze the
@@ -191,11 +195,16 @@ class AbstractContextTest
191195
throw StateError('Only dart files can be changed after analysis.');
192196
}
193197

194-
final file = super.newFile(path, content);
198+
final file = super.newFile(path, normalizeSource(content));
195199
_addAnalyzedFileToDrivers(file);
196200
return file;
197201
}
198202

203+
/// Convenience function to normalize newlines in [code] for the current
204+
/// platform if [useLineEndingsForPlatform] is `true`.
205+
String normalizeSource(String code) =>
206+
useLineEndingsForPlatform ? normalizeNewlinesForPlatform(code) : code;
207+
199208
Future<AnalysisSession> sessionFor(File file) async {
200209
var analysisContext = _contextFor(file);
201210
await analysisContext.applyPendingFileChanges();
@@ -237,6 +246,11 @@ class AbstractContextTest
237246

238247
void verifyCreatedCollection() {}
239248

249+
/// Writes string content as an analysis options file.
250+
void writeAnalysisOptionsFile(String content) {
251+
newFile(analysisOptionsPath, content);
252+
}
253+
240254
void _addAnalyzedFilesToDrivers() {
241255
for (var analysisContext in _analysisContextCollection!.contexts) {
242256
for (var path in analysisContext.contextRoot.analyzedFiles()) {

pkg/analysis_server/test/abstract_single_unit.dart

-16
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,13 @@ import 'package:analyzer/file_system/file_system.dart';
1010
import 'package:analyzer/src/error/codes.g.dart';
1111
import 'package:analyzer/src/test_utilities/find_element.dart';
1212
import 'package:analyzer/src/test_utilities/find_node.dart';
13-
import 'package:analyzer/src/test_utilities/platform.dart';
1413
import 'package:test/test.dart';
1514

1615
import 'abstract_context.dart';
1716

1817
class AbstractSingleUnitTest extends AbstractContextTest {
1918
bool verifyNoTestUnitErrors = true;
2019

21-
/// Whether to rewrite line endings in test code based on platform.
22-
bool useLineEndingsForPlatform = false;
23-
2420
late String testCode;
2521
late ParsedUnitResult testParsedResult;
2622
late ResolvedUnitResult testAnalysisResult;
@@ -31,7 +27,6 @@ class AbstractSingleUnitTest extends AbstractContextTest {
3127
late FindElement findElement;
3228

3329
void addTestSource(String code) {
34-
code = normalizeSource(code);
3530
testCode = code;
3631
newFile(testFile.path, code);
3732
}
@@ -81,17 +76,6 @@ class AbstractSingleUnitTest extends AbstractContextTest {
8176
return result;
8277
}
8378

84-
@override
85-
File newFile(String path, String content) {
86-
content = normalizeSource(content);
87-
return super.newFile(path, content);
88-
}
89-
90-
/// Convenient function to normalize newlines in [code] for the current
91-
/// platform if [useLineEndingsForPlatform] is `true`.
92-
String normalizeSource(String code) =>
93-
useLineEndingsForPlatform ? normalizeNewlinesForPlatform(code) : code;
94-
9579
Future<void> parseTestCode(String code) async {
9680
addTestSource(code);
9781
await getParsedUnit(testFile);

pkg/analysis_server/test/src/services/correction/fix/ignore_diagnostic_test.dart

+38-20
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ class IgnoreDiagnosticAnaylsisOptionFileTest extends FixProcessorTest {
2121
@override
2222
FixKind get kind => DartFixKind.IGNORE_ERROR_ANALYSIS_FILE;
2323

24+
@override
25+
void setUp() {
26+
useLineEndingsForPlatform = true;
27+
super.setUp();
28+
}
29+
2430
Future<void> test_addFixToExistingErrorMap() async {
2531
createAnalysisOptionsFile(
2632
errors: {'unused_label': 'ignore'},
@@ -44,31 +50,30 @@ analyzer:
4450

4551
Future<void> test_emptyAnalysisOptionsFile() async {
4652
// This overwrites the file created by `super.setUp` method.
47-
resourceProvider.getFile(analysisOptionsPath).writeAsStringSync('');
53+
writeBlankAnalysisOptionsFile();
4854

4955
await resolveTestCode('''
50-
void f() {
51-
var a = 1;
52-
}
53-
''');
56+
void f() {
57+
var a = 1;
58+
}
59+
''');
5460
await assertHasFix(
5561
'''
5662
analyzer:
5763
errors:
58-
unused_local_variable: ignore''',
64+
unused_local_variable: ignore
65+
''',
5966
target: analysisOptionsPath,
6067
);
6168
}
6269

6370
Future<void> test_invalidAnalysisOptionsFormat() async {
6471
// This overwrites the file created by `super.setUp` method.
6572
// Note: a label without a value is an `invalid_section_format` for dart.
66-
resourceProvider.getFile(analysisOptionsPath).writeAsStringSync(
67-
'''
73+
writeAnalysisOptionsFile('''
6874
analyzer:
6975
linter:
70-
''',
71-
);
76+
''');
7277

7378
await resolveTestCode('''
7479
void f() {
@@ -88,15 +93,15 @@ analyzer:
8893
// This deletes the file created by `super.setUp` method.
8994
resourceProvider.getFile(analysisOptionsPath).delete();
9095
await resolveTestCode('''
91-
void f() {
92-
var a = 1;
93-
}
94-
''');
96+
void f() {
97+
var a = 1;
98+
}
99+
''');
95100
await assertNoFix();
96101
}
97102

98103
Future<void> test_noAnalyzerLabel() async {
99-
createAnalysisOptionsFile();
104+
writeBlankAnalysisOptionsFile();
100105

101106
await resolveTestCode('''
102107
void f() {
@@ -107,7 +112,8 @@ void f() {
107112
'''
108113
analyzer:
109114
errors:
110-
unused_local_variable: ignore''',
115+
unused_local_variable: ignore
116+
''',
111117
target: analysisOptionsPath,
112118
);
113119
}
@@ -151,9 +157,12 @@ void f() {
151157

152158
Future<void> test_onlyIncludeLabel() async {
153159
// This overwrites the file created by `super.setUp` method.
154-
resourceProvider.getFile(analysisOptionsPath).writeAsStringSync(
155-
'include: package:lints/recommended.yaml',
156-
);
160+
// Having a newline is important because yaml_edit copies existing
161+
// newlines and we want to test the current platforms EOLs.
162+
// The content is normalized in newFile().
163+
writeAnalysisOptionsFile('''
164+
include: package:lints/recommended.yaml
165+
''');
157166

158167
await resolveTestCode('''
159168
void f() {
@@ -165,7 +174,8 @@ void f() {
165174
analyzer:
166175
errors:
167176
unused_local_variable: ignore
168-
include: package:lints/recommended.yaml''',
177+
include: package:lints/recommended.yaml
178+
''',
169179
target: analysisOptionsPath,
170180
);
171181
}
@@ -183,6 +193,14 @@ void f() {
183193
''');
184194
await assertNoFix();
185195
}
196+
197+
void writeBlankAnalysisOptionsFile() {
198+
// Include a newline because yaml_edit will copy existing newlines and
199+
// this newline will be normalized for the current platform. Without it,
200+
// yaml_edit will produce new content using \n on Windows which may not
201+
// match the expectations here depending on Git EOL settings.
202+
writeAnalysisOptionsFile('\n');
203+
}
186204
}
187205

188206
@reflectiveTest

0 commit comments

Comments
 (0)