Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Features
Bug Fixes
---------
* Correct how password help is rendered in the helpdoc.
* Respect `--no-show-warnings`, overriding settings in `~/.myclirc`.


Internal
Expand Down
14 changes: 7 additions & 7 deletions mycli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,6 @@ def __init__(
defaults_file: str | None = None,
login_path: str | None = None,
auto_vertical_output: bool = False,
show_warnings: bool = False,
warn: bool | None = None,
myclirc: str = "~/.myclirc",
) -> None:
Expand Down Expand Up @@ -277,7 +276,7 @@ def __init__(

# read from cli argument or user config file
self.auto_vertical_output = auto_vertical_output or c["main"].as_bool("auto_vertical_output")
self.show_warnings = show_warnings or c["main"].as_bool("show_warnings")
self.show_warnings = c["main"].as_bool("show_warnings")

# Write user config if system config wasn't the last config loaded.
if c.filename not in self.system_config_files and not os.path.exists(myclirc):
Expand Down Expand Up @@ -608,6 +607,7 @@ def connect(
use_keyring: bool | None = None,
reset_keyring: bool | None = None,
keepalive_ticks: int | None = None,
show_warnings: bool | None = None,
) -> None:
cnf = {
"database": None,
Expand Down Expand Up @@ -637,6 +637,8 @@ def connect(
ssl_config: dict[str, Any] = ssl or {}
user_connection_config = self.config_without_package_defaults.get('connection', {})
self.keepalive_ticks = keepalive_ticks
if show_warnings is not None:
self.show_warnings = show_warnings

int_port = port and int(port)
if not int_port:
Expand Down Expand Up @@ -2093,9 +2095,10 @@ class CliArgs:
is_flag=True,
help='Automatically switch to vertical output mode if the result is wider than the terminal width.',
)
show_warnings: bool = clickdc.option(
show_warnings: bool | None = clickdc.option(
'--show-warnings/--no-show-warnings',
is_flag=True,
default=None,
clickdc=None,
help='Automatically show warnings after executing a SQL statement.',
)
Expand Down Expand Up @@ -2517,10 +2520,6 @@ def get_password_from_file(password_file: str | None) -> str | None:

combined_init_cmd = "; ".join(cmd.strip() for cmd in init_cmds if cmd)

# --show-warnings / --no-show-warnings
if cli_args.show_warnings:
mycli.show_warnings = cli_args.show_warnings

if cli_args.use_keyring is not None and cli_args.use_keyring.lower() == 'reset':
use_keyring = True
reset_keyring = True
Expand Down Expand Up @@ -2627,6 +2626,7 @@ def get_password_from_file(password_file: str | None) -> str | None:
use_keyring=use_keyring,
reset_keyring=reset_keyring,
keepalive_ticks=keepalive_ticks,
show_warnings=cli_args.show_warnings,
)

if combined_init_cmd:
Expand Down
43 changes: 43 additions & 0 deletions test/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,49 @@ def test_output_with_warning_and_show_warnings_disabled(executor):
assert expected not in result.output


@dbtest
def test_no_show_warnings_overrides_myclirc_setting(executor):
runner = CliRunner()
sql = 'EXPLAIN SELECT 1'
expected = 'select 1'

with NamedTemporaryFile(prefix=TEMPFILE_PREFIX, mode='w', delete=False) as myclirc:
myclirc.write(
dedent("""\
[main]
show_warnings = True
""")
)
myclirc.flush()
args = [
'--user',
USER,
'--host',
HOST,
'--port',
PORT,
'--password',
PASSWORD,
'--myclirc',
myclirc.name,
'--defaults-file',
default_config_file,
TEST_DATABASE,
]

result = runner.invoke(click_entrypoint, args=args, input=sql)
assert expected in result.output

result = runner.invoke(click_entrypoint, args=args + ['--no-show-warnings'], input=sql)
assert expected not in result.output

try:
if os.path.exists(myclirc.name):
os.remove(myclirc.name)
except Exception as e:
print(f"An error occurred while attempting to delete the file: {e}")


@dbtest
def test_output_with_multiple_warnings_in_single_statement(executor):
runner = CliRunner()
Expand Down
Loading