Skip to content

Commit 2f0d41b

Browse files
[flutter_tools] set test directory base as additional root, allow running without index.html (flutter#55531)
1 parent 873b21e commit 2f0d41b

File tree

5 files changed

+46
-35
lines changed

5 files changed

+46
-35
lines changed

packages/flutter_tools/lib/src/build_runner/devfs_web.dart

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,15 @@ typedef DwdsLauncher = Future<Dwds> Function({
4848
ExpressionCompiler expressionCompiler,
4949
});
5050

51+
// A minimal index for projects that do not yet support web.
52+
const String _kDefaultIndex = '''
53+
<html>
54+
<body>
55+
<script src="main.dart.js"></script>
56+
</body>
57+
</html>
58+
''';
59+
5160
/// An expression compiler connecting to FrontendServer
5261
///
5362
/// This is only used in development mode
@@ -277,8 +286,11 @@ class WebAssetServer implements AssetReader {
277286
headers[HttpHeaders.contentTypeHeader] = 'text/html';
278287
headers[HttpHeaders.contentLengthHeader] = indexFile.lengthSync().toString();
279288
return shelf.Response.ok(indexFile.openRead(), headers: headers);
289+
} else {
290+
headers[HttpHeaders.contentTypeHeader] = 'text/html';
291+
headers[HttpHeaders.contentLengthHeader] = _kDefaultIndex.length.toString();
292+
return shelf.Response.ok(_kDefaultIndex, headers: headers);
280293
}
281-
return shelf.Response.notFound('');
282294
}
283295

284296
// Track etag headers for better caching of resources.

