Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(dart_frog_cli): move runtime compatibility check to dev server runner #1138

Merged
merged 4 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 1 addition & 12 deletions packages/dart_frog_cli/lib/src/commands/dev/dev.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import 'package:dart_frog_cli/src/command.dart';
import 'package:dart_frog_cli/src/commands/commands.dart';
import 'package:dart_frog_cli/src/commands/dev/templates/dart_frog_dev_server_bundle.dart';
import 'package:dart_frog_cli/src/dev_server_runner/dev_server_runner.dart';
import 'package:dart_frog_cli/src/runtime_compatibility.dart'
as runtime_compatibility;
import 'package:mason/mason.dart';

/// {@template dev_command}
Expand All @@ -18,11 +16,7 @@ class DevCommand extends DartFrogCommand {
super.logger,
GeneratorBuilder? generator,
DevServerRunnerBuilder? devServerRunnerBuilder,
runtime_compatibility.RuntimeCompatibilityCallback?
ensureRuntimeCompatibility,
}) : _ensureRuntimeCompatibility = ensureRuntimeCompatibility ??
runtime_compatibility.ensureRuntimeCompatibility,
_generator = generator ?? MasonGenerator.fromBundle,
}) : _generator = generator ?? MasonGenerator.fromBundle,
_devServerRunnerBuilder =
devServerRunnerBuilder ?? DevServerRunner.new {
argParser
Expand Down Expand Up @@ -50,9 +44,6 @@ class DevCommand extends DartFrogCommand {

final GeneratorBuilder _generator;
final DevServerRunnerBuilder _devServerRunnerBuilder;
final runtime_compatibility.RuntimeCompatibilityCallback
_ensureRuntimeCompatibility;

@override
final String description = 'Run a local development server.';

Expand Down Expand Up @@ -109,8 +100,6 @@ class DevCommand extends DartFrogCommand {

@override
Future<int> run() async {
_ensureRuntimeCompatibility(cwd);

final port = io.Platform.environment['PORT'] ?? results['port'] as String;

final dartVmServicePort = (results['dart-vm-service-port'] as String?) ??
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:convert';
import 'dart:io' as io;

import 'package:dart_frog_cli/src/dev_server_runner/restorable_directory_generator_target.dart';
import 'package:dart_frog_cli/src/runtime_compatibility.dart';
import 'package:mason/mason.dart';
import 'package:meta/meta.dart';
import 'package:path/path.dart' as path;
Expand Down Expand Up @@ -78,13 +79,17 @@ class DevServerRunner {
@visibleForTesting io.ProcessSignal? sigint,
@visibleForTesting ProcessStart? startProcess,
@visibleForTesting ProcessRun? runProcess,
@visibleForTesting
RuntimeCompatibilityCallback? runtimeCompatibilityCallback,
}) : _directoryWatcher = directoryWatcher ?? DirectoryWatcher.new,
_isWindows = isWindows ?? io.Platform.isWindows,
_sigint = sigint ?? io.ProcessSignal.sigint,
_startProcess = startProcess ?? io.Process.start,
_runProcess = runProcess ?? io.Process.run,
_generatorTarget =
generatorTarget ?? RestorableDirectoryGeneratorTarget.new,
_ensureRuntimeCompatibility =
runtimeCompatibilityCallback ?? ensureRuntimeCompatibility,
assert(port.isNotEmpty, 'port cannot be empty'),
assert(
dartVmServicePort.isNotEmpty,
Expand Down Expand Up @@ -121,6 +126,7 @@ class DevServerRunner {
final RestorableDirectoryGeneratorTargetBuilder _generatorTarget;
final bool _isWindows;
final io.ProcessSignal _sigint;
final RuntimeCompatibilityCallback _ensureRuntimeCompatibility;

late final _generatedDirectory = io.Directory(
path.join(workingDirectory.path, '.dart_frog'),
Expand Down Expand Up @@ -241,6 +247,8 @@ class DevServerRunner {
);
}

_ensureRuntimeCompatibility(workingDirectory);

Future<void> serve() async {
var isHotReloadingEnabled = false;
final enableVmServiceFlag = '--enable-vm-service=$dartVmServicePort';
Expand Down
34 changes: 0 additions & 34 deletions packages/dart_frog_cli/test/src/commands/dev/dev_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import 'dart:io';
import 'package:args/args.dart';
import 'package:dart_frog_cli/src/commands/commands.dart';
import 'package:dart_frog_cli/src/dev_server_runner/dev_server_runner.dart';
import 'package:dart_frog_cli/src/runtime_compatibility.dart';
import 'package:mason/mason.dart';
import 'package:mocktail/mocktail.dart';
import 'package:test/test.dart';
Expand Down Expand Up @@ -45,34 +44,6 @@ void main() {
expect(command, isNotNull);
});

test('throws if ensureRuntimeCompatibility fails', () {
final command = DevCommand(
generator: (_) async => generator,
ensureRuntimeCompatibility: (_) {
throw const DartFrogCompatibilityException('oops');
},
devServerRunnerBuilder: ({
required logger,
required port,
required address,
required devServerBundleGenerator,
required dartVmServicePort,
required workingDirectory,
void Function()? onHotReloadEnabled,
}) {
return runner;
},
logger: logger,
)
..testArgResults = argResults
..testStdin = stdin;

expect(
command.run(),
throwsA(isA<DartFrogCompatibilityException>()),
);
});

test('run the dev server with the given parameters', () async {
when(() => runner.start()).thenAnswer((_) => Future.value());
when(() => runner.exitCode).thenAnswer(
Expand All @@ -94,7 +65,6 @@ void main() {

final command = DevCommand(
generator: (_) async => generator,
ensureRuntimeCompatibility: (_) {},
devServerRunnerBuilder: ({
required logger,
required port,
Expand Down Expand Up @@ -133,7 +103,6 @@ void main() {
test('results with dev server exit code', () async {
final command = DevCommand(
generator: (_) async => generator,
ensureRuntimeCompatibility: (_) {},
devServerRunnerBuilder: ({
required logger,
required port,
Expand Down Expand Up @@ -164,7 +133,6 @@ void main() {
test('fails if dev server runner fails on start', () async {
final command = DevCommand(
generator: (_) async => generator,
ensureRuntimeCompatibility: (_) {},
devServerRunnerBuilder: ({
required logger,
required port,
Expand Down Expand Up @@ -203,7 +171,6 @@ void main() {

final command = DevCommand(
generator: (_) async => generator,
ensureRuntimeCompatibility: (_) {},
devServerRunnerBuilder: ({
required logger,
required port,
Expand Down Expand Up @@ -275,7 +242,6 @@ void main() {

command = DevCommand(
generator: (_) async => generator,
ensureRuntimeCompatibility: (_) {},
devServerRunnerBuilder: ({
required logger,
required port,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'dart:io' hide exitCode;

import 'package:dart_frog_cli/src/dev_server_runner/dev_server_runner.dart';
import 'package:dart_frog_cli/src/dev_server_runner/restorable_directory_generator_target.dart';
import 'package:dart_frog_cli/src/runtime_compatibility.dart';
import 'package:mason/mason.dart';
import 'package:mocktail/mocktail.dart';
import 'package:path/path.dart' as path;
Expand Down Expand Up @@ -83,6 +84,7 @@ void main() {
startProcess: (_, __, {runInShell = false}) async => process,
sigint: sigint,
runProcess: (_, __) async => processResult,
runtimeCompatibilityCallback: (_) {},
);

when(() => process.stdout).thenAnswer((_) => const Stream.empty());
Expand Down Expand Up @@ -156,6 +158,37 @@ void main() {
}).called(1);
});

test('throws if ensureRuntimeCompatibility fails', () async {
devServerRunner = DevServerRunner(
logger: logger,
port: port,
address: null,
devServerBundleGenerator: generator,
dartVmServicePort: dartVmServicePort,
workingDirectory: Directory.current,
directoryWatcher: (_) => directoryWatcher,
generatorTarget: (_, {createFile, logger}) => generatorTarget,
isWindows: isWindows,
startProcess: (_, __, {runInShell = false}) async => process,
sigint: sigint,
runProcess: (_, __) async => processResult,
runtimeCompatibilityCallback: (_) {
throw const DartFrogCompatibilityException('oops');
},
);

await expectLater(
devServerRunner.start(),
throwsA(
isA<DartFrogCompatibilityException>().having(
(e) => e.message,
'message',
'oops',
),
),
);
});

test('throws when server process is already running', () async {
await expectLater(devServerRunner.start(), completes);

Expand Down Expand Up @@ -211,6 +244,7 @@ void main() {
},
sigint: sigint,
runProcess: (_, __) async => processResult,
runtimeCompatibilityCallback: (_) => true,
);

await expectLater(devServerRunner.start(), completes);
Expand Down Expand Up @@ -261,6 +295,7 @@ void main() {
},
sigint: sigint,
runProcess: (_, __) async => processResult,
runtimeCompatibilityCallback: (_) => true,
);

await expectLater(devServerRunner.start(), completes);
Expand Down Expand Up @@ -318,6 +353,7 @@ void main() {
processRunCalls.add([executable, ...arguments]);
return processResult;
},
runtimeCompatibilityCallback: (_) => true,
);
await expectLater(devServerRunner.start(), completes);

Expand Down Expand Up @@ -480,6 +516,7 @@ void main() {
},
sigint: sigint,
runProcess: (_, __) async => processResult,
runtimeCompatibilityCallback: (_) => true,
);
await expectLater(devServerRunner.start(), completes);

Expand Down Expand Up @@ -802,6 +839,7 @@ runs codegen with debounce when changes are made to the public or routes directo
processRunCalls.add([executable, ...arguments]);
return processResult;
},
runtimeCompatibilityCallback: (_) => true,
);

await expectLater(devServerRunner.start(), completes);
Expand Down Expand Up @@ -903,6 +941,7 @@ runs codegen with debounce when changes are made to the public or routes directo
processRunCalls.add([executable, ...arguments]);
return processResult;
},
runtimeCompatibilityCallback: (_) => true,
);

await expectLater(devServerRunner.start(), completes);
Expand Down Expand Up @@ -940,6 +979,7 @@ lib/my_model.g.dart:53:20: Warning: Operand of null-aware operation '!' has type
directoryWatcher: (_) => directoryWatcher,
startProcess: (_, __, {runInShell = false}) async => process,
sigint: sigint,
runtimeCompatibilityCallback: (_) => true,
);

await expectLater(devServerRunner.start(), completes);
Expand Down Expand Up @@ -986,6 +1026,7 @@ Could not start the VM service: localhost:8181 is already in use.''';
processRunCalls.add([executable, ...arguments]);
return processResult;
},
runtimeCompatibilityCallback: (_) => true,
);

await expectLater(devServerRunner.start(), completes);
Expand Down