2
2
// for details. All rights reserved. Use of this source code is governed by a
3
3
// BSD-style license that can be found in the LICENSE file.
4
4
5
- @Timeout (const Duration (minutes: 8 ))
5
+ @Timeout (const Duration (minutes: 5 ))
6
6
7
+ import 'dart:async' ;
7
8
import 'dart:io' ;
8
9
9
10
import 'package:path/path.dart' as p;
@@ -14,26 +15,39 @@ import 'package:webdev/src/util.dart';
14
15
15
16
import 'test_utils.dart' ;
16
17
18
+ /// Key: name of file in web directory
19
+ /// Value: `null` - exists in both modes
20
+ /// `true` - DDC only
21
+ /// `false` - dart2js only
22
+ const _testItems = const < String , bool > {
23
+ 'main.dart.js' : null ,
24
+ 'main.dart.bootstrap.js' : true ,
25
+ 'main.ddc.js' : true
26
+ };
27
+
17
28
void main () {
18
- group ('should succeed with valid configuration' , () {
19
- for (var withDDC in [true , false ]) {
20
- test (withDDC ? 'DDC' : 'dart2js' , () async {
21
- var exampleDirectory = p.absolute (p.join (p.current, '..' , 'example' ));
22
- var process = await TestProcess .start (pubPath, ['get' ],
23
- workingDirectory: exampleDirectory,
24
- environment: _getPubEnvironment ());
29
+ String exampleDirectory;
30
+ setUpAll (() async {
31
+ exampleDirectory = p.absolute (p.join (p.current, '..' , 'example' ));
25
32
26
- await process.shouldExit (0 );
33
+ var process = await TestProcess .start (pubPath, ['get' ],
34
+ workingDirectory: exampleDirectory, environment: _getPubEnvironment ());
27
35
28
- await d.file ('.packages' , isNotEmpty).validate (exampleDirectory);
29
- await d.file ('pubspec.lock' , isNotEmpty).validate (exampleDirectory);
36
+ await process.shouldExit (0 );
30
37
38
+ await d.file ('.packages' , isNotEmpty).validate (exampleDirectory);
39
+ await d.file ('pubspec.lock' , isNotEmpty).validate (exampleDirectory);
40
+ });
41
+
42
+ group ('should build with valid configuration' , () {
43
+ for (var withDDC in [true , false ]) {
44
+ test (withDDC ? 'DDC' : 'dart2js' , () async {
31
45
var args = ['build' , '-o' , 'web:${d .sandbox }' ];
32
46
if (withDDC) {
33
47
args.add ('--no-release' );
34
48
}
35
49
36
- process = await runWebDev (args, workingDirectory: exampleDirectory);
50
+ var process = await runWebDev (args, workingDirectory: exampleDirectory);
37
51
38
52
var expectedItems = < Object > ['[INFO] Succeeded' ];
39
53
if (! withDDC) {
@@ -44,18 +58,69 @@ void main() {
44
58
await checkProcessStdout (process, expectedItems);
45
59
await process.shouldExit (0 );
46
60
47
- await d.file ('main.dart.js' , isNotEmpty).validate ();
61
+ for (var entry in _testItems.entries) {
62
+ var shouldExist = (entry.value ?? withDDC) == withDDC;
48
63
49
- for (var ddcFile in ['main.dart.bootstrap.js' , 'main.ddc.js' ]) {
50
- if (withDDC) {
51
- await d.file (ddcFile, isNotEmpty).validate ();
64
+ if (shouldExist) {
65
+ await d.file (entry.key, isNotEmpty).validate ();
52
66
} else {
53
- await d.nothing (ddcFile ).validate ();
67
+ await d.nothing (entry.key ).validate ();
54
68
}
55
69
}
56
70
});
57
71
}
58
72
});
73
+
74
+ group ('should serve with valid configuration' , () {
75
+ for (var withDDC in [true , false ]) {
76
+ test (withDDC ? 'DDC' : 'dart2js' , () async {
77
+ var openPort = await _getOpenPort ();
78
+ var args = ['serve' , 'web:$openPort ' ];
79
+ if (! withDDC) {
80
+ args.add ('--release' );
81
+ }
82
+
83
+ var process = await runWebDev (args, workingDirectory: exampleDirectory);
84
+
85
+ var hostUrl = 'http://localhost:$openPort ' ;
86
+
87
+ await expectLater (
88
+ process.stdout, emitsThrough ('Serving `web` on $hostUrl ' ));
89
+
90
+ var client = new HttpClient ();
91
+
92
+ try {
93
+ for (var entry in _testItems.entries) {
94
+ var url = Uri .parse ('$hostUrl /${entry .key }' );
95
+
96
+ var request = await client.getUrl (url);
97
+ var response = await request.close ();
98
+
99
+ var shouldExist = (entry.value ?? withDDC) == withDDC;
100
+
101
+ if (entry.key == 'main.ddc.js' ) {
102
+ // This file SHOULD NOT be output in dart2js mode
103
+ // But there is an issue here
104
+ // https://github.com/dart-lang/build/issues/1033
105
+ shouldExist = true ;
106
+ }
107
+
108
+ expect (response.statusCode, shouldExist ? 200 : 404 );
109
+ }
110
+ } finally {
111
+ client.close (force: true );
112
+ }
113
+
114
+ if (Platform .isWindows) {
115
+ await process.kill ();
116
+ await process.shouldExit (- 1 );
117
+ } else {
118
+ process.signal (ProcessSignal .SIGINT );
119
+ await process.shouldExit (0 );
120
+ }
121
+ });
122
+ }
123
+ });
59
124
}
60
125
61
126
/// Returns an environment map that includes `PUB_ENVIRONMENT` .
@@ -74,3 +139,22 @@ Map<String, String> _getPubEnvironment() {
74
139
75
140
return environment;
76
141
}
142
+
143
+ /// Returns an open port by creating a temporary Socket
144
+ Future <int > _getOpenPort () async {
145
+ ServerSocket socket;
146
+
147
+ try {
148
+ socket = await ServerSocket .bind (InternetAddress .LOOPBACK_IP_V4 , 0 );
149
+ } catch (_) {
150
+ // try again v/ V6 only. Slight possibility that V4 is disabled
151
+ socket = await ServerSocket .bind (InternetAddress .LOOPBACK_IP_V6 , 0 ,
152
+ v6Only: true );
153
+ }
154
+
155
+ try {
156
+ return socket.port;
157
+ } finally {
158
+ await socket.close ();
159
+ }
160
+ }
0 commit comments