Skip to content

Commit

Permalink
[tests] Fix formatting when writing diffs
Browse files Browse the repository at this point in the history
* 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 <markzipan@google.com>
Commit-Queue: Nicholas Shahan <nshahan@google.com>
  • Loading branch information
nshahan authored and Commit Queue committed Jan 24, 2025
1 parent a0db69f commit 4fce27b
Show file tree
Hide file tree
Showing 130 changed files with 254 additions and 188 deletions.
29 changes: 16 additions & 13 deletions pkg/dev_compiler/test/hot_reload_suite.dart
Original file line number Diff line number Diff line change
Expand Up @@ -575,8 +575,10 @@ abstract class HotReloadSuiteRunner {
}
final previousTempUri = generatedCodeDir.uri.resolve('__previous');
final currentTempUri = generatedCodeDir.uri.resolve('__current');
File.fromUri(previousTempUri).writeAsStringSync(previousCode);
File.fromUri(currentTempUri).writeAsStringSync(currentCode);
// Avoid 'No newline at end of file' messages in the output by
// appending a newline to the trimmed source code strings.
File.fromUri(previousTempUri).writeAsStringSync('$previousCode\n');
File.fromUri(currentTempUri).writeAsStringSync('$currentCode\n');
final diffOutput = _diffWithFileUris(
previousTempUri, currentTempUri,
label: test.name);
Expand Down Expand Up @@ -623,16 +625,18 @@ abstract class HotReloadSuiteRunner {
(currentCode, currentDiff) = _splitTestByDiff(currentEdit.fileUri);
final previousTempUri = generatedCodeDir.uri.resolve('__previous');
final currentTempUri = generatedCodeDir.uri.resolve('__current');
File.fromUri(previousTempUri).writeAsStringSync(previousCode);
File.fromUri(currentTempUri).writeAsStringSync(currentCode);
// Avoid 'No newline at end of file' messages in the output by
// appending a newline to the trimmed source code strings.
File.fromUri(previousTempUri).writeAsStringSync('$previousCode\n');
File.fromUri(currentTempUri).writeAsStringSync('$currentCode\n');
final diffOutput = _diffWithFileUris(
previousTempUri, currentTempUri,
label: test.name);
File.fromUri(previousTempUri).deleteSync();
File.fromUri(currentTempUri).deleteSync();
final newCurrentText = '$currentCode'
'${currentCode.endsWith('\n') ? '' : '\n'}'
'$diffOutput\n';
// Write an empty line between the code and the diff comment to
// agree with the dart formatter.
final newCurrentText = '$currentCode\n\n$diffOutput\n';
File.fromUri(currentEdit.fileUri).writeAsStringSync(newCurrentText);
_print('Writing updated diff to $currentEdit.fileUri',
label: test.name);
Expand Down Expand Up @@ -943,16 +947,15 @@ abstract class HotReloadSuiteRunner {
return (diff1Lines.join('\n'), diff2Lines.join('\n'));
}

/// Returns the code and diff portions of [file].
/// Returns the code and diff portions of [file] with all leading and trailing
/// whitespace trimmed.
(String, String) _splitTestByDiff(Uri file) {
final text = File.fromUri(file).readAsStringSync();
final diffIndex = text.indexOf(testDiffSeparator);
final diffSplitIndex = diffIndex == -1 ? text.length - 1 : diffIndex;
final codeText = text.substring(0, diffSplitIndex);
final diffText = text.substring(diffSplitIndex, text.length - 1);
// Avoid 'No newline at end of file' messages in the output by appending a
// newline if one is not already trailing.
return ('$codeText${codeText.endsWith('\n') ? '' : '\n'}', diffText);
final codeText = text.substring(0, diffSplitIndex).trim();
final diffText = text.substring(diffSplitIndex, text.length - 1).trim();
return (codeText, diffText);
}
}

Expand Down
1 change: 1 addition & 0 deletions tests/hot_reload/add_library_imports/main.1.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Future<void> main() async {
await hotReload();
validate();
}