packages/flutter_tools/lib/src/build_runner/resident_web_runner.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -368,9 +368,8 @@ class _ResidentWebRunner extends ResidentWebRunner {
368368
applicationBinary: null,
369369
);
370370
if (package == null) {
371-
globals.printError('This application is not configured to build on the web.');
372-
globals.printError('To add web support to a project, run `flutter create .`.');
373-
return 1;
371+
globals.printStatus('This application is not configured to build on the web.');
372+
globals.printStatus('To add web support to a project, run `flutter create .`.');
374373
}
375374
if (!globals.fs.isFileSync(mainPath)) {
376375
String message = 'Tried to run $mainPath, but that file does not exist.';
@@ -569,6 +568,7 @@ class _ResidentWebRunner extends ResidentWebRunner {
569568
if (importedEntrypoint == null) {
570569
final String parent = globals.fs.file(mainUri).parent.path;
571570
flutterDevices.first.generator.addFileSystemRoot(parent);
571+
flutterDevices.first.generator.addFileSystemRoot(globals.fs.directory('test').absolute.path);
572572
importedEntrypoint = Uri(
573573
scheme: 'org-dartlang-app',
574574
path: '/' + mainUri.pathSegments.last,

packages/flutter_tools/test/commands.shard/hermetic/build_web_test.dart

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,9 @@ import 'package:flutter_tools/src/cache.dart';
1313
import 'package:flutter_tools/src/commands/build.dart';
1414
import 'package:flutter_tools/src/commands/build_web.dart';
1515
import 'package:flutter_tools/src/dart/pub.dart';
16-
import 'package:flutter_tools/src/device.dart';
1716
import 'package:flutter_tools/src/features.dart';
1817
import 'package:flutter_tools/src/globals.dart' as globals;
1918
import 'package:flutter_tools/src/project.dart';
20-
import 'package:flutter_tools/src/build_runner/resident_web_runner.dart';
2119
import 'package:flutter_tools/src/web/compile.dart';
2220
import 'package:mockito/mockito.dart';
2321

@@ -69,26 +67,6 @@ void main() {
6967
ProcessManager: () => FakeProcessManager.any(),
7068
});
7169

72-
testUsingContext('Refuses to build using runner when missing index.html', () async {
73-
fileSystem.file(fileSystem.path.join('web', 'index.html')).deleteSync();
74-
75-
final ResidentWebRunner runner = DwdsWebRunnerFactory().createWebRunner(
76-
null,
77-
flutterProject: FlutterProject.current(),
78-
ipv6: false,
79-
debuggingOptions: DebuggingOptions.enabled(BuildInfo.debug),
80-
stayResident: true,
81-
urlTunneller: null,
82-
) as ResidentWebRunner;
83-
expect(await runner.run(), 1);
84-
}, overrides: <Type, Generator>{
85-
Platform: () => fakePlatform,
86-
FileSystem: () => fileSystem,
87-
FeatureFlags: () => TestFeatureFlags(isWebEnabled: true),
88-
Pub: () => MockPub(),
89-
ProcessManager: () => FakeProcessManager.any(),
90-
});
91-
9270
testUsingContext('Refuses to build a debug build for web', () async {
9371
final CommandRunner<void> runner = createTestCommandRunner(BuildCommand());
9472

packages/flutter_tools/test/general.shard/resident_web_runner_test.dart

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -227,18 +227,11 @@ void main() {
227227
expect(residentWebRunner.supportsServiceProtocol, true);
228228
}));
229229

230-
test('Exits on run if application does not support the web', () => testbed.run(() async {
231-
fakeVmServiceHost = FakeVmServiceHost(requests: <VmServiceExpectation>[]);
232-
globals.fs.file('pubspec.yaml').createSync();
233-
234-
expect(await residentWebRunner.run(), 1);
235-
expect(testLogger.errorText, contains('This application is not configured to build on the web'));
236-
}));
237-
238230
test('Exits on run if target file does not exist', () => testbed.run(() async {
239231
fakeVmServiceHost = FakeVmServiceHost(requests: <VmServiceExpectation>[]);
240232
globals.fs.file('pubspec.yaml').createSync();
241-
globals.fs.file(globals.fs.path.join('web', 'index.html')).createSync(recursive: true);
233+
globals.fs.file(globals.fs.path.join('web', 'index.html'))
234+
.createSync(recursive: true);
242235

243236
expect(await residentWebRunner.run(), 1);
244237
final String absoluteMain = globals.fs.path.absolute(globals.fs.path.join('lib', 'main.dart'));
@@ -277,6 +270,25 @@ void main() {
277270
)),
278271
}));
279272

273+
test('Can successfully run without an index.html including status warning', () => testbed.run(() async {
274+
fakeVmServiceHost = FakeVmServiceHost(requests: kAttachExpectations.toList());
275+
_setupMocks();
276+
globals.fs.file(globals.fs.path.join('web', 'index.html'))
277+
.deleteSync();
278+
residentWebRunner = DwdsWebRunnerFactory().createWebRunner(
279+
mockFlutterDevice,
280+
flutterProject: FlutterProject.current(),
281+
debuggingOptions: DebuggingOptions.enabled(BuildInfo.debug),
282+
ipv6: true,
283+
stayResident: false,
284+
urlTunneller: null,
285+
) as ResidentWebRunner;
286+
287+
expect(await residentWebRunner.run(), 0);
288+
expect(testLogger.statusText,
289+
contains('This application is not configured to build on the web'));
290+
}));
291+
280292
test('Can successfully run and disconnect with --no-resident', () => testbed.run(() async {
281293
fakeVmServiceHost = FakeVmServiceHost(requests: kAttachExpectations.toList());
282294
_setupMocks();

packages/flutter_tools/test/general.shard/web/devfs_web_test.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,15 @@ void main() {
157157
expect(response.statusCode, HttpStatus.notFound);
158158
}));
159159

160+
test('serves default index.html', () => testbed.run(() async {
161+
final Response response = await webAssetServer
162+
.handleRequest(Request('GET', Uri.parse('http://foobar/')));
163+
164+
expect(response.statusCode, HttpStatus.ok);
165+
expect((await response.read().toList()).first,
166+
containsAllInOrder(utf8.encode('<html>')));
167+
}));
168+
160169
test('handles web server paths without .lib extension', () => testbed.run(() async {
161170
final File source = globals.fs.file('source')
162171
..writeAsStringSync('main() {}');

0 commit comments

Comments
 (0)