From c9f37d98dc4d5a4697c5f03ab0985779117a8584 Mon Sep 17 00:00:00 2001 From: Mathieu Dupuy Date: Sat, 2 Dec 2023 14:08:28 +0100 Subject: [PATCH 1/7] change verbosity level to normal --- src/pdm_dotenv/__init__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/pdm_dotenv/__init__.py b/src/pdm_dotenv/__init__.py index 5a69cd5..177c3ba 100644 --- a/src/pdm_dotenv/__init__.py +++ b/src/pdm_dotenv/__init__.py @@ -10,11 +10,12 @@ from pdm.project import ConfigItem, Project from pdm.signals import pre_invoke from pdm.core import Core +from pdm.termui import Verbosity def load_dotenv(project: Project, **_: Any) -> None: dotenv_path = project.root / project.config["dotenv.path"] - project.core.ui.echo(f"Loading dotenv file {dotenv_path}") + project.core.ui.echo(f"Loading dotenv file {dotenv_path}", verbosity=Verbosity.NORMAL) dotenv.load_dotenv(dotenv_path) From 59a47e7c2f717ea7c7d42110bd9f563365ff7649 Mon Sep 17 00:00:00 2001 From: Zane Dufour Date: Thu, 7 Dec 2023 20:48:59 -0500 Subject: [PATCH 2/7] echo to stderr; add test for --quiet --- .pre-commit-config.yaml | 16 +++-- pyproject.toml | 1 + src/pdm_dotenv/__init__.py | 2 +- tests/test_core.py | 138 ++++++++++++++++++++++--------------- 4 files changed, 95 insertions(+), 62 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index bbe814a..733886b 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,3 +1,5 @@ +ci: + skip: ["pytype"] # See https://pre-commit.com for more information # See https://pre-commit.com/hooks.html for more hooks repos: @@ -15,9 +17,13 @@ repos: - id: ruff args: [--fix, --exit-non-zero-on-fix, --show-fixes] - - repo: https://github.com/pre-commit/mirrors-mypy - rev: v1.7.0 + - repo: https://github.com/google/pytype + rev: 2023.11.29 hooks: - - id: mypy - additional_dependencies: - - types-toml + - id: pytype + # - repo: https://github.com/pre-commit/mirrors-mypy + # rev: v1.7.0 + # hooks: + # - id: mypy + # additional_dependencies: + # - types-toml diff --git a/pyproject.toml b/pyproject.toml index 296160c..5c9b0c8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -34,6 +34,7 @@ dependencies = [ "pytest-cov>=4.0.0", "python-dotenv[cli]", "toml", + "pytype", ] scripts.test = "pytest {args:tests}" diff --git a/src/pdm_dotenv/__init__.py b/src/pdm_dotenv/__init__.py index 177c3ba..0c37c80 100644 --- a/src/pdm_dotenv/__init__.py +++ b/src/pdm_dotenv/__init__.py @@ -15,7 +15,7 @@ def load_dotenv(project: Project, **_: Any) -> None: dotenv_path = project.root / project.config["dotenv.path"] - project.core.ui.echo(f"Loading dotenv file {dotenv_path}", verbosity=Verbosity.NORMAL) + project.core.ui.echo(f"Loading dotenv file {dotenv_path}", err=True, verbosity=Verbosity.NORMAL) dotenv.load_dotenv(dotenv_path) diff --git a/tests/test_core.py b/tests/test_core.py index 83431d6..85dce82 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -1,46 +1,41 @@ from pdm.cli.actions import textwrap +import _pytest +import pytest +import typing from pdm.project import Project +from pdm.pytest import RunResult import pathlib import tempfile -from typing import TYPE_CHECKING, Tuple, List +from typing import TYPE_CHECKING, List import toml if TYPE_CHECKING: from pdm.pytest import PDMCallable -def dotenv_set(file: pathlib.Path, key: str, val: str) -> List[str]: - return [ - "run", - "dotenv", - f"--file={file}", - "set", - key, - val, - ] - - -def check_env(project: Project, pdm: "PDMCallable", environ: Tuple[Tuple[str, str]]) -> None: - for key, val in environ: - with tempfile.TemporaryDirectory() as td: - fp = pathlib.Path(td) / "foo.txt" - fp.touch() - pdm( - [ - "run", - "python", - "-c", - textwrap.dedent( - f""" - import os, pathlib - pathlib.Path({str(fp)!r}).write_text(os.environ.get({key!r}, "")) - """ - ), - ], - strict=True, - obj=project, - ) - assert val == fp.read_text().strip() +def assert_in_env( + pdm_in_project: typing.Callable[[List[str]], RunResult], + key: str, + val: str, +) -> None: + with tempfile.TemporaryDirectory() as td: + fp = pathlib.Path(td) + out_path = fp / "foo.txt" + out_path.touch() + cmd = [ + "run", + "python", + "-c", + textwrap.dedent( + f""" + import os, pathlib + pathlib.Path({str(out_path)!r}).write_text(os.environ.get({key!r}, "")) + """ + ), + ] + pdm_in_project(cmd) + + assert val == (fp / "foo.txt").read_text().strip() def test_build(project: Project, pdm: "PDMCallable") -> None: @@ -82,33 +77,64 @@ def test_build(project: Project, pdm: "PDMCallable") -> None: assert (project.root / "dist" / f"foo-{exp_ver}-py3-none-any.whl").exists() -def test_happy_path(project: Project, pdm: "PDMCallable") -> None: - environ = (("FOO_BAR_BAZ", "hello"),) - for key, val in environ: - pdm( - dotenv_set(project.root / ".env", key, val), - obj=project, - strict=True, - ) +def pdm_in_project_factory(pdm: "PDMCallable", project: Project) -> RunResult: + def wrapped(args: List[str]) -> RunResult: + return pdm(args, strict=True, obj=project) - check_env(project, pdm, environ) + return wrapped -def test_different_file(project: Project, pdm: "PDMCallable") -> None: - environ = (("FOO_BAR_BAZ", "hello"),) - for key, val in environ: - pdm( - dotenv_set(project.root / ".foo.env", key, val), - obj=project, - strict=True, - ) - pdm( +def dotenv_set(pdm_in_project, key, val, dotenv): + pdm_in_project( [ - "config", - "dotenv.path", - ".foo.env", + "run", + "dotenv", + f"--file={dotenv}", + "set", + key, + val, ], - obj=project, ) - check_env(project, pdm, environ) + +@pytest.mark.parametrize("quiet", (True, False)) +def test_quiet(project: Project, pdm: "PDMCallable", quiet: bool) -> None: + dotenv_set(pdm_in_project_factory(pdm, project), "foo", "bar", dotenv=project.root / ".env") + + cmd = ["run"] + if quiet: + cmd.append("--quiet") + cmd.extend(["python", "-c", "print()"]) + + result = pdm(cmd, strict=True, obj=project) + loading_snippet = "Loading dotenv file" + assert (loading_snippet in result.stderr) != quiet + assert loading_snippet not in result.stdout + + +ENVIRON = (("FOO_BAR_BAZ", "hello"),) + + +@pytest.fixture(params=[".env", "foo.env", None]) +def dotenv_file(request: _pytest.fixtures.SubRequest, project: Project, pdm: "PDMCallable") -> str: + pdm_in_project = pdm_in_project_factory(pdm, project) + + for key, val in ENVIRON: + dotenv_set(pdm_in_project, key, val, dotenv=project.root / (request.param or ".env")) + + if request.param: + pdm_in_project( + [ + "config", + "dotenv.path", + request.param, + ], + ) + + return request.param + + +def test_happy_path(dotenv_file, project: Project, pdm: "PDMCallable") -> None: + pdm_in_project = pdm_in_project_factory(pdm, project) + for key, val in ENVIRON: + assert_in_env(pdm_in_project, key, val) From ac0a8024526fce2a784e792faf7c244ef228ce75 Mon Sep 17 00:00:00 2001 From: Zane Dufour Date: Thu, 7 Dec 2023 21:04:46 -0500 Subject: [PATCH 3/7] improve the pytpye pre-commit experience --- .pre-commit-config.yaml | 7 +++++-- pyproject.toml | 6 +++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 733886b..87e65b2 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -17,10 +17,13 @@ repos: - id: ruff args: [--fix, --exit-non-zero-on-fix, --show-fixes] - - repo: https://github.com/google/pytype - rev: 2023.11.29 + - repo: local hooks: - id: pytype + name: pytype + language: system + types: [python] + entry: hatch env run -e typecheck pytype # - repo: https://github.com/pre-commit/mirrors-mypy # rev: v1.7.0 # hooks: diff --git a/pyproject.toml b/pyproject.toml index 5c9b0c8..2bead45 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -34,13 +34,17 @@ dependencies = [ "pytest-cov>=4.0.0", "python-dotenv[cli]", "toml", - "pytype", ] scripts.test = "pytest {args:tests}" scripts.test-cov = "pytest --cov=src {args:tests}" scripts.cov = "hatch run test-cov {args:tests} --cov-report=xml" +[tool.hatch.envs.typecheck] +dependencies = ["pytype"] +scripts.commit = "git commit" + + [tool.hatch.envs.docs] dependencies = ["mkdocs>=1.1", "mkdocs-material>=6.2"] scripts.serve = "mkdocs serve" From 810da141a76ba5ec30458c07e5ec5500b5235faf Mon Sep 17 00:00:00 2001 From: Zane Dufour Date: Thu, 7 Dec 2023 21:12:59 -0500 Subject: [PATCH 4/7] add pytype checking to github workflows --- .github/workflows/ci.yml | 13 +++++++++++++ .pre-commit-config.yaml | 2 +- pyproject.toml | 2 +- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d6933b8..81f8b2a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -16,6 +16,19 @@ on: - "*.md" jobs: + Type Checking: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - run: pip install hatch + - uses: actions/cache@v3 + id: hatch-cache + with: + # cache to runner + path: hatch-cache + key: ${{ hashFiles('pyproject.toml') }} + - name: Run pytype + run: hatch --cache-dir hatch-cache pytype src Coverage: runs-on: ubuntu-latest steps: diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 87e65b2..87cf590 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -23,7 +23,7 @@ repos: name: pytype language: system types: [python] - entry: hatch env run -e typecheck pytype + entry: hatch env run pytype # - repo: https://github.com/pre-commit/mirrors-mypy # rev: v1.7.0 # hooks: diff --git a/pyproject.toml b/pyproject.toml index 2bead45..6fb4797 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -42,7 +42,7 @@ scripts.cov = "hatch run test-cov {args:tests} --cov-report=xml" [tool.hatch.envs.typecheck] dependencies = ["pytype"] -scripts.commit = "git commit" +scripts.pytype = "pytype {args:src}" [tool.hatch.envs.docs] From 8e5f9ea3adcd5c9f005fb750367e46d169131b36 Mon Sep 17 00:00:00 2001 From: Zane Dufour Date: Thu, 7 Dec 2023 21:54:18 -0500 Subject: [PATCH 5/7] Type Checking -> Type-Checking --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 81f8b2a..88a7bbe 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -16,7 +16,7 @@ on: - "*.md" jobs: - Type Checking: + Type-Checking: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 From eea06a4ee836479ca1ce550d31542754a7c596c9 Mon Sep 17 00:00:00 2001 From: Zane Dufour Date: Thu, 7 Dec 2023 21:56:41 -0500 Subject: [PATCH 6/7] hatch ... pytype -> hatch ... run pytype --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 88a7bbe..ec1a7f7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -28,7 +28,7 @@ jobs: path: hatch-cache key: ${{ hashFiles('pyproject.toml') }} - name: Run pytype - run: hatch --cache-dir hatch-cache pytype src + run: hatch --cache-dir hatch-cache run pytype src Coverage: runs-on: ubuntu-latest steps: From 357cdf98fa2025340922b1bbec6f5658ac730a48 Mon Sep 17 00:00:00 2001 From: Zane Dufour Date: Thu, 7 Dec 2023 22:34:04 -0500 Subject: [PATCH 7/7] switch to static:pytype --- .github/workflows/ci.yml | 2 +- .pre-commit-config.yaml | 2 +- pyproject.toml | 3 +-- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ec1a7f7..079d5a7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -28,7 +28,7 @@ jobs: path: hatch-cache key: ${{ hashFiles('pyproject.toml') }} - name: Run pytype - run: hatch --cache-dir hatch-cache run pytype src + run: hatch --cache-dir hatch-cache run static:pytype src Coverage: runs-on: ubuntu-latest steps: diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 87cf590..b07014e 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -23,7 +23,7 @@ repos: name: pytype language: system types: [python] - entry: hatch env run pytype + entry: hatch run static:pytype # - repo: https://github.com/pre-commit/mirrors-mypy # rev: v1.7.0 # hooks: diff --git a/pyproject.toml b/pyproject.toml index 6fb4797..96ac3c9 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -40,9 +40,8 @@ scripts.test = "pytest {args:tests}" scripts.test-cov = "pytest --cov=src {args:tests}" scripts.cov = "hatch run test-cov {args:tests} --cov-report=xml" -[tool.hatch.envs.typecheck] +[tool.hatch.envs.static] dependencies = ["pytype"] -scripts.pytype = "pytype {args:src}" [tool.hatch.envs.docs]