Skip to content
Snippets Groups Projects
Commit 0ce91f8e authored by Moul's avatar Moul
Browse files

Fix PTH123 `open("foo")` should be replaced by `Path("foo").open()`

Use Path().{read,write}_txt() when possible
parent fd5eb09b
No related branches found
No related tags found
No related merge requests found
......@@ -51,8 +51,7 @@ class G1MonetaryLicense:
"""
selected_language_code = self.language_prompt()
license_path = self.get_license_path(selected_language_code)
with open(license_path, encoding="utf-8") as _license:
click.echo_via_pager(_license.read())
click.echo_via_pager(license_path.read_text(encoding="utf-8"))
def language_prompt(self) -> str:
return click.prompt(
......
......@@ -17,6 +17,7 @@ import math
import re
import shlex
import time
from pathlib import Path
from typing import List, Optional, Tuple
import click
......@@ -212,7 +213,7 @@ def parse_file_containing_amounts_recipients(
"""
reference = ""
amounts, recipients = [], []
with open(file_path, encoding="utf-8") as file:
with Path(file_path).open(encoding="utf-8") as file:
for n, raw_line in enumerate(file):
line = shlex.split(raw_line, True)
if line:
......
......@@ -196,9 +196,7 @@ generated revocation document corresponding to {pubkey_cksum} public key?",
else:
click.echo("Ok, goodbye!")
sys.exit(SUCCESS_EXIT_STATUS)
# write doc
with open(rev_path, "w", encoding="utf-8") as file:
file.write(content)
rev_path.write_text(content, encoding="utf-8")
click.echo(
f"Revocation document file stored into `{path}` for following public key: {pubkey_cksum}",
)
......@@ -218,8 +216,7 @@ def verify_document(doc: str) -> Revocation:
if not Path(doc).is_file():
sys.exit(f"Error: file {doc} does not exist")
with open(doc, encoding="utf-8") as document:
original_doc = document.read()
original_doc = Path(doc).read_text(encoding="utf-8")
try:
rev_doc = Revocation.from_signed_raw(original_doc)
......
......@@ -13,6 +13,8 @@
# You should have received a copy of the GNU Affero General Public License
# along with Silkaj. If not, see <https://www.gnu.org/licenses/>.
from pathlib import Path
import pytest
from click.testing import CliRunner
......@@ -50,8 +52,7 @@ def test_parse_file_containing_amounts_recipients(
runner = CliRunner()
with runner.isolated_filesystem():
with open(FILE_PATH, "w", encoding="utf-8") as f:
f.write(file_content)
Path(FILE_PATH).write_text(file_content, encoding="utf-8")
amounts, recipients = parse_file_containing_amounts_recipients(FILE_PATH)
assert amounts == amounts_exp
assert recipients == recipients_exp
......@@ -77,8 +78,7 @@ SPEC_ERR = "No amounts or recipients specified"
def test_parse_file_containing_amounts_recipients_errors(file_content, error, capsys):
runner = CliRunner()
with runner.isolated_filesystem():
with open(FILE_PATH, "w", encoding="utf-8") as f:
f.write(file_content)
Path(FILE_PATH).write_text(file_content, encoding="utf-8")
with pytest.raises(SystemExit):
parse_file_containing_amounts_recipients(FILE_PATH)
assert error in capsys.readouterr().out
......@@ -13,6 +13,8 @@
# You should have received a copy of the GNU Affero General Public License
# along with Silkaj. If not, see <https://www.gnu.org/licenses/>.
from pathlib import Path
import pytest
from click.testing import CliRunner
......@@ -41,7 +43,6 @@ pubkey_seedhex_authfile = (
)
def test_checksum_command(command, excepted_output):
with CliRunner().isolated_filesystem():
with open("authfile", "w", encoding="utf-8") as f:
f.write(pubkey_seedhex_authfile)
Path("authfile").write_text(pubkey_seedhex_authfile, encoding="utf-8")
result = CliRunner().invoke(cli, args=command)
assert result.output == f"{excepted_output}\n"
......@@ -13,6 +13,7 @@
# You should have received a copy of the GNU Affero General Public License
# along with Silkaj. If not, see <https://www.gnu.org/licenses/>.
from pathlib import Path
from unittest.mock import patch
import pytest
......@@ -78,8 +79,7 @@ def test_long_language_code_handling():
license_path = g1ml.get_license_path(language_code)
runner = CliRunner()
with runner.isolated_filesystem():
with open(license_path, "w", encoding="utf-8") as f:
f.write(content)
license_path.write_text(content, encoding="utf-8")
result = runner.invoke(cli.cli, args=["license"], input=language_code)
assert language_code in result.output
assert content in result.output
......
......@@ -184,8 +184,7 @@ def test_revocation_cli_dry_run(subcommand, expected_warn, monkeypatch):
file = "revocation.txt"
runner = CliRunner()
with runner.isolated_filesystem():
with open(file, "w", encoding="utf-8") as f:
f.write(REV_DOC.signed_raw())
Path(file).write_text(REV_DOC.signed_raw(), encoding="utf-8")
result = runner.invoke(cli, args=command)
assert "6upqFiJ66WV6N3bPc8x8y7rXT3syqKRmwnVyunCtEj7o" in result.output
assert "Version: 10" in result.output
......@@ -433,8 +432,7 @@ def test_revocation_cli_verify(
# verify file
runner = CliRunner()
with runner.isolated_filesystem():
with open(file, "w", encoding="utf-8") as f:
f.write(doc.signed_raw())
Path(file).write_text(doc.signed_raw(), encoding="utf-8")
result = runner.invoke(cli, args=command)
for expect in expected:
assert expect in result.output
......@@ -706,8 +704,7 @@ def test_revocation_cli_publish(
# test publication
runner = CliRunner()
with runner.isolated_filesystem():
with open(file, "w", encoding="utf-8") as f:
f.write(doc.signed_raw())
Path(file).write_text(doc.signed_raw(), encoding="utf-8")
result = runner.invoke(cli, args=command, input=user_input)
if user_input == "yes\n" and not dry_run:
patched_send_bma_revoke.assert_called_once_with(
......@@ -794,8 +791,7 @@ def test_revocation_cli_publish_send_errors(
# test publication
runner = CliRunner()
with runner.isolated_filesystem():
with open(file, "w", encoding="utf-8") as f:
f.write(REV_DOC.signed_raw())
Path(file).write_text(REV_DOC.signed_raw(), encoding="utf-8")
result = runner.invoke(cli, args=command, input=user_input)
for expect in expected:
assert expect in result.output
......@@ -974,16 +970,14 @@ def test_save_doc(path, rev_1, rev_2, pubkey, capsys, monkeypatch):
test_path = Path(path)
revocation.save_doc(path, rev_1.signed_raw(), pubkey)
assert test_path.is_file()
with open(path, encoding="utf-8") as f:
assert f.read() == rev_1.signed_raw()
assert Path(path).read_text(encoding="utf-8") == rev_1.signed_raw()
# test file is overwritten if confirm
monkeypatch.setattr(click, "confirm", value=conf_true)
revocation.save_doc(path, rev_2.signed_raw(), pubkey)
expected_confirm = f"Revocation document file stored into `{path}` \
for following public key: {gen_pubkey_checksum(pubkey)}"
assert expected_confirm in capsys.readouterr().out
with open(path, encoding="utf-8") as f:
assert f.read() == rev_2.signed_raw()
assert Path(path).read_text(encoding="utf-8") == rev_2.signed_raw()
# test file is not overwritten if not confirm
monkeypatch.setattr(click, "confirm", value=conf_false)
with pytest.raises(SystemExit) as pytest_exit:
......@@ -992,8 +986,7 @@ for following public key: {gen_pubkey_checksum(pubkey)}"
assert pytest_exit.value.code == SUCCESS_EXIT_STATUS
expected_confirm = "Ok, goodbye!"
assert expected_confirm in capsys.readouterr().out
with open(path, encoding="utf-8") as f:
assert f.read() == rev_2.signed_raw()
assert Path(path).read_text(encoding="utf-8") == rev_2.signed_raw()
# test verify_document
......@@ -1015,8 +1008,7 @@ def test_verify_document(doc, lookup, capsys, monkeypatch):
# test
runner = CliRunner()
with runner.isolated_filesystem():
with open(path, "w", encoding="utf-8") as f:
f.write(doc.signed_raw())
Path(path).write_text(doc.signed_raw(), encoding="utf-8")
result = revocation.verify_document(path)
display = capsys.readouterr().out
if len(lookup["results"]) > 1:
......@@ -1054,8 +1046,7 @@ def test_verify_document_missing_id(doc, lookup, capsys, monkeypatch):
# test
runner = CliRunner()
with runner.isolated_filesystem():
with open(path, "w", encoding="utf-8") as f:
f.write(doc.signed_raw())
Path(path).write_text(doc.signed_raw(), encoding="utf-8")
with pytest.raises(SystemExit) as pytest_exit:
revocation.verify_document(path)
assert pytest_exit.type == SystemExit
......@@ -1089,11 +1080,11 @@ def test_verify_document_sign_errors(doc, currency, monkeypatch):
# test
runner = CliRunner()
with runner.isolated_filesystem():
with open(path, "w", encoding="utf-8") as f:
if isinstance(doc, str):
f.write(doc)
elif isinstance(doc, Revocation):
f.write(doc.signed_raw())
file = Path(path)
if isinstance(doc, str):
file.write_text(doc, encoding="utf-8")
elif isinstance(doc, Revocation):
file.write_text(doc.signed_raw(), encoding="utf-8")
with pytest.raises(SystemExit) as pytest_exit:
revocation.verify_document(path)
assert pytest_exit.type == SystemExit
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment