/
run_test.dart
972 lines (866 loc) · 28.9 KB
/
run_test.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
// 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.
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:dartdev/src/resident_frontend_constants.dart';
import 'package:dartdev/src/resident_frontend_utils.dart';
import 'package:path/path.dart' as path;
import 'package:test/test.dart';
import '../utils.dart';
const String soundNullSafetyMessage = 'Info: Compiling with sound null safety.';
const devToolsMessagePrefix =
'The Dart DevTools debugger and profiler is available at: http://127.0.0.1:';
const dartVMServiceMessagePrefix =
'The Dart VM service is listening on http://127.0.0.1:';
final dartVMServiceRegExp =
RegExp(r'The Dart VM service is listening on (http://127.0.0.1:.*)');
const residentFrontendServerPrefix =
'The Resident Frontend Compiler is listening at 127.0.0.1:';
void main() async {
ensureRunFromSdkBinDart();
group('run', run, timeout: longTimeout);
group('run --resident', residentRun, timeout: longTimeout);
}
void run() {
late TestProject p;
tearDown(() async => await p.dispose());
test('--help', () async {
p = project();
var result = await p.run(['run', '--help']);
expect(result.stdout, contains('Run a Dart program.'));
expect(result.stdout, contains('Debugging options:'));
expect(
result.stdout,
contains(
'Usage: dart run [arguments] [<dart-file|package-target> [args]]',
),
);
expect(result.stderr, isEmpty);
expect(result.exitCode, 0);
});
test('--help --verbose', () async {
p = project();
var result = await p.run(['run', '--help', '--verbose']);
expect(result.stdout, contains('Run a Dart program.'));
expect(result.stdout, contains('Debugging options:'));
expect(
result.stdout,
contains(
'Usage: dart [vm-options] run [arguments] [<dart-file|package-target> [args]]',
),
);
expect(result.stderr, isEmpty);
expect(result.exitCode, 0);
});
test("'Hello World'", () async {
p = project(mainSrc: "void main() { print('Hello World'); }");
ProcessResult result = await p.run(['run', p.relativeFilePath]);
expect(result.stdout, contains('Hello World'));
expect(result.stderr, isEmpty);
expect(result.exitCode, 0);
});
test('no such file', () async {
p = project(mainSrc: "void main() { print('Hello World'); }");
ProcessResult result =
await p.run(['run', 'no/such/file/${p.relativeFilePath}']);
expect(result.stderr, isNotEmpty);
expect(result.exitCode, isNot(0));
});
test('implicit packageName.dart', () async {
// TODO(jwren) circle back to reimplement this test if possible, the file
// name (package name) will be the name of the temporary directory on disk
p = project(mainSrc: "void main() { print('Hello World'); }");
p.file('bin/main.dart', "void main() { print('Hello main.dart'); }");
ProcessResult result = await p.run(['run']);
expect(result.stdout, contains('Hello main.dart'));
expect(result.stderr, isEmpty);
expect(result.exitCode, 0);
}, skip: true);
// Could not find the implicit file to run: bin
test('missing implicit packageName.dart', () async {
p = project(mainSrc: "void main() { print('Hello World'); }");
p.file('bin/foo.dart', "void main() { print('Hello main.dart'); }");
ProcessResult result = await p.run(['run']);
expect(result.stdout, isEmpty);
expect(
result.stderr,
contains('Could not find `bin${path.separator}dartdev_temp.dart` in '
'package `dartdev_temp`.'));
expect(result.exitCode, 255);
});
test('experiments are enabled correctly', () async {
// TODO(bkonyi): find a more robust way to test experiments by exposing
// enabled experiments for an isolate (e.g., through dart:developer or the
// VM service).
//
// See https://github.com/dart-lang/sdk/issues/50230
p = project();
p.file('main.dart', 'void main(args) { print("Record: \${(1, 2)}"); }');
ProcessResult result = await p.run([
'run',
'--enable-experiment=records',
'main.dart',
]);
// The records experiment should be enabled.
expect(result.stdout, contains('Record: '));
expect(result.stderr, isEmpty);
expect(result.exitCode, 0);
// Run again with the experiment disabled to make sure the test is actually
// working as expected.
result = await p.run([
'run',
'main.dart',
]);
// The records experiment should not be enabled and the program should fail
// to run.
expect(result.stdout, isEmpty);
expect(result.stderr, isNotEmpty);
expect(result.exitCode, 254);
});
test('arguments are properly passed', () async {
p = project();
p.file('main.dart', 'void main(args) { print(args); }');
ProcessResult result = await p.run([
'run',
'--enable-experiment=test-experiment',
'main.dart',
'argument1',
'argument2',
]);
// --enable-experiment and main.dart should not be passed.
expect(result.stdout, equals('[argument1, argument2]\n'));
expect(result.stderr, isEmpty);
expect(result.exitCode, 0);
});
test('from path-dependency with cyclic dependency', () async {
p = project(name: 'foo');
final bar = TestProject(name: 'bar');
p.file('pubspec.yaml', '''
name: foo
environment:
sdk: '>=2.9.0<3.0.0'
dependencies: { 'bar': {'path': '${bar.dir.path}'}}
''');
p.file('lib/foo.dart', r'''
import 'package:bar/bar.dart';
final b = "FOO $bar";
''');
try {
bar.file('lib/bar.dart', 'final bar = "BAR";');
bar.file('bin/main.dart', r'''
import 'package:foo/foo.dart';
void main(List<String> args) => print("$b $args");
''');
ProcessResult result = await p.run(['run', 'bar:main', '--arg1', 'arg2']);
expect(result.stderr, isEmpty);
expect(result.stdout, contains('FOO BAR [--arg1, arg2]'));
expect(result.exitCode, 0);
} finally {
await bar.dispose();
}
});
test('with absolute file path', () async {
p = project();
p.file('main.dart', 'void main(args) { print(args); }');
// Test with absolute path
final name = path.join(p.dirPath, 'main.dart');
final result = await p.run([
'run',
'--enable-experiment=test-experiment',
name,
'--argument1',
'argument2',
]);
// --enable-experiment and main.dart should not be passed.
expect(result.stderr, isEmpty);
expect(result.stdout, equals('[--argument1, argument2]\n'));
expect(result.exitCode, 0);
});
test('with file uri', () async {
p = project();
p.file('main.dart', 'void main(args) { print(args); }');
// Test with File uri
final name = path.join(p.dirPath, 'main.dart');
final result = await p.run([
'run',
Uri.file(name).toString(),
'--argument1',
'argument2',
]);
// --enable-experiment and main.dart should not be passed.
expect(result.stderr, isEmpty);
expect(result.stdout, equals('[--argument1, argument2]\n'));
expect(result.exitCode, 0);
});
test('with accepted VM flags', () async {
p = project(mainSrc: "void main() { print('Hello World'); }");
// --observe sets the following flags by default:
// --enable-vm-service
// --pause-isolate-on-exit
// --pause-isolate-on-unhandled-exception
// --warn-on-pause-with-no-debugger
//
// This test ensures that allowed arguments for dart run which are valid VM
// arguments are properly handled by the VM.
ProcessResult result = await p.run([
'run',
'--observe',
'--pause-isolates-on-start',
// This should negate the above flag.
'--no-pause-isolates-on-start',
'--no-pause-isolates-on-exit',
'--no-pause-isolates-on-unhandled-exceptions',
'-Dfoo=bar',
'--define=bar=foo',
p.relativeFilePath,
]);
expect(
result.stdout,
matches(
r'The Dart VM service is listening on http:\/\/127.0.0.1:8181\/[a-zA-Z0-9_-]+=\/\n.*'),
);
expect(result.stderr, isEmpty);
expect(result.exitCode, 0);
// Again, with --disable-service-auth-codes.
result = await p.run([
'run',
'--observe',
'--pause-isolates-on-start',
// This should negate the above flag.
'--no-pause-isolates-on-start',
'--no-pause-isolates-on-exit',
'--no-pause-isolates-on-unhandled-exceptions',
'--disable-service-auth-codes',
'-Dfoo=bar',
'--define=bar=foo',
p.relativeFilePath,
]);
expect(
result.stdout,
contains('The Dart VM service is listening on http://127.0.0.1:8181/\n'),
);
expect(result.stderr, isEmpty);
expect(result.exitCode, 0);
// Again, with IPv6.
result = await p.run([
'run',
'--observe=8181/::1',
'--pause-isolates-on-start',
// This should negate the above flag.
'--no-pause-isolates-on-start',
'--no-pause-isolates-on-exit',
'--no-pause-isolates-on-unhandled-exceptions',
'-Dfoo=bar',
'--define=bar=foo',
p.relativeFilePath,
]);
expect(
result.stdout,
matches(
r'The Dart VM service is listening on http:\/\/\[::1\]:8181\/[a-zA-Z0-9_-]+=\/\n.*'),
);
expect(result.stderr, isEmpty);
expect(result.exitCode, 0);
});
test('fails when provided verbose VM flags', () async {
p = project(mainSrc: "void main() { print('Hello World'); }");
// Any VM flags not listed under 'dart run help --verbose' should be passed
// before a dartdev command.
ProcessResult result = await p.run([
'run',
'--vm-name=foo',
p.relativeFilePath,
]);
expect(result.stdout, isEmpty);
expect(
result.stderr,
contains('Could not find an option named "vm-name".'),
);
expect(result.exitCode, 64);
});
test('fails when provided unlisted VM flags', () async {
p = project(mainSrc: "void main() { print('Hello World'); }");
// Any VM flags not listed under 'dart run help --verbose' should be passed
// before a dartdev command.
ProcessResult result = await p.run([
'run',
'--verbose_gc',
p.relativeFilePath,
]);
expect(result.stdout, isEmpty);
expect(
result.stderr,
contains('Could not find an option named "verbose_gc".'),
);
expect(result.exitCode, 64);
});
test('--enable-asserts', () async {
p = project(mainSrc: 'void main() { assert(false); }');
// Ensure --enable-asserts doesn't cause the dartdev isolate to fail to
// load. Regression test for: https://github.com/dart-lang/sdk/issues/42831
ProcessResult result = await p.run([
'run',
'--enable-asserts',
p.relativeFilePath,
]);
expect(result.stdout, isEmpty);
expect(result.stderr, contains('Unhandled exception'));
expect(result.exitCode, 255);
});
test('does not interpret VM flags provided after script', () async {
p = project(mainSrc: 'void main() { assert(false); }');
// Any VM flags passed after the script shouldn't be interpreted by the VM.
ProcessResult result = await p.run([
'run',
p.relativeFilePath,
'--enable-asserts',
]);
expect(result.stdout, isEmpty);
expect(result.stderr, isEmpty);
expect(result.exitCode, 0);
});
test('--enable-service-port-fallback', () async {
final p = project(mainSrc: '''void main() {}''');
final server = await HttpServer.bind(InternetAddress.loopbackIPv4, 0);
final result = await p.run(
[
'run',
'--enable-vm-service=${server.port}',
'--enable-service-port-fallback',
p.relativeFilePath,
],
);
final regexp = RegExp(
r'The Dart VM service is listening on http:\/\/127.0.0.1:(\d*)\/[a-zA-Z0-9_-]+=\/\n.*',
);
final vmServicePort =
int.parse(regexp.firstMatch(result.stdout)!.group(1)!);
expect(server.port != vmServicePort, isTrue);
await server.close();
});
test('without verbose CFE info', () async {
final p = project(mainSrc: '''void main() {}''');
var result = await p.run(
[
'run',
'--verbosity=warning',
p.relativeFilePath,
],
);
expect(result.stdout,
predicate((dynamic o) => !'$o'.contains(soundNullSafetyMessage)));
expect(result.stderr, isEmpty);
expect(result.exitCode, 0);
});
group('DDS', () {
group('disable', () {
test('dart run simple', () async {
p = project(mainSrc: "void main() { print('Hello World'); }");
ProcessResult result = await p.run([
'run',
'--no-dds',
'--enable-vm-service',
p.relativeFilePath,
]);
expect(result.stdout, isNot(contains(devToolsMessagePrefix)));
expect(result.stdout, contains(dartVMServiceMessagePrefix));
});
test('dart simple', () async {
p = project(mainSrc: "void main() { print('Hello World'); }");
ProcessResult result = await p.run([
'--no-dds',
'--enable-vm-service',
p.relativeFilePath,
]);
expect(result.stdout, isNot(contains(devToolsMessagePrefix)));
expect(result.stdout, contains(dartVMServiceMessagePrefix));
});
});
group('explicit enable', () {
test('dart run simple', () async {
p = project(mainSrc: "void main() { print('Hello World'); }");
ProcessResult result = await p.run([
'run',
'--dds',
'--enable-vm-service',
p.relativeFilePath,
]);
expect(result.stdout, contains(devToolsMessagePrefix));
expect(result.stdout, contains(dartVMServiceMessagePrefix));
});
test('dart simple', () async {
p = project(mainSrc: "void main() { print('Hello World'); }");
ProcessResult result = await p.run([
'--dds',
'--enable-vm-service',
p.relativeFilePath,
]);
expect(result.stdout, contains(devToolsMessagePrefix));
expect(result.stdout, contains(dartVMServiceMessagePrefix));
});
});
});
group('DevTools', () {
test('dart run simple', () async {
p = project(mainSrc: "void main() { print('Hello World'); }");
ProcessResult result = await p.run([
'run',
'--enable-vm-service',
p.relativeFilePath,
]);
expect(result.stdout, contains(devToolsMessagePrefix));
});
test('dart simple', () async {
p = project(mainSrc: "void main() { print('Hello World'); }");
ProcessResult result = await p.run([
'--enable-vm-service',
p.relativeFilePath,
]);
expect(result.stdout, contains(devToolsMessagePrefix));
});
test('dart run explicit', () async {
p = project(mainSrc: "void main() { print('Hello World'); }");
ProcessResult result = await p.run([
'run',
'--serve-devtools',
'--enable-vm-service',
p.relativeFilePath,
]);
expect(result.stdout, contains(devToolsMessagePrefix));
});
test('dart explicit', () async {
p = project(mainSrc: "void main() { print('Hello World'); }");
ProcessResult result = await p.run([
'--serve-devtools',
'--enable-vm-service',
p.relativeFilePath,
]);
expect(result.stdout, contains(devToolsMessagePrefix));
});
test('dart run disabled', () async {
p = project(mainSrc: "void main() { print('Hello World'); }");
ProcessResult result = await p.run([
'run',
'--enable-vm-service',
'--no-serve-devtools',
p.relativeFilePath,
]);
expect(result.stdout, isNot(contains(devToolsMessagePrefix)));
});
test('dart disabled', () async {
p = project(mainSrc: "void main() { print('Hello World'); }");
ProcessResult result = await p.run([
'--enable-vm-service',
'--no-serve-devtools',
p.relativeFilePath,
]);
expect(result.stdout, isNot(contains(devToolsMessagePrefix)));
});
test('dart run VM service not enabled', () async {
p = project(mainSrc: "void main() { print('Hello World'); }");
ProcessResult result = await p.run([
'run',
'--serve-devtools',
p.relativeFilePath,
]);
expect(result.stdout, isNot(contains(devToolsMessagePrefix)));
});
test('dart VM service not enabled', () async {
p = project(mainSrc: "void main() { print('Hello World'); }");
ProcessResult result = await p.run([
'--serve-devtools',
p.relativeFilePath,
]);
expect(result.stdout, isNot(contains(devToolsMessagePrefix)));
});
test(
'spawn via SIGQUIT',
() async {
p = project(
mainSrc:
'void main() { print("ready"); int i = 0; while(true) { i++; } }',
);
Process process = await p.start([
p.relativeFilePath,
]);
final readyCompleter = Completer<void>();
final completer = Completer<void>();
late StreamSubscription sub;
sub = process.stdout.transform(utf8.decoder).listen((event) async {
if (event.contains('ready')) {
readyCompleter.complete();
} else if (event.contains(devToolsMessagePrefix)) {
await sub.cancel();
completer.complete();
}
});
// Wait for process to start.
await readyCompleter.future;
process.kill(ProcessSignal.sigquit);
await completer.future;
process.kill();
},
// No support for SIGQUIT on Windows.
skip: Platform.isWindows,
);
});
group('Observatory', () {
void generateServedTest({
required bool serve,
required bool enableAuthCodes,
required bool explicitRun,
required bool withDds,
}) {
test(
'${serve ? 'served by default' : 'not served'} ${enableAuthCodes ? "with" : "without"} '
'auth codes, ${explicitRun ? 'explicit' : 'implicit'} run,${withDds ? ' ' : 'no'} DDS',
() async {
p = project(
mainSrc:
'void main() { print("ready"); int i = 0; while(true) { i++; } }',
);
Process process = await p.start([
if (explicitRun) 'run',
'--enable-vm-service',
if (!withDds) '--no-dds',
if (!enableAuthCodes) '--disable-service-auth-codes',
if (!serve) '--no-serve-observatory',
p.relativeFilePath,
]);
final completer = Completer<void>();
late StreamSubscription sub;
late String uri;
sub = process.stdout.transform(utf8.decoder).listen((event) async {
if (event.contains(dartVMServiceRegExp)) {
uri = dartVMServiceRegExp.firstMatch(event)!.group(1)!;
await sub.cancel();
completer.complete();
}
});
// Wait for process to start.
await completer.future;
final client = HttpClient();
final request = await client.getUrl(Uri.parse(uri));
final response = await request.close();
final content = await response.transform(utf8.decoder).join();
expect(content.contains('Dart VM Observatory'), serve);
if (!serve) {
if (withDds) {
expect(content.contains('DevTools'), true);
} else {
expect(
content,
'This VM does not have a registered Dart '
'Development Service (DDS) instance and is not currently serving '
'Dart DevTools.',
);
}
}
process.kill();
},
);
}
const flags = <bool>[true, false];
for (final serve in flags) {
for (final enableAuthCodes in flags) {
for (final explicitRun in flags) {
for (final withDds in flags) {
generateServedTest(
serve: serve,
enableAuthCodes: enableAuthCodes,
explicitRun: explicitRun,
withDds: withDds,
);
}
}
}
}
});
}
void residentRun() {
late TestProject serverInfoDirectory, p;
late String serverInfoFile;
setUpAll(() async {
serverInfoDirectory = project(mainSrc: 'void main() {}');
serverInfoFile = path.join(serverInfoDirectory.dirPath, 'info');
final result = await serverInfoDirectory.run([
'run',
'--$serverInfoOption=$serverInfoFile',
serverInfoDirectory.relativeFilePath,
]);
expect(result.exitCode, 0);
expect(File(serverInfoFile).existsSync(), true);
expect(
Directory(path.join(
serverInfoDirectory.dirPath,
'.dart_tool',
dartdevKernelCache,
)).listSync(),
isNotEmpty);
});
tearDownAll(() async {
try {
await sendAndReceiveResponse(
residentServerShutdownCommand,
File(path.join(serverInfoDirectory.dirPath, 'info')),
);
} catch (_) {}
serverInfoDirectory.dispose();
});
tearDown(() async => await p.dispose());
test("'Hello World'", () async {
p = project(mainSrc: "void main() { print('Hello World'); }");
final result = await p.run([
'run',
'--$serverInfoOption=$serverInfoFile',
p.relativeFilePath,
]);
Directory? kernelCache = p.findDirectory('.dart_tool/kernel');
expect(result.exitCode, 0);
expect(
result.stdout,
allOf(
contains('Hello World'),
isNot(contains(residentFrontendServerPrefix)),
),
);
expect(result.stderr, isEmpty);
expect(kernelCache, isNot(null));
});
test('same server used from different directories', () async {
p = project(mainSrc: "void main() { print('1'); }");
TestProject p2 = project(mainSrc: "void main() { print('2'); }");
addTearDown(() async => p2.dispose());
final runResult1 = await p.run([
'run',
'--$serverInfoOption=$serverInfoFile',
p.relativeFilePath,
]);
final runResult2 = await p2.run([
'run',
'--$serverInfoOption=$serverInfoFile',
p2.relativeFilePath,
]);
expect(runResult1.exitCode, allOf(0, equals(runResult2.exitCode)));
expect(
runResult1.stdout,
allOf(
contains('1'),
isNot(contains(residentFrontendServerPrefix)),
),
);
expect(
runResult2.stdout,
allOf(
contains('2'),
isNot(contains(residentFrontendServerPrefix)),
),
);
});
test('kernel cache respects directory structure', () async {
p = project(name: 'foo');
p.file('lib/main.dart', 'void main() {}');
p.file('bin/main.dart', 'void main() {}');
final runResult1 = await p.run([
'run',
'--$serverInfoOption=$serverInfoFile',
path.join(p.dirPath, 'lib/main.dart'),
]);
expect(runResult1.exitCode, 0);
expect(runResult1.stdout, isEmpty);
expect(runResult1.stderr, isEmpty);
final runResult2 = await p.run([
'run',
'--$serverInfoOption=$serverInfoFile',
path.join(p.dirPath, 'bin/main.dart'),
]);
expect(runResult2.exitCode, 0);
expect(runResult2.stdout, isEmpty);
expect(runResult2.stderr, isEmpty);
final cache = p.findDirectory('.dart_tool/kernel');
expect(cache, isNot(null));
expect(Directory(path.join(cache!.path, 'lib')).existsSync(), true);
expect(Directory(path.join(cache.path, 'bin')).existsSync(), true);
});
test('standalone dart program', () async {
p = project(mainSrc: 'void main() {}');
p.deleteFile('pubspec.yaml');
final runResult = await p.run([
'run',
'--$serverInfoOption=$serverInfoFile',
p.relativeFilePath,
]);
expect(runResult.stderr,
contains('resident mode is only supported for Dart packages.'));
expect(runResult.exitCode, isNot(0));
expect(File(serverInfoFile).existsSync(), true);
});
test('directory that the server is started in is deleted', () async {
// The first command will start the server process in the p2
// project directory.
// This directory is deleted. The second command will attempt to run again
// The server process should not fail on this second attempt. If it does,
// the 3rd command will result in a new server starting.
Directory tempServerInfoDir = Directory.systemTemp.createTempSync('a');
String tempServerInfoFile = path.join(tempServerInfoDir.path, 'info');
addTearDown(() async {
try {
await sendAndReceiveResponse(
residentServerShutdownCommand,
File(tempServerInfoFile),
);
} catch (_) {}
await deleteDirectory(tempServerInfoDir);
});
p = project(mainSrc: 'void main() {}');
TestProject p2 = project(mainSrc: 'void main() {}');
final runResult1 = await p2.run([
'run',
'--$serverInfoOption=$tempServerInfoFile',
p2.relativeFilePath,
]);
await deleteDirectory(p2.dir);
expect(runResult1.exitCode, 0);
expect(runResult1.stdout, contains(residentFrontendServerPrefix));
await p.run([
'run',
'--$serverInfoOption=$tempServerInfoFile',
p.relativeFilePath,
]);
final runResult2 = await p.run([
'run',
'--$serverInfoOption=$tempServerInfoFile',
p.relativeFilePath,
]);
expect(runResult2.exitCode, 0);
expect(runResult2.stderr, isEmpty);
expect(runResult2.stdout, isNot(contains(residentFrontendServerPrefix)));
});
test('VM flags are passed properly', () async {
p = project(mainSrc: "void main() { print('Hello World'); }");
// --observe sets the following flags by default:
// --enable-vm-service
// --pause-isolate-on-exit
// --pause-isolate-on-unhandled-exception
// --warn-on-pause-with-no-debugger
//
// This test ensures that allowed arguments for dart run which are valid VM
// arguments are properly handled by the VM.
ProcessResult result = await p.run([
'run',
'--$serverInfoOption=$serverInfoFile',
'--observe',
'--pause-isolates-on-start',
// This should negate the above flag.
'--no-pause-isolates-on-start',
'--no-pause-isolates-on-exit',
'--no-pause-isolates-on-unhandled-exceptions',
'-Dfoo=bar',
'--define=bar=foo',
p.relativeFilePath,
]);
expect(
result.stdout,
isNot(
matches(
r'The Resident Frontend Compiler is listening at 127.0.0.1:[0-9]+'),
),
);
expect(
result.stdout,
matches(
r'The Dart VM service is listening on http:\/\/127.0.0.1:8181\/[a-zA-Z0-9_-]+=\/\n.*'),
);
expect(result.stderr, isEmpty);
expect(result.exitCode, 0);
// Again, with --disable-service-auth-codes.
result = await p.run([
'run',
'--$serverInfoOption=$serverInfoFile',
'--observe',
'--pause-isolates-on-start',
// This should negate the above flag.
'--no-pause-isolates-on-start',
'--no-pause-isolates-on-exit',
'--no-pause-isolates-on-unhandled-exceptions',
'--disable-service-auth-codes',
'-Dfoo=bar',
'--define=bar=foo',
p.relativeFilePath,
]);
expect(
result.stdout,
isNot(
matches(
r'The Resident Frontend Compiler is listening at 127.0.0.1:[0-9]+'),
),
);
expect(
result.stdout,
contains('The Dart VM service is listening on http://127.0.0.1:8181/\n'),
);
expect(result.stderr, isEmpty);
expect(result.exitCode, 0);
// Again, with IPv6.
result = await p.run([
'run',
'--$serverInfoOption=$serverInfoFile',
'--observe=8181/::1',
'--pause-isolates-on-start',
// This should negate the above flag.
'--no-pause-isolates-on-start',
'--no-pause-isolates-on-exit',
'--no-pause-isolates-on-unhandled-exceptions',
'-Dfoo=bar',
'--define=bar=foo',
p.relativeFilePath,
]);
expect(
result.stdout,
isNot(
matches(
r'The Resident Frontend Compiler is listening at 127.0.0.1:[0-9]+'),
),
);
expect(
result.stdout,
matches(
r'The Dart VM service is listening on http:\/\/\[::1\]:8181\/[a-zA-Z0-9_-]+=\/\n.*'),
);
expect(result.stderr, isEmpty);
expect(result.exitCode, 0);
});
test('custom package_config path', () async {
p = project(name: 'foo');
final bar = TestProject(name: 'bar');
final baz = TestProject(name: 'baz', mainSrc: '''
import 'package:bar/bar.dart'
void main() {}
''');
addTearDown(() async => bar.dispose());
addTearDown(() async => baz.dispose());
p.file('custom_packages.json', '''
{
"configVersion": 2,
"packages": [
{
"name": "bar",
"rootUri": "${bar.dirPath}",
"packageUri": "${path.join(bar.dirPath, 'lib')}"
}
]
}
''');
final runResult = await baz.run([
'run',
'--$serverInfoOption=$serverInfoFile',
'--packages=${path.join(p.dirPath, 'custom_packages.json')}',
baz.relativeFilePath,
]);
expect(runResult.exitCode, 0);
expect(runResult.stderr, isEmpty);
expect(runResult.stdout, isEmpty);
}, skip: 'until a --packages flag is added to the run command');
}