|
| 1 | +// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:io'; |
| 6 | + |
| 7 | +import 'package:path/path.dart' as p; |
| 8 | +import 'package:test/test.dart'; |
| 9 | + |
| 10 | +const String program1 = ''' |
| 11 | +class A { |
| 12 | + final int i; |
| 13 | + const A(this.i); |
| 14 | +} |
| 15 | +void main() { |
| 16 | + final a = A(1); |
| 17 | +} |
| 18 | +'''; |
| 19 | + |
| 20 | +const String program2Valid = ''' |
| 21 | +class A { |
| 22 | + final int i; |
| 23 | + const A(this.i); |
| 24 | +} |
| 25 | +void main() { |
| 26 | + final a = A(2); |
| 27 | +} |
| 28 | +'''; |
| 29 | + |
| 30 | +const String program2Error = ''' |
| 31 | +class A { |
| 32 | + const A(); |
| 33 | +} |
| 34 | +void main() { |
| 35 | + final a = A(); |
| 36 | +} |
| 37 | +'''; |
| 38 | + |
| 39 | +String _resolvePath(String executableRelativePath) { |
| 40 | + return Uri.file(Platform.resolvedExecutable) |
| 41 | + .resolve(executableRelativePath) |
| 42 | + .toFilePath(); |
| 43 | +} |
| 44 | + |
| 45 | +Future<void> main() async { |
| 46 | + group('hot reload flags', () { |
| 47 | + late Directory tmp; |
| 48 | + late File outputJs; |
| 49 | + late File deltaDill; |
| 50 | + late File lastAcceptedDill; |
| 51 | + setUp(() { |
| 52 | + File file(String path) => File.fromUri(tmp.uri.resolve(path)); |
| 53 | + |
| 54 | + tmp = Directory.systemTemp.createTempSync('hot_reload_flags_test'); |
| 55 | + outputJs = file('output.js'); |
| 56 | + deltaDill = file('delta.dill'); |
| 57 | + lastAcceptedDill = file('lastAccepted.dill'); |
| 58 | + }); |
| 59 | + |
| 60 | + tearDown(() { |
| 61 | + tmp.deleteSync(recursive: true); |
| 62 | + }); |
| 63 | + |
| 64 | + void runDDC(List<String> flags, String programSource, |
| 65 | + {String? expectError}) async { |
| 66 | + final sdkPath = p.dirname(Platform.executable); |
| 67 | + final dartAotRuntime = p.absolute(sdkPath, |
| 68 | + Platform.isWindows ? 'dartaotruntime.exe' : 'dartaotruntime'); |
| 69 | + final snapshotName = _resolvePath('snapshots/dartdevc_aot.dart.snapshot'); |
| 70 | + final outlinePath = _resolvePath('../lib/_internal/ddc_outline.dill'); |
| 71 | + |
| 72 | + final sourceFile = File.fromUri(tmp.uri.resolve('main.dart')); |
| 73 | + sourceFile.writeAsStringSync(programSource); |
| 74 | + |
| 75 | + final args = <String>[ |
| 76 | + snapshotName, |
| 77 | + '--dart-sdk-summary', |
| 78 | + outlinePath, |
| 79 | + '-o', |
| 80 | + outputJs.path, |
| 81 | + ...flags, |
| 82 | + sourceFile.path, |
| 83 | + ]; |
| 84 | + |
| 85 | + final process = Process.runSync(dartAotRuntime, args); |
| 86 | + if (expectError != null) { |
| 87 | + expect(process.exitCode, isNonNegative); |
| 88 | + expect(process.stderr, contains(expectError)); |
| 89 | + } else { |
| 90 | + if (process.exitCode != 0) { |
| 91 | + print(process.stdout); |
| 92 | + print(process.stderr); |
| 93 | + } |
| 94 | + expect(process.exitCode, 0); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + test('providing no flag skips inspector', () { |
| 99 | + runDDC(const [], program1); |
| 100 | + expect(outputJs.existsSync(), isTrue); |
| 101 | + expect(outputJs.statSync().size, isNonNegative); |
| 102 | + }); |
| 103 | + |
| 104 | + test('providing only delta output flag does not error', () { |
| 105 | + runDDC(['--reload-delta-kernel=${deltaDill.path}'], program1); |
| 106 | + expect(deltaDill.existsSync(), isTrue); |
| 107 | + expect(deltaDill.statSync().size, isNonNegative); |
| 108 | + expect(outputJs.existsSync(), isTrue); |
| 109 | + expect(outputJs.statSync().size, isNonNegative); |
| 110 | + }); |
| 111 | + |
| 112 | + test('providing delta output and last accepted input flag valid change', |
| 113 | + () { |
| 114 | + runDDC(['--reload-delta-kernel=${lastAcceptedDill.path}'], program1); |
| 115 | + expect(lastAcceptedDill.existsSync(), isTrue); |
| 116 | + expect(lastAcceptedDill.statSync().size, isNonNegative); |
| 117 | + |
| 118 | + runDDC([ |
| 119 | + '--reload-last-accepted-kernel=${lastAcceptedDill.path}', |
| 120 | + '--reload-delta-kernel=${deltaDill.path}' |
| 121 | + ], program2Valid); |
| 122 | + expect(lastAcceptedDill.existsSync(), isTrue); |
| 123 | + expect(lastAcceptedDill.statSync().size, isNonNegative); |
| 124 | + expect(deltaDill.existsSync(), isTrue); |
| 125 | + expect(deltaDill.statSync().size, isNonNegative); |
| 126 | + expect(outputJs.existsSync(), isTrue); |
| 127 | + expect(outputJs.statSync().size, isNonNegative); |
| 128 | + }); |
| 129 | + |
| 130 | + test('providing delta output and last accepted input flag invalid change', |
| 131 | + () { |
| 132 | + runDDC(['--reload-delta-kernel=${lastAcceptedDill.path}'], program1); |
| 133 | + runDDC([ |
| 134 | + '--reload-last-accepted-kernel=${lastAcceptedDill.path}', |
| 135 | + '--reload-delta-kernel=${deltaDill.path}' |
| 136 | + ], program2Error, expectError: 'Const class cannot remove fields'); |
| 137 | + expect(lastAcceptedDill.existsSync(), isTrue); |
| 138 | + expect(lastAcceptedDill.statSync().size, isNonNegative); |
| 139 | + expect(deltaDill.existsSync(), isFalse); |
| 140 | + }); |
| 141 | + }); |
| 142 | +} |
0 commit comments