Skip to content

Commit 4fce27b

Browse files
nshahanCommit Queue
authored andcommitted
[tests] Fix formatting when writing diffs
* Normalize white space around source code strings before producing diffs by trimming and appending a single newline character. * Write an empty line between the source code and diff text in generation files. * Run dart format on all test files. With these changes you should be able to run the hot reload suite with `--diff write` and dart format the code multiple times without introducing changes in the file. Change-Id: Ifc0b1dd7032bd448f5f6568c5aa3c18dde076c71 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/405247 Reviewed-by: Mark Zhou <[email protected]> Commit-Queue: Nicholas Shahan <[email protected]>
1 parent a0db69f commit 4fce27b

File tree

130 files changed

+254
-188
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

130 files changed

+254
-188
lines changed

pkg/dev_compiler/test/hot_reload_suite.dart

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -575,8 +575,10 @@ abstract class HotReloadSuiteRunner {
575575
}
576576
final previousTempUri = generatedCodeDir.uri.resolve('__previous');
577577
final currentTempUri = generatedCodeDir.uri.resolve('__current');
578-
File.fromUri(previousTempUri).writeAsStringSync(previousCode);
579-
File.fromUri(currentTempUri).writeAsStringSync(currentCode);
578+
// Avoid 'No newline at end of file' messages in the output by
579+
// appending a newline to the trimmed source code strings.
580+
File.fromUri(previousTempUri).writeAsStringSync('$previousCode\n');
581+
File.fromUri(currentTempUri).writeAsStringSync('$currentCode\n');
580582
final diffOutput = _diffWithFileUris(
581583
previousTempUri, currentTempUri,
582584
label: test.name);
@@ -623,16 +625,18 @@ abstract class HotReloadSuiteRunner {
623625
(currentCode, currentDiff) = _splitTestByDiff(currentEdit.fileUri);
624626
final previousTempUri = generatedCodeDir.uri.resolve('__previous');
625627
final currentTempUri = generatedCodeDir.uri.resolve('__current');
626-
File.fromUri(previousTempUri).writeAsStringSync(previousCode);
627-
File.fromUri(currentTempUri).writeAsStringSync(currentCode);
628+
// Avoid 'No newline at end of file' messages in the output by
629+
// appending a newline to the trimmed source code strings.
630+
File.fromUri(previousTempUri).writeAsStringSync('$previousCode\n');
631+
File.fromUri(currentTempUri).writeAsStringSync('$currentCode\n');
628632
final diffOutput = _diffWithFileUris(
629633
previousTempUri, currentTempUri,
630634
label: test.name);
631635
File.fromUri(previousTempUri).deleteSync();
632636
File.fromUri(currentTempUri).deleteSync();
633-
final newCurrentText = '$currentCode'
634-
'${currentCode.endsWith('\n') ? '' : '\n'}'
635-
'$diffOutput\n';
637+
// Write an empty line between the code and the diff comment to
638+
// agree with the dart formatter.
639+
final newCurrentText = '$currentCode\n\n$diffOutput\n';
636640
File.fromUri(currentEdit.fileUri).writeAsStringSync(newCurrentText);
637641
_print('Writing updated diff to $currentEdit.fileUri',
638642
label: test.name);
@@ -943,16 +947,15 @@ abstract class HotReloadSuiteRunner {
943947
return (diff1Lines.join('\n'), diff2Lines.join('\n'));
944948
}
945949

946-
/// Returns the code and diff portions of [file].
950+
/// Returns the code and diff portions of [file] with all leading and trailing
951+
/// whitespace trimmed.
947952
(String, String) _splitTestByDiff(Uri file) {
948953
final text = File.fromUri(file).readAsStringSync();
949954
final diffIndex = text.indexOf(testDiffSeparator);
950955
final diffSplitIndex = diffIndex == -1 ? text.length - 1 : diffIndex;
951-
final codeText = text.substring(0, diffSplitIndex);
952-
final diffText = text.substring(diffSplitIndex, text.length - 1);
953-
// Avoid 'No newline at end of file' messages in the output by appending a
954-
// newline if one is not already trailing.
955-
return ('$codeText${codeText.endsWith('\n') ? '' : '\n'}', diffText);
956+
final codeText = text.substring(0, diffSplitIndex).trim();
957+
final diffText = text.substring(diffSplitIndex, text.length - 1).trim();
958+
return (codeText, diffText);
956959
}
957960
}
958961

tests/hot_reload/add_library_imports/main.1.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Future<void> main() async {
1818
await hotReload();
1919
validate();
2020
}
21+
2122
/** DIFF **/
2223
/*
2324
@@ -2,12 +2,15 @@

tests/hot_reload/add_new_static_field/main.1.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Future<void> main() async {
2525
Expect.equals('42', helper());
2626
Expect.equals(1, hotReloadGeneration);
2727
}
28+
2829
/** DIFF **/
2930
/*
3031
@@ -8,10 +8,12 @@

tests/hot_reload/bad_class/main.1.reject.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Future<void> main() async {
2121
foo = Foo(10);
2222
Expect.equals(10, foo.a);
2323
}
24+
2425
/** DIFF **/
2526
/*
2627
@@ -9,7 +9,7 @@

tests/hot_reload/call_deleted_top_level_function/main.1.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Future<void> main() async {
2828
Expect.contains('deleted', helper());
2929
Expect.equals(1, hotReloadGeneration);
3030
}
31+
3132
/** DIFF **/
3233
/*
3334
@@ -8,15 +8,14 @@

tests/hot_reload/call_deleted_top_level_function_arity_change/main.1.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Future<void> main() async {
3232
Expect.contains('deleted', helper());
3333
Expect.equals(1, hotReloadGeneration);
3434
}
35+
3536
/** DIFF **/
3637
/*
3738
@@ -8,15 +8,18 @@

tests/hot_reload/class_added/main.1.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Future<void> main() async {
2222

2323
Expect.equals('hello from A', helper());
2424
}
25+
2526
/** DIFF **/
2627
/*
2728
@@ -9,7 +9,11 @@

tests/hot_reload/class_field_added/main.1.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Future<void> main() async {
2727
Expect.isNotNull(Foo());
2828
Expect.equals(2, hotReloadGeneration);
2929
}
30+
3031
/** DIFF **/
3132
/*
3233
@@ -10,6 +10,7 @@

tests/hot_reload/class_field_added/main.2.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Future<void> main() async {
2828
Expect.isNotNull(Foo());
2929
Expect.equals(2, hotReloadGeneration);
3030
}
31+
3132
/** DIFF **/
3233
/*
3334
@@ -11,6 +11,7 @@

tests/hot_reload/class_field_removed/main.1.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Future<void> main() async {
2727
Expect.isNotNull(Foo());
2828
Expect.equals(2, hotReloadGeneration);
2929
}
30+
3031
/** DIFF **/
3132
/*
3233
@@ -11,7 +11,6 @@

0 commit comments

Comments
 (0)