From fe0db5d6772c29032d2466b5863915be3073692a Mon Sep 17 00:00:00 2001 From: Josef Friedrich Date: Fri, 19 Jan 2024 16:03:38 +0100 Subject: [PATCH] Move read_file from test helper to utils --- mscxyz/score.py | 3 +-- mscxyz/utils.py | 9 +++++++++ tests/helper.py | 7 ------- tests/test_meta.py | 8 ++++---- tests/test_score.py | 6 ++---- tests/test_utils.py | 2 +- 6 files changed, 17 insertions(+), 18 deletions(-) diff --git a/mscxyz/score.py b/mscxyz/score.py index 719320d..7d8d049 100644 --- a/mscxyz/score.py +++ b/mscxyz/score.py @@ -283,8 +283,7 @@ def read_as_text(self) -> str: :return: The content of the MuseScore XML file as text. """ - with open(self.xml_file, "r") as f: - return f.read() + return utils.read_file(self.xml_file) def reload(self, save: bool = False) -> Score: """ diff --git a/mscxyz/utils.py b/mscxyz/utils.py index 726409c..4a0f2a3 100644 --- a/mscxyz/utils.py +++ b/mscxyz/utils.py @@ -591,3 +591,12 @@ def save(self, dest: str | Path) -> None: for file_name in files: zip.write(root / file_name, relpath / file_name) zip.close() + + +def read_file(filename: str | Path) -> str: + """Read the MuseScore XML file as text. + + :return: The content of the MuseScore XML file as text. + """ + with open(filename, "r") as f: + return f.read() diff --git a/tests/helper.py b/tests/helper.py index 2db43bf..100a9e8 100644 --- a/tests/helper.py +++ b/tests/helper.py @@ -104,13 +104,6 @@ def reload(src: Score | str | Path) -> Score: return Score(src.path) -def read_file(filename: str | Path) -> str: - tmp = open(filename) - output = tmp.read() - tmp.close() - return output - - def get_file_type(file: str | Path) -> str: """Get the type of a file using the `file` command.""" output: str = subprocess.check_output( diff --git a/tests/test_meta.py b/tests/test_meta.py index 8a23493..5cdea2c 100644 --- a/tests/test_meta.py +++ b/tests/test_meta.py @@ -9,7 +9,7 @@ import mscxyz import mscxyz.meta -from mscxyz import meta, supported_versions +from mscxyz import meta, supported_versions, utils from mscxyz.meta import ( Interface, InterfaceReadOnly, @@ -1078,13 +1078,13 @@ def test_legacy(self) -> None: ).score() json = score.json_file assert json.exists() - assert '"readonly_basename": "meta-all-values"' in helper.read_file(json) + assert '"readonly_basename": "meta-all-values"' in utils.read_file(json) def test_json(self) -> None: score = (Cli("--json").append_score("meta-all-values.mscz").execute()).score() json = score.json_file assert json.exists() - assert '"readonly_basename": "meta-all-values"' in helper.read_file(json) + assert '"readonly_basename": "meta-all-values"' in utils.read_file(json) class TestClassMeta: @@ -1114,7 +1114,7 @@ def test_method_export_json(self) -> None: result_path: Path = self.meta.export_json() assert result_path.exists() - json: str = helper.read_file(result_path) + json: str = utils.read_file(result_path) # json = ( # '{\n "combined_composer": "vbox_composer",\n' # ' "combined_lyricist": "vbox_lyricist",\n' diff --git a/tests/test_score.py b/tests/test_score.py index e03fdbb..3b79b88 100644 --- a/tests/test_score.py +++ b/tests/test_score.py @@ -62,14 +62,12 @@ def test_property_basename(self) -> None: def test_method_save(self) -> None: score: Score = helper.get_score("simple.mscx") score.save() - result = helper.read_file(score.path) - assert '' in result + assert '' in score.read_as_text() def test_method_save_new_name(self) -> None: score: Score = helper.get_score("simple.mscx") score.save(new_dest=str(score.path)) - result = helper.read_file(score.path) - assert '' in result + assert '' in score.read_as_text() def test_mscz(self) -> None: score: Score = helper.get_score("simple.mscz") diff --git a/tests/test_utils.py b/tests/test_utils.py index c1d78fd..6066bcd 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -155,7 +155,7 @@ def test_xml_write(tmp_path: Path) -> None: dest = tmp_path / "test.xml" element = lxml.etree.XML("") utils.xml.write(dest, element) - result: str = helper.read_file(dest) + result: str = utils.read_file(dest) print(result) assert result == ( '\n'