@@ -11,11 +11,14 @@ import 'package:flutter_tools/src/base/common.dart';
1111import 'package:flutter_tools/src/base/error_handling_io.dart' ;
1212import 'package:flutter_tools/src/base/file_system.dart' ;
1313import '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' ;
1416import 'package:flutter_tools/src/base/signals.dart' ;
1517import 'package:flutter_tools/src/base/time.dart' ;
1618import 'package:flutter_tools/src/base/user_messages.dart' ;
1719import 'package:flutter_tools/src/build_info.dart' ;
1820import 'package:flutter_tools/src/cache.dart' ;
21+ import 'package:flutter_tools/src/commands/run.dart' ;
1922import 'package:flutter_tools/src/dart/pub.dart' ;
2023import 'package:flutter_tools/src/device.dart' ;
2124import 'package:flutter_tools/src/globals.dart' as globals;
@@ -700,6 +703,67 @@ void main() {
700703 expect (testLogger.statusText, contains (UserMessages ().flutterSpecifyDevice));
701704 });
702705 });
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+ });
703767 });
704768}
705769
@@ -853,3 +917,22 @@ class FakePub extends Fake implements Pub {
853917 PubOutputMode outputMode = PubOutputMode .all,
854918 }) async { }
855919}
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