Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
disrupted committed Oct 11, 2023
1 parent f5b5fb9 commit d2830a5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
18 changes: 11 additions & 7 deletions kpops/utils/environment.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
from collections import UserDict
from collections.abc import MutableMapping
from collections.abc import ItemsView, KeysView, MutableMapping, ValuesView


class Environment(UserDict[str, str]):
Expand All @@ -25,14 +25,18 @@ def __getitem__(self, key: str) -> str:
def __contains__(self, key: object) -> bool:
return super().__contains__(key) or self._global.__contains__(key)

def keys(self) -> set[str]:
return set(super().keys()).union(self._global.keys())
@property
def dict(self) -> dict[str, str]:
return {**self._global, **self.data}

def values(self) -> set[str]:
return set(super().values()).union(self._global.values())
def keys(self) -> KeysView[str]:
return KeysView(self.dict)

def items(self) -> dict[str, str]:
return {**self._global, **self.data}
def values(self) -> ValuesView[str]:
return ValuesView(self.dict)

def items(self) -> ItemsView[str, str]:
return ItemsView(self.dict)


ENV = Environment()
11 changes: 11 additions & 0 deletions tests/utils/test_environment.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
from unittest.mock import ANY

import pytest

Expand All @@ -7,6 +8,8 @@

@pytest.fixture(autouse=True)
def environment(monkeypatch: pytest.MonkeyPatch) -> Environment:
for key in os.environ:
monkeypatch.delenv(key)
monkeypatch.setenv("MY", "fake")
monkeypatch.setenv("ENVIRONMENT", "here")
return Environment()
Expand Down Expand Up @@ -54,3 +57,11 @@ def test_kwargs():
assert environment["ENVIRONMENT"] == "here"
assert environment["kwarg1"] == "value1"
assert environment["kwarg2"] == "value2"


def test_dict(environment: Environment):
assert environment.dict == {
"MY": "fake",
"ENVIRONMENT": "here",
"PYTEST_CURRENT_TEST": ANY,
}

0 comments on commit d2830a5

Please sign in to comment.