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 1e94027f05 Refactor: use tmp_path in utils test (#33542)
1e94027f05 is described below

commit 1e94027f053017b03691163edbaa92e05dc62061
Author: Miroslav Šedivý <[email protected]>
AuthorDate: Mon Aug 21 08:15:48 2023 +0000

    Refactor: use tmp_path in utils test (#33542)
---
 tests/utils/test_email.py         | 85 ++++++++++++++++++---------------------
 tests/utils/test_log_handlers.py  | 47 ++++++++++------------
 tests/utils/test_process_utils.py | 27 ++++++-------
 3 files changed, 74 insertions(+), 85 deletions(-)

diff --git a/tests/utils/test_email.py b/tests/utils/test_email.py
index 397cd3ac29..a970cb4f6f 100644
--- a/tests/utils/test_email.py
+++ b/tests/utils/test_email.py
@@ -18,7 +18,6 @@
 from __future__ import annotations
 
 import os
-import tempfile
 from email.mime.application import MIMEApplication
 from email.mime.multipart import MIMEMultipart
 from email.mime.text import MIMEText
@@ -148,23 +147,22 @@ class TestEmail:
 
 class TestEmailSmtp:
     @mock.patch("airflow.utils.email.send_mime_email")
-    def test_send_smtp(self, mock_send_mime):
-        with tempfile.NamedTemporaryFile() as attachment:
-            attachment.write(b"attachment")
-            attachment.seek(0)
-            email.send_email_smtp("to", "subject", "content", 
files=[attachment.name])
-            assert mock_send_mime.called
-            _, call_args = mock_send_mime.call_args
-            assert conf.get("smtp", "SMTP_MAIL_FROM") == call_args["e_from"]
-            assert ["to"] == call_args["e_to"]
-            msg = call_args["mime_msg"]
-            assert "subject" == msg["Subject"]
-            assert conf.get("smtp", "SMTP_MAIL_FROM") == msg["From"]
-            assert 2 == len(msg.get_payload())
-            filename = 'attachment; filename="' + 
os.path.basename(attachment.name) + '"'
-            assert filename == msg.get_payload()[-1].get("Content-Disposition")
-            mimeapp = MIMEApplication("attachment")
-            assert mimeapp.get_payload() == msg.get_payload()[-1].get_payload()
+    def test_send_smtp(self, mock_send_mime, tmp_path):
+        path = tmp_path / "testfile"
+        path.write_bytes(b"attachment")
+        email.send_email_smtp("to", "subject", "content", 
files=[os.fspath(path)])
+        assert mock_send_mime.called
+        _, call_args = mock_send_mime.call_args
+        assert conf.get("smtp", "SMTP_MAIL_FROM") == call_args["e_from"]
+        assert ["to"] == call_args["e_to"]
+        msg = call_args["mime_msg"]
+        assert "subject" == msg["Subject"]
+        assert conf.get("smtp", "SMTP_MAIL_FROM") == msg["From"]
+        assert 2 == len(msg.get_payload())
+        filename = f'attachment; filename="{path.name}"'
+        assert filename == msg.get_payload()[-1].get("Content-Disposition")
+        mimeapp = MIMEApplication("attachment")
+        assert mimeapp.get_payload() == msg.get_payload()[-1].get_payload()
 
     @mock.patch("airflow.utils.email.send_mime_email")
     def test_send_smtp_with_multibyte_content(self, mock_send_mime):
@@ -176,33 +174,30 @@ class TestEmailSmtp:
         assert mimetext.get_payload() == msg.get_payload()[0].get_payload()
 
     @mock.patch("airflow.utils.email.send_mime_email")
-    def test_send_bcc_smtp(self, mock_send_mime):
-        with tempfile.NamedTemporaryFile() as attachment:
-            attachment.write(b"attachment")
-            attachment.seek(0)
-            email.send_email_smtp(
-                "to",
-                "subject",
-                "content",
-                files=[attachment.name],
-                cc="cc",
-                bcc="bcc",
-                custom_headers={"Reply-To": "[email protected]"},
-            )
-            assert mock_send_mime.called
-            _, call_args = mock_send_mime.call_args
-            assert conf.get("smtp", "SMTP_MAIL_FROM") == call_args["e_from"]
-            assert ["to", "cc", "bcc"] == call_args["e_to"]
-            msg = call_args["mime_msg"]
-            assert "subject" == msg["Subject"]
-            assert conf.get("smtp", "SMTP_MAIL_FROM") == msg["From"]
-            assert 2 == len(msg.get_payload())
-            assert 'attachment; filename="' + 
os.path.basename(attachment.name) + '"' == msg.get_payload()[
-                -1
-            ].get("Content-Disposition")
-            mimeapp = MIMEApplication("attachment")
-            assert mimeapp.get_payload() == msg.get_payload()[-1].get_payload()
-            assert msg["Reply-To"] == "[email protected]"
+    def test_send_bcc_smtp(self, mock_send_mime, tmp_path):
+        path = tmp_path / "testfile"
+        path.write_bytes(b"attachment")
+        email.send_email_smtp(
+            "to",
+            "subject",
+            "content",
+            files=[os.fspath(path)],
+            cc="cc",
+            bcc="bcc",
+            custom_headers={"Reply-To": "[email protected]"},
+        )
+        assert mock_send_mime.called
+        _, call_args = mock_send_mime.call_args
+        assert conf.get("smtp", "SMTP_MAIL_FROM") == call_args["e_from"]
+        assert ["to", "cc", "bcc"] == call_args["e_to"]
+        msg = call_args["mime_msg"]
+        assert "subject" == msg["Subject"]
+        assert conf.get("smtp", "SMTP_MAIL_FROM") == msg["From"]
+        assert 2 == len(msg.get_payload())
+        assert f'attachment; filename="{path.name}"' == 
msg.get_payload()[-1].get("Content-Disposition")
+        mimeapp = MIMEApplication("attachment")
+        assert mimeapp.get_payload() == msg.get_payload()[-1].get_payload()
+        assert msg["Reply-To"] == "[email protected]"
 
     @mock.patch("smtplib.SMTP_SSL")
     @mock.patch("smtplib.SMTP")
diff --git a/tests/utils/test_log_handlers.py b/tests/utils/test_log_handlers.py
index 30eb480076..4845c83f7e 100644
--- a/tests/utils/test_log_handlers.py
+++ b/tests/utils/test_log_handlers.py
@@ -21,7 +21,6 @@ import logging
 import logging.config
 import os
 import re
-import tempfile
 from pathlib import Path
 from unittest import mock
 from unittest.mock import patch
@@ -254,23 +253,22 @@ class TestFileTaskLogHandler:
         mock_read_local.assert_called_with(path)
         assert actual == ("*** the messages\nthe log", {"end_of_log": True, 
"log_pos": 7})
 
-    def test__read_from_local(self):
+    def test__read_from_local(self, tmp_path):
         """Tests the behavior of method _read_from_local"""
 
-        with tempfile.TemporaryDirectory() as td:
-            file1 = Path(td, "hello1.log")
-            file2 = Path(td, "hello1.log.suffix.log")
-            file1.write_text("file1 content")
-            file2.write_text("file2 content")
-            fth = FileTaskHandler("")
-            assert fth._read_from_local(file1) == (
-                [
-                    "Found local files:",
-                    f"  * {td}/hello1.log",
-                    f"  * {td}/hello1.log.suffix.log",
-                ],
-                ["file1 content", "file2 content"],
-            )
+        path1 = tmp_path / "hello1.log"
+        path2 = tmp_path / "hello1.log.suffix.log"
+        path1.write_text("file1 content")
+        path2.write_text("file2 content")
+        fth = FileTaskHandler("")
+        assert fth._read_from_local(path1) == (
+            [
+                "Found local files:",
+                f"  * {path1}",
+                f"  * {path2}",
+            ],
+            ["file1 content", "file2 content"],
+        )
 
     @mock.patch(
         
"airflow.providers.cncf.kubernetes.executors.kubernetes_executor.KubernetesExecutor.get_task_log"
@@ -455,7 +453,7 @@ class TestFileTaskLogHandler:
         assert FileTaskHandler.add_triggerer_suffix(sample, job_id="123") == 
sample + ".trigger.123.log"
 
     @pytest.mark.parametrize("is_a_trigger", [True, False])
-    def test_set_context_trigger(self, create_dummy_dag, dag_maker, 
is_a_trigger, session):
+    def test_set_context_trigger(self, create_dummy_dag, dag_maker, 
is_a_trigger, session, tmp_path):
         create_dummy_dag(dag_id="test_fth", task_id="dummy")
         (ti,) = dag_maker.create_dagrun(execution_date=pendulum.datetime(2023, 
1, 1, tz="UTC")).task_instances
         assert isinstance(ti, TaskInstance)
@@ -466,14 +464,13 @@ class TestFileTaskLogHandler:
             t.triggerer_job = job
             ti.triggerer = t
             t.task_instance = ti
-        with tempfile.TemporaryDirectory() as td:
-            h = FileTaskHandler(base_log_folder=td)
-            h.set_context(ti)
-            expected = 
"dag_id=test_fth/run_id=test/task_id=dummy/attempt=1.log"
-            if is_a_trigger:
-                expected += f".trigger.{job.id}.log"
-            actual = h.handler.baseFilename
-            assert actual.replace(td + "/", "") == expected
+        h = FileTaskHandler(base_log_folder=os.fspath(tmp_path))
+        h.set_context(ti)
+        expected = "dag_id=test_fth/run_id=test/task_id=dummy/attempt=1.log"
+        if is_a_trigger:
+            expected += f".trigger.{job.id}.log"
+        actual = h.handler.baseFilename
+        assert actual == os.fspath(tmp_path / expected)
 
 
 class TestFilenameRendering:
diff --git a/tests/utils/test_process_utils.py 
b/tests/utils/test_process_utils.py
index 505085388a..322f10eb66 100644
--- a/tests/utils/test_process_utils.py
+++ b/tests/utils/test_process_utils.py
@@ -25,7 +25,6 @@ import subprocess
 import time
 from contextlib import suppress
 from subprocess import CalledProcessError
-from tempfile import NamedTemporaryFile
 from time import sleep
 from unittest import mock
 
@@ -206,22 +205,20 @@ class TestCheckIfPidfileProcessIsRunning:
     def test_ok_if_no_file(self):
         check_if_pidfile_process_is_running("some/pid/file", 
process_name="test")
 
-    def test_remove_if_no_process(self):
+    def test_remove_if_no_process(self, tmp_path):
+        path = tmp_path / "testfile"
+        # limit pid as max of int32, otherwise this test could fail on some 
platform
+        path.write_text(f"{2**31 - 1}")
+        check_if_pidfile_process_is_running(os.fspath(path), 
process_name="test")
         # Assert file is deleted
-        with pytest.raises(FileNotFoundError):
-            with NamedTemporaryFile("+w") as f:
-                # limit pid as max of int32, otherwise this test could fail on 
some platform
-                f.write(f"{2**31 - 1}")
-                f.flush()
-                check_if_pidfile_process_is_running(f.name, 
process_name="test")
-
-    def test_raise_error_if_process_is_running(self):
+        assert not path.exists()
+
+    def test_raise_error_if_process_is_running(self, tmp_path):
+        path = tmp_path / "testfile"
         pid = os.getpid()
-        with NamedTemporaryFile("+w") as f:
-            f.write(str(pid))
-            f.flush()
-            with pytest.raises(AirflowException, match="is already running 
under PID"):
-                check_if_pidfile_process_is_running(f.name, 
process_name="test")
+        path.write_text(f"{pid}")
+        with pytest.raises(AirflowException, match="is already running under 
PID"):
+            check_if_pidfile_process_is_running(os.fspath(path), 
process_name="test")
 
 
 class TestSetNewProcessGroup:

Reply via email to