@@ -101,49 +101,64 @@ def test_dev_arg(self, args, expected_command):
101101 close_fds = True ,
102102 )
103103
104- def test_apps_env_var_set_unset (self ):
104+ @pytest .mark .parametrize (
105+ "args" ,
106+ [
107+ (["api-server" ]),
108+ (["api-server" , "--apps" , "all" ]),
109+ (["api-server" , "--apps" , "core,execution" ]),
110+ (["api-server" , "--apps" , "core" ]),
111+ (["api-server" , "--apps" , "execution" ]),
112+ ],
113+ ids = [
114+ "default_apps" ,
115+ "all_apps_explicit" ,
116+ "multiple_apps_explicit" ,
117+ "single_app_core" ,
118+ "single_app_execution" ,
119+ ],
120+ )
121+ @pytest .mark .parametrize ("dev_mode" , [True , False ])
122+ @pytest .mark .parametrize (
123+ "original_env" ,
124+ [None , "some_value" ],
125+ )
126+ def test_api_apps_env (self , args , dev_mode , original_env ):
105127 """
106128 Test that AIRFLOW_API_APPS is set and unset in the environment when
107129 calling the airflow api-server command
108130 """
131+ expected_setitem_calls = []
132+
133+ if dev_mode :
134+ args .append ("--dev" )
135+
109136 with (
110- mock .patch ("subprocess.Popen" ) as Popen ,
111137 mock .patch ("os.environ" , autospec = True ) as mock_environ ,
138+ mock .patch ("uvicorn.run" ),
139+ mock .patch ("subprocess.Popen" ),
112140 ):
113- apps_value = "core,execution"
114- port = "9092"
115- host = "somehost"
141+ # Mock the environment variable with initial value or None
142+ mock_environ .get .return_value = original_env
116143
117- # Parse the command line arguments
118- args = self .parser .parse_args (
119- ["api-server" , "--port" , port , "--host" , host , "--apps" , apps_value , "--dev" ]
120- )
121-
122- # Ensure AIRFLOW_API_APPS is not set initially
123- mock_environ .get .return_value = None
144+ # Parse the command line arguments and call the api_server command
145+ parsed_args = self .parser .parse_args (args )
146+ api_server_command .api_server (parsed_args )
124147
125- # Call the fastapi_api command
126- api_server_command . api_server ( args )
127-
128- # Assert that AIRFLOW_API_APPS was set in the environment before subprocess
129- mock_environ . __setitem__ . assert_called_with ( "AIRFLOW_API_APPS" , apps_value )
148+ # Verify the AIRFLOW_API_APPS was set correctly
149+ if "--apps" in args :
150+ expected_setitem_calls . append (
151+ mock . call ( api_server_command . AIRFLOW_API_APPS , parsed_args . apps )
152+ )
130153
131- # Simulate subprocess execution
132- Popen .assert_called_with (
133- [
134- "fastapi" ,
135- "dev" ,
136- "airflow-core/src/airflow/api_fastapi/main.py" ,
137- "--port" ,
138- port ,
139- "--host" ,
140- host ,
141- ],
142- close_fds = True ,
143- )
154+ # Verify AIRFLOW_API_APPS was cleaned up
155+ if original_env is not None :
156+ expected_setitem_calls .append (mock .call (api_server_command .AIRFLOW_API_APPS , original_env ))
157+ else :
158+ mock_environ .pop .assert_called_with (api_server_command .AIRFLOW_API_APPS , None )
144159
145- # Assert that AIRFLOW_API_APPS was unset after subprocess
146- mock_environ .pop . assert_called_with ( "AIRFLOW_API_APPS" )
160+ # Verify that the environment variable was set and cleaned up correctly
161+ mock_environ .__setitem__ . assert_has_calls ( expected_setitem_calls )
147162
148163 @pytest .mark .parametrize (
149164 "cli_args, expected_additional_kwargs" ,
0 commit comments