Skip to content

Commit

Permalink
refactor: change Destination.__setitem__ and __delitem__ methods (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
ovsds authored Feb 20, 2024
1 parent 6d01a70 commit 3dcc90c
Show file tree
Hide file tree
Showing 12 changed files with 31 additions and 31 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -389,12 +389,12 @@ import secret_transfer.utils.types as secret_transfer_types
class CustomDestination(secret_transfer_core.AbstractDestination):
def set(self, key: str, value: secret_transfer_types.Literal) -> None:
def __setitem__(self, key: str, value: secret_transfer_types.Literal) -> None:
# Required to implement
# Set the value of the secret by key
...
def clean(self, key: str) -> None:
def __delitem__(self, key: str) -> None:
# Optional to implement
# Clean all secrets in the destination
...
Expand Down
4 changes: 2 additions & 2 deletions examples/advanced/import_local_files/custom_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ def __getitem__(self, key: str) -> str:


class CustomDestination(secret_transfer_core.AbstractDestination):
def set(self, key: str, value: secret_transfer_types.Literal):
def __setitem__(self, key: str, value: secret_transfer_types.Literal):
print(f"CustomDestination.set called with key: {key} and value: {value}")

def clean(self, key: str) -> None:
def __delitem__(self, key: str) -> None:
print("CustomDestination.clean called with key: {key}")
4 changes: 2 additions & 2 deletions secret_transfer/core/destination/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ class AbstractDestination(core_base.BaseResource, metaclass=DestinationRegistry)
__register__ = False

@abc.abstractmethod
def set(self, key: str, value: utils_types.Literal) -> None:
def __setitem__(self, key: str, value: utils_types.Literal) -> None:
...

@abc.abstractmethod
def clean(self, key: str) -> None:
def __delitem__(self, key: str) -> None:
...
4 changes: 2 additions & 2 deletions secret_transfer/default/destination/bash_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@


class BashExportDestination(core.AbstractDestination):
def set(self, key: str, value: utils_types.Literal) -> None:
def __setitem__(self, key: str, value: utils_types.Literal) -> None:
print(f"export {key}={value}")

def clean(self, key: str) -> None:
def __delitem__(self, key: str) -> None:
print(f"unset {key}")

@classmethod
Expand Down
4 changes: 2 additions & 2 deletions secret_transfer/default/destination/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@


class EnvDestination(core.AbstractDestination):
def set(self, key: str, value: utils_types.Literal) -> None:
def __setitem__(self, key: str, value: utils_types.Literal) -> None:
os.environ[key] = str(value)

def clean(self, key: str) -> None:
def __delitem__(self, key: str) -> None:
try:
del os.environ[key]
except KeyError:
Expand Down
4 changes: 2 additions & 2 deletions secret_transfer/default/destination/gh_cli_secrets.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ def __init__(self, *, repo_name: str, owner_name: str, base_url: str = DEFAULT_B
def _repo_url(self) -> str:
return f"{self._base_url}/{self._owner_name}/{self._repo_name}"

def set(self, key: str, value: utils_types.Literal) -> None:
def __setitem__(self, key: str, value: utils_types.Literal) -> None:
cli_utils.GH.secret.set(key=key, value=str(value), repo_url=self._repo_url)

def clean(self, key: str) -> None:
def __delitem__(self, key: str) -> None:
try:
cli_utils.GH.secret.delete(key=key, repo_url=self._repo_url)
except cli_utils.GH.secret.KeyNotFoundError:
Expand Down
4 changes: 2 additions & 2 deletions secret_transfer/default/transfer/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ def run(self) -> None:
raise self.CollectionError("Collection failed") from exc

for key, value in items:
destination.set(key=key, value=value)
destination[key] = value

def clean(self) -> None:
collection = self._collection
destination = self._destination

for key in collection:
destination.clean(key=key)
del destination[key]
4 changes: 2 additions & 2 deletions secret_transfer/protocol/destination.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

@typing.runtime_checkable
class DestinationProtocol(base.BaseResourceProtocol, typing.Protocol):
def set(self, key: str, value: utils_types.Literal) -> None:
def __setitem__(self, key: str, value: utils_types.Literal) -> None:
...

def clean(self, key: str) -> None:
def __delitem__(self, key: str) -> None:
...
4 changes: 2 additions & 2 deletions tests/unit/default/destination/test_bash_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def test_set(mocker: pytest_mock.MockFixture):
mocked_print = mocker.patch("builtins.print")

destination = secret_transfer.BashExportDestination()
destination.set(TEST_KEY, TEST_VALUE)
destination[TEST_KEY] = TEST_VALUE

mocked_print.assert_called_once_with(f"export {TEST_KEY}={TEST_VALUE}")

Expand All @@ -28,6 +28,6 @@ def test_clean(mocker: pytest_mock.MockFixture):
mocked_print = mocker.patch("builtins.print")

destination = secret_transfer.BashExportDestination()
destination.clean(TEST_KEY)
del destination[TEST_KEY]

mocked_print.assert_called_once_with(f"unset {TEST_KEY}")
8 changes: 4 additions & 4 deletions tests/unit/default/destination/test_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def test_registered():

def test_set():
destination = secret_transfer.EnvDestination()
destination.set(TEST_KEY, TEST_VALUE)
destination[TEST_KEY] = TEST_VALUE

assert os.environ.get(TEST_KEY) == TEST_VALUE

Expand All @@ -26,7 +26,7 @@ def test_set_overwrite():
os.environ[TEST_KEY] = "old_value"

destination = secret_transfer.EnvDestination()
destination.set(TEST_KEY, TEST_VALUE)
destination[TEST_KEY] = TEST_VALUE

assert os.environ.get(TEST_KEY) == TEST_VALUE

Expand All @@ -35,13 +35,13 @@ def test_clean():
os.environ[TEST_KEY] = TEST_VALUE

destination = secret_transfer.EnvDestination()
destination.clean(TEST_KEY)
del destination[TEST_KEY]

assert os.environ.get(TEST_KEY) is None


def test_clean_not_existing():
destination = secret_transfer.EnvDestination()
destination.clean(TEST_KEY)
del destination[TEST_KEY]

assert os.environ.get(TEST_KEY) is None
6 changes: 3 additions & 3 deletions tests/unit/default/destination/test_gh_cli_secrets.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def test_set(gh_mock: test_utils.Mock):
owner_name=TEST_ORGANIZATION_NAME,
repo_name=TEST_REPO_NAME,
)
destination.set(key=TEST_KEY, value=TEST_VALUE)
destination[TEST_KEY] = TEST_VALUE
gh_mock.secret.set.assert_called_once_with(
key=TEST_KEY,
value=TEST_VALUE,
Expand All @@ -61,7 +61,7 @@ def test_clean(gh_mock: test_utils.Mock):
owner_name=TEST_ORGANIZATION_NAME,
repo_name=TEST_REPO_NAME,
)
destination.clean(key=TEST_KEY)
del destination[TEST_KEY]
gh_mock.secret.delete.assert_called_once_with(
key=TEST_KEY,
repo_url=TEST_REPO_URL,
Expand All @@ -75,7 +75,7 @@ def test_clean_not_existing(gh_mock: test_utils.Mock):
)
gh_mock.secret.delete.side_effect = gh_mock.secret.KeyNotFoundError

destination.clean(key=TEST_KEY)
del destination[TEST_KEY]

gh_mock.secret.delete.assert_called_once_with(
key=TEST_KEY,
Expand Down
12 changes: 6 additions & 6 deletions tests/unit/default/transfer/test_default.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def fixture_test_collection(mocker: pytest_mock.MockerFixture) -> test_utils.Mag

@pytest.fixture(name="test_destination")
def fixture_test_destination(mocker: pytest_mock.MockerFixture) -> test_utils.Mock:
test_destination = mocker.Mock(spec=protocol.DestinationProtocol)
test_destination = mocker.MagicMock(spec=protocol.DestinationProtocol)
return test_destination


Expand All @@ -60,9 +60,9 @@ def test_run(test_collection: test_utils.Mock, test_destination: test_utils.Mock

transfer.run()
test_collection.items.assert_called_once_with()
test_destination.set.assert_has_calls(
test_destination.__setitem__.assert_has_calls(
calls=[
((), {"key": TEST_KEY, "value": TEST_VALUE}),
((TEST_KEY, TEST_VALUE), {}),
],
any_order=True,
)
Expand All @@ -82,7 +82,7 @@ def test_run_collection_error(
with pytest.raises(secret_transfer.DefaultTransfer.CollectionError):
transfer.run()

test_destination.set.assert_not_called()
test_destination.__setitem__.assert_not_called()


def test_clean(test_collection: test_utils.Mock, test_destination: test_utils.Mock):
Expand All @@ -94,9 +94,9 @@ def test_clean(test_collection: test_utils.Mock, test_destination: test_utils.Mo
)
transfer.clean()
test_collection.__iter__.assert_called_once_with()
test_destination.clean.assert_has_calls(
test_destination.__delitem__.assert_has_calls(
calls=[
((), {"key": TEST_KEY}),
((TEST_KEY,), {}),
],
any_order=True,
)

0 comments on commit 3dcc90c

Please sign in to comment.