Skip to content

Commit c8dae6f

Browse files
Add offline mode
1 parent 302b6db commit c8dae6f

File tree

6 files changed

+166
-6
lines changed

6 files changed

+166
-6
lines changed

webdev/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## 3.7.2-wip
22

3+
- Adds `--offline` flag [#2483](https://github.com/dart-lang/webdev/pull/2483)
4+
35
## 3.7.1
46

57
- Update `dwds` constraint to `24.3.5`.

webdev/lib/src/command/configuration.dart

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const nullSafetyAuto = 'auto';
3737
const disableDdsFlag = 'disable-dds';
3838
const enableExperimentOption = 'enable-experiment';
3939
const canaryFeaturesFlag = 'canary';
40+
const offlineFlag = 'offline';
4041

4142
ReloadConfiguration _parseReloadConfiguration(ArgResults argResults) {
4243
var auto = argResults.options.contains(autoOption)
@@ -107,6 +108,7 @@ class Configuration {
107108
final String? _nullSafety;
108109
final List<String>? _experiments;
109110
final bool? _canaryFeatures;
111+
final bool? _offline;
110112

111113
Configuration({
112114
bool? autoRun,
@@ -133,6 +135,7 @@ class Configuration {
133135
String? nullSafety,
134136
List<String>? experiments,
135137
bool? canaryFeatures,
138+
bool? offline,
136139
}) : _autoRun = autoRun,
137140
_chromeDebugPort = chromeDebugPort,
138141
_debugExtension = debugExtension,
@@ -154,7 +157,8 @@ class Configuration {
154157
_verbose = verbose,
155158
_nullSafety = nullSafety,
156159
_experiments = experiments,
157-
_canaryFeatures = canaryFeatures {
160+
_canaryFeatures = canaryFeatures,
161+
_offline = offline {
158162
_validateConfiguration();
159163
}
160164

@@ -229,7 +233,8 @@ class Configuration {
229233
verbose: other._verbose ?? _verbose,
230234
nullSafety: other._nullSafety ?? _nullSafety,
231235
experiments: other._experiments ?? _experiments,
232-
canaryFeatures: other._canaryFeatures ?? _canaryFeatures);
236+
canaryFeatures: other._canaryFeatures ?? _canaryFeatures,
237+
offline: other._offline ?? _offline);
233238

234239
factory Configuration.noInjectedClientDefaults() =>
235240
Configuration(autoRun: false, debug: false, debugExtension: false);
@@ -284,6 +289,8 @@ class Configuration {
284289

285290
bool get canaryFeatures => _canaryFeatures ?? false;
286291

292+
bool get offline => _offline ?? false;
293+
287294
/// Returns a new configuration with values updated from the parsed args.
288295
static Configuration fromArgs(ArgResults? argResults,
289296
{Configuration? defaultConfiguration}) {
@@ -408,6 +415,10 @@ class Configuration {
408415
? argResults[canaryFeaturesFlag] as bool?
409416
: defaultConfiguration.canaryFeatures;
410417

418+
final offline = argResults.options.contains(offlineFlag)
419+
? argResults[offlineFlag] as bool?
420+
: defaultConfiguration.verbose;
421+
411422
return Configuration(
412423
autoRun: defaultConfiguration.autoRun,
413424
chromeDebugPort: chromeDebugPort,
@@ -433,6 +444,7 @@ class Configuration {
433444
nullSafety: nullSafety,
434445
experiments: experiments,
435446
canaryFeatures: canaryFeatures,
447+
offline: offline,
436448
);
437449
}
438450
}

webdev/lib/src/command/shared.dart

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,11 @@ void addSharedArgs(ArgParser argParser,
7474
abbr: 'v',
7575
defaultsTo: false,
7676
negatable: false,
77-
help: 'Enables verbose logging.');
77+
help: 'Enables verbose logging.')
78+
..addFlag(offlineFlag,
79+
defaultsTo: false,
80+
negatable: false,
81+
help: 'Disable fetching from pub.dev.');
7882
}
7983

8084
/// Parses the provided [Configuration] to return a list of
@@ -103,7 +107,7 @@ List<String> buildRunnerArgs(Configuration configuration) {
103107
}
104108

105109
Future<void> validatePubspecLock(Configuration configuration) async {
106-
final pubspecLock = await PubspecLock.read();
110+
final pubspecLock = await PubspecLock.read(offline: configuration.offline);
107111
await checkPubspecLock(pubspecLock,
108112
requireBuildWebCompilers: configuration.requireBuildWebCompilers);
109113
}

webdev/lib/src/pubspec.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,10 @@ class PubspecLock {
6868

6969
PubspecLock(this._packages);
7070

71-
static Future<PubspecLock> read() async {
72-
await _runPubDeps();
71+
static Future<PubspecLock> read({bool offline = false}) async {
72+
if (!offline) {
73+
await _runPubDeps();
74+
}
7375
var dir = p.absolute(p.current);
7476
while (true) {
7577
final candidate = p.join(

webdev/test/installation_test.dart

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ void main() {
3030
Process? serveProcess;
3131
Directory? tempDir0;
3232

33+
final testScript =
34+
File(p.join(p.dirname(Platform.script.toFilePath()), 'test.dart'))
35+
.readAsStringSync();
36+
final thisScript = File.fromUri(Uri.parse(testScript.substring(
37+
testScript.lastIndexOf('import', testScript.indexOf('as test;')) + 8,
38+
testScript.indexOf('as test;') - 2)));
39+
final packageDir = p.dirname(p.dirname(thisScript.path));
40+
3341
Future<void> expectStdoutAndCleanExit(Process process,
3442
{required String expectedStdout}) async {
3543
final stdoutCompleter = _captureOutput(
@@ -141,6 +149,41 @@ void main() {
141149
await expectStdoutThenExit(serveProcess!,
142150
expectedStdout: 'Serving `web` on');
143151
});
152+
153+
test('activate and serve webdev fails with offline', () async {
154+
final tempDir = tempDir0!;
155+
final tempPath = tempDir.path;
156+
157+
// Verify that we can create a new Dart app:
158+
createProcess = await Process.start(
159+
'dart',
160+
['create', '--no-pub', '--template', 'web', 'temp_app'],
161+
workingDirectory: tempPath,
162+
);
163+
await expectStdoutAndCleanExit(
164+
createProcess!,
165+
expectedStdout: 'Created project temp_app in temp_app!',
166+
);
167+
final appPath = p.join(tempPath, 'temp_app');
168+
expect(await Directory(appPath).exists(), isTrue);
169+
170+
// Verify that `dart pub global activate` works:
171+
activateProcess = await Process.start(
172+
'dart',
173+
['pub', 'global', 'activate', '--source', 'path', packageDir],
174+
);
175+
await expectStdoutAndCleanExit(
176+
activateProcess!,
177+
expectedStdout: 'Activated webdev',
178+
);
179+
180+
// Verify that `webdev serve` works for our new app:
181+
serveProcess = await Process.start('dart',
182+
['pub', 'global', 'run', 'webdev', 'serve', '--offline', 'web:8081'],
183+
workingDirectory: appPath);
184+
await expectStdoutThenExit(serveProcess!,
185+
expectedStdout: 'Cannot open file\n pubspec.lock\n');
186+
});
144187
}
145188

146189
Future<int> _waitForExitOrTimeout(Process process) {

webdev/test/integration_test.dart

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,62 @@
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+
@Timeout(Duration(minutes: 3))
6+
57
import 'dart:async';
8+
import 'dart:convert';
9+
import 'dart:io';
610

11+
import 'package:path/path.dart' as p;
712
import 'package:test/test.dart';
813
import 'package:test_descriptor/test_descriptor.dart' as d;
914

1015
import 'test_utils.dart';
1116

17+
enum StreamType {
18+
stdout,
19+
stderr,
20+
}
21+
22+
const processTimeout = Duration(minutes: 1);
23+
1224
void main() {
1325
final testRunner = TestRunner();
1426
setUpAll(testRunner.setUpAll);
1527
tearDownAll(testRunner.tearDownAll);
1628

29+
Future<void> expectStdoutAndCleanExit(Process process,
30+
{required String expectedStdout}) async {
31+
final stdoutCompleter = _captureOutput(
32+
process,
33+
streamType: StreamType.stdout,
34+
stopCaptureFuture: process.exitCode,
35+
);
36+
final stderrCompleter = _captureOutput(
37+
process,
38+
streamType: StreamType.stderr,
39+
stopCaptureFuture: process.exitCode,
40+
);
41+
final exitCode = await _waitForExitOrTimeout(process);
42+
final stderrLogs = await stderrCompleter.future;
43+
final stdoutLogs = await stdoutCompleter.future;
44+
expect(
45+
exitCode,
46+
equals(0),
47+
// Include the stderr and stdout logs if the process does not terminate
48+
// cleanly:
49+
reason: 'stderr: $stderrLogs, stdout: $stdoutLogs',
50+
);
51+
expect(
52+
stderrLogs,
53+
isEmpty,
54+
);
55+
expect(
56+
stdoutLogs,
57+
contains(expectedStdout),
58+
);
59+
}
60+
1761
test('non-existent commands create errors', () async {
1862
final process = await testRunner.runWebDev(['monkey']);
1963

@@ -214,6 +258,26 @@ dependencies:
214258
await checkProcessStdout(process, ['webdev could not run']);
215259
await process.shouldExit(78);
216260
});
261+
262+
if (command != 'daemon') {
263+
test('failure with offline and unresolved dependencies', () async {
264+
final createProcess = await Process.start(
265+
'dart',
266+
['create', '--no-pub', '--template', 'web', 'temp_app'],
267+
workingDirectory: d.sandbox,
268+
);
269+
await expectStdoutAndCleanExit(createProcess,
270+
expectedStdout: 'Created project temp_app');
271+
272+
final appPath = p.join(d.sandbox, 'temp_app');
273+
274+
final process = await testRunner
275+
.runWebDev([command, '--offline'], workingDirectory: appPath);
276+
277+
await checkProcessStdout(process, ['webdev could not run']);
278+
await process.shouldExit(78);
279+
});
280+
}
217281
});
218282
}
219283
}
@@ -286,3 +350,36 @@ packages:
286350

287351
return buffer.toString();
288352
}
353+
354+
Future<int> _waitForExitOrTimeout(Process process) {
355+
Timer(processTimeout, () {
356+
process.kill(ProcessSignal.sigint);
357+
});
358+
return process.exitCode;
359+
}
360+
361+
Completer<String> _captureOutput(
362+
Process process, {
363+
required StreamType streamType,
364+
required Future stopCaptureFuture,
365+
}) {
366+
final stream =
367+
streamType == StreamType.stdout ? process.stdout : process.stderr;
368+
final completer = Completer<String>();
369+
var output = '';
370+
stream.transform(utf8.decoder).listen((line) {
371+
output += line;
372+
if (line.contains('[SEVERE]')) {
373+
process.kill(ProcessSignal.sigint);
374+
if (!completer.isCompleted) {
375+
completer.complete(output);
376+
}
377+
}
378+
});
379+
unawaited(stopCaptureFuture.then((_) {
380+
if (!completer.isCompleted) {
381+
completer.complete(output);
382+
}
383+
}));
384+
return completer;
385+
}

0 commit comments

Comments
 (0)