From fa5fbf8e84f572abe9aaf7e238214770573bae5e Mon Sep 17 00:00:00 2001 From: Martynov Maxim Date: Thu, 21 Dec 2023 21:05:42 +0300 Subject: [PATCH] Fix AirflowSkipException message raised by BashOperator --- airflow/operators/bash.py | 6 +++--- tests/operators/test_bash.py | 22 +++++++++++++++------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/airflow/operators/bash.py b/airflow/operators/bash.py index d913b2b282cbc..ff8edcba51aa7 100644 --- a/airflow/operators/bash.py +++ b/airflow/operators/bash.py @@ -160,7 +160,7 @@ def __init__( skip_on_exit_code if isinstance(skip_on_exit_code, Container) else [skip_on_exit_code] - if skip_on_exit_code + if skip_on_exit_code is not None else [] ) self.cwd = cwd @@ -235,8 +235,8 @@ def execute(self, context: Context): output_encoding=self.output_encoding, cwd=self.cwd, ) - if self.skip_on_exit_code is not None and result.exit_code in self.skip_on_exit_code: - raise AirflowSkipException(f"Bash command returned exit code {self.skip_on_exit_code}. Skipping.") + if result.exit_code in self.skip_on_exit_code: + raise AirflowSkipException(f"Bash command returned exit code {result.exit_code}. Skipping.") elif result.exit_code != 0: raise AirflowException( f"Bash command failed. The command returned a non-zero exit code {result.exit_code}." diff --git a/tests/operators/test_bash.py b/tests/operators/test_bash.py index fb2e4146bdc3a..49030817473f7 100644 --- a/tests/operators/test_bash.py +++ b/tests/operators/test_bash.py @@ -188,15 +188,23 @@ def test_valid_cwd(self, context, tmp_path): @pytest.mark.parametrize( "extra_kwargs,actual_exit_code,expected_exc", [ - (None, 99, AirflowSkipException), - ({"skip_on_exit_code": 100}, 100, AirflowSkipException), - ({"skip_on_exit_code": 100}, 101, AirflowException), + ({}, 0, None), + ({}, 100, AirflowException), + ({}, 99, AirflowSkipException), + ({"skip_on_exit_code": None}, 0, None), + ({"skip_on_exit_code": None}, 100, AirflowException), ({"skip_on_exit_code": None}, 99, AirflowException), + ({"skip_on_exit_code": 100}, 0, None), + ({"skip_on_exit_code": 100}, 100, AirflowSkipException), + ({"skip_on_exit_code": 100}, 99, AirflowException), + ({"skip_on_exit_code": 0}, 0, AirflowSkipException), + ({"skip_on_exit_code": [100]}, 0, None), ({"skip_on_exit_code": [100]}, 100, AirflowSkipException), - ({"skip_on_exit_code": (100, 101)}, 100, AirflowSkipException), - ({"skip_on_exit_code": 100}, 101, AirflowException), - ({"skip_on_exit_code": [100, 102]}, 101, AirflowException), - ({"skip_on_exit_code": None}, 0, None), + ({"skip_on_exit_code": [100]}, 99, AirflowException), + ({"skip_on_exit_code": [100, 102]}, 99, AirflowException), + ({"skip_on_exit_code": (100,)}, 0, None), + ({"skip_on_exit_code": (100,)}, 100, AirflowSkipException), + ({"skip_on_exit_code": (100,)}, 99, AirflowException), ], ) def test_skip(self, extra_kwargs, actual_exit_code, expected_exc, context):