From 096ac34b08a4a6a68e651b8f88e1f154f9755a1e Mon Sep 17 00:00:00 2001 From: Nanahuse <109294061+Nanahuse@users.noreply.github.com> Date: Fri, 9 Jan 2026 01:36:41 +1300 Subject: [PATCH 1/7] chore: setup python tool chain ( uv, ruff, ty ) (#7) * chore: setup `uv` * chore: change flake8 -> ruff * apply format --- .flake8 | 3 --- .gitignore | 4 ++++ pyproject.toml | 29 ++++++++++++++++++++++++++--- test/test_voicepeak.py | 6 +++--- voicepeak_wrapper/voicepeak.py | 2 +- 5 files changed, 34 insertions(+), 10 deletions(-) delete mode 100644 .flake8 diff --git a/.flake8 b/.flake8 deleted file mode 100644 index 1cf25ae..0000000 --- a/.flake8 +++ /dev/null @@ -1,3 +0,0 @@ -[flake8] -max-line-length = 120 -ignore = E722,E203,ANN101,ANN204 \ No newline at end of file diff --git a/.gitignore b/.gitignore index e6ef6f2..2b64263 100644 --- a/.gitignore +++ b/.gitignore @@ -159,4 +159,8 @@ cython_debug/ # option (not recommended) you can uncomment the following to ignore the entire idea folder. #.idea/ +# uv +uv.lock +.python-version + .vscode diff --git a/pyproject.toml b/pyproject.toml index 0f97e77..5a5f57f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -14,8 +14,31 @@ classifiers = [ [project.urls] Homepage = "https://github.com/Nanahuse/VoicepeakWrapper" -[project.optional-dependencies] -dev = ["pytest", "pytest-asyncio", "flake8", "black", "isort", "mypy"] +[dependency-groups] +dev = [ + "pytest>=9.0.2", + "pytest-asyncio>=1.3.0", + "ruff>=0.14.10", + "ty>=0.0.9", +] -[tool.black] +[tool.ruff] line-length = 120 +lint.fixable = ["ALL"] +lint.select = ["ALL"] +lint.ignore = [ + "A002", + "EM101", + "EM102", + "FIX002", + "S101", + "TRY003", + "TRY300", + "COM812", + "RUF002", + "D", + "TD", +] + +[tool.ruff.per-file-ignores] +"test/*" = ["INP001", "PLR2004", "SLF001"] diff --git a/test/test_voicepeak.py b/test/test_voicepeak.py index cc92285..675e780 100644 --- a/test/test_voicepeak.py +++ b/test/test_voicepeak.py @@ -2,9 +2,10 @@ # This software is released under the MIT License # https://opensource.org/license/mit/ -import pytest import os +import pytest + TEST_DIRECTORY = os.path.dirname(os.path.abspath(__file__)) OUTPUT_PATH = os.path.join(TEST_DIRECTORY, "output") @@ -37,8 +38,7 @@ async def test_get_narrator_name_list(): client = voicepeak_wrapper.Voicepeak() narrator_names = await client.get_narrator_name_list() with open(os.path.join(OUTPUT_PATH, "narrator_names.txt"), mode="w", encoding="UTF-8") as f: - for name in narrator_names: - f.write(f"{name}\n") + f.writelines(f"{name}\n" for name in narrator_names) @pytest.mark.asyncio diff --git a/voicepeak_wrapper/voicepeak.py b/voicepeak_wrapper/voicepeak.py index 26c8999..4f33e3e 100644 --- a/voicepeak_wrapper/voicepeak.py +++ b/voicepeak_wrapper/voicepeak.py @@ -8,7 +8,7 @@ @dataclass -class Narrator(object): +class Narrator: name: str emotions: tuple[str, ...] From 8f2971078e19fce76ea356cf6c5a02f1832a5231 Mon Sep 17 00:00:00 2001 From: Nanahuse <109294061+Nanahuse@users.noreply.github.com> Date: Thu, 6 Aug 2026 23:20:56 +0900 Subject: [PATCH 2/7] Test terminal output in CI (#11) * Test terminal output in CI * Exercise subprocess output in tests * Support tests without ProgramFiles * Run tests only for pull requests * Align tests with v1.2 toolchain --- .github/workflows/test.yml | 20 ++++ test/manual_voicepeak_file_generation.py | 102 +++++++++++++++++ test/test_voicepeak.py | 133 ++++++++++++----------- 3 files changed, 190 insertions(+), 65 deletions(-) create mode 100644 .github/workflows/test.yml create mode 100644 test/manual_voicepeak_file_generation.py diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..48dc079 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,20 @@ +name: Test + +on: + pull_request: + +permissions: + contents: read + +jobs: + test: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v6 + + - name: Setup uv + uses: astral-sh/setup-uv@v7 + + - name: Run tests + run: uv run --group dev --with-editable . pytest diff --git a/test/manual_voicepeak_file_generation.py b/test/manual_voicepeak_file_generation.py new file mode 100644 index 0000000..1dfd812 --- /dev/null +++ b/test/manual_voicepeak_file_generation.py @@ -0,0 +1,102 @@ +"""VOICEPEAK integration tests that create output files. + +These tests require a local VOICEPEAK installation. Pytest does not collect this +module by default; run it explicitly when file-generation coverage is needed: + + uv run --extra dev --with-editable . pytest test/manual_voicepeak_file_generation.py +""" + +import os + +import pytest + +TEST_DIRECTORY = os.path.dirname(os.path.abspath(__file__)) +OUTPUT_PATH = os.path.join(TEST_DIRECTORY, "output") + + +@pytest.mark.asyncio +async def test_get_narrator_list(): + import voicepeak_wrapper + + client = voicepeak_wrapper.Voicepeak() + narrators = await client.get_narrator_list() + with open(os.path.join(OUTPUT_PATH, "narrators.txt"), mode="w", encoding="UTF-8") as f: + for narrator in narrators: + f.write(narrator.name) + f.write(" : ") + f.write(", ".join(narrator.emotions)) + f.write("\n") + + await client.say_text( + "本日は晴天なり", + output_path=os.path.join(OUTPUT_PATH, f"narrator_{narrator.name}.wav"), + narrator=narrator, + emotions={narrator.emotions[1]: 100}, + ) + + +@pytest.mark.asyncio +async def test_get_narrator_name_list(): + import voicepeak_wrapper + + client = voicepeak_wrapper.Voicepeak() + narrator_names = await client.get_narrator_name_list() + with open(os.path.join(OUTPUT_PATH, "narrator_names.txt"), mode="w", encoding="UTF-8") as f: + f.writelines(f"{name}\n" for name in narrator_names) + + +@pytest.mark.asyncio +async def test_get_emotion_list(): + import voicepeak_wrapper + + client = voicepeak_wrapper.Voicepeak() + + narrator_names = await client.get_narrator_name_list() + with open(os.path.join(OUTPUT_PATH, "emotions.txt"), mode="w", encoding="UTF-8") as f: + for name in narrator_names: + emotion_list = await client.get_emotion_list(name) + + f.write(name) + f.write(" : ") + f.write(", ".join(emotion_list)) + f.write("\n") + + with pytest.raises(RuntimeError): + await client.get_emotion_list("hogehoge") + + +@pytest.mark.asyncio +async def test_say_text(): + import voicepeak_wrapper + + client = voicepeak_wrapper.Voicepeak() + await client.say_text("本日は晴天なり", output_path=os.path.join(OUTPUT_PATH, "test_say_text.wav")) + await client.say_text( + "本日は晴天なり", output_path=os.path.join(OUTPUT_PATH, "test_say_text_upper.wav"), speed=200, pitch=300 + ) + await client.say_text( + "本日は晴天なり", output_path=os.path.join(OUTPUT_PATH, "test_say_text_lower.wav"), speed=50, pitch=-300 + ) + + with pytest.raises(ValueError): + await client.say_text("エラー", output_path=os.path.join(OUTPUT_PATH, "error.wav"), speed=201) + with pytest.raises(ValueError): + await client.say_text("エラー", output_path=os.path.join(OUTPUT_PATH, "error.wav"), speed=49) + with pytest.raises(ValueError): + await client.say_text("エラー", output_path=os.path.join(OUTPUT_PATH, "error.wav"), pitch=-301) + with pytest.raises(ValueError): + await client.say_text("エラー", output_path=os.path.join(OUTPUT_PATH, "error.wav"), pitch=301) + + with pytest.raises(RuntimeError): + await client.say_text("1" * 141, output_path=os.path.join(OUTPUT_PATH, "error.wav")) + + +@pytest.mark.asyncio +async def test_say_testfile(): + import voicepeak_wrapper + + client = voicepeak_wrapper.Voicepeak() + + text_file = os.path.join(TEST_DIRECTORY, "sample.txt") + + await client.say_textfile(text_file, output_path=os.path.join(OUTPUT_PATH, "say_text.wav")) diff --git a/test/test_voicepeak.py b/test/test_voicepeak.py index 675e780..36d1283 100644 --- a/test/test_voicepeak.py +++ b/test/test_voicepeak.py @@ -2,97 +2,100 @@ # This software is released under the MIT License # https://opensource.org/license/mit/ +import asyncio import os +from unittest.mock import AsyncMock import pytest -TEST_DIRECTORY = os.path.dirname(os.path.abspath(__file__)) -OUTPUT_PATH = os.path.join(TEST_DIRECTORY, "output") +class ProcessResult: + def __init__(self, stdout: bytes = b"", stderr: bytes = b""): + self.communicate = AsyncMock(return_value=(stdout, stderr)) -@pytest.mark.asyncio -async def test_get_narrator_list(): - import voicepeak_wrapper - client = voicepeak_wrapper.Voicepeak() - narrators = await client.get_narrator_list() - with open(os.path.join(OUTPUT_PATH, "narrators.txt"), mode="w", encoding="UTF-8") as f: - for narrator in narrators: - f.write(narrator.name) - f.write(" : ") - f.write(", ".join(narrator.emotions)) - f.write("\n") - - await client.say_text( - "本日は晴天なり", - output_path=os.path.join(OUTPUT_PATH, f"narrator_{narrator.name}.wav"), - narrator=narrator, - emotions={narrator.emotions[1]: "100"}, - ) +def create_client(monkeypatch): + monkeypatch.setenv("ProgramFiles", os.path.dirname(__file__)) + import voicepeak_wrapper + return voicepeak_wrapper.Voicepeak(exe_path=__file__) -@pytest.mark.asyncio -async def test_get_narrator_name_list(): - import voicepeak_wrapper - client = voicepeak_wrapper.Voicepeak() - narrator_names = await client.get_narrator_name_list() - with open(os.path.join(OUTPUT_PATH, "narrator_names.txt"), mode="w", encoding="UTF-8") as f: - f.writelines(f"{name}\n" for name in narrator_names) +def mock_process(monkeypatch, *results): + create_subprocess = AsyncMock(side_effect=results) + monkeypatch.setattr(asyncio, "create_subprocess_exec", create_subprocess) + return create_subprocess @pytest.mark.asyncio -async def test_get_emotion_list(): - import voicepeak_wrapper - - client = voicepeak_wrapper.Voicepeak() +async def test_get_narrator_name_list_decodes_terminal_output(monkeypatch): + client = create_client(monkeypatch) + process = ProcessResult("Japanese Female 1\nJapanese Male 1\n".encode()) + create_subprocess = mock_process(monkeypatch, process) + + assert await client.get_narrator_name_list() == ("Japanese Female 1", "Japanese Male 1") + create_subprocess.assert_awaited_once_with( + __file__, + "--list-narrator", + stdout=asyncio.subprocess.PIPE, + stderr=asyncio.subprocess.PIPE, + ) + process.communicate.assert_awaited_once_with() - narrator_names = await client.get_narrator_name_list() - with open(os.path.join(OUTPUT_PATH, "emotions.txt"), mode="w", encoding="UTF-8") as f: - for name in narrator_names: - emotion_list = await client.get_emotion_list(name) - f.write(name) - f.write(" : ") - f.write(", ".join(emotion_list)) - f.write("\n") +@pytest.mark.asyncio +async def test_get_emotion_list_decodes_terminal_output(monkeypatch): + client = create_client(monkeypatch) + process = ProcessResult("happy\nsad\n".encode()) + mock_process(monkeypatch, process) - with pytest.raises(RuntimeError): - await client.get_emotion_list("hogehoge") + assert await client.get_emotion_list("Japanese Female 1") == ("happy", "sad") @pytest.mark.asyncio -async def test_say_text(): +async def test_get_narrator_list_uses_each_terminal_output(monkeypatch): + client = create_client(monkeypatch) import voicepeak_wrapper - client = voicepeak_wrapper.Voicepeak() - await client.say_text("本日は晴天なり", output_path=os.path.join(OUTPUT_PATH, "test_say_text.wav")) - await client.say_text( - "本日は晴天なり", output_path=os.path.join(OUTPUT_PATH, "test_say_text_upper.wav"), speed=200, pitch=300 + processes = ( + ProcessResult("Japanese Female 1\nJapanese Male 1\n".encode()), + ProcessResult("happy\nsad\n".encode()), + ProcessResult("happy\nangry\n".encode()), ) - await client.say_text( - "本日は晴天なり", output_path=os.path.join(OUTPUT_PATH, "test_say_text_lower.wav"), speed=50, pitch=-300 - ) - - with pytest.raises(ValueError): - await client.say_text("エラー", output_path=os.path.join(OUTPUT_PATH, "error.wav"), speed=201) - with pytest.raises(ValueError): - await client.say_text("エラー", output_path=os.path.join(OUTPUT_PATH, "error.wav"), speed=49) - with pytest.raises(ValueError): - await client.say_text("エラー", output_path=os.path.join(OUTPUT_PATH, "error.wav"), pitch=-301) - with pytest.raises(ValueError): - await client.say_text("エラー", output_path=os.path.join(OUTPUT_PATH, "error.wav"), pitch=301) + mock_process(monkeypatch, *processes) - with pytest.raises(RuntimeError): - await client.say_text("1" * 141, output_path=os.path.join(OUTPUT_PATH, "error.wav")) + assert await client.get_narrator_list() == ( + voicepeak_wrapper.Narrator("Japanese Female 1", ("happy", "sad")), + voicepeak_wrapper.Narrator("Japanese Male 1", ("happy", "angry")), + ) @pytest.mark.asyncio -async def test_say_testfile(): - import voicepeak_wrapper +async def test_say_text_returns_decoded_terminal_output(monkeypatch): + client = create_client(monkeypatch) + process = ProcessResult("completed\n".encode()) + create_subprocess = mock_process(monkeypatch, process) + + result = await client.say_text("本日は晴天なり", output_path="output.wav") + + assert isinstance(result, str) + assert result == "completed\n" + create_subprocess.assert_awaited_once_with( + __file__, + "-s", + "本日は晴天なり", + "-o", + "output.wav", + stdout=asyncio.subprocess.PIPE, + stderr=asyncio.subprocess.PIPE, + ) - client = voicepeak_wrapper.Voicepeak() - text_file = os.path.join(TEST_DIRECTORY, "sample.txt") +@pytest.mark.asyncio +async def test_terminal_error_is_decoded_as_runtime_error(monkeypatch): + client = create_client(monkeypatch) + process = ProcessResult(stderr="narrator not found\n".encode()) + mock_process(monkeypatch, process) - await client.say_textfile(text_file, output_path=os.path.join(OUTPUT_PATH, "say_text.wav")) + with pytest.raises(RuntimeError, match="narrator not found"): + await client.get_emotion_list("unknown") From b5856b5d023bd9eaffd04b313e8aaac4195d7426 Mon Sep 17 00:00:00 2001 From: Nanahuse <109294061+Nanahuse@users.noreply.github.com> Date: Fri, 7 Aug 2026 00:05:25 +0900 Subject: [PATCH 3/7] refactor: add ruff and ty for linting and type checking (#10) * refactor: add ruff and ty for linting and type checking - Add ruff and ty to project dependencies - Configure pyproject.toml with ruff settings and ty task runner - Update pre-commit hooks to run ruff check and ty check - Adjust docstrings and code style to satisfy new lint rules * Fix ruff and ty diagnostics * Ignore local agent instructions * Add code quality checks to CI --- .github/workflows/quality.yml | 44 +++++++++++++++++++++++ .gitignore | 12 ++++--- README.md | 6 ++-- pyproject.toml | 21 ++++++----- test/manual_voicepeak_file_generation.py | 46 ++++++++++++------------ test/test_voicepeak.py | 14 ++++---- voicepeak_wrapper/voicepeak.py | 2 +- 7 files changed, 101 insertions(+), 44 deletions(-) create mode 100644 .github/workflows/quality.yml diff --git a/.github/workflows/quality.yml b/.github/workflows/quality.yml new file mode 100644 index 0000000..a2df3f8 --- /dev/null +++ b/.github/workflows/quality.yml @@ -0,0 +1,44 @@ +name: Quality + +on: + pull_request: + +permissions: + contents: read + +jobs: + lint: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v6 + + - name: Setup uv + uses: astral-sh/setup-uv@v7 + + - name: Run lint + run: uv run ruff check . + + format: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v6 + + - name: Setup uv + uses: astral-sh/setup-uv@v7 + + - name: Check formatting + run: uv run ruff format --check . + + typecheck: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v6 + + - name: Setup uv + uses: astral-sh/setup-uv@v7 + + - name: Run type checker + run: uv run ty check diff --git a/.gitignore b/.gitignore index 2b64263..0ec374c 100644 --- a/.gitignore +++ b/.gitignore @@ -138,10 +138,11 @@ venv.bak/ # mkdocs documentation /site -# mypy -.mypy_cache/ -.dmypy.json -dmypy.json +# ruff +.ruff_cache/ + +# ty +.ty_cache/ # Pyre type checker .pyre/ @@ -164,3 +165,6 @@ uv.lock .python-version .vscode + +# Local agent instructions +AGENTS.md diff --git a/README.md b/README.md index dd30c5e..750585b 100644 --- a/README.md +++ b/README.md @@ -13,12 +13,14 @@ pip install voicepeak-wrapper import asyncio import voicepeak_wrapper + async def main(): client = voicepeak_wrapper.Voicepeak() - await client.say_text("本日は快晴なり", "./test.wav") # 出力のサンプル + await client.say_text("本日は快晴なり", "./test.wav") # 出力のサンプル + + narrators = await client.get_narrator_list() # ナレーターのリスト取得。時間がかかります。 - narrators = await client.get_narrator_list() # ナレーターのリスト取得。時間がかかります。 asyncio.run(main()) ``` diff --git a/pyproject.toml b/pyproject.toml index 5a5f57f..f63e51c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -24,21 +24,26 @@ dev = [ [tool.ruff] line-length = 120 -lint.fixable = ["ALL"] -lint.select = ["ALL"] -lint.ignore = [ + +[tool.ruff.lint] +ignore = [ "A002", + "COM812", + "D", + "E203", + "E722", "EM101", "EM102", "FIX002", + "RUF002", "S101", + "TD", "TRY003", "TRY300", - "COM812", - "RUF002", - "D", - "TD", ] -[tool.ruff.per-file-ignores] +[tool.ruff.lint.per-file-ignores] "test/*" = ["INP001", "PLR2004", "SLF001"] + +[tool.ty.environment] +python-version = "3.10" diff --git a/test/manual_voicepeak_file_generation.py b/test/manual_voicepeak_file_generation.py index 1dfd812..7642bd2 100644 --- a/test/manual_voicepeak_file_generation.py +++ b/test/manual_voicepeak_file_generation.py @@ -6,7 +6,9 @@ uv run --extra dev --with-editable . pytest test/manual_voicepeak_file_generation.py """ +import asyncio import os +from pathlib import Path import pytest @@ -20,19 +22,18 @@ async def test_get_narrator_list(): client = voicepeak_wrapper.Voicepeak() narrators = await client.get_narrator_list() - with open(os.path.join(OUTPUT_PATH, "narrators.txt"), mode="w", encoding="UTF-8") as f: - for narrator in narrators: - f.write(narrator.name) - f.write(" : ") - f.write(", ".join(narrator.emotions)) - f.write("\n") + lines = [] + for narrator in narrators: + lines.append(f"{narrator.name} : {', '.join(narrator.emotions)}\n") - await client.say_text( - "本日は晴天なり", - output_path=os.path.join(OUTPUT_PATH, f"narrator_{narrator.name}.wav"), - narrator=narrator, - emotions={narrator.emotions[1]: 100}, - ) + await client.say_text( + "本日は晴天なり", + output_path=os.path.join(OUTPUT_PATH, f"narrator_{narrator.name}.wav"), + narrator=narrator, + emotions={narrator.emotions[1]: 100}, + ) + + await asyncio.to_thread(Path(OUTPUT_PATH, "narrators.txt").write_text, "".join(lines), encoding="UTF-8") @pytest.mark.asyncio @@ -41,8 +42,11 @@ async def test_get_narrator_name_list(): client = voicepeak_wrapper.Voicepeak() narrator_names = await client.get_narrator_name_list() - with open(os.path.join(OUTPUT_PATH, "narrator_names.txt"), mode="w", encoding="UTF-8") as f: - f.writelines(f"{name}\n" for name in narrator_names) + await asyncio.to_thread( + Path(OUTPUT_PATH, "narrator_names.txt").write_text, + "".join(f"{name}\n" for name in narrator_names), + encoding="UTF-8", + ) @pytest.mark.asyncio @@ -52,14 +56,12 @@ async def test_get_emotion_list(): client = voicepeak_wrapper.Voicepeak() narrator_names = await client.get_narrator_name_list() - with open(os.path.join(OUTPUT_PATH, "emotions.txt"), mode="w", encoding="UTF-8") as f: - for name in narrator_names: - emotion_list = await client.get_emotion_list(name) - - f.write(name) - f.write(" : ") - f.write(", ".join(emotion_list)) - f.write("\n") + lines = [] + for name in narrator_names: + emotion_list = await client.get_emotion_list(name) + lines.append(f"{name} : {', '.join(emotion_list)}\n") + + await asyncio.to_thread(Path(OUTPUT_PATH, "emotions.txt").write_text, "".join(lines), encoding="UTF-8") with pytest.raises(RuntimeError): await client.get_emotion_list("hogehoge") diff --git a/test/test_voicepeak.py b/test/test_voicepeak.py index 36d1283..92968eb 100644 --- a/test/test_voicepeak.py +++ b/test/test_voicepeak.py @@ -30,7 +30,7 @@ def mock_process(monkeypatch, *results): @pytest.mark.asyncio async def test_get_narrator_name_list_decodes_terminal_output(monkeypatch): client = create_client(monkeypatch) - process = ProcessResult("Japanese Female 1\nJapanese Male 1\n".encode()) + process = ProcessResult(b"Japanese Female 1\nJapanese Male 1\n") create_subprocess = mock_process(monkeypatch, process) assert await client.get_narrator_name_list() == ("Japanese Female 1", "Japanese Male 1") @@ -46,7 +46,7 @@ async def test_get_narrator_name_list_decodes_terminal_output(monkeypatch): @pytest.mark.asyncio async def test_get_emotion_list_decodes_terminal_output(monkeypatch): client = create_client(monkeypatch) - process = ProcessResult("happy\nsad\n".encode()) + process = ProcessResult(b"happy\nsad\n") mock_process(monkeypatch, process) assert await client.get_emotion_list("Japanese Female 1") == ("happy", "sad") @@ -58,9 +58,9 @@ async def test_get_narrator_list_uses_each_terminal_output(monkeypatch): import voicepeak_wrapper processes = ( - ProcessResult("Japanese Female 1\nJapanese Male 1\n".encode()), - ProcessResult("happy\nsad\n".encode()), - ProcessResult("happy\nangry\n".encode()), + ProcessResult(b"Japanese Female 1\nJapanese Male 1\n"), + ProcessResult(b"happy\nsad\n"), + ProcessResult(b"happy\nangry\n"), ) mock_process(monkeypatch, *processes) @@ -73,7 +73,7 @@ async def test_get_narrator_list_uses_each_terminal_output(monkeypatch): @pytest.mark.asyncio async def test_say_text_returns_decoded_terminal_output(monkeypatch): client = create_client(monkeypatch) - process = ProcessResult("completed\n".encode()) + process = ProcessResult(b"completed\n") create_subprocess = mock_process(monkeypatch, process) result = await client.say_text("本日は晴天なり", output_path="output.wav") @@ -94,7 +94,7 @@ async def test_say_text_returns_decoded_terminal_output(monkeypatch): @pytest.mark.asyncio async def test_terminal_error_is_decoded_as_runtime_error(monkeypatch): client = create_client(monkeypatch) - process = ProcessResult(stderr="narrator not found\n".encode()) + process = ProcessResult(stderr=b"narrator not found\n") mock_process(monkeypatch, process) with pytest.raises(RuntimeError, match="narrator not found"): diff --git a/voicepeak_wrapper/voicepeak.py b/voicepeak_wrapper/voicepeak.py index 4f33e3e..44da2da 100644 --- a/voicepeak_wrapper/voicepeak.py +++ b/voicepeak_wrapper/voicepeak.py @@ -183,7 +183,7 @@ async def get_narrator_list(self) -> tuple[Narrator, ...]: tuple[Narrator]: ナレーター一覧 """ narrators = await self.get_narrator_name_list() - narrator_list = list() + narrator_list = [] for name in narrators: emotions = await self.get_emotion_list(name) narrator_list.append(Narrator(name, emotions)) From 138f8c22241bf48a0ed22c66278871afa053b3d7 Mon Sep 17 00:00:00 2001 From: Nanahuse <109294061+Nanahuse@users.noreply.github.com> Date: Sun, 9 Aug 2026 10:35:41 +0900 Subject: [PATCH 4/7] Fix pytest imports and gate manual VOICEPEAK tests (#15) * Fix local pytest package imports * Gate VOICEPEAK integration tests * Use pytest runxfail for manual tests --- pyproject.toml | 3 +++ ... test_manual_voicepeak_file_generation.py} | 19 ++++++++++++++++--- 2 files changed, 19 insertions(+), 3 deletions(-) rename test/{manual_voicepeak_file_generation.py => test_manual_voicepeak_file_generation.py} (83%) diff --git a/pyproject.toml b/pyproject.toml index f63e51c..68cdff5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -42,6 +42,9 @@ ignore = [ "TRY300", ] +[tool.pytest.ini_options] +pythonpath = ["."] + [tool.ruff.lint.per-file-ignores] "test/*" = ["INP001", "PLR2004", "SLF001"] diff --git a/test/manual_voicepeak_file_generation.py b/test/test_manual_voicepeak_file_generation.py similarity index 83% rename from test/manual_voicepeak_file_generation.py rename to test/test_manual_voicepeak_file_generation.py index 7642bd2..ff03f93 100644 --- a/test/manual_voicepeak_file_generation.py +++ b/test/test_manual_voicepeak_file_generation.py @@ -1,9 +1,20 @@ +# Copyright (c) 2023 Nanahuse +# This software is released under the MIT License +# https://opensource.org/license/mit/ + """VOICEPEAK integration tests that create output files. -These tests require a local VOICEPEAK installation. Pytest does not collect this -module by default; run it explicitly when file-generation coverage is needed: +These tests require a local VOICEPEAK installation and are marked xfail with +``run=False``, so they are reported as NOTRUN without launching VOICEPEAK. Run +them with pytest's built-in ``--runxfail`` option when file-generation coverage +is needed: + + uv run --group dev --with-editable . pytest --runxfail - uv run --extra dev --with-editable . pytest test/manual_voicepeak_file_generation.py +Or run just this module: + + uv run --group dev --with-editable . pytest \\ + test/test_manual_voicepeak_file_generation.py --runxfail """ import asyncio @@ -12,6 +23,8 @@ import pytest +pytestmark = pytest.mark.xfail(run=False, reason="requires a local VOICEPEAK installation; pass --runxfail to run") + TEST_DIRECTORY = os.path.dirname(os.path.abspath(__file__)) OUTPUT_PATH = os.path.join(TEST_DIRECTORY, "output") From a3785f87cd20f48631d8c5c683f911c57c84cddb Mon Sep 17 00:00:00 2001 From: Nanahuse <109294061+Nanahuse@users.noreply.github.com> Date: Sun, 9 Aug 2026 16:16:04 +0900 Subject: [PATCH 5/7] Support pathlib.Path inputs (#14) * Support pathlib.Path inputs * Use explicit Path annotations * Unify Path naming --- README.md | 2 ++ test/test_voicepeak.py | 59 ++++++++++++++++++++++++++++++++++ voicepeak_wrapper/voicepeak.py | 36 ++++++++++++--------- 3 files changed, 82 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 750585b..66a345b 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,8 @@ async def main(): asyncio.run(main()) ``` +パスを受け取る引数(exe_path, output_path, text_path)には`str`だけでなく`pathlib.Path`(`Path`)も渡せます。 + # License MITライセンス 詳しくは[LICENSE](./LICENSE)を確認ください。 \ No newline at end of file diff --git a/test/test_voicepeak.py b/test/test_voicepeak.py index 92968eb..c5bc65c 100644 --- a/test/test_voicepeak.py +++ b/test/test_voicepeak.py @@ -4,6 +4,7 @@ import asyncio import os +from pathlib import Path from unittest.mock import AsyncMock import pytest @@ -99,3 +100,61 @@ async def test_terminal_error_is_decoded_as_runtime_error(monkeypatch): with pytest.raises(RuntimeError, match="narrator not found"): await client.get_emotion_list("unknown") + + +@pytest.mark.asyncio +async def test_init_accepts_path_for_exe_path(monkeypatch): + monkeypatch.setenv("ProgramFiles", os.path.dirname(__file__)) + import voicepeak_wrapper + + client = voicepeak_wrapper.Voicepeak(exe_path=Path(__file__)) + process = ProcessResult(b"Japanese Female 1\n") + create_subprocess = mock_process(monkeypatch, process) + + assert await client.get_narrator_name_list() == ("Japanese Female 1",) + create_subprocess.assert_awaited_once_with( + __file__, + "--list-narrator", + stdout=asyncio.subprocess.PIPE, + stderr=asyncio.subprocess.PIPE, + ) + + +@pytest.mark.asyncio +async def test_say_text_accepts_path_for_output_path(monkeypatch): + client = create_client(monkeypatch) + process = ProcessResult(b"completed\n") + create_subprocess = mock_process(monkeypatch, process) + + result = await client.say_text("本日は晴天なり", output_path=Path("output.wav")) + + assert result == "completed\n" + create_subprocess.assert_awaited_once_with( + __file__, + "-s", + "本日は晴天なり", + "-o", + "output.wav", + stdout=asyncio.subprocess.PIPE, + stderr=asyncio.subprocess.PIPE, + ) + + +@pytest.mark.asyncio +async def test_say_textfile_accepts_paths(monkeypatch): + client = create_client(monkeypatch) + process = ProcessResult(b"completed\n") + create_subprocess = mock_process(monkeypatch, process) + + result = await client.say_textfile(Path("input.txt"), output_path=Path("output.wav")) + + assert result == "completed\n" + create_subprocess.assert_awaited_once_with( + __file__, + "-t", + "input.txt", + "-o", + "output.wav", + stdout=asyncio.subprocess.PIPE, + stderr=asyncio.subprocess.PIPE, + ) diff --git a/voicepeak_wrapper/voicepeak.py b/voicepeak_wrapper/voicepeak.py index 44da2da..260add3 100644 --- a/voicepeak_wrapper/voicepeak.py +++ b/voicepeak_wrapper/voicepeak.py @@ -5,6 +5,9 @@ import asyncio import os from dataclasses import dataclass +from pathlib import Path + +_DEFAULT_EXE_PATH = os.path.join(os.environ["ProgramFiles"], "VOICEPEAK", "voicepeak.exe") @dataclass @@ -16,13 +19,13 @@ class Narrator: class Voicepeak: def __init__( self, - exe_path: str = os.path.join(os.environ["ProgramFiles"], "VOICEPEAK", "voicepeak.exe"), + exe_path: str | Path = _DEFAULT_EXE_PATH, ): """ 標準のインストール先ではない場所にVOICEPEAKインストールした場合はexe_pathを指定してください。 Args: - exe_path (str, optional): voicepeak.exeへのパス。Defaultは標準のインストール先。 + exe_path (str | Path, optional): voicepeak.exeへのパス。Defaultは標準のインストール先。 """ if not os.path.exists(exe_path): @@ -31,7 +34,7 @@ def __init__( async def __async_run(self, args: list[str]) -> str: proc = await asyncio.create_subprocess_exec( - self.__exe_path, + os.fspath(self.__exe_path), *args, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE, @@ -48,8 +51,8 @@ async def __async_run(self, args: list[str]) -> str: def __make_say_command( self, text: str | None = None, - text_file: str | None = None, - output_path: str | None = None, + text_file: str | Path | None = None, + output_path: str | Path | None = None, narrator: Narrator | str | None = None, emotions: dict[str, int] | None = None, speed: int | None = None, @@ -58,19 +61,21 @@ def __make_say_command( args = [] match text, text_file: - case str(), str(): + case str(), str() | Path(): raise ValueError("textかtext_fileの一方のみ指定してください") case str(), None: args += ["-s", text] - case None, str(): - args += ["-t", text_file] + case None, str() as text_file_path: + args += ["-t", os.fspath(text_file_path)] + case None, Path() as text_file_path: + args += ["-t", os.fspath(text_file_path)] case None, None: raise ValueError("textまたはtext_fileが設定されている必要があります。") case _: raise ValueError("textまたはtext_fileが不正な値です。") if output_path is not None: - args += ["-o", output_path] + args += ["-o", os.fspath(output_path)] match narrator: case Narrator(): @@ -105,7 +110,7 @@ async def say_text( self, text: str, *, - output_path: str | None = None, + output_path: str | Path | None = None, narrator: Narrator | str | None = None, emotions: dict[str, int] | None = None, speed: int | None = None, @@ -117,7 +122,8 @@ async def say_text( Args: text (str): 読み上げるテキスト - output_path (str | None, optional): wavファイル出力先。指定しないとvoicepeak.exeと同じ階層にoutput.wavが生成される。 Defaults to None. + output_path (str | Path | None, optional): wavファイル出力先。 + 指定しないとvoicepeak.exeと同じ階層にoutput.wavが生成される。 Defaults to None. narrator (Narrator | str | None, optional): 読み上げを行うナレータの種類。Narrator型またはstr型の名前で指定する。 Defaults to None. @@ -140,9 +146,9 @@ async def say_text( async def say_textfile( self, - text_path: str, + text_path: str | Path, *, - output_path: str = "./output.wav", + output_path: str | Path = "./output.wav", narrator: Narrator | str | None = None, emotions: dict[str, int] | None = None, speed: int | None = None, @@ -152,9 +158,9 @@ async def say_textfile( テキストファイル内のテキストを読み上げたwavファイルを保存する。 Args: - text_path (str): 読み上げるテキストファイルのパス + text_path (str | Path): 読み上げるテキストファイルのパス - output_path (str , optional): wavファイル出力先。Defaultはoutput.wavが生成される。 + output_path (str | Path, optional): wavファイル出力先。Defaultはoutput.wavが生成される。 narrator (Narrator | str | None, optional): 読み上げを行うナレータの種類。Narrator型またはstr型の名前で指定する。 Defaults to None. From 5e8e3f7901d7f9804dc80d34a333f297c0100286 Mon Sep 17 00:00:00 2001 From: Nanahuse <109294061+Nanahuse@users.noreply.github.com> Date: Sun, 9 Aug 2026 17:17:58 +0900 Subject: [PATCH 6/7] Improve Voicepeak test coverage (#16) * Improve Voicepeak test coverage * Automate VOICEPEAK file generation tests * Restore real VOICEPEAK integration tests --- test/test_manual_voicepeak_file_generation.py | 54 ++++ test/test_voicepeak.py | 263 ++++++++++++++++++ 2 files changed, 317 insertions(+) diff --git a/test/test_manual_voicepeak_file_generation.py b/test/test_manual_voicepeak_file_generation.py index ff03f93..90b5f16 100644 --- a/test/test_manual_voicepeak_file_generation.py +++ b/test/test_manual_voicepeak_file_generation.py @@ -115,3 +115,57 @@ async def test_say_testfile(): text_file = os.path.join(TEST_DIRECTORY, "sample.txt") await client.say_textfile(text_file, output_path=os.path.join(OUTPUT_PATH, "say_text.wav")) + + +@pytest.mark.asyncio +async def test_say_text_overwrites_existing_output_wav(): + import voicepeak_wrapper + + client = voicepeak_wrapper.Voicepeak() + output_path = Path(OUTPUT_PATH) / "test_overwrite.wav" + await asyncio.to_thread(output_path.write_bytes, b"previous generation") + + await client.say_text("本日は晴天なり", output_path=output_path) + + content = await asyncio.to_thread(output_path.read_bytes) + assert len(content) > 0 + assert content != b"previous generation" + assert content[:4] == b"RIFF" + + +@pytest.mark.asyncio +async def test_say_text_with_pathlib_output_narrator_and_emotions(): + import voicepeak_wrapper + + client = voicepeak_wrapper.Voicepeak() + narrators = await client.get_narrator_list() + assert len(narrators) > 0 + narrator = narrators[0] + + output_path = Path(OUTPUT_PATH) / "test_narrator_emotions.wav" + emotions = {narrator.emotions[0]: 100} if narrator.emotions else None + await client.say_text( + "本日は晴天なり", + output_path=output_path, + narrator=narrator.name, + emotions=emotions, + ) + + content = await asyncio.to_thread(output_path.read_bytes) + assert len(content) > 0 + assert content[:4] == b"RIFF" + + +@pytest.mark.asyncio +async def test_say_textfile_with_pathlib_input_and_output(): + import voicepeak_wrapper + + client = voicepeak_wrapper.Voicepeak() + text_file = Path(TEST_DIRECTORY) / "sample.txt" + output_path = Path(OUTPUT_PATH) / "test_textfile_path.wav" + + await client.say_textfile(text_file, output_path=output_path, speed=120, pitch=-50) + + content = await asyncio.to_thread(output_path.read_bytes) + assert len(content) > 0 + assert content[:4] == b"RIFF" diff --git a/test/test_voicepeak.py b/test/test_voicepeak.py index c5bc65c..7346070 100644 --- a/test/test_voicepeak.py +++ b/test/test_voicepeak.py @@ -158,3 +158,266 @@ async def test_say_textfile_accepts_paths(monkeypatch): stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE, ) + + +def test_init_raises_when_exe_path_does_not_exist(monkeypatch): + monkeypatch.setenv("ProgramFiles", os.path.dirname(__file__)) + import voicepeak_wrapper + + missing_path = os.path.join(os.path.dirname(__file__), "missing.exe") + + with pytest.raises(FileNotFoundError, match="VOICEPEAKの実行ファイルが見つかりません"): + voicepeak_wrapper.Voicepeak(exe_path=missing_path) + + +@pytest.mark.asyncio +async def test_say_textfile_accepts_str_path(monkeypatch): + client = create_client(monkeypatch) + process = ProcessResult(b"completed\n") + create_subprocess = mock_process(monkeypatch, process) + + result = await client.say_textfile("input.txt", output_path="output.wav") + + assert result == "completed\n" + create_subprocess.assert_awaited_once_with( + __file__, + "-t", + "input.txt", + "-o", + "output.wav", + stdout=asyncio.subprocess.PIPE, + stderr=asyncio.subprocess.PIPE, + ) + + +@pytest.mark.asyncio +async def test_say_textfile_uses_default_output_path(monkeypatch): + client = create_client(monkeypatch) + process = ProcessResult(b"completed\n") + create_subprocess = mock_process(monkeypatch, process) + + result = await client.say_textfile("input.txt") + + assert result == "completed\n" + create_subprocess.assert_awaited_once_with( + __file__, + "-t", + "input.txt", + "-o", + "./output.wav", + stdout=asyncio.subprocess.PIPE, + stderr=asyncio.subprocess.PIPE, + ) + + +@pytest.mark.asyncio +async def test_say_text_accepts_narrator_name(monkeypatch): + client = create_client(monkeypatch) + process = ProcessResult(b"completed\n") + create_subprocess = mock_process(monkeypatch, process) + + result = await client.say_text("本日は晴天なり", narrator="naruko") + + assert result == "completed\n" + create_subprocess.assert_awaited_once_with( + __file__, + "-s", + "本日は晴天なり", + "-n", + "naruko", + stdout=asyncio.subprocess.PIPE, + stderr=asyncio.subprocess.PIPE, + ) + + +@pytest.mark.asyncio +async def test_say_text_accepts_narrator_object(monkeypatch): + client = create_client(monkeypatch) + import voicepeak_wrapper + + process = ProcessResult(b"completed\n") + create_subprocess = mock_process(monkeypatch, process) + + result = await client.say_text("本日は晴天なり", narrator=voicepeak_wrapper.Narrator("naruko", ("happy",))) + + assert result == "completed\n" + create_subprocess.assert_awaited_once_with( + __file__, + "-s", + "本日は晴天なり", + "-n", + "naruko", + stdout=asyncio.subprocess.PIPE, + stderr=asyncio.subprocess.PIPE, + ) + + +@pytest.mark.asyncio +async def test_say_text_accepts_emotions(monkeypatch): + client = create_client(monkeypatch) + process = ProcessResult(b"completed\n") + create_subprocess = mock_process(monkeypatch, process) + + result = await client.say_text("本日は晴天なり", emotions={"happy": 100, "angry": 50}) + + assert result == "completed\n" + create_subprocess.assert_awaited_once_with( + __file__, + "-s", + "本日は晴天なり", + "-e", + "happy=100,angry=50", + stdout=asyncio.subprocess.PIPE, + stderr=asyncio.subprocess.PIPE, + ) + + +@pytest.mark.parametrize("speed", [50, 200]) +@pytest.mark.asyncio +async def test_say_text_accepts_valid_speed(monkeypatch, speed): + client = create_client(monkeypatch) + process = ProcessResult(b"completed\n") + create_subprocess = mock_process(monkeypatch, process) + + result = await client.say_text("本日は晴天なり", speed=speed) + + assert result == "completed\n" + create_subprocess.assert_awaited_once_with( + __file__, + "-s", + "本日は晴天なり", + "--speed", + str(speed), + stdout=asyncio.subprocess.PIPE, + stderr=asyncio.subprocess.PIPE, + ) + + +@pytest.mark.parametrize("speed", [49, 201, 100.5, "100"]) +@pytest.mark.asyncio +async def test_say_text_rejects_invalid_speed(monkeypatch, speed): + client = create_client(monkeypatch) + mock_process(monkeypatch, ProcessResult()) + + with pytest.raises(ValueError, match="speedは50 - 200の範囲内の整数"): + await client.say_text("本日は晴天なり", speed=speed) + + +@pytest.mark.parametrize("pitch", [-300, 300]) +@pytest.mark.asyncio +async def test_say_text_accepts_valid_pitch(monkeypatch, pitch): + client = create_client(monkeypatch) + process = ProcessResult(b"completed\n") + create_subprocess = mock_process(monkeypatch, process) + + result = await client.say_text("本日は晴天なり", pitch=pitch) + + assert result == "completed\n" + create_subprocess.assert_awaited_once_with( + __file__, + "-s", + "本日は晴天なり", + "--pitch", + str(pitch), + stdout=asyncio.subprocess.PIPE, + stderr=asyncio.subprocess.PIPE, + ) + + +@pytest.mark.parametrize("pitch", [-301, 301, 0.5, "0"]) +@pytest.mark.asyncio +async def test_say_text_rejects_invalid_pitch(monkeypatch, pitch): + client = create_client(monkeypatch) + mock_process(monkeypatch, ProcessResult()) + + with pytest.raises(ValueError, match="pitchは-300 - 300の範囲内の整数"): + await client.say_text("本日は晴天なり", pitch=pitch) + + +@pytest.mark.asyncio +async def test_say_text_with_all_options_builds_full_command(monkeypatch): + client = create_client(monkeypatch) + import voicepeak_wrapper + + process = ProcessResult(b"completed\n") + create_subprocess = mock_process(monkeypatch, process) + + result = await client.say_text( + "こんにちは", + output_path="out.wav", + narrator=voicepeak_wrapper.Narrator("Japanese Female 1", ("happy", "sad")), + emotions={"happy": 100, "sad": 50}, + speed=120, + pitch=-50, + ) + + assert result == "completed\n" + create_subprocess.assert_awaited_once_with( + __file__, + "-s", + "こんにちは", + "-o", + "out.wav", + "-n", + "Japanese Female 1", + "-e", + "happy=100,sad=50", + "--speed", + "120", + "--pitch", + "-50", + stdout=asyncio.subprocess.PIPE, + stderr=asyncio.subprocess.PIPE, + ) + + +def test_make_say_command_rejects_text_and_text_file_together(monkeypatch): + client = create_client(monkeypatch) + + with pytest.raises(ValueError, match="textかtext_fileの一方のみ指定してください"): + client._Voicepeak__make_say_command(text="text", text_file="input.txt") + + +def test_make_say_command_requires_text_or_text_file(monkeypatch): + client = create_client(monkeypatch) + + with pytest.raises(ValueError, match="textまたはtext_fileが設定されている必要があります"): + client._Voicepeak__make_say_command() + + +def test_make_say_command_rejects_invalid_text_type(monkeypatch): + client = create_client(monkeypatch) + + with pytest.raises(ValueError, match="textまたはtext_fileが不正な値です"): + client._Voicepeak__make_say_command(text=123) # type: ignore[arg-type] + + +def test_make_say_command_rejects_invalid_text_file_type(monkeypatch): + client = create_client(monkeypatch) + + with pytest.raises(ValueError, match="textまたはtext_fileが不正な値です"): + client._Voicepeak__make_say_command(text="text", text_file=123) # type: ignore[arg-type] + + +@pytest.mark.asyncio +async def test_get_emotion_list_returns_empty_tuple_for_empty_output(monkeypatch): + client = create_client(monkeypatch) + mock_process(monkeypatch, ProcessResult(b"")) + + assert await client.get_emotion_list("Japanese Female 1") == () + + +@pytest.mark.asyncio +async def test_get_narrator_name_list_handles_output_without_trailing_newline(monkeypatch): + client = create_client(monkeypatch) + mock_process(monkeypatch, ProcessResult(b"Japanese Female 1")) + + assert await client.get_narrator_name_list() == ("Japanese Female 1",) + + +@pytest.mark.asyncio +async def test_get_narrator_list_returns_empty_for_empty_output(monkeypatch): + client = create_client(monkeypatch) + mock_process(monkeypatch, ProcessResult(b"")) + + assert await client.get_narrator_list() == () From 4fa86130cb52cf98891e24577ba2d62264e0d62f Mon Sep 17 00:00:00 2001 From: Nanahuse <109294061+Nanahuse@users.noreply.github.com> Date: Sun, 9 Aug 2026 17:32:21 +0900 Subject: [PATCH 7/7] Bump version to 1.2.0 --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 68cdff5..8c9fd6b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "voicepeak_wrapper" -version = "1.1.1" +version = "1.2.0" description = "VOICEPEAKの非公式ラッパーです。詳しくはHomepage(Github)をご覧ください" requires-python = ">=3.10" license = { file = "LICENSE" }