Skip to content

Commit

Permalink
Fix encoding warnings with PEP 597 enabled (#398)
Browse files Browse the repository at this point in the history
Co-authored-by: Bartosz Sławecki <[email protected]>
  • Loading branch information
danyeaw and Bartosz Sławecki authored Feb 8, 2024
1 parent e8710e2 commit d091b7c
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 48 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ on:
push:
branches: [main]

env:
PYTHONWARNDEFAULTENCODING: true

jobs:
Tests:
name: ${{ matrix.os }} / ${{ matrix.python-version }}
Expand Down
1 change: 1 addition & 0 deletions news/398.misc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix encoding warnings with PEP 597 enabled
2 changes: 1 addition & 1 deletion src/cleo/ui/exception_trace/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def file_content(self) -> str:
return ""
if self._filename not in type(self)._content_cache:
try:
file_content = Path(self._filename).read_text()
file_content = Path(self._filename).read_text(encoding="utf-8")
except OSError:
file_content = ""
type(self)._content_cache[self._filename] = file_content
Expand Down
2 changes: 1 addition & 1 deletion src/cleo/ui/question.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ def _read_from_input(self, io: IO) -> str:
return ret.strip()

def _has_stty_available(self) -> bool:
with Path(os.devnull).open("w") as devnull:
with Path(os.devnull).open("w", encoding="utf-8") as devnull:
try:
exit_code = subprocess.call(["stty"], stdout=devnull, stderr=devnull)
except Exception:
Expand Down
14 changes: 11 additions & 3 deletions tests/commands/completion/test_completions_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ def test_bash(mocker: MockerFixture) -> None:
tester = CommandTester(command)
tester.execute("bash")

with open(os.path.join(os.path.dirname(__file__), "fixtures", "bash.txt")) as f:
with open(
os.path.join(os.path.dirname(__file__), "fixtures", "bash.txt"),
encoding="utf-8",
) as f:
expected = f.read()

assert expected == tester.io.fetch_output().replace("\r\n", "\n")
Expand All @@ -70,7 +73,9 @@ def test_zsh(mocker: MockerFixture) -> None:
tester = CommandTester(command)
tester.execute("zsh")

with open(os.path.join(os.path.dirname(__file__), "fixtures", "zsh.txt")) as f:
with open(
os.path.join(os.path.dirname(__file__), "fixtures", "zsh.txt"), encoding="utf-8"
) as f:
expected = f.read()

assert expected == tester.io.fetch_output().replace("\r\n", "\n")
Expand All @@ -92,7 +97,10 @@ def test_fish(mocker: MockerFixture) -> None:
tester = CommandTester(command)
tester.execute("fish")

with open(os.path.join(os.path.dirname(__file__), "fixtures", "fish.txt")) as f:
with open(
os.path.join(os.path.dirname(__file__), "fixtures", "fish.txt"),
encoding="utf-8",
) as f:
expected = f.read()

assert expected == tester.io.fetch_output().replace("\r\n", "\n")
74 changes: 33 additions & 41 deletions tests/test_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ def test_long_version() -> None:


def test_help(app: Application) -> None:
assert app.help == FIXTURES_PATH.joinpath("application_help.txt").read_text()
assert app.help == FIXTURES_PATH.joinpath("application_help.txt").read_text(
encoding="utf-8"
)


def test_all(app: Application) -> None:
Expand Down Expand Up @@ -218,10 +220,9 @@ def test_set_catch_exceptions(app: Application, environ: dict[str, str]) -> None
tester.execute("foo", decorated=False)

assert tester.io.fetch_output() == ""
assert (
tester.io.fetch_error()
== FIXTURES_PATH.joinpath("application_exception1.txt").read_text()
)
assert tester.io.fetch_error() == FIXTURES_PATH.joinpath(
"application_exception1.txt"
).read_text(encoding="utf-8")

app.catch_exceptions(False)

Expand Down Expand Up @@ -256,42 +257,37 @@ def test_run(app: Application, argv: list[str]) -> None:
def test_run_runs_the_list_command_without_arguments(tester: ApplicationTester) -> None:
tester.execute("", decorated=False)

assert (
tester.io.fetch_output()
== FIXTURES_PATH.joinpath("application_run1.txt").read_text()
)
assert tester.io.fetch_output() == FIXTURES_PATH.joinpath(
"application_run1.txt"
).read_text(encoding="utf-8")


def test_run_runs_help_command_if_required(tester: ApplicationTester) -> None:
tester.execute("--help", decorated=False)

assert (
tester.io.fetch_output()
== FIXTURES_PATH.joinpath("application_run2.txt").read_text()
)
assert tester.io.fetch_output() == FIXTURES_PATH.joinpath(
"application_run2.txt"
).read_text(encoding="utf-8")

tester.execute("-h", decorated=False)

assert (
tester.io.fetch_output()
== FIXTURES_PATH.joinpath("application_run2.txt").read_text()
)
assert tester.io.fetch_output() == FIXTURES_PATH.joinpath(
"application_run2.txt"
).read_text(encoding="utf-8")


def test_run_runs_help_command_with_command(tester: ApplicationTester) -> None:
tester.execute("--help list", decorated=False)

assert (
tester.io.fetch_output()
== FIXTURES_PATH.joinpath("application_run3.txt").read_text()
)
assert tester.io.fetch_output() == FIXTURES_PATH.joinpath(
"application_run3.txt"
).read_text(encoding="utf-8")

tester.execute("list -h", decorated=False)

assert (
tester.io.fetch_output()
== FIXTURES_PATH.joinpath("application_run3.txt").read_text()
)
assert tester.io.fetch_output() == FIXTURES_PATH.joinpath(
"application_run3.txt"
).read_text(encoding="utf-8")


def test_run_removes_all_output_if_quiet(tester: ApplicationTester) -> None:
Expand Down Expand Up @@ -325,33 +321,29 @@ def test_run_with_verbosity(tester: ApplicationTester) -> None:
def test_run_with_version(tester: ApplicationTester) -> None:
tester.execute("--version")

assert (
tester.io.fetch_output()
== FIXTURES_PATH.joinpath("application_run4.txt").read_text()
)
assert tester.io.fetch_output() == FIXTURES_PATH.joinpath(
"application_run4.txt"
).read_text(encoding="utf-8")

tester.execute("-V")

assert (
tester.io.fetch_output()
== FIXTURES_PATH.joinpath("application_run4.txt").read_text()
)
assert tester.io.fetch_output() == FIXTURES_PATH.joinpath(
"application_run4.txt"
).read_text(encoding="utf-8")


def test_run_with_help(tester: ApplicationTester) -> None:
tester.execute("help --help")

assert (
tester.io.fetch_output()
== FIXTURES_PATH.joinpath("application_run5.txt").read_text()
)
assert tester.io.fetch_output() == FIXTURES_PATH.joinpath(
"application_run5.txt"
).read_text(encoding="utf-8")

tester.execute("-h help")

assert (
tester.io.fetch_output()
== FIXTURES_PATH.joinpath("application_run5.txt").read_text()
)
assert tester.io.fetch_output() == FIXTURES_PATH.joinpath(
"application_run5.txt"
).read_text(encoding="utf-8")


def test_run_with_input() -> None:
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/exception_trace/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def test_frame() -> None:
assert frame.function == "test_frame"
assert frame.line == " simple_exception()\n"

with open(__file__) as f:
with open(__file__, encoding="utf-8") as f:
assert f.read() == frame.file_content

assert repr(frame) == f"<Frame {__file__}, test_frame, 12>"
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/test_question.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@


def has_tty_available() -> bool:
with open(os.devnull, "w") as devnull:
with open(os.devnull, "w", encoding="utf-8") as devnull:
exit_code = subprocess.call(["stty", "2"], stdout=devnull, stderr=devnull)

return exit_code == 0
Expand Down

0 comments on commit d091b7c

Please sign in to comment.