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 @@ -6,6 +6,7 @@ Bug Fixes
* Refresh completions after `CREATE`, `ALTER`, `DROP` or `RENAME` statements that start with a SQL comment.
* Reset completions after `USE`, `\u`, `\r` or `connect` statements that start with a SQL comment.
* Don't flag a single-table `UPDATE` with a `WHERE` clause as destructive when it starts with a `/* ... */` comment.
* Read the password file and the contributors, sponsors and tips files as UTF-8 instead of the locale encoding.


Internal
Expand Down
2 changes: 1 addition & 1 deletion mycli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ def get_password_from_file(password_file: str | None) -> str | None:
if not password_file:
return None
try:
with open(password_file) as fp:
with open(password_file, encoding='utf-8') as fp:
return fp.readline().removesuffix('\n')
except FileNotFoundError:
click.secho(f"Password file '{password_file}' not found", err=True, fg='red')
Expand Down
6 changes: 3 additions & 3 deletions mycli/main_modes/repl.py
Original file line number Diff line number Diff line change
Expand Up @@ -1170,7 +1170,7 @@ def _contributors_picker() -> str:
lines: str = ""

try:
with resources.files(mycli_package).joinpath("AUTHORS.txt").open('r') as f:
with resources.files(mycli_package).joinpath("AUTHORS.txt").open('r', encoding='utf-8') as f:
lines += f.read()
except FileNotFoundError:
pass
Expand All @@ -1186,7 +1186,7 @@ def _sponsors_picker() -> str:
lines: str = ""

try:
with resources.files(mycli_package).joinpath("SPONSORS.txt").open('r') as f:
with resources.files(mycli_package).joinpath("SPONSORS.txt").open('r', encoding='utf-8') as f:
lines += f.read()
except FileNotFoundError:
pass
Expand All @@ -1202,7 +1202,7 @@ def _tips_picker() -> str:
tips = []

try:
with resources.files(mycli_package).joinpath('TIPS.txt').open('r') as f:
with resources.files(mycli_package).joinpath('TIPS.txt').open('r', encoding='utf-8') as f:
for line in f:
if line.startswith("#"):
continue
Expand Down
19 changes: 19 additions & 0 deletions mycli_test/pytests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from pathlib import Path
import runpy
import shutil
import subprocess
import sys
from tempfile import NamedTemporaryFile
from textwrap import dedent
Expand Down Expand Up @@ -1405,6 +1406,24 @@ def close(self, **args):
assert resolve_connect_password(MockMyCli.connect_args) == ('literal', 'cleartext_password')


def test_get_password_from_file_does_not_use_locale_encoding(tmp_path: Path) -> None:
# EncodingWarning is raised for any open() that falls back to the locale encoding.
password_file = tmp_path / 'password.txt'
password_file.write_text('p\u00e4ssw\u00f6rd\n', encoding='utf-8')
script = '\n'.join((
'import sys',
'from mycli.main import get_password_from_file',
'assert get_password_from_file(sys.argv[1]) == "p\u00e4ssw\u00f6rd"',
))
result = subprocess.run(
[sys.executable, '-X', 'warn_default_encoding', '-W', 'error::EncodingWarning', '-c', script, str(password_file)],
capture_output=True,
encoding='utf8',
errors='replace',
)
assert result.returncode == 0, result.stderr


def test_password_option_overrides_password_file_and_mysql_pwd(monkeypatch):
class Formatter:
format_name = None
Expand Down
21 changes: 20 additions & 1 deletion mycli_test/pytests/test_main_modes_repl.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from io import StringIO
import os
from pathlib import Path
import subprocess
import sys
from threading import Event
from types import SimpleNamespace
from typing import Any, Literal, cast
Expand Down Expand Up @@ -134,7 +136,7 @@ def __init__(self, files: dict[str, str], path: str | None = None) -> None:
def joinpath(self, path: str) -> 'FakeResourceTree':
return FakeResourceTree(self.files, path)

def open(self, mode: str) -> StringIO:
def open(self, mode: str, encoding: str | None = None) -> StringIO:
assert self.path is not None
if self.path not in self.files:
raise FileNotFoundError(self.path)
Expand Down Expand Up @@ -820,6 +822,23 @@ def test_repl_picker_helpers_cover_present_and_missing_resources(monkeypatch: py
assert repl_mode._tips_picker() == r'/? or /help for help!'


def test_repl_picker_helpers_do_not_use_locale_encoding() -> None:
# EncodingWarning is raised for any open() that falls back to the locale encoding.
script = '\n'.join((
'from mycli.main_modes import repl',
'repl._contributors_picker()',
'repl._sponsors_picker()',
'repl._tips_picker()',
))
result = subprocess.run(
[sys.executable, '-X', 'warn_default_encoding', '-W', 'error::EncodingWarning', '-c', script],
capture_output=True,
encoding='utf8',
errors='replace',
)
assert result.returncode == 0, result.stderr


def test_configure_editor_uses_configured_editor(monkeypatch: pytest.MonkeyPatch) -> None:
cli = make_repl_cli()
cli.config['editor']['editor_command'] = 'nano'
Expand Down
Loading