From 5ab3ac4f47c303d055ebbde3d2596fdd35dbc35c Mon Sep 17 00:00:00 2001 From: Stepland <16676308+Stepland@users.noreply.github.com> Date: Tue, 1 Jun 2021 19:38:31 +0200 Subject: [PATCH] Make dump_and_load_then_compare default to using a temporary directory Dump path format variables all need to be strings --- jubeatools/formats/dump_tools.py | 8 ++++---- .../jubeat_analyser/tests/memo/test_memo.py | 3 +-- .../jubeat_analyser/tests/memo1/test_memo1.py | 3 +-- .../jubeat_analyser/tests/memo2/test_memo2.py | 3 +-- .../mono_column/test_mono_column_hypothesis.py | 3 +-- jubeatools/formats/konami/eve/tests/test_eve.py | 3 +-- jubeatools/formats/konami/jbsq/test_jbsq.py | 3 +-- jubeatools/formats/konami/testutils.py | 10 ---------- jubeatools/formats/malody/tests/test_malody.py | 2 -- jubeatools/testutils/test_patterns.py | 14 +++++++++++--- 10 files changed, 21 insertions(+), 31 deletions(-) diff --git a/jubeatools/formats/dump_tools.py b/jubeatools/formats/dump_tools.py index ab7ec4e..4c00c9b 100644 --- a/jubeatools/formats/dump_tools.py +++ b/jubeatools/formats/dump_tools.py @@ -75,9 +75,9 @@ class FormatParameters(TypedDict, total=False): # uppercase BSC ADV EXT difficulty: str # 0-based - difficulty_index: int + difficulty_index: str # 1-based - difficulty_number: int + difficulty_number: str dedup: str @@ -85,8 +85,8 @@ def extract_format_params(chartfile: ChartFile, dedup_index: int) -> FormatParam return FormatParameters( title=chartfile.song.metadata.title or "", difficulty=chartfile.difficulty, - difficulty_index=DIFFICULTY_INDEX.get(chartfile.difficulty, 3), - difficulty_number=DIFFICULTY_NUMBER.get(chartfile.difficulty, 4), + difficulty_index=str(DIFFICULTY_INDEX.get(chartfile.difficulty, 2)), + difficulty_number=str(DIFFICULTY_NUMBER.get(chartfile.difficulty, 3)), dedup="" if dedup_index == 0 else f"-{dedup_index}", ) diff --git a/jubeatools/formats/jubeat_analyser/tests/memo/test_memo.py b/jubeatools/formats/jubeat_analyser/tests/memo/test_memo.py index 28a7654..7275340 100644 --- a/jubeatools/formats/jubeat_analyser/tests/memo/test_memo.py +++ b/jubeatools/formats/jubeat_analyser/tests/memo/test_memo.py @@ -13,7 +13,7 @@ from jubeatools.formats.jubeat_analyser.memo.load import MemoParser from jubeatools.testutils import strategies as jbst from jubeatools.testutils.test_patterns import dump_and_load_then_compare -from ..test_utils import memo_compatible_song, temp_file_named_txt +from ..test_utils import memo_compatible_song from . import example1, example2, example3 @@ -48,7 +48,6 @@ def test_that_full_chart_roundtrips(song: song.Song, circle_free: bool) -> None: dump_and_load_then_compare( Format.MEMO, song, - temp_path=temp_file_named_txt(), bytes_decoder=lambda b: b.decode("shift-jis-2004", errors="surrogateescape"), dump_options={"circle_free": circle_free}, ) diff --git a/jubeatools/formats/jubeat_analyser/tests/memo1/test_memo1.py b/jubeatools/formats/jubeat_analyser/tests/memo1/test_memo1.py index e2c880d..b70d900 100644 --- a/jubeatools/formats/jubeat_analyser/tests/memo1/test_memo1.py +++ b/jubeatools/formats/jubeat_analyser/tests/memo1/test_memo1.py @@ -12,7 +12,7 @@ from jubeatools.formats.jubeat_analyser.memo1.load import Memo1Parser from jubeatools.testutils.strategies import notes as notes_strat from jubeatools.testutils.test_patterns import dump_and_load_then_compare -from ..test_utils import memo_compatible_song, temp_file_named_txt +from ..test_utils import memo_compatible_song from . import example1 @@ -44,7 +44,6 @@ def test_that_full_chart_roundtrips(song: song.Song, circle_free: bool) -> None: dump_and_load_then_compare( Format.MEMO_1, song, - temp_path=temp_file_named_txt(), bytes_decoder=lambda b: b.decode("shift-jis-2004", errors="surrogateescape"), dump_options={"circle_free": circle_free}, ) diff --git a/jubeatools/formats/jubeat_analyser/tests/memo2/test_memo2.py b/jubeatools/formats/jubeat_analyser/tests/memo2/test_memo2.py index 9c21815..87c8fb1 100644 --- a/jubeatools/formats/jubeat_analyser/tests/memo2/test_memo2.py +++ b/jubeatools/formats/jubeat_analyser/tests/memo2/test_memo2.py @@ -22,7 +22,7 @@ from jubeatools.song import ( from jubeatools.testutils.strategies import notes as notes_strat from jubeatools.testutils.test_patterns import dump_and_load_then_compare -from ..test_utils import memo_compatible_song, temp_file_named_txt +from ..test_utils import memo_compatible_song from . import example1, example2, example3 @@ -55,7 +55,6 @@ def test_that_full_chart_roundtrips(song: Song, circle_free: bool) -> None: dump_and_load_then_compare( Format.MEMO_2, song, - temp_path=temp_file_named_txt(), bytes_decoder=lambda b: b.decode("shift-jis-2004", errors="surrogateescape"), dump_options={"circle_free": circle_free}, ) diff --git a/jubeatools/formats/jubeat_analyser/tests/mono_column/test_mono_column_hypothesis.py b/jubeatools/formats/jubeat_analyser/tests/mono_column/test_mono_column_hypothesis.py index 381e9a6..a3c6197 100644 --- a/jubeatools/formats/jubeat_analyser/tests/mono_column/test_mono_column_hypothesis.py +++ b/jubeatools/formats/jubeat_analyser/tests/mono_column/test_mono_column_hypothesis.py @@ -24,7 +24,7 @@ from jubeatools.testutils.strategies import notes as notes_strat from jubeatools.testutils.strategies import tap_note from jubeatools.testutils.test_patterns import dump_and_load_then_compare -from ..test_utils import memo_compatible_song, temp_file_named_txt +from ..test_utils import memo_compatible_song @given(st.sets(tap_note(), min_size=1, max_size=100)) @@ -88,7 +88,6 @@ def test_that_full_chart_roundtrips(song: Song, circle_free: bool) -> None: dump_and_load_then_compare( Format.MONO_COLUMN, song, - temp_path=temp_file_named_txt(), bytes_decoder=lambda b: b.decode("shift-jis-2004", errors="surrogateescape"), dump_options={"circle_free": circle_free}, ) diff --git a/jubeatools/formats/konami/eve/tests/test_eve.py b/jubeatools/formats/konami/eve/tests/test_eve.py index 2ac2607..0026e4c 100644 --- a/jubeatools/formats/konami/eve/tests/test_eve.py +++ b/jubeatools/formats/konami/eve/tests/test_eve.py @@ -2,7 +2,7 @@ from hypothesis import given from jubeatools import song from jubeatools.formats import Format -from jubeatools.formats.konami.testutils import eve_compatible_song, open_temp_dir +from jubeatools.formats.konami.testutils import eve_compatible_song from jubeatools.testutils.test_patterns import dump_and_load_then_compare @@ -11,7 +11,6 @@ def test_that_full_chart_roundtrips(song: song.Song) -> None: dump_and_load_then_compare( Format.EVE, song, - temp_path=open_temp_dir(), bytes_decoder=lambda b: b.decode("ascii"), load_options={"beat_snap": 12}, ) diff --git a/jubeatools/formats/konami/jbsq/test_jbsq.py b/jubeatools/formats/konami/jbsq/test_jbsq.py index 32a40a1..15bf74e 100644 --- a/jubeatools/formats/konami/jbsq/test_jbsq.py +++ b/jubeatools/formats/konami/jbsq/test_jbsq.py @@ -2,7 +2,7 @@ from hypothesis import given from jubeatools import song from jubeatools.formats import Format -from jubeatools.formats.konami.testutils import eve_compatible_song, open_temp_dir +from jubeatools.formats.konami.testutils import eve_compatible_song from jubeatools.testutils.test_patterns import dump_and_load_then_compare from .construct import jbsq @@ -13,7 +13,6 @@ def test_that_full_chart_roundtrips(song: song.Song) -> None: dump_and_load_then_compare( Format.JBSQ, song, - temp_path=open_temp_dir(), bytes_decoder=lambda b: str(jbsq.parse(b)), load_options={"beat_snap": 12}, ) diff --git a/jubeatools/formats/konami/testutils.py b/jubeatools/formats/konami/testutils.py index 0da2934..4c62990 100644 --- a/jubeatools/formats/konami/testutils.py +++ b/jubeatools/formats/konami/testutils.py @@ -1,8 +1,4 @@ -import tempfile -from contextlib import contextmanager from decimal import Decimal -from pathlib import Path -from typing import Iterator from hypothesis import strategies as st @@ -53,9 +49,3 @@ def eve_compatible_song(draw: DrawFunc) -> song.Song: metadata=song.Metadata(), charts={diff: chart}, ) - - -@contextmanager -def open_temp_dir() -> Iterator[Path]: - with tempfile.TemporaryDirectory() as temp_dir: - yield Path(temp_dir) diff --git a/jubeatools/formats/malody/tests/test_malody.py b/jubeatools/formats/malody/tests/test_malody.py index 76c2115..7c5e6bc 100644 --- a/jubeatools/formats/malody/tests/test_malody.py +++ b/jubeatools/formats/malody/tests/test_malody.py @@ -8,7 +8,6 @@ from hypothesis import strategies as st from jubeatools import song from jubeatools.formats import Format -from jubeatools.formats.konami.testutils import open_temp_dir from jubeatools.formats.malody import schema as malody from jubeatools.formats.malody.dump import dump_malody_chart from jubeatools.testutils import strategies as jbst @@ -50,7 +49,6 @@ def test_that_full_chart_roundtrips(s: song.Song) -> None: dump_and_load_then_compare( Format.MALODY, s, - temp_path=open_temp_dir(), bytes_decoder=lambda b: b.decode("utf-8"), ) diff --git a/jubeatools/testutils/test_patterns.py b/jubeatools/testutils/test_patterns.py index f91c2ca..176265b 100644 --- a/jubeatools/testutils/test_patterns.py +++ b/jubeatools/testutils/test_patterns.py @@ -1,5 +1,7 @@ +import tempfile +from contextlib import contextmanager from pathlib import Path -from typing import Callable, ContextManager, Optional +from typing import Callable, ContextManager, Iterator, Optional from hypothesis import note @@ -9,11 +11,17 @@ from jubeatools.formats.enum import Format from jubeatools.formats.guess import guess_format +@contextmanager +def open_temp_dir() -> Iterator[Path]: + with tempfile.TemporaryDirectory() as temp_dir: + yield Path(temp_dir) + + def dump_and_load_then_compare( format_: Format, song: song.Song, - temp_path: ContextManager[Path], bytes_decoder: Callable[[bytes], str], + temp_path: Callable[[], ContextManager[Path]] = open_temp_dir, load_options: Optional[dict] = None, dump_options: Optional[dict] = None, ) -> None: @@ -21,7 +29,7 @@ def dump_and_load_then_compare( dump_options = dump_options or {} loader = LOADERS[format_] dumper = DUMPERS[format_] - with temp_path as folder_path: + with temp_path() as folder_path: files = dumper(song, folder_path, **dump_options) for file_path, bytes_ in files.items(): file_path.write_bytes(bytes_)