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

This commit is contained in:
Felix Angelov
2024-12-16 15:32:11 -06:00
committed by GitHub
parent 11cef956b7
commit 3625d0a81c
2 changed files with 35 additions and 19 deletions
@@ -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;
}
}
@@ -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';
@@ -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);
});
});
});