From 802844129c824eadfcff11271d06b6281b43f680 Mon Sep 17 00:00:00 2001 From: Zane Dufour Date: Wed, 15 Nov 2023 09:30:04 -0500 Subject: [PATCH] get refactored tests working --- pyproject.toml | 5 +++++ tests/test_core.py | 36 ++++++++++++++++++++++++++++-------- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 2ff35a4..296160c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -155,3 +155,8 @@ known_first_party = ["pdm_dotenv"] [tool.pytest.ini_options] filterwarnings = ["ignore::DeprecationWarning"] +addopts = [ + # "-ra", + # "-q", +] +testpaths = ["tests"] diff --git a/tests/test_core.py b/tests/test_core.py index 672b46f..7c34775 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -1,5 +1,7 @@ from pdm.cli.actions import textwrap from pdm.project import Project +import pathlib +import tempfile from typing import TYPE_CHECKING, Tuple import toml @@ -9,19 +11,24 @@ def check_env(project: Project, pdm: "PDMCallable", environ: Tuple[Tuple[str, str]]) -> None: for key, val in environ: - assert ( - val - == pdm( + with tempfile.NamedTemporaryFile() as f: + fp = pathlib.Path(f.name) + pdm( [ "run", "python", "-c", - f"import os; print(os.environ['{key}'])", + textwrap.dedent( + f""" + import os, pathlib + pathlib.Path({f.name!r}).write_text(os.environ.get({key!r}, "")) + """ + ), ], strict=True, obj=project, - ).stdout - ) + ) + assert val == fp.read_text().strip() def test_build(project: Project, pdm: "PDMCallable") -> None: @@ -66,7 +73,19 @@ def test_build(project: Project, pdm: "PDMCallable") -> None: def test_happy_path(project: Project, pdm: "PDMCallable") -> None: environ = (("FOO_BAR_BAZ", "hello"),) for key, val in environ: - pdm(["run", "dotenv", "set", key, val]) + pdm( + [ + "run", + "dotenv", + f"--file={project.root / '.env'}", + "set", + key, + val, + ], + obj=project, + strict=True, + ) + print((project.root / ".env").read_text()) check_env(project, pdm, environ) @@ -78,12 +97,13 @@ def test_different_file(project: Project, pdm: "PDMCallable") -> None: [ "run", "dotenv", - f"--file={'.foo.env'}", + f"--file={project.root / '.foo.env'}", "set", key, val, ], obj=project, + strict=True, ) pdm( [