@@ -11,11 +11,14 @@ import 'package:flutter_tools/src/base/common.dart';
11
11
import 'package:flutter_tools/src/base/error_handling_io.dart' ;
12
12
import 'package:flutter_tools/src/base/file_system.dart' ;
13
13
import 'package:flutter_tools/src/base/io.dart' ;
14
+ import 'package:flutter_tools/src/base/logger.dart' ;
15
+ import 'package:flutter_tools/src/base/platform.dart' ;
14
16
import 'package:flutter_tools/src/base/signals.dart' ;
15
17
import 'package:flutter_tools/src/base/time.dart' ;
16
18
import 'package:flutter_tools/src/base/user_messages.dart' ;
17
19
import 'package:flutter_tools/src/build_info.dart' ;
18
20
import 'package:flutter_tools/src/cache.dart' ;
21
+ import 'package:flutter_tools/src/commands/run.dart' ;
19
22
import 'package:flutter_tools/src/dart/pub.dart' ;
20
23
import 'package:flutter_tools/src/device.dart' ;
21
24
import 'package:flutter_tools/src/globals.dart' as globals;
@@ -700,6 +703,67 @@ void main() {
700
703
expect (testLogger.statusText, contains (UserMessages ().flutterSpecifyDevice));
701
704
});
702
705
});
706
+
707
+ group ('--flavor' , () {
708
+ late _TestDeviceManager testDeviceManager;
709
+ late Logger logger;
710
+ late FileSystem fileSystem;
711
+
712
+ setUp (() {
713
+ logger = BufferLogger .test ();
714
+ testDeviceManager = _TestDeviceManager (logger: logger);
715
+ fileSystem = MemoryFileSystem .test ();
716
+ });
717
+
718
+ testUsingContext ("tool exits when FLUTTER_APP_FLAVOR is already set in user's environment" , () async {
719
+ fileSystem.file ('lib/main.dart' ).createSync (recursive: true );
720
+ fileSystem.file ('pubspec.yaml' ).createSync ();
721
+ fileSystem.file ('.packages' ).createSync ();
722
+
723
+ final FakeDevice device = FakeDevice ('name' , 'id' );
724
+ testDeviceManager.devices = < Device > [device];
725
+ final _TestRunCommandThatOnlyValidates command = _TestRunCommandThatOnlyValidates ();
726
+ final CommandRunner <void > runner = createTestCommandRunner (command);
727
+
728
+ expect (runner.run (< String > ['run' , '--no-pub' , '--no-hot' , '--flavor=strawberry' ]),
729
+ throwsToolExit (message: 'FLUTTER_APP_FLAVOR is used by the framework and cannot be set in the environment.' ));
730
+
731
+ }, overrides: < Type , Generator > {
732
+ DeviceManager : () => testDeviceManager,
733
+ Platform : () => FakePlatform (
734
+ environment: < String , String > {
735
+ 'FLUTTER_APP_FLAVOR' : 'I was already set'
736
+ }
737
+ ),
738
+ Cache : () => Cache .test (processManager: FakeProcessManager .any ()),
739
+ FileSystem : () => fileSystem,
740
+ ProcessManager : () => FakeProcessManager .any (),
741
+ });
742
+
743
+ testUsingContext ('tool exits when FLUTTER_APP_FLAVOR is set in --dart-define or --dart-define-from-file' , () async {
744
+ fileSystem.file ('lib/main.dart' ).createSync (recursive: true );
745
+ fileSystem.file ('pubspec.yaml' ).createSync ();
746
+ fileSystem.file ('.packages' ).createSync ();
747
+ fileSystem.file ('config.json' )..createSync ()..writeAsStringSync ('{"FLUTTER_APP_FLAVOR": "strawberry"}' );
748
+
749
+ final FakeDevice device = FakeDevice ('name' , 'id' );
750
+ testDeviceManager.devices = < Device > [device];
751
+ final _TestRunCommandThatOnlyValidates command = _TestRunCommandThatOnlyValidates ();
752
+ final CommandRunner <void > runner = createTestCommandRunner (command);
753
+
754
+ expect (runner.run (< String > ['run' , '--dart-define=FLUTTER_APP_FLAVOR=strawberry' , '--no-pub' , '--no-hot' , '--flavor=strawberry' ]),
755
+ throwsToolExit (message: 'FLUTTER_APP_FLAVOR is used by the framework and cannot be set using --dart-define or --dart-define-from-file' ));
756
+
757
+ expect (runner.run (< String > ['run' , '--dart-define-from-file=config.json' , '--no-pub' , '--no-hot' , '--flavor=strawberry' ]),
758
+ throwsToolExit (message: 'FLUTTER_APP_FLAVOR is used by the framework and cannot be set using --dart-define or --dart-define-from-file' ));
759
+ }, overrides: < Type , Generator > {
760
+ DeviceManager : () => testDeviceManager,
761
+ Platform : () => FakePlatform (),
762
+ Cache : () => Cache .test (processManager: FakeProcessManager .any ()),
763
+ FileSystem : () => fileSystem,
764
+ ProcessManager : () => FakeProcessManager .any (),
765
+ });
766
+ });
703
767
});
704
768
}
705
769
@@ -853,3 +917,22 @@ class FakePub extends Fake implements Pub {
853
917
PubOutputMode outputMode = PubOutputMode .all,
854
918
}) async { }
855
919
}
920
+
921
+ class _TestDeviceManager extends DeviceManager {
922
+ _TestDeviceManager ({required super .logger});
923
+ List <Device > devices = < Device > [];
924
+
925
+ @override
926
+ List <DeviceDiscovery > get deviceDiscoverers {
927
+ final FakePollingDeviceDiscovery discoverer = FakePollingDeviceDiscovery ();
928
+ devices.forEach (discoverer.addDevice);
929
+ return < DeviceDiscovery > [discoverer];
930
+ }
931
+ }
932
+
933
+ class _TestRunCommandThatOnlyValidates extends RunCommand {
934
+ @override
935
+ Future <FlutterCommandResult > runCommand () async {
936
+ return FlutterCommandResult .success ();
937
+ }
938
+ }
0 commit comments