Skip to content

Commit c356286

Browse files
committed
prepare to mock
1 parent ffe798d commit c356286

File tree

4 files changed

+82
-9
lines changed

4 files changed

+82
-9
lines changed

packages/flutterfire_cli/lib/src/command_runner.dart

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import 'commands/install.dart';
2323
import 'commands/reconfigure.dart';
2424
import 'commands/update.dart';
2525
import 'commands/upload_symbols.dart';
26+
import 'common/cli_io.dart';
2627
import 'common/utils.dart';
2728
import 'flutter_app.dart';
2829

@@ -59,6 +60,6 @@ class FlutterFireCommandRunner extends CommandRunner<void> {
5960
addCommand(UploadCrashlyticsSymbols(flutterApp));
6061
addCommand(BundleServiceFile(flutterApp));
6162
addCommand(Reconfigure(flutterApp));
62-
addCommand(InstallCommand(flutterApp));
63+
addCommand(InstallCommand(flutterApp, RealCliIO()));
6364
}
6465
}

packages/flutterfire_cli/lib/src/commands/install.dart

+11-8
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import 'dart:io';
2121
import 'package:ansi_styles/ansi_styles.dart';
2222
import 'package:collection/collection.dart';
2323

24+
import '../common/cli_io.dart';
2425
import '../common/utils.dart';
2526
import '../flutter_app.dart';
2627
import 'base.dart';
@@ -62,10 +63,12 @@ enum FlutterFirePlugins {
6263
}
6364

6465
class InstallCommand extends FlutterFireCommand {
65-
InstallCommand(FlutterApp? flutterApp) : super(flutterApp) {
66+
InstallCommand(FlutterApp? flutterApp, this.cliIO) : super(flutterApp) {
6667
setupDefaultFirebaseCliOptions();
6768
}
6869

70+
final CliIO cliIO;
71+
6972
@override
7073
final String name = 'install';
7174

@@ -179,9 +182,9 @@ class InstallCommand extends FlutterFireCommand {
179182
.toList();
180183

181184
if (pluginsToDelete.isNotEmpty) {
182-
stdout.writeln('The following plugins will be removed:');
185+
cliIO.output.writeln('The following plugins will be removed:');
183186
for (final plugin in pluginsToDelete) {
184-
stdout.writeln(' - $plugin');
187+
cliIO.output.writeln(' - $plugin');
185188
}
186189
final removingSpinner = spinner(
187190
(done) {
@@ -193,7 +196,7 @@ class InstallCommand extends FlutterFireCommand {
193196
);
194197

195198
await Process.run(
196-
'flutter.bat',
199+
'dart',
197200
[
198201
'pub',
199202
'remove',
@@ -206,13 +209,13 @@ class InstallCommand extends FlutterFireCommand {
206209
}
207210

208211
if (selectedPlugins.isEmpty) {
209-
stdout.writeln('No plugins selected.');
212+
cliIO.output.writeln('No plugins selected.');
210213
return;
211214
}
212215

213-
stdout.writeln('Installing the following plugins version:');
216+
cliIO.output.writeln('Installing the following plugins version:');
214217
for (final plugin in selectedPlugins) {
215-
stdout.writeln(
218+
cliIO.output.writeln(
216219
' - ${plugin.displayName}: ${pluginVersions[plugin.name]}',
217220
);
218221
}
@@ -310,7 +313,7 @@ class InstallCommand extends FlutterFireCommand {
310313
}
311314
}
312315

313-
stdout.writeln(
316+
cliIO.output.writeln(
314317
AnsiStyles.green(
315318
'Successfully installed BoM version $bomVersion 🚀',
316319
),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import 'dart:async';
2+
import 'dart:convert';
3+
import 'dart:io';
4+
5+
abstract class CliIO {
6+
Stream<String> get input;
7+
IOSink get output;
8+
}
9+
10+
class RealCliIO implements CliIO {
11+
final _input = stdin.transform(utf8.decoder);
12+
final _output = stdout;
13+
14+
@override
15+
Stream<String> get input => _input;
16+
17+
@override
18+
IOSink get output => _output;
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import 'dart:async';
2+
import 'dart:io';
3+
4+
import 'package:path/path.dart' as p;
5+
import 'package:test/test.dart';
6+
7+
import 'test_utils.dart';
8+
9+
void main() {
10+
String? projectPath;
11+
setUp(() async {
12+
projectPath = await createFlutterProject();
13+
});
14+
15+
tearDown(() {
16+
Directory(p.dirname(projectPath!)).delete(recursive: true);
17+
});
18+
19+
test(
20+
'flutterfire install 0.1.0',
21+
() async {
22+
// Prepare mock input stream
23+
final input = Stream.fromIterable([
24+
' ', // Simulate space
25+
'\x1B[A', // Simulate arrow up (using ANSI escape code)
26+
'\x1B[B', // Simulate arrow down
27+
]).map((s) => s.codeUnits).expand((i) => i);
28+
29+
// Redirect stdin
30+
stdin = StreamIterator(input);
31+
32+
// Capture output
33+
final output = StringBuffer();
34+
final originalStdout = stdout; // preserve original stdout
35+
stdout = IOSink(
36+
StreamController<List<int>>()
37+
..stream.transform(systemEncoding.decoder).listen(output.write),
38+
);
39+
40+
// Call your CLI function
41+
await runMyCliApplication(); // This should be your function to run the CLI
42+
43+
// Check expected output or behavior
44+
expect(output.toString(), contains('Expected Output or Behavior'));
45+
46+
// Clean up: Restore original stdout
47+
stdout = originalStdout;
48+
},
49+
);
50+
}

0 commit comments

Comments
 (0)