Skip to content

Commit fe39123

Browse files
authored
Re-enable reload_tests (#1877)
1 parent e71c0e3 commit fe39123

File tree

3 files changed

+95
-62
lines changed

3 files changed

+95
-62
lines changed

dwds/test/devtools_test.dart

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,11 @@ void main() {
102102
// https://github.com/dart-lang/webdev/pull/901#issuecomment-586438132
103103
final client = context.debugConnection.vmService;
104104
await client.streamListen('Isolate');
105-
await context.changeInput();
105+
context.makeEditToDartEntryFile(
106+
toReplace: 'Hello World!',
107+
replaceWith: 'Bonjour le monde!',
108+
);
109+
await context.waitForSuccessfulBuild(propogateToBrowser: true);
106110

107111
final eventsDone = expectLater(
108112
client.onIsolateEvent,
@@ -115,6 +119,11 @@ void main() {
115119
await context.webDriver.driver.refresh();
116120

117121
await eventsDone;
122+
// Re-set the edited file:
123+
context.makeEditToDartEntryFile(
124+
toReplace: 'Bonjour le monde!',
125+
replaceWith: 'Hello World!',
126+
);
118127
});
119128
});
120129

dwds/test/fixtures/context.dart

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -77,19 +77,14 @@ class TestContext {
7777
return webCompatiblePath([...pathParts, htmlEntryFileName]);
7878
}
7979

