diff --git a/silkaj/g1_monetary_license.py b/silkaj/g1_monetary_license.py index ea623c3ce729eb5740de455f0a6dc1194d2d969e..a651fdd09ec1b9737385068a7053a5d4bf580b90 100644 --- a/silkaj/g1_monetary_license.py +++ b/silkaj/g1_monetary_license.py @@ -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( diff --git a/silkaj/money/transfer.py b/silkaj/money/transfer.py index 0d32cd2cbf21e49e7d498410e173fe687a673201..ca821d3b99a6674ecb9c6e668ca035ad3100c35f 100644 --- a/silkaj/money/transfer.py +++ b/silkaj/money/transfer.py @@ -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: diff --git a/silkaj/wot/revocation.py b/silkaj/wot/revocation.py index 1d83396dc885dc6b6e3674e2ee63aa53a646a09a..263bcf77ddc4053ff822e88284be63d5e159c582 100644 --- a/silkaj/wot/revocation.py +++ b/silkaj/wot/revocation.py @@ -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) diff --git a/tests/unit/money/test_transfer_file.py b/tests/unit/money/test_transfer_file.py index 95965a3709dd6754284c3aa568439307cf39cb65..dd1424a50cdb2a3185cf247ec38ec3f9b6c6af29 100644 --- a/tests/unit/money/test_transfer_file.py +++ b/tests/unit/money/test_transfer_file.py @@ -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 diff --git a/tests/unit/test_checksum.py b/tests/unit/test_checksum.py index 8652e85a8dec6966fe3df7d4568b4ae09c6d8fc0..62325534c888888da8e96a4b8a5e151ba15222fa 100644 --- a/tests/unit/test_checksum.py +++ b/tests/unit/test_checksum.py @@ -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" diff --git a/tests/unit/test_g1_monetary_license.py b/tests/unit/test_g1_monetary_license.py index b7c28d26dab5e0873ee54040b9b528101b2e1c02..ccba52922bd188ee8e85414289cc1d76d9b24dd6 100644 --- a/tests/unit/test_g1_monetary_license.py +++ b/tests/unit/test_g1_monetary_license.py @@ -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 diff --git a/tests/unit/wot/test_revocation.py b/tests/unit/wot/test_revocation.py index aa69e2555fa58800e21f1277f7f5d3e4223dc490..42713a058b294cf395bae88cacc7be87f1ab37a7 100644 --- a/tests/unit/wot/test_revocation.py +++ b/tests/unit/wot/test_revocation.py @@ -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