From 6357371c0aaf16fef8c3056150959e3d222ad66c Mon Sep 17 00:00:00 2001 From: MarkZ Date: Fri, 26 Apr 2024 23:05:10 +0000 Subject: [PATCH] [reload_test] Adding diffs to existing tests. Change-Id: Icf7135ae46159ca834a54849c72405eec83f9b4f Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/364384 Reviewed-by: Nicholas Shahan Commit-Queue: Mark Zhou --- pkg/dev_compiler/test/hot_reload_suite.dart | 39 +++++++++++++------ .../add_library_imports/main.1.dart | 21 ++++++++++ tests/hot_reload/bad_class/main.1.dart | 12 ++++++ .../hot_reload/compile_base_class/lib.1.dart | 12 ++++++ tests/hot_reload/compile_generics/main.1.dart | 12 ++++++ .../framework_timing_test/main.1.dart | 37 ++++++++++++++++++ .../framework_timing_test/main.2.dart | 37 ++++++++++++++++++ tests/hot_reload/general_test/b.1.dart | 6 +++ tests/hot_reload/general_test/b.3.dart | 6 +++ tests/hot_reload/general_test/b.4.dart | 6 +++ tests/hot_reload/general_test/c.4.dart | 15 +++++++ tests/hot_reload/general_test/c.5.dart | 15 +++++++ tests/hot_reload/general_test/main.1.dart | 3 ++ tests/hot_reload/general_test/main.2.dart | 12 ++++++ tests/hot_reload/general_test/main.3.dart | 12 ++++++ tests/hot_reload/general_test/main.4.dart | 3 ++ tests/hot_reload/general_test/main.5.dart | 3 ++ .../library_b.1.dart | 10 +++++ .../hot_restart_constant_equality/main.1.dart | 12 ++++++ .../lazy_static_initializers/main.1.dart | 12 ++++++ .../lazy_static_initializers/main.2.dart | 11 ++++++ .../lazy_static_initializers/main.3.dart | 20 ++++++++++ .../run_lazy_field_initializers/main.1.dart | 28 +++++++++++++ tests/hot_reload/timer_test/main.1.dart | 3 ++ tests/hot_reload/timer_test/main.2.dart | 3 ++ 25 files changed, 338 insertions(+), 12 deletions(-) diff --git a/pkg/dev_compiler/test/hot_reload_suite.dart b/pkg/dev_compiler/test/hot_reload_suite.dart index 13f3b35903f6..32a110ecd5cc 100644 --- a/pkg/dev_compiler/test/hot_reload_suite.dart +++ b/pkg/dev_compiler/test/hot_reload_suite.dart @@ -268,7 +268,14 @@ Future main(List args) async { throw Exception('Too many generations specified in test ' '(requested: $maxGenerations, max: $globalMaxGenerations).'); } - switch (argResults['diff']) { + + var diffMode = argResults['diff']!; + if (fe_shared.isWindows && diffMode != 'ignore') { + _print("Diffing isn't supported on Windows. Defaulting to 'ignore'.", + label: testName); + diffMode = 'ignore'; + } + switch (diffMode) { case 'check': _print('Checking source file diffs.', label: testName); filesByGeneration.forEach((basename, filesQueue) { @@ -701,26 +708,32 @@ void _debugPrint(String message, {String? label}) { /// /// If [commented] is set, the output will be wrapped in multiline comments /// and the diff separator. +/// +/// If [trimHeaders] is set, the leading '+++' and '---' file headers will be +/// removed. String _diffWithFileUris(Uri file1, Uri file2, - {String label = '', bool commented = true}) { + {String label = '', bool commented = true, bool trimHeaders = true}) { final file1Path = file1.toFilePath(); final file2Path = file2.toFilePath(); - _debugPrint( - "Running diff with 'diff -dy --suppress-common-lines" - " --expand-tabs $file1Path $file2Path'.", - label: label); - final diffProcess = Process.runSync('diff', [ - '-dy', - '--suppress-common-lines', + final diffArgs = [ + '-du', + '--width=120', '--expand-tabs', file1Path, file2Path - ]); + ]; + _debugPrint("Running diff with 'diff ${diffArgs.join(' ')}'.", label: label); + final diffProcess = Process.runSync('diff', diffArgs); final errOutput = diffProcess.stderr as String; if (errOutput.isNotEmpty) { throw Exception('diff failed with:\n$errOutput'); } - final output = diffProcess.stdout as String; + var output = diffProcess.stdout as String; + if (trimHeaders) { + // Skip the first two lines. + // TODO(markzipan): Add support for Windows-style line endings. + output = output.split('\n').skip(2).join('\n'); + } return commented ? '$testDiffSeparator\n/*\n$output*/' : output; } @@ -731,7 +744,9 @@ String _diffWithFileUris(Uri file1, Uri file2, final diffSplitIndex = diffIndex == -1 ? text.length - 1 : diffIndex; final codeText = text.substring(0, diffSplitIndex); final diffText = text.substring(diffSplitIndex, text.length - 1); - return (codeText, diffText); + // 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); } abstract class HotReloadSuiteRunner { diff --git a/tests/hot_reload/add_library_imports/main.1.dart b/tests/hot_reload/add_library_imports/main.1.dart index 34a12852337c..b00f6205bfa7 100644 --- a/tests/hot_reload/add_library_imports/main.1.dart +++ b/tests/hot_reload/add_library_imports/main.1.dart @@ -18,3 +18,24 @@ Future main() async { await hotReload(); validate(); } +/** DIFF **/ +/* +@@ -2,12 +2,15 @@ + import 'package:reload_test/reload_test_utils.dart'; + + import 'dart:math'; ++import 'dart:convert'; + + void validate() { +- // Initial program is valid. Symbols in 'dart:math' are visible. +- Expect.equals(0, hotReloadGeneration); ++ // Symbols in 'dart:convert' are visible after hot reload. ++ Expect.equals(1, hotReloadGeneration); + Expect.equals(e, 2.718281828459045); + Expect.type(e); ++ Expect.type(utf8); ++ Expect.type(jsonEncode); + } + + Future main() async { +*/ diff --git a/tests/hot_reload/bad_class/main.1.dart b/tests/hot_reload/bad_class/main.1.dart index 653efa7d848d..30c127540c3f 100644 --- a/tests/hot_reload/bad_class/main.1.dart +++ b/tests/hot_reload/bad_class/main.1.dart @@ -19,3 +19,15 @@ Future main() async { await hotReload(); throw Exception('This should never run.'); } +/** DIFF **/ +/* +@@ -9,7 +9,7 @@ + // https://github.com/dart-lang/sdk/blob/36c0788137d55c6c77f4b9a8be12e557bc764b1c/runtime/vm/isolate_reload_test.cc#L364 + + class Foo { +- final a; ++ final a kjsdf ksjdf; + Foo(this.a); + } + +*/ diff --git a/tests/hot_reload/compile_base_class/lib.1.dart b/tests/hot_reload/compile_base_class/lib.1.dart index eec8eeb900a3..671c067c6d25 100644 --- a/tests/hot_reload/compile_base_class/lib.1.dart +++ b/tests/hot_reload/compile_base_class/lib.1.dart @@ -10,3 +10,15 @@ class State { u = l[1] is U ? l[1] : null; } } +/** DIFF **/ +/* +@@ -2,7 +2,7 @@ + // for details. All rights reserved. Use of this source code is governed by a + // BSD-style license that can be found in the LICENSE file. + +-class State { ++class State { + T? t; + U? u; + State(List l) { +*/ diff --git a/tests/hot_reload/compile_generics/main.1.dart b/tests/hot_reload/compile_generics/main.1.dart index 706f9d54deec..88c545da4076 100644 --- a/tests/hot_reload/compile_generics/main.1.dart +++ b/tests/hot_reload/compile_generics/main.1.dart @@ -26,3 +26,15 @@ Future main() async { balance = (MyAccountState(Account())).howAreTheThings().balance(); Expect.equals(24, balance); } +/** DIFF **/ +/* +@@ -11,7 +11,7 @@ + // https://github.com/dart-lang/sdk/blob/36c0788137d55c6c77f4b9a8be12e557bc764b1c/runtime/vm/isolate_reload_test.cc#L204 + + class Account { +- int balance() => 42; ++ int balance() => 24; + } + + class MyAccountState extends State { +*/ diff --git a/tests/hot_reload/framework_timing_test/main.1.dart b/tests/hot_reload/framework_timing_test/main.1.dart index 52bbbe46c782..d3be3e190cd4 100644 --- a/tests/hot_reload/framework_timing_test/main.1.dart +++ b/tests/hot_reload/framework_timing_test/main.1.dart @@ -31,3 +31,40 @@ void main() { hotRestart(); } +/** DIFF **/ +/* +@@ -7,25 +7,25 @@ + import 'package:expect/expect.dart'; + import 'package:reload_test/reload_test_utils.dart'; + +-var x = 'Hello World'; ++var x = 'Hello Foo'; + + void main() { +- Expect.equals('Hello World', x); +- Expect.equals(0, hotRestartGeneration); ++ Expect.equals('Hello Foo', x); ++ Expect.equals(1, hotRestartGeneration); + + scheduleMicrotask(() { +- Expect.equals(0, hotRestartGeneration); ++ Expect.equals(1, hotRestartGeneration); + }); + Future.microtask(() { + throw x; + }).catchError((e, stackTrace) { +- Expect.equals("Hello World", e); +- Expect.equals(0, hotRestartGeneration); ++ Expect.equals("Hello Foo", e); ++ Expect.equals(1, hotRestartGeneration); + }).then((_) { +- Expect.equals(0, hotRestartGeneration); ++ Expect.equals(1, hotRestartGeneration); + }); + Future.delayed(Duration(seconds: 5), () { +- throw Exception('Future from main.0.dart before hot restart. ' ++ throw Exception('Future from main.1.dart before hot restart. ' + 'This should never run.'); + }); + +*/ diff --git a/tests/hot_reload/framework_timing_test/main.2.dart b/tests/hot_reload/framework_timing_test/main.2.dart index 37af36ec11d5..7497d01ccfee 100644 --- a/tests/hot_reload/framework_timing_test/main.2.dart +++ b/tests/hot_reload/framework_timing_test/main.2.dart @@ -31,3 +31,40 @@ void main() { hotRestart(); } +/** DIFF **/ +/* +@@ -7,25 +7,25 @@ + import 'package:expect/expect.dart'; + import 'package:reload_test/reload_test_utils.dart'; + +-var x = 'Hello Foo'; ++var x = 'Hello Bar'; + + void main() { +- Expect.equals('Hello Foo', x); +- Expect.equals(1, hotRestartGeneration); ++ Expect.equals('Hello Bar', x); ++ Expect.equals(2, hotRestartGeneration); + + scheduleMicrotask(() { +- Expect.equals(1, hotRestartGeneration); ++ Expect.equals(2, hotRestartGeneration); + }); + Future.microtask(() { + throw x; + }).catchError((e, stackTrace) { +- Expect.equals("Hello Foo", e); +- Expect.equals(1, hotRestartGeneration); ++ Expect.equals("Hello Bar", e); ++ Expect.equals(2, hotRestartGeneration); + }).then((_) { +- Expect.equals(1, hotRestartGeneration); ++ Expect.equals(2, hotRestartGeneration); + }); + Future.delayed(Duration(seconds: 5), () { +- throw Exception('Future from main.1.dart before hot restart. ' ++ throw Exception('Future from main.2.dart before hot restart. ' + 'This should never run.'); + }); + +*/ diff --git a/tests/hot_reload/general_test/b.1.dart b/tests/hot_reload/general_test/b.1.dart index bbfaf01482d3..2b963a81e9ae 100644 --- a/tests/hot_reload/general_test/b.1.dart +++ b/tests/hot_reload/general_test/b.1.dart @@ -1 +1,7 @@ get line => "part3"; +/** DIFF **/ +/* +@@ -1 +1 @@ +-get line => "part1"; ++get line => "part3"; +*/ diff --git a/tests/hot_reload/general_test/b.3.dart b/tests/hot_reload/general_test/b.3.dart index b50f4ac21b23..b72be33c34a6 100644 --- a/tests/hot_reload/general_test/b.3.dart +++ b/tests/hot_reload/general_test/b.3.dart @@ -1 +1,7 @@ get line => "part5"; +/** DIFF **/ +/* +@@ -1 +1 @@ +-get line => "part3"; ++get line => "part5"; +*/ diff --git a/tests/hot_reload/general_test/b.4.dart b/tests/hot_reload/general_test/b.4.dart index 62af173940da..9f591c9e3d45 100644 --- a/tests/hot_reload/general_test/b.4.dart +++ b/tests/hot_reload/general_test/b.4.dart @@ -1 +1,7 @@ get line => "part4"; +/** DIFF **/ +/* +@@ -1 +1 @@ +-get line => "part5"; ++get line => "part4"; +*/ diff --git a/tests/hot_reload/general_test/c.4.dart b/tests/hot_reload/general_test/c.4.dart index b1c871a5b733..847992175601 100644 --- a/tests/hot_reload/general_test/c.4.dart +++ b/tests/hot_reload/general_test/c.4.dart @@ -8,3 +8,18 @@ class B { } var bField = B(a: (String s) => "$s"); +/** DIFF **/ +/* +@@ -1 +1,10 @@ +-String g() => ""; ++String g() { ++ return bField.a("a"); ++} ++ ++class B { ++ dynamic a; ++ B({this.a}); ++} ++ ++var bField = B(a: (String s) => "$s"); +*/ diff --git a/tests/hot_reload/general_test/c.5.dart b/tests/hot_reload/general_test/c.5.dart index f0ad10fda4a1..e6ac65701e8c 100644 --- a/tests/hot_reload/general_test/c.5.dart +++ b/tests/hot_reload/general_test/c.5.dart @@ -9,3 +9,18 @@ class B { } var bField = B(a: (String s) => "$s"); +/** DIFF **/ +/* +@@ -1,9 +1,10 @@ + String g() { +- return bField.a("a"); ++ return bField.a("a") + (bField.b ?? "c"); + } + + class B { + dynamic a; ++ dynamic b; + B({this.a}); + } + +*/ diff --git a/tests/hot_reload/general_test/main.1.dart b/tests/hot_reload/general_test/main.1.dart index 2ac4c0a5f420..3543f7a814ba 100644 --- a/tests/hot_reload/general_test/main.1.dart +++ b/tests/hot_reload/general_test/main.1.dart @@ -53,3 +53,6 @@ Future main() async { Expect.equals('ac', topLevel); Expect.equals(5, hotReloadGeneration); } +/** DIFF **/ +/* +*/ diff --git a/tests/hot_reload/general_test/main.2.dart b/tests/hot_reload/general_test/main.2.dart index c5f4b167f998..5f51e6fe5571 100644 --- a/tests/hot_reload/general_test/main.2.dart +++ b/tests/hot_reload/general_test/main.2.dart @@ -53,3 +53,15 @@ Future main() async { Expect.equals('ac', topLevel); Expect.equals(5, hotReloadGeneration); } +/** DIFF **/ +/* +@@ -11,7 +11,7 @@ + import 'b.dart'; + import 'c.dart'; + +-f() => "$line part2"; ++f() => "$line part4"; + + Future main() async { + // Initial program is valid. +*/ diff --git a/tests/hot_reload/general_test/main.3.dart b/tests/hot_reload/general_test/main.3.dart index 2d6530703a66..05f38596568c 100644 --- a/tests/hot_reload/general_test/main.3.dart +++ b/tests/hot_reload/general_test/main.3.dart @@ -53,3 +53,15 @@ Future main() async { Expect.equals('ac', topLevel); Expect.equals(5, hotReloadGeneration); } +/** DIFF **/ +/* +@@ -11,7 +11,7 @@ + import 'b.dart'; + import 'c.dart'; + +-f() => "$line part4"; ++f() => "$line part6"; + + Future main() async { + // Initial program is valid. +*/ diff --git a/tests/hot_reload/general_test/main.4.dart b/tests/hot_reload/general_test/main.4.dart index 2d6530703a66..67e0c7ea7efd 100644 --- a/tests/hot_reload/general_test/main.4.dart +++ b/tests/hot_reload/general_test/main.4.dart @@ -53,3 +53,6 @@ Future main() async { Expect.equals('ac', topLevel); Expect.equals(5, hotReloadGeneration); } +/** DIFF **/ +/* +*/ diff --git a/tests/hot_reload/general_test/main.5.dart b/tests/hot_reload/general_test/main.5.dart index 2d6530703a66..67e0c7ea7efd 100644 --- a/tests/hot_reload/general_test/main.5.dart +++ b/tests/hot_reload/general_test/main.5.dart @@ -53,3 +53,6 @@ Future main() async { Expect.equals('ac', topLevel); Expect.equals(5, hotReloadGeneration); } +/** DIFF **/ +/* +*/ diff --git a/tests/hot_reload/hot_restart_constant_equality/library_b.1.dart b/tests/hot_reload/hot_restart_constant_equality/library_b.1.dart index b9a5a9c2e3ae..6007b9a8451d 100644 --- a/tests/hot_reload/hot_restart_constant_equality/library_b.1.dart +++ b/tests/hot_reload/hot_restart_constant_equality/library_b.1.dart @@ -6,3 +6,13 @@ import 'library_a.dart'; int variableToModifyToForceRecompile = 45; B get value2 => const B(2); +/** DIFF **/ +/* +@@ -4,5 +4,5 @@ + + import 'library_a.dart'; + +-int variableToModifyToForceRecompile = 23; ++int variableToModifyToForceRecompile = 45; + B get value2 => const B(2); +*/ diff --git a/tests/hot_reload/hot_restart_constant_equality/main.1.dart b/tests/hot_reload/hot_restart_constant_equality/main.1.dart index 157514038864..b22ed53d934a 100644 --- a/tests/hot_reload/hot_restart_constant_equality/main.1.dart +++ b/tests/hot_reload/hot_restart_constant_equality/main.1.dart @@ -31,3 +31,15 @@ void main() { Expect.equals('ConstObject(reloadVariable: 45, ConstantEqualitySuccess)', '${const ConstObject().text}'); } +/** DIFF **/ +/* +@@ -28,7 +28,6 @@ + } + + void main() { +- Expect.equals('ConstObject(reloadVariable: 23, ConstantEqualitySuccess)', ++ Expect.equals('ConstObject(reloadVariable: 45, ConstantEqualitySuccess)', + '${const ConstObject().text}'); +- hotRestart(); + } +*/ diff --git a/tests/hot_reload/lazy_static_initializers/main.1.dart b/tests/hot_reload/lazy_static_initializers/main.1.dart index ba2aa97e7f8d..bcc5b2b6ba59 100644 --- a/tests/hot_reload/lazy_static_initializers/main.1.dart +++ b/tests/hot_reload/lazy_static_initializers/main.1.dart @@ -25,3 +25,15 @@ Future main() async { Expect.equals(3, hotReloadGeneration); Expect.equals("before", value); } +/** DIFF **/ +/* +@@ -5,7 +5,7 @@ + import 'package:expect/expect.dart'; + import 'package:reload_test/reload_test_utils.dart'; + +-var value = "unused"; ++var value = "before"; + + Future main() async { + // Declare an unreferenced lazy static field. +*/ diff --git a/tests/hot_reload/lazy_static_initializers/main.2.dart b/tests/hot_reload/lazy_static_initializers/main.2.dart index 574c31f5da95..31397ca924f2 100644 --- a/tests/hot_reload/lazy_static_initializers/main.2.dart +++ b/tests/hot_reload/lazy_static_initializers/main.2.dart @@ -26,3 +26,14 @@ Future main() async { Expect.equals(3, hotReloadGeneration); Expect.equals("before", value); } +/** DIFF **/ +/* +@@ -17,6 +17,7 @@ + await hotReload(); + + // The lazy static is now read and contains the updated value. ++ print(value); + Expect.equals(2, hotReloadGeneration); + Expect.equals("before", value); + await hotReload(); +*/ diff --git a/tests/hot_reload/lazy_static_initializers/main.3.dart b/tests/hot_reload/lazy_static_initializers/main.3.dart index 0fbbf774b3ef..9d897ffe019c 100644 --- a/tests/hot_reload/lazy_static_initializers/main.3.dart +++ b/tests/hot_reload/lazy_static_initializers/main.3.dart @@ -25,3 +25,23 @@ Future main() async { Expect.equals(3, hotReloadGeneration); Expect.equals("before", value); } +/** DIFF **/ +/* +@@ -5,7 +5,7 @@ + import 'package:expect/expect.dart'; + import 'package:reload_test/reload_test_utils.dart'; + +-var value = "before"; ++var value = "after"; + + Future main() async { + // Declare an unreferenced lazy static field. +@@ -17,7 +17,6 @@ + await hotReload(); + + // The lazy static is now read and contains the updated value. +- print(value); + Expect.equals(2, hotReloadGeneration); + Expect.equals("before", value); + await hotReload(); +*/ diff --git a/tests/hot_reload/run_lazy_field_initializers/main.1.dart b/tests/hot_reload/run_lazy_field_initializers/main.1.dart index ea9fc52db55e..34ddf97fb9f1 100644 --- a/tests/hot_reload/run_lazy_field_initializers/main.1.dart +++ b/tests/hot_reload/run_lazy_field_initializers/main.1.dart @@ -34,3 +34,31 @@ Future main() async { await hotReload(); validate(); } +/** DIFF **/ +/* +@@ -12,18 +12,21 @@ + + class Foo { + int x = 4; ++ int y = myInitialValue++; + } + + late Foo value; + late Foo value1; + + void validate() { +- Expect.equals(0, hotReloadGeneration); +- value = Foo(); +- value1 = Foo(); ++ // Add field 'y' with side effects and verify that initializers ran lazily. ++ Expect.equals(1, hotReloadGeneration); + Expect.equals(4, value.x); + Expect.equals(4, value1.x); + Expect.equals(56, myInitialValue); ++ Expect.equals(56, value.y); ++ Expect.equals(57, value1.y); ++ Expect.equals(58, myInitialValue); + } + + Future main() async { +*/ diff --git a/tests/hot_reload/timer_test/main.1.dart b/tests/hot_reload/timer_test/main.1.dart index 2a1b897e6452..5f8f81270b64 100644 --- a/tests/hot_reload/timer_test/main.1.dart +++ b/tests/hot_reload/timer_test/main.1.dart @@ -45,3 +45,6 @@ Future main() async { await periodicTimerDone.future; } +/** DIFF **/ +/* +*/ diff --git a/tests/hot_reload/timer_test/main.2.dart b/tests/hot_reload/timer_test/main.2.dart index 2a1b897e6452..5f8f81270b64 100644 --- a/tests/hot_reload/timer_test/main.2.dart +++ b/tests/hot_reload/timer_test/main.2.dart @@ -45,3 +45,6 @@ Future main() async { await periodicTimerDone.future; } +/** DIFF **/ +/* +*/