80-
/// The entry file is the Dart entry file, e.g,
80+
/// The path to the Dart entry file, e.g,
8181
/// "/workstation/webdev/fixtures/_testSound/example/hello_world/main.dart":
82-
File get _entryFile => File(
83-
absolutePath(
84-
pathFromFixtures: p.joinAll(
85-
[packageName, webAssetsPath, dartEntryFileName],
86-
),
82+
String get _dartEntryFilePath => absolutePath(
83+
pathFromFixtures: p.joinAll(
84+
[packageName, webAssetsPath, dartEntryFileName],
8785
),
8886
);
8987

90-
/// The contents of the Dart entry file:
91-
String get _entryContents => _entryFile.readAsStringSync();
92-
9388
/// The URI for the package_config.json is located in:
9489
/// <project directory>/.dart_tool/package_config
9590
Uri get _packageConfigFile =>
@@ -183,7 +178,7 @@ class TestContext {
183178
_logger.info('Serving: $directoryToServe/$filePathToServe');
184179
_logger.info('Project: $workingDirectory');
185180
_logger.info('Packages: $_packageConfigFile');
186-
_logger.info('Entry: ${_entryFile.path}');
181+
_logger.info('Entry: $_dartEntryFilePath');
187182
}
188183

189184
Future<void> setUp({
@@ -291,10 +286,7 @@ class TestContext {
291286
DefaultBuildTarget((b) => b..target = directoryToServe));
292287
daemonClient.startBuild();
293288

294-
await daemonClient.buildResults
295-
.firstWhere((results) => results.results
296-
.any((result) => result.status == BuildStatus.succeeded))
297-
.timeout(const Duration(seconds: 60));
289+
await waitForSuccessfulBuild();
298290

299291
final assetServerPort = daemonPort(workingDirectory);
300292
_assetHandler = proxyHandler(
@@ -467,7 +459,6 @@ class TestContext {
467459
await _webDriver?.quit(closeSession: true);
468460
_chromeDriver?.kill();
469461
DartUri.currentDirectory = p.current;
470-
_entryFile.writeAsStringSync(_entryContents);
471462
await _daemonClient?.close();
472463
await ddcService?.stop();
473464
await _webRunner?.stop();
@@ -487,20 +478,31 @@ class TestContext {
487478
_outputDir = null;
488479
}
489480

490-
Future<void> changeInput() async {
491-
_entryFile.writeAsStringSync(
492-
_entryContents.replaceAll('Hello World!', 'Gary is awesome!'));
493-
494-
// Wait for the build.
495-
await _daemonClient?.buildResults.firstWhere((results) => results.results
496-
.any((result) => result.status == BuildStatus.succeeded));
481+
void makeEditToDartEntryFile({
482+
required String toReplace,
483+
required String replaceWith,
484+
}) async {
485+
final file = File(_dartEntryFilePath);
486+
final fileContents = file.readAsStringSync();
487+
file.writeAsStringSync(fileContents.replaceAll(toReplace, replaceWith));
488+
}
497489

498-
// Allow change to propagate to the browser.
499-
// Windows, or at least Travis on Windows, seems to need more time.
500-
final delay = Platform.isWindows
501-
? const Duration(seconds: 5)
502-
: const Duration(seconds: 2);
503-
await Future.delayed(delay);
490+
Future<void> waitForSuccessfulBuild(
491+
{Duration? timeout, bool propogateToBrowser = false}) async {
492+
// Wait for the build until the timeout is reached:
493+
await daemonClient.buildResults
494+
.firstWhere((results) => results.results
495+
.any((result) => result.status == BuildStatus.succeeded))
496+
.timeout(timeout ?? const Duration(seconds: 60));
497+
498+
if (propogateToBrowser) {
499+
// Allow change to propagate to the browser.
500+
// Windows, or at least Travis on Windows, seems to need more time.
501+
final delay = Platform.isWindows
502+
? const Duration(seconds: 5)
503+
: const Duration(seconds: 2);
504+
await Future.delayed(delay);
505+
}
504506
}
505507

506508
Future<void> _buildDebugExtension() async {

dwds/test/reload_test.dart

Lines changed: 55 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
@Skip('https://github.com/dart-lang/webdev/issues/1845 - Move to cron job.')
65
@TestOn('vm')
76
@Timeout(Duration(minutes: 5))
87
import 'package:dwds/src/loaders/strategy.dart';
@@ -19,6 +18,9 @@ final context = TestContext.withSoundNullSafety(
1918
htmlEntryFileName: 'index.html',
2019
);
2120

21+
const originalString = 'Hello World!';
22+
const newString = 'Bonjour le monde!';
23+
2224
void main() {
2325
// set to true for debug logging.
2426
final debug = false;
@@ -32,17 +34,17 @@ void main() {
3234
});
3335

3436
tearDown(() async {
37+
undoEdit();
3538
await context.tearDown();
3639
});
3740

3841
test('can live reload changes ', () async {
39-
await context.changeInput();
40-
42+
await makeEditAndWaitForRebuild();
4143
final source = await context.webDriver.pageSource;
4244

4345
// A full reload should clear the state.
44-
expect(source.contains('Hello World!'), isFalse);
45-
expect(source.contains('Gary is awesome!'), isTrue);
46+
expect(source.contains(originalString), isFalse);
47+
expect(source.contains(newString), isTrue);
4648
});
4749
});
4850

@@ -56,17 +58,18 @@ void main() {
5658
});
5759

5860
tearDown(() async {
61+
undoEdit();
5962
await context.tearDown();
6063
});
6164

6265
test('can live reload changes ', () async {
63-
await context.changeInput();
66+
await makeEditAndWaitForRebuild();
6467

6568
final source = await context.webDriver.pageSource;
6669

6770
// A full reload should clear the state.
68-
expect(source.contains('Hello World!'), isFalse);
69-
expect(source.contains('Gary is awesome!'), isTrue);
71+
expect(source.contains(originalString), isFalse);
72+
expect(source.contains(newString), isTrue);
7073
});
7174
});
7275

@@ -82,16 +85,17 @@ void main() {
8285

8386
tearDown(() async {
8487
await context.tearDown();
88+
undoEdit();
8589
});
8690

8791
test('can live reload changes ', () async {
88-
await context.changeInput();
92+
await makeEditAndWaitForRebuild();
8993

9094
final source = await context.webDriver.pageSource;
9195

9296
// A full reload should clear the state.
93-
expect(source.contains('Hello World!'), isFalse);
94-
expect(source.contains('Gary is awesome!'), isTrue);
97+
expect(source.contains(originalString), isFalse);
98+
expect(source.contains(newString), isTrue);
9599
});
96100
});
97101
});
@@ -104,12 +108,13 @@ void main() {
104108

105109
tearDown(() async {
106110
await context.tearDown();
111+
undoEdit();
107112
});
108113

109114
test('destroys and recreates the isolate during a hot restart', () async {
110115
final client = context.debugConnection.vmService;
111116
await client.streamListen('Isolate');
112-
await context.changeInput();
117+
await makeEditAndWaitForRebuild();
113118

114119
final eventsDone = expectLater(
115120
client.onIsolateEvent,
@@ -128,7 +133,7 @@ void main() {
128133
test('destroys and recreates the isolate during a page refresh', () async {
129134
final client = context.debugConnection.vmService;
130135
await client.streamListen('Isolate');
131-
await context.changeInput();
136+
await makeEditAndWaitForRebuild();
132137

133138
final eventsDone = expectLater(
134139
client.onIsolateEvent,
@@ -146,7 +151,7 @@ void main() {
146151
test('can hot restart via the service extension', () async {
147152
final client = context.debugConnection.vmService;
148153
await client.streamListen('Isolate');
149-
await context.changeInput();
154+
await makeEditAndWaitForRebuild();
150155

151156
final eventsDone = expectLater(
152157
client.onIsolateEvent,
@@ -163,8 +168,8 @@ void main() {
163168

164169
final source = await context.webDriver.pageSource;
165170
// Main is re-invoked which shouldn't clear the state.
166-
expect(source, contains('Hello World!'));
167-
expect(source, contains('Gary is awesome!'));
171+
expect(source, contains(originalString));
172+
expect(source, contains(newString));
168173
});
169174

170175
test('can send events before and after hot restart', () async {
@@ -218,7 +223,7 @@ void main() {
218223
test('can refresh the page via the fullReload service extension', () async {
219224
final client = context.debugConnection.vmService;
220225
await client.streamListen('Isolate');
221-
await context.changeInput();
226+
await makeEditAndWaitForRebuild();
222227

223228
final eventsDone = expectLater(
224229
client.onIsolateEvent,
@@ -234,8 +239,8 @@ void main() {
234239

235240
final source = await context.webDriver.pageSource;
236241
// Should see only the new text
237-
expect(source, isNot(contains('Hello World!')));
238-
expect(source, contains('Gary is awesome!'));
242+
expect(source.contains(originalString), isFalse);
243+
expect(source.contains(newString), isTrue);
239244
});
240245

241246
test('can hot restart while paused', () async {
@@ -253,13 +258,13 @@ void main() {
253258
await stream
254259
.firstWhere((event) => event.kind == EventKind.kPauseBreakpoint);
255260

256-
await context.changeInput();
261+
await makeEditAndWaitForRebuild();
257262
await client.callServiceExtension('hotRestart');
258263
final source = await context.webDriver.pageSource;
259264

260265
// Main is re-invoked which shouldn't clear the state.
261-
expect(source.contains('Hello World!'), isTrue);
262-
expect(source.contains('Gary is awesome!'), isTrue);
266+
expect(source.contains(originalString), isTrue);
267+
expect(source.contains(newString), isTrue);
263268

264269
vm = await client.getVM();
265270
isolateId = vm.isolates!.first.id!;
@@ -336,19 +341,20 @@ void main() {
336341

337342
tearDown(() async {
338343
await context.tearDown();
344+
undoEdit();
339345
});
340346

341347
test('can hot restart changes ', () async {
342-
await context.changeInput();
348+
await makeEditAndWaitForRebuild();
343349

344350
final source = await context.webDriver.pageSource;
345351

346352
// Main is re-invoked which shouldn't clear the state.
347-
expect(source.contains('Hello World!'), isTrue);
348-
expect(source.contains('Gary is awesome!'), isTrue);
353+
expect(source.contains(originalString), isTrue);
354+
expect(source.contains(newString), isTrue);
349355
// The ext.flutter.disassemble callback is invoked and waited for.
350-
expect(source,
351-
contains('start disassemble end disassemble Gary is awesome'));
356+
expect(
357+
source, contains('start disassemble end disassemble $newString'));
352358
});
353359

354360
test('fires isolate create/destroy events during hot restart', () async {
@@ -363,7 +369,7 @@ void main() {
363369
_hasKind(EventKind.kIsolateRunnable),
364370
])));
365371

366-
await context.changeInput();
372+
await makeEditAndWaitForRebuild();
367373

368374
await eventsDone;
369375
});
@@ -380,23 +386,39 @@ void main() {
380386

381387
tearDown(() async {
382388
await context.tearDown();
389+
undoEdit();
383390
});
384391

385392
test('can hot restart changes ', () async {
386-
await context.changeInput();
393+
await makeEditAndWaitForRebuild();
387394

388395
final source = await context.webDriver.pageSource;
389396

390397
// Main is re-invoked which shouldn't clear the state.
391-
expect(source.contains('Hello World!'), isTrue);
392-
expect(source.contains('Gary is awesome!'), isTrue);
398+
expect(source.contains(originalString), isTrue);
399+
expect(source.contains(newString), isTrue);
393400
// The ext.flutter.disassemble callback is invoked and waited for.
394-
expect(source,
395-
contains('start disassemble end disassemble Gary is awesome'));
401+
expect(
402+
source, contains('start disassemble end disassemble $newString'));
396403
});
397404
});
398405
});
399406
}
400407

408+
Future<void> makeEditAndWaitForRebuild() async {
409+
context.makeEditToDartEntryFile(
410+
toReplace: originalString,
411+
replaceWith: newString,
412+
);
413+
await context.waitForSuccessfulBuild(propogateToBrowser: true);
414+
}
415+
416+
void undoEdit() {
417+
context.makeEditToDartEntryFile(
418+
toReplace: newString,
419+
replaceWith: originalString,
420+
);
421+
}
422+
401423
TypeMatcher<Event> _hasKind(String kind) =>
402424
isA<Event>().having((e) => e.kind, 'kind', kind);

0 commit comments

Comments
 (0)