-
Notifications
You must be signed in to change notification settings - Fork 0
Refactoring/260 added unit tests for cli param wrappers #261
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ckunki
merged 8 commits into
main
from
refactoring/260-Added_unit_tests_for_CLI_param_wrappers
Sep 25, 2025
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3c9c6b5
Added unit tests for SCS CLI parameter wrappers
ckunki f6a9411
updated changelog
ckunki f6bf6a7
project:fix
ckunki f9540d9
Simplified ScsSecretOption.displayed_value()
ckunki 9f0f014
added docstring to ScsMock
ckunki ae61220
Updated exasol-transformers-extension
ckunki c3d936f
Fixed review finding
ckunki f27e267
project:fix
ckunki File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,32 +2,71 @@ | |
| Wrappers for adding custom properties to click parameters, e.g. SCS key. | ||
| """ | ||
|
|
||
| import getpass | ||
| import os | ||
| import re | ||
| from abc import abstractmethod | ||
| from typing import Any | ||
|
|
||
| import click | ||
|
|
||
| from exasol.nb_connector.ai_lab_config import AILabConfig as CKey | ||
| from exasol.nb_connector.cli import reporting as report | ||
| from exasol.nb_connector.secret_store import Secrets | ||
|
|
||
|
|
||
| class ScsArgument: | ||
| class ScsParam: | ||
| """ | ||
| Represents a CLI argument for the SCS command. | ||
| Abstract base class for ScsArgument and ScsOption. | ||
| """ | ||
|
|
||
| def __init__(self, *args, scs_key: CKey | None = None, **kwargs): | ||
| self._args = args | ||
| def __init__(self, scs_key: CKey | None = None, **kwargs): | ||
| self.scs_key = scs_key | ||
| self._kwargs = kwargs | ||
|
|
||
| @property | ||
| def arg_name(self) -> str: | ||
| return "" | ||
|
|
||
| def needs_entry(self, scs: Secrets) -> bool: | ||
| return False | ||
|
|
||
| @property | ||
| def default(self) -> Any: | ||
| return self._kwargs.get("default") | ||
|
|
||
| def displayed_value(self, scs: Secrets) -> str | None: | ||
| return None | ||
|
|
||
| @abstractmethod | ||
| def decorate(self, func): | ||
| """ | ||
| This method is to be called when decorating the functions in the | ||
| actual CLI declaration. | ||
| """ | ||
| pass | ||
|
|
||
|
|
||
| class ScsArgument(ScsParam): | ||
| """ | ||
| Represents a CLI argument for the SCS command. | ||
| """ | ||
|
|
||
| def __init__(self, name: str, scs_key: CKey | None = None, **kwargs): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Much better |
||
| super().__init__(scs_key, **kwargs) | ||
| self.name = name | ||
|
|
||
| def decorate(self, func): | ||
| """ | ||
| This method is to be called when decorating the functions in the | ||
| actual CLI declaration. Hence, ScsArgument calls click.argument() | ||
| under the hood. | ||
| """ | ||
| decorator = click.argument(*self._args, **self._kwargs) | ||
| decorator = click.argument(self.name, **self._kwargs) | ||
| return decorator(func) | ||
|
|
||
|
|
||
| class ScsOption(ScsArgument): | ||
| class ScsOption(ScsParam): | ||
| """ | ||
| CLI option for saving and checking values to the Secure Configuration | ||
| Storage (SCS). | ||
|
|
@@ -53,30 +92,70 @@ class ScsOption(ScsArgument): | |
|
|
||
| def __init__( | ||
| self, | ||
| cli_option, | ||
| *args, | ||
| scs_key: CKey | None = None, | ||
| scs_alternative_key: CKey | None = None, | ||
| scs_required: bool = True, | ||
| get_default_from: str | None = None, | ||
| **kwargs, | ||
| ): | ||
| super().__init__(*args, scs_key=scs_key, **kwargs) | ||
| super().__init__(scs_key=scs_key, **kwargs) | ||
| self._cli_option = cli_option | ||
| self._args = args | ||
| self.scs_alternative_key = scs_alternative_key | ||
| self.scs_required = scs_required | ||
| self.get_default_from = get_default_from | ||
|
|
||
| def cli_option(self, full=False) -> str: | ||
| raw = self._cli_option | ||
| return raw if full else re.sub(r"/--.*$", "", raw) | ||
|
|
||
| @property | ||
| def arg_name(self) -> str: | ||
| for arg in self._args: | ||
| if not arg.startswith("--"): | ||
| return arg | ||
| name = self.cli_option() | ||
| return name[2:].replace("-", "_") | ||
|
|
||
| def decorate(self, func): | ||
| """ | ||
| This method is to be called when decorating the functions in the | ||
| actual CLI declaration. ScsOption calls click.option(). | ||
| """ | ||
| decorator = click.option( | ||
| self._cli_option, | ||
| *self._args, | ||
| **self._kwargs, | ||
| show_default=True, | ||
| ) | ||
| return decorator(func) | ||
|
|
||
| def displayed_value(self, scs: Secrets) -> str | None: | ||
| return scs.get(self.scs_key) if self.scs_key else None | ||
|
|
||
| def needs_entry(self, scs: Secrets) -> bool: | ||
| """ | ||
| Return True, if the current option is configured to be saved to | ||
| the SCS but SCS does not yet contain a value. | ||
| """ | ||
|
|
||
| def has_value() -> bool: | ||
| if not self.scs_key: | ||
| return False | ||
| if scs.get(self.scs_key) is not None: | ||
| return True | ||
| if alt := self.scs_alternative_key: | ||
| return scs.get(alt) is not None | ||
| return False | ||
|
|
||
| return bool(self.scs_key) and self.scs_required and not has_value() | ||
|
|
||
| def __repr__(self) -> str: | ||
| cls_name = type(self).__name__ | ||
| return f"{cls_name}<{self.cli_option(full=True)}>" | ||
|
|
||
|
|
||
| class ScsSecretOption(ScsOption): | ||
| """ | ||
|
|
@@ -107,6 +186,22 @@ def __init__( | |
| self.prompt = prompt | ||
| self.name = name | ||
|
|
||
| def displayed_value(self, scs: Secrets) -> str | None: | ||
| return "****" if self.scs_key and scs.get(self.scs_key) else None | ||
|
|
||
| def get_secret(self, interactive: Any) -> str: | ||
| """ | ||
| If interactive is True and the related environment variable is not | ||
| set then ask for the secret interactively. | ||
| """ | ||
| if value := os.getenv(self.envvar): | ||
| report.info(f"Reading {self.name} from environment variable {self.envvar}.") | ||
| return value | ||
| if not interactive: | ||
| return "" | ||
| prompt = f"{self.prompt} (option {self.name}): " | ||
| return getpass.getpass(prompt) | ||
|
|
||
|
|
||
| def add_params(scs_options: list[ScsArgument]): | ||
| def multi_decorator(func): | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import click | ||
|
|
||
|
|
||
| def info(text: str): | ||
| click.echo(click.style(text, fg="green")) | ||
|
|
||
|
|
||
| def success(text: str): | ||
| click.echo(click.style(text, fg="green")) | ||
|
|
||
|
|
||
| def error(text: str): | ||
| click.echo(click.style(f"Error: {text}", fg="bright_red")) | ||
|
|
||
|
|
||
| def warning(text: str): | ||
| click.echo(click.style(f"Warning: {text}", fg="yellow")) |
ckunki marked this conversation as resolved.
Show resolved
Hide resolved
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| from exasol.nb_connector.ai_lab_config import AILabConfig as CKey | ||
| from exasol.nb_connector.ai_lab_config import StorageBackend | ||
|
|
||
|
|
||
| class ScsMock: | ||
| """ | ||
| Instead of using a real Secure Configuration Storage, this mock | ||
| simpulates it using a simple dict(). | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| backend: StorageBackend | None = None, | ||
| use_itde: bool | None = None, | ||
| ): | ||
| self._dict = dict() | ||
| if backend: | ||
| self.save(CKey.storage_backend, backend.name) | ||
| if use_itde is not None: | ||
| self.save(CKey.use_itde, str(use_itde)) | ||
|
|
||
| def save(self, key: CKey, value: str) -> None: | ||
| self._dict[key.name] = str(value) | ||
|
|
||
| def get(self, key: CKey, default: str | None = None) -> str | None: | ||
| return self._dict.get(key.name, default) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This had been forgotten in the last PR