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

feat(shorebird_cli): improve shorebird preview logs on MacOS #2690

Merged
merged 1 commit into from
Dec 16, 2024
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
27 changes: 13 additions & 14 deletions packages/shorebird_cli/lib/src/executables/open.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,22 @@ class Open {
/// Opens a new application at the provided [path] and streams the stdout and
/// stderr.
Future<Stream<List<int>>> newApplication({required String path}) async {
final tmp = Directory.systemTemp.createTempSync();
final stdout = File(p.join(tmp.path, 'stdout.log'))..createSync();
await process.start(
'open',
final app = Directory(p.join(path, 'Contents', 'MacOS'))
.listSync()
.firstWhere((f) => f is File);

await process.start('open', ['-n', path]);

final logStreamProcess = await process.start(
'log',
[
'-n',
path,
'--stdout=${stdout.path}',
'--stderr=${stdout.path}',
'stream',
'--style=compact',
'--process',
p.basenameWithoutExtension(app.path),
],
);

final stdoutProcess = await process.start(
'tail',
['-fn+1', stdout.path],
);

return stdoutProcess.stdout;
return logStreamProcess.stdout;
}
}
27 changes: 22 additions & 5 deletions packages/shorebird_cli/test/src/executables/open_test.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import 'dart:convert';
import 'dart:io';

import 'package:mocktail/mocktail.dart';
import 'package:path/path.dart' as p;
import 'package:scoped_deps/scoped_deps.dart';
import 'package:shorebird_cli/src/executables/executables.dart';
import 'package:shorebird_cli/src/shorebird_process.dart';
Expand Down Expand Up @@ -28,26 +30,41 @@ void main() {
});

group('newApplication', () {
late Directory workingDirectory;

setUp(() {
workingDirectory = Directory.systemTemp.createTempSync();
File(p.join(workingDirectory.path, 'Contents', 'MacOS', 'test'))
.createSync(recursive: true);
});

test('executes correct command and streams logs', () async {
final openProcess = MockProcess();
final tailProcess = MockProcess();
final logProcess = MockProcess();

when(() => process.start('open', any())).thenAnswer((_) async {
return openProcess;
});
when(() => process.start('tail', any())).thenAnswer((_) async {
return tailProcess;
when(() => process.start('log', any())).thenAnswer((_) async {
return logProcess;
});

when(() => tailProcess.stdout).thenAnswer(
when(() => logProcess.stdout).thenAnswer(
(_) => Stream.fromIterable([utf8.encode('hello world') as List<int>]),
);

final stream = await runWithOverrides(
() => open.newApplication(path: 'test'),
() => open.newApplication(path: workingDirectory.path),
);

expect(stream, emits(utf8.encode('hello world')));
verify(() => process.start('open', ['-n', workingDirectory.path]));
verify(
() => process.start(
'log',
['stream', '--style=compact', '--process', 'test'],
),
).called(1);
});
});
});
Expand Down
Loading