From: Daniele Varrazzo Date: Mon, 9 Nov 2020 01:16:44 +0000 (+0000) Subject: Use pytest tmpdir fixture X-Git-Tag: 3.0.dev0~391 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=352a67fd631e401c92836c8e5fb811c90d958179;p=thirdparty%2Fpsycopg.git Use pytest tmpdir fixture --- diff --git a/tests/test_concurrency.py b/tests/test_concurrency.py index aafbdd270..40b6b05a2 100644 --- a/tests/test_concurrency.py +++ b/tests/test_concurrency.py @@ -7,8 +7,6 @@ import sys import time import queue import pytest -import shutil -import tempfile import threading import subprocess as sp @@ -62,7 +60,7 @@ def test_commit_concurrency(conn): @pytest.mark.slow -def test_multiprocess_close(dsn): +def test_multiprocess_close(dsn, tmpdir): # Check the problem reported in psycopg2#829 # Subprocess gcs the copy of the fd after fork so it closes connection. module = f"""\ @@ -70,7 +68,7 @@ import time import psycopg3 def thread(): - conn = psycopg3.connect({repr(dsn)}) + conn = psycopg3.connect({dsn!r}) curs = conn.cursor() for i in range(10): curs.execute("select 1") @@ -93,20 +91,16 @@ multiprocessing.Process(target=mptest.process, name='myprocess').start() t.join() """ - dir = tempfile.mkdtemp() - try: - with open(os.path.join(dir, "mptest.py"), "w") as f: - f.write(module) - env = dict(os.environ) - env["PYTHONPATH"] = dir + os.pathsep + env.get("PYTHONPATH", "") - # TODO: debug this. Importing c module fails on travis in this scenario - env.pop("PSYCOPG3_IMPL", None) - out = sp.check_output( - [sys.executable, "-c", script], stderr=sp.STDOUT, env=env - ).decode("utf8", "replace") - assert out == "", out.strip().splitlines()[-1] - finally: - shutil.rmtree(dir, ignore_errors=True) + with (tmpdir / "mptest.py").open("w") as f: + f.write(module) + env = dict(os.environ) + env["PYTHONPATH"] = tmpdir + os.pathsep + env.get("PYTHONPATH", "") + # TODO: debug this. Importing c module fails on travis in this scenario + env.pop("PSYCOPG3_IMPL", None) + out = sp.check_output( + [sys.executable, "-c", script], stderr=sp.STDOUT, env=env + ).decode("utf8", "replace") + assert out == "", out.strip().splitlines()[-1] @pytest.mark.slow