This is an automated email from the ASF dual-hosted git repository.

potiuk pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/main by this push:
     new 02af225e7b Refactor: Use tmp_path in tests/plugins (#33738)
02af225e7b is described below

commit 02af225e7b75552e074d7dfcfc1af5336c42b84d
Author: Miroslav Šedivý <[email protected]>
AuthorDate: Sun Aug 27 16:54:55 2023 +0000

    Refactor: Use tmp_path in tests/plugins (#33738)
---
 tests/plugins/test_plugin_ignore.py   | 103 +++++++++++++++-------------------
 tests/plugins/test_plugins_manager.py |  11 ++--
 2 files changed, 48 insertions(+), 66 deletions(-)

diff --git a/tests/plugins/test_plugin_ignore.py 
b/tests/plugins/test_plugin_ignore.py
index 651b2b41a5..d995fabd08 100644
--- a/tests/plugins/test_plugin_ignore.py
+++ b/tests/plugins/test_plugin_ignore.py
@@ -17,62 +17,52 @@
 # under the License.
 from __future__ import annotations
 
-import os
-import shutil
-import tempfile
+from pathlib import Path
 from unittest.mock import patch
 
 from airflow import settings
 from airflow.utils.file import find_path_from_directory
 
 
-class TestIgnorePluginFile:
+def populate_dir(root_path):
     """
-    Test that the .airflowignore work and whether the file is properly ignored.
+    Create all directories and files that should be ignored. And set base path.
     """
+    # Temp dir name includes an ignored token "not", but it shouldn't matter 
since it's in the base path.
+    test_dir = root_path / "onotole"
+    plugin_folder_path = test_dir / "test_ignore"
+    plugin_folder_path.mkdir(parents=True)
+    for name in ("subdir1", "subdir2", "subdir3"):
+        (plugin_folder_path / name).mkdir()
+    files_content = [
+        ["test_load.py", "#Should not be ignored file"],
+        ["test_notload.py", 'raise Exception("This file should have been 
ignored!")'],
+        [".airflowignore", "#ignore test\nnot\nsubdir2"],
+        [".airflowignore_glob", "#ignore test\n**/*not*\nsubdir2/"],
+        ["subdir1/.airflowignore", "#ignore test\nnone\n_glob"],
+        ["subdir1/.airflowignore_glob", "#ignore test\n*none*"],
+        ["subdir1/test_load_sub1.py", "#Should not be ignored file"],
+        ["test_notload_sub.py", 'raise Exception("This file should have been 
ignored!")'],
+        ["subdir1/test_noneload_sub1.py", 'raise Exception("This file should 
have been ignored!")'],
+        ["subdir2/test_shouldignore.py", 'raise Exception("This file should 
have been ignored!")'],
+        ["subdir3/test_notload_sub3.py", 'raise Exception("This file should 
have been ignored!")'],
+    ]
+    for file_path, content in files_content:
+        (plugin_folder_path / file_path).write_text(content)
+    patch.object(settings, "PLUGINS_FOLDER", return_value=plugin_folder_path)
+    return plugin_folder_path
 
-    def setup_method(self):
-        """
-        Make tmp folder and files that should be ignored. And set base path.
-        """
-        # Temp dir name includes an ignored token "not", but it shouldn't 
matter since it's in the base path.
-        self.test_dir = tempfile.mkdtemp(prefix="onotole")
-        self.test_file = os.path.join(self.test_dir, "test_file.txt")
-        self.plugin_folder_path = os.path.join(self.test_dir, "test_ignore")
-        os.mkdir(self.plugin_folder_path)
-        os.mkdir(os.path.join(self.plugin_folder_path, "subdir1"))
-        os.mkdir(os.path.join(self.plugin_folder_path, "subdir2"))
-        os.mkdir(os.path.join(self.plugin_folder_path, "subdir3"))
-        files_content = [
-            ["test_load.py", "#Should not be ignored file"],
-            ["test_notload.py", 'raise Exception("This file should have been 
ignored!")'],
-            [".airflowignore", "#ignore test\nnot\nsubdir2"],
-            [".airflowignore_glob", "#ignore test\n**/*not*\nsubdir2/"],
-            ["subdir1/.airflowignore", "#ignore test\nnone\n_glob"],
-            ["subdir1/.airflowignore_glob", "#ignore test\n*none*"],
-            ["subdir1/test_load_sub1.py", "#Should not be ignored file"],
-            ["test_notload_sub.py", 'raise Exception("This file should have 
been ignored!")'],
-            ["subdir1/test_noneload_sub1.py", 'raise Exception("This file 
should have been ignored!")'],
-            ["subdir2/test_shouldignore.py", 'raise Exception("This file 
should have been ignored!")'],
-            ["subdir3/test_notload_sub3.py", 'raise Exception("This file 
should have been ignored!")'],
-        ]
-        for file_path, content in files_content:
-            with open(os.path.join(self.plugin_folder_path, file_path), "w") 
as f:
-                f.write(content)
-        self.mock_plugins_folder = patch.object(
-            settings, "PLUGINS_FOLDER", return_value=self.plugin_folder_path
-        )
 
-    def teardown_method(self):
-        """
-        Delete tmp folder
-        """
-        shutil.rmtree(self.test_dir)
+class TestIgnorePluginFile:
+    """
+    Test that the .airflowignore work and whether the file is properly ignored.
+    """
 
-    def test_find_not_should_ignore_path_regexp(self):
+    def test_find_not_should_ignore_path_regexp(self, tmp_path):
         """
         Test that the .airflowignore regexp works and whether the files are 
properly ignored.
         """
+        plugin_folder_path = populate_dir(tmp_path)
 
         detected_files = set()
         should_ignore_files = {
@@ -87,20 +77,18 @@ class TestIgnorePluginFile:
             "test_load_sub1.py",
         }
         ignore_list_file = ".airflowignore"
-        for file_path in find_path_from_directory(self.plugin_folder_path, 
ignore_list_file):
-            if not os.path.isfile(file_path):
-                continue
-            _, file_ext = os.path.splitext(os.path.split(file_path)[-1])
-            if file_ext != ".py":
-                continue
-            detected_files.add(os.path.basename(file_path))
+        for file_path in find_path_from_directory(plugin_folder_path, 
ignore_list_file):
+            file_path = Path(file_path)
+            if file_path.is_file() and file_path.suffix == ".py":
+                detected_files.add(file_path.name)
         assert detected_files == should_not_ignore_files
-        assert detected_files & should_ignore_files == set()
+        assert detected_files.isdisjoint(should_ignore_files)
 
-    def test_find_not_should_ignore_path_glob(self):
+    def test_find_not_should_ignore_path_glob(self, tmp_path):
         """
         Test that the .airflowignore glob syntax works and whether the files 
are properly ignored.
         """
+        plugin_folder_path = populate_dir(tmp_path)
 
         detected_files = set()
         should_ignore_files = {
@@ -114,12 +102,9 @@ class TestIgnorePluginFile:
             "test_load_sub1.py",
         }
         ignore_list_file = ".airflowignore_glob"
-        for file_path in find_path_from_directory(self.plugin_folder_path, 
ignore_list_file, "glob"):
-            if not os.path.isfile(file_path):
-                continue
-            _, file_ext = os.path.splitext(os.path.split(file_path)[-1])
-            if file_ext != ".py":
-                continue
-            detected_files.add(os.path.basename(file_path))
+        for file_path in find_path_from_directory(plugin_folder_path, 
ignore_list_file, "glob"):
+            file_path = Path(file_path)
+            if file_path.is_file() and file_path.suffix == ".py":
+                detected_files.add(file_path.name)
         assert detected_files == should_not_ignore_files
-        assert detected_files & should_ignore_files == set()
+        assert detected_files.isdisjoint(should_ignore_files)
diff --git a/tests/plugins/test_plugins_manager.py 
b/tests/plugins/test_plugins_manager.py
index bf74863407..24faaeed34 100644
--- a/tests/plugins/test_plugins_manager.py
+++ b/tests/plugins/test_plugins_manager.py
@@ -21,7 +21,6 @@ import importlib
 import logging
 import os
 import sys
-import tempfile
 from pathlib import Path
 from unittest import mock
 
@@ -216,16 +215,14 @@ class TestPluginsManager:
 
             assert caplog.record_tuples == []
 
-    def test_loads_filesystem_plugins_exception(self, caplog):
+    def test_loads_filesystem_plugins_exception(self, caplog, tmp_path):
         from airflow import plugins_manager
 
         with mock.patch("airflow.plugins_manager.plugins", []):
-            with tempfile.TemporaryDirectory() as tmpdir:
-                with open(os.path.join(tmpdir, "testplugin.py"), "w") as f:
-                    f.write(ON_LOAD_EXCEPTION_PLUGIN)
+            (tmp_path / "testplugin.py").write_text(ON_LOAD_EXCEPTION_PLUGIN)
 
-                with conf_vars({("core", "plugins_folder"): tmpdir}):
-                    plugins_manager.load_plugins_from_plugin_directory()
+            with conf_vars({("core", "plugins_folder"): os.fspath(tmp_path)}):
+                plugins_manager.load_plugins_from_plugin_directory()
 
             assert plugins_manager.plugins == []
 

Reply via email to