Skip to content

Commit

Permalink
@sanderegg review: refactor types and assert
Browse files Browse the repository at this point in the history
  • Loading branch information
pcrespov committed Jan 19, 2024
1 parent 829c460 commit 40b6832
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from typing import TypeAlias

EnvVarsDict: TypeAlias = dict[str, str]
EnvVarsList: TypeAlias = Iterable[str]
EnvVarsIterable: TypeAlias = Iterable[str]


# SEE packages/pytest-simcore/tests/test_helpers_utils_envs.py
34 changes: 19 additions & 15 deletions packages/pytest-simcore/src/pytest_simcore/helpers/utils_envs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,42 @@
"""

import os
from copy import deepcopy
from io import StringIO
from pathlib import Path

import dotenv
import pytest

from .typing_env import EnvVarsDict, EnvVarsList
from .typing_env import EnvVarsDict, EnvVarsIterable

#
# monkeypatch using dict
#


def setenvs_from_dict(
monkeypatch: pytest.MonkeyPatch, envs: EnvVarsDict
monkeypatch: pytest.MonkeyPatch, envs: dict[str, str | bool]
) -> EnvVarsDict:
env_vars = {}

for key, value in envs.items():
assert isinstance(key, str)
assert (
value is not None
), f"{key=},{value=}" # None keys cannot be is defined w/o value
converted_value = value
assert value is not None, f"{key=},{value=}"

v = value

if isinstance(value, bool):
converted_value = f"{'true' if value else 'false'}"
assert isinstance(
converted_value, str
), f"client MUST explicitly stringify values since some cannot be done automatically e.g. json-like values. problematic {key=},{value=}"
v = "true" if value else "false"

assert isinstance(v, str), (
"caller MUST explicitly stringify values since some cannot be done automatically"
f"e.g. json-like values. Check {key=},{value=}"
)

monkeypatch.setenv(key, v)
env_vars[key] = v

monkeypatch.setenv(key, converted_value)
return deepcopy(envs)
return env_vars


def load_dotenv(envfile_content_or_path: Path | str, **options) -> EnvVarsDict:
Expand All @@ -52,13 +57,12 @@ def load_dotenv(envfile_content_or_path: Path | str, **options) -> EnvVarsDict:

def delenvs_from_dict(
monkeypatch: pytest.MonkeyPatch,
envs: EnvVarsList,
envs: EnvVarsIterable,
*,
raising: bool = True,
) -> None:
for key in envs:
assert isinstance(key, str)
assert key is not None # None keys cannot be is defined w/o value
monkeypatch.delenv(key, raising)


Expand Down

0 comments on commit 40b6832

Please sign in to comment.