Repository: apache/airflow
File: airflow-core/tests/unit/models/test_dagbag.py (Python)

Code before:
        dagbag = DagBag(dag_folder=str(dag_file), bundle_path=tmp_path, include_examples=False)

        # Check import was successful
        assert len(dagbag.dags) == 1
        assert not dagbag.import_errors

        dag = dagbag.get_dag("test_import")
        assert dag is not None
        assert str(tmp_path) in dag.description  # sys.path is enhanced during parse

        assert str(tmp_path) not in sys.path  # bundle path is not preserved in sys.path
        assert sys.path == syspath_before  # sys.path doesn't change

    def test_bundle_path_syspath_is_cleanup_on_failure(self, tmp_path):
        """
        Test that bundle_path is correctly added to and removed from sys.path during DAG import.
        """
        util_file = tmp_path / "bundle_util.py"
        util_file.write_text('def get_message(): return "Hello from bundle!"')

        dag_file = tmp_path / "test_dag.py"
        dag_file.write_text(
            textwrap.dedent(
                """\
                from airflow import DAG
                from airflow.decorators import task
                import bundle_utils # typo

                with DAG('test_import'):
                    @task
                    def mytask():
                        print(util.get_message())
                    mytask()
                """
            )
        )
        syspath_before = deepcopy(sys.path)
        dagbag = DagBag(dag_folder=str(dag_file), bundle_path=tmp_path, include_examples=False)

        assert dagbag.import_errors  # Check import failed
        assert sys.path == syspath_before  # sys.path doesn't change

    def test_bundle_path_none_no_syspath_manipulation(self, tmp_path, caplog):
        """
        Test that no sys.path manipulation occurs when bundle_path is None.
        """
        dag_file = tmp_path / "simple_dag.py"
        dag_file.write_text(
            textwrap.dedent(
                """\
            from datetime import datetime

Review comment (suggestion):
```suggestion
                from airflow.sdk import DAG
```

Code after:
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
from __future__ import annotations

import pytest

pytestmark = pytest.mark.db_test

# This file previously contained tests for DagBag functionality, but those tests
# have been moved to airflow-core/tests/unit/dag_processing/test_dagbag.py to match
# the source code reorganization where DagBag moved from models to dag_processing.
#
# Tests for models-specific functionality (DBDagBag, DagPriorityParsingRequest, etc.)
# would remain in this file, but currently no such tests exist.