Skip to content

Commit

Permalink
Move read_file from test helper to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
Josef-Friedrich committed Jan 19, 2024
1 parent ce0b556 commit fe0db5d
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 18 deletions.
3 changes: 1 addition & 2 deletions mscxyz/score.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand Down
9 changes: 9 additions & 0 deletions mscxyz/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
7 changes: 0 additions & 7 deletions tests/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
8 changes: 4 additions & 4 deletions tests/test_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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'
Expand Down
6 changes: 2 additions & 4 deletions tests/test_score.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 '<metaTag name="arranger"></metaTag>' in result
assert '<metaTag name="arranger"></metaTag>' 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 '<metaTag name="arranger"></metaTag>' in result
assert '<metaTag name="arranger"></metaTag>' in score.read_as_text()

def test_mscz(self) -> None:
score: Score = helper.get_score("simple.mscz")
Expand Down
2 changes: 1 addition & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def test_xml_write(tmp_path: Path) -> None:
dest = tmp_path / "test.xml"
element = lxml.etree.XML("<root><a><b/><c/></a><d><e/></d></root>")
utils.xml.write(dest, element)
result: str = helper.read_file(dest)
result: str = utils.read_file(dest)
print(result)
assert result == (
'<?xml version="1.0" encoding="UTF-8"?>\n'
Expand Down

0 comments on commit fe0db5d

Please sign in to comment.