/** DIFF **/
/*
@@ -2,12 +2,15 @@
Expand Down
1 change: 1 addition & 0 deletions tests/hot_reload/add_new_static_field/main.1.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Future<void> main() async {
Expect.equals('42', helper());
Expect.equals(1, hotReloadGeneration);
}

/** DIFF **/
/*
@@ -8,10 +8,12 @@
Expand Down
1 change: 1 addition & 0 deletions tests/hot_reload/bad_class/main.1.reject.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Future<void> main() async {
foo = Foo(10);
Expect.equals(10, foo.a);
}

/** DIFF **/
/*
@@ -9,7 +9,7 @@
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Future<void> main() async {
Expect.contains('deleted', helper());
Expect.equals(1, hotReloadGeneration);
}

/** DIFF **/
/*
@@ -8,15 +8,14 @@
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Future<void> main() async {
Expect.contains('deleted', helper());
Expect.equals(1, hotReloadGeneration);
}

/** DIFF **/
/*
@@ -8,15 +8,18 @@
Expand Down
1 change: 1 addition & 0 deletions tests/hot_reload/class_added/main.1.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Future<void> main() async {

Expect.equals('hello from A', helper());
}

/** DIFF **/
/*
@@ -9,7 +9,11 @@
Expand Down
1 change: 1 addition & 0 deletions tests/hot_reload/class_field_added/main.1.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Future<void> main() async {
Expect.isNotNull(Foo());
Expect.equals(2, hotReloadGeneration);
}

/** DIFF **/
/*
@@ -10,6 +10,7 @@
Expand Down
1 change: 1 addition & 0 deletions tests/hot_reload/class_field_added/main.2.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Future<void> main() async {
Expect.isNotNull(Foo());
Expect.equals(2, hotReloadGeneration);
}

/** DIFF **/
/*
@@ -11,6 +11,7 @@
Expand Down
1 change: 1 addition & 0 deletions tests/hot_reload/class_field_removed/main.1.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Future<void> main() async {
Expect.isNotNull(Foo());
Expect.equals(2, hotReloadGeneration);
}

/** DIFF **/
/*
@@ -11,7 +11,6 @@
Expand Down
1 change: 1 addition & 0 deletions tests/hot_reload/class_field_removed/main.2.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Future<void> main() async {
Expect.isNotNull(Foo());
Expect.equals(2, hotReloadGeneration);
}

/** DIFF **/
/*
@@ -10,7 +10,6 @@
Expand Down
2 changes: 2 additions & 0 deletions tests/hot_reload/class_removed/main.0.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import 'package:reload_test/reload_test_utils.dart';
class A {
toString() => 'hello from A';
}

var list = <dynamic>[];

helper() {
list.add(A());
return list[0].toString();
Expand Down
6 changes: 5 additions & 1 deletion tests/hot_reload/class_removed/main.1.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:reload_test/reload_test_utils.dart';
// https://github.com/dart-lang/sdk/blob/f34a2ed99fc1b34cedbd974a5801f8d922121126/runtime/vm/isolate_reload_test.cc#L585

var list = <dynamic>[];

helper() {
return list[0].toString();
}
Expand All @@ -19,16 +20,19 @@ Future<void> main() async {

Expect.equals('hello from A', helper());
}

/** DIFF **/
/*
@@ -8,12 +8,8 @@
@@ -8,14 +8,9 @@
// Adapted from:
// https://github.com/dart-lang/sdk/blob/f34a2ed99fc1b34cedbd974a5801f8d922121126/runtime/vm/isolate_reload_test.cc#L585
-class A {
- toString() => 'hello from A';
-}
-
var list = <dynamic>[];
helper() {
- list.add(A());
return list[0].toString();
Expand Down
1 change: 1 addition & 0 deletions tests/hot_reload/compile_base_class/lib.1.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class State<U, T> {
u = l[1] is U ? l[1] : null;
}
}

/** DIFF **/
/*
@@ -2,7 +2,7 @@
Expand Down
1 change: 1 addition & 0 deletions tests/hot_reload/compile_generics/main.1.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Future<void> main() async {
balance = (MyAccountState(Account())).howAreTheThings().balance();
Expect.equals(24, balance);
}

/** DIFF **/
/*
@@ -11,7 +11,7 @@
Expand Down
1 change: 1 addition & 0 deletions tests/hot_reload/const_field_update/main.1.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Future<void> main() async {
Expect.equals('0:00:02.000000', helper());
Expect.equals(1, hotReloadGeneration);
}

/** DIFF **/
/*
@@ -8,7 +8,7 @@
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Future<void> main() async {
await hotReload(expectRejection: true);
helper();
}

/** DIFF **/
/*
@@ -8,12 +8,12 @@
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Future<void> main() async {
await hotReload(expectRejection: true);
helper();
}

/** DIFF **/
/*
@@ -8,11 +8,12 @@
Expand Down
1 change: 1 addition & 0 deletions tests/hot_reload/constant_identical/main.1.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Future<void> main() async {
Expect.identical(x, const Fruit('Pear'));
Expect.identical(x, helper());
}

/** DIFF **/
/*
*/
1 change: 1 addition & 0 deletions tests/hot_reload/constant_nonidentical/main.1.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Future<void> main() async {
Expect.identical(x, const Fruit('Pear'));
Expect.notIdentical(x, helper());
}

/** DIFF **/
/*
@@ -11,6 +11,7 @@
Expand Down
1 change: 1 addition & 0 deletions tests/hot_reload/constructor_changed/main.1.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Future<void> main() async {
Expect.equals(20, savedA.field);
Expect.equals(10, helper().field);
}

/** DIFF **/
/*
@@ -11,7 +11,7 @@
Expand Down
1 change: 1 addition & 0 deletions tests/hot_reload/delete_static_field/main.1.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Future<void> main() async {

Expect.throws(closure);
}

/** DIFF **/
/*
@@ -16,15 +16,11 @@
Expand Down
5 changes: 0 additions & 5 deletions tests/hot_reload/enum_addition/main.1.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,4 @@ Future<void> main() async {
Future<void> main() async {
Expect.equals(0, Fruit.Apple.index);
@@ -25,3 +28,4 @@
Expect.equals('Fruit.Banana', Fruit.Banana.toString());
helper();
}
+
*/
5 changes: 0 additions & 5 deletions tests/hot_reload/enum_delete/main.1.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,4 @@ Future<void> main() async {
Expect.equals('Fruit.Cantaloupe', x.toString());
Expect.type<int>(x.hashCode);
Expect.equals(2, x.index);
@@ -23,3 +22,4 @@
Expect.type<int>(x.hashCode);
Expect.equals(-1, x.index);
}
+
*/
5 changes: 0 additions & 5 deletions tests/hot_reload/enum_equality/main.1.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,4 @@ Future<void> main() async {

/** DIFF **/
/*
@@ -17,3 +17,4 @@
await hotReload();
Expect.equals(Fruit.Banana, x);
}
+
*/
5 changes: 0 additions & 5 deletions tests/hot_reload/enum_identical/main.1.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,4 @@ Future<void> main() async {

/** DIFF **/
/*
@@ -17,3 +17,4 @@
await hotReload();
Expect.identical(Fruit.Banana, x);
}
+
*/
5 changes: 0 additions & 5 deletions tests/hot_reload/enum_identity_reload/main.1.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,4 @@ Future<void> main() async {

/** DIFF **/
/*
@@ -33,3 +33,4 @@
Expect.equals(z, Fruit.values[x[Fruit.Banana]]);
Expect.equals(w, Fruit.values[x[Fruit.Cantaloupe]]);
}
+
*/
5 changes: 0 additions & 5 deletions tests/hot_reload/enum_in_main_library_modified/main.1.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,4 @@ Future<void> main() async {
toString() => 'foo';
}
@@ -27,3 +28,4 @@
// Modification of an imported library propagates to the importing library.
Expect.equals('foo', helper());
}
+
*/
5 changes: 0 additions & 5 deletions tests/hot_reload/enum_referent_shape_change_add/main.1.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,4 @@ Future<void> main() async {
const Fruit(this.name, this.initial);
final String name;
@@ -37,3 +40,4 @@
await hotReload();
Expect.equals('Fruit.Apple', retained.toString());
}
+
*/
5 changes: 0 additions & 5 deletions tests/hot_reload/enum_reorder_identical/main.1.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,4 @@ Future<void> main() async {
var x;
@@ -18,3 +18,4 @@
Expect.equals(Fruit.Banana, x);
Expect.identical(Fruit.Banana, x);
}
+
*/
5 changes: 0 additions & 5 deletions tests/hot_reload/enum_retained_hash/main.1.dart
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,4 @@ Future<void> main() async {
}
enum C {
@@ -97,3 +97,4 @@
Expect.identical(c3, C.C3, 'i-c3');
Expect.equals(c3.hashCode, C.C3.hashCode, 'h-c3');
}
+
*/
12 changes: 3 additions & 9 deletions tests/hot_reload/enum_shape_change/main.1.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,20 @@ Future<void> main() async {

/** DIFF **/
/*
@@ -8,8 +8,15 @@
@@ -8,7 +8,14 @@
// Adapted from:
// https://github.com/dart-lang/sdk/blob/be2aabd91c67f7f331c49cb74e18fe5e469f04db/runtime/vm/isolate_reload_test.cc#L2308
-enum Fruit { Apple, Banana }
+enum Fruit {
+ Apple('Apple', 'A'),
+ Banana('Banana', 'B');
+
+ const Fruit(this.name, this.initial);
+ final String name;
+ final String initial;
+}
+
var retained;
Future<void> main() async {
@@ -18,3 +25,4 @@
await hotReload();
Expect.equals('A', retained.initial);
}
+
*/
5 changes: 0 additions & 5 deletions tests/hot_reload/enum_shape_change_add/main.1.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,4 @@ Future<void> main() async {
Future<void> main() async {
retained = Fruit.Apple;
@@ -20,3 +32,4 @@
await hotReload();
helper();
}
+
*/
3 changes: 1 addition & 2 deletions tests/hot_reload/enum_shape_change_remove/main.1.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Future<void> main() async {

/** DIFF **/
/*
@@ -8,12 +8,18 @@
@@ -8,12 +8,17 @@
// Adapted from:
// https://github.com/dart-lang/sdk/blob/be2aabd91c67f7f331c49cb74e18fe5e469f04db/runtime/vm/isolate_reload_test.cc#L2371
Expand All @@ -45,5 +45,4 @@ Future<void> main() async {
await hotReload();
Expect.equals('Fruit.Deleted enum value from Fruit', retained.toString());
}
+
*/
Loading

0 comments on commit 4fce27b

Please sign in to comment.