Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ permissions:

jobs:
test:
runs-on: ubuntu-latest
runs-on: windows-latest
steps:
- name: Checkout
uses: actions/checkout@v6
Expand Down
4 changes: 1 addition & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,4 @@ lint.ignore = [
"D",
"TD",
]

[tool.ruff.per-file-ignores]
"test/*" = ["INP001", "PLR2004", "SLF001"]
lint.per-file-ignores = { "test/*" = ["INP001", "PLR2004", "SLF001"] }
106 changes: 51 additions & 55 deletions test/manual_voicepeak_file_generation.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 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
Expand All @@ -6,97 +10,89 @@
uv run --extra dev --with-editable . pytest test/manual_voicepeak_file_generation.py
"""

import os
import asyncio
from pathlib import Path

import pytest

TEST_DIRECTORY = os.path.dirname(os.path.abspath(__file__))
OUTPUT_PATH = os.path.join(TEST_DIRECTORY, "output")
import voicepeak_wrapper

TEST_DIRECTORY = Path(__file__).resolve().parent
OUTPUT_PATH = TEST_DIRECTORY / "output"

@pytest.mark.asyncio
async def test_get_narrator_list():
import voicepeak_wrapper

def _write_text(path: Path, text: str) -> None:
with path.open(mode="w", encoding="UTF-8") as f:
f.write(text)


@pytest.mark.asyncio
async def test_get_narrator_list() -> None:
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},
)
output = []
for narrator in narrators:
output.append(f"{narrator.name} : {', '.join(narrator.emotions)}\n")
await client.say_text(
"本日は晴天なり",
output_path=OUTPUT_PATH / f"narrator_{narrator.name}.wav",
narrator=narrator,
emotions={narrator.emotions[1]: 100},
)
await asyncio.to_thread(_write_text, OUTPUT_PATH / "narrators.txt", "".join(output))


@pytest.mark.asyncio
async def test_get_narrator_name_list():
import voicepeak_wrapper

async def test_get_narrator_name_list() -> None:
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)
content = "".join(f"{name}\n" for name in narrator_names)
await asyncio.to_thread(_write_text, OUTPUT_PATH / "narrator_names.txt", content)


@pytest.mark.asyncio
async def test_get_emotion_list():
import voicepeak_wrapper

async def test_get_emotion_list() -> None:
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")
output = []
for name in narrator_names:
emotion_list = await client.get_emotion_list(name)
output.append(f"{name} : {', '.join(emotion_list)}\n")
await asyncio.to_thread(_write_text, OUTPUT_PATH / "emotions.txt", "".join(output))

with pytest.raises(RuntimeError):
await client.get_emotion_list("hogehoge")


@pytest.mark.asyncio
async def test_say_text():
import voicepeak_wrapper

async def test_say_text() -> None:
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=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
"本日は晴天なり", output_path=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
"本日は晴天なり", output_path=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(ValueError, match="speed"):
await client.say_text("エラー", output_path=OUTPUT_PATH / "error.wav", speed=201)
with pytest.raises(ValueError, match="speed"):
await client.say_text("エラー", output_path=OUTPUT_PATH / "error.wav", speed=49)
with pytest.raises(ValueError, match="pitch"):
await client.say_text("エラー", output_path=OUTPUT_PATH / "error.wav", pitch=-301)
with pytest.raises(ValueError, match="pitch"):
await client.say_text("エラー", output_path=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"))
await client.say_text("1" * 141, output_path=OUTPUT_PATH / "error.wav")


@pytest.mark.asyncio
async def test_say_testfile():
import voicepeak_wrapper

async def test_say_testfile() -> None:
client = voicepeak_wrapper.Voicepeak()

text_file = os.path.join(TEST_DIRECTORY, "sample.txt")
text_file = TEST_DIRECTORY / "sample.txt"

await client.say_textfile(text_file, output_path=os.path.join(OUTPUT_PATH, "say_text.wav"))
await client.say_textfile(text_file, output_path=OUTPUT_PATH / "say_text.wav")
55 changes: 26 additions & 29 deletions test/test_voicepeak.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,34 @@

import pytest

import voicepeak_wrapper


class ProcessResult:
def __init__(self, stdout: bytes = b"", stderr: bytes = b""):
def __init__(self, stdout: bytes = b"", stderr: bytes = b"") -> None:
self.communicate = AsyncMock(return_value=(stdout, stderr))


def create_client(monkeypatch):
monkeypatch.setenv("ProgramFiles", os.path.dirname(__file__))
import voicepeak_wrapper

def create_client(monkeypatch: pytest.MonkeyPatch) -> voicepeak_wrapper.Voicepeak:
monkeypatch.setenv("PROGRAMFILES", str(Path(__file__).parent))
return voicepeak_wrapper.Voicepeak(exe_path=__file__)


def create_client_with_pathlib_path(monkeypatch):
monkeypatch.setenv("ProgramFiles", os.path.dirname(__file__))
import voicepeak_wrapper

def create_client_with_pathlib_path(monkeypatch: pytest.MonkeyPatch) -> voicepeak_wrapper.Voicepeak:
monkeypatch.setenv("PROGRAMFILES", str(Path(__file__).parent))
return voicepeak_wrapper.Voicepeak(exe_path=Path(__file__))


def mock_process(monkeypatch, *results):
def mock_process(monkeypatch: pytest.MonkeyPatch, *results: ProcessResult) -> AsyncMock:
create_subprocess = AsyncMock(side_effect=results)
monkeypatch.setattr(asyncio, "create_subprocess_exec", create_subprocess)
return create_subprocess


@pytest.mark.asyncio
async def test_get_narrator_name_list_decodes_terminal_output(monkeypatch):
async def test_get_narrator_name_list_decodes_terminal_output(monkeypatch: pytest.MonkeyPatch) -> None:
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")
Expand All @@ -52,23 +50,22 @@ async def test_get_narrator_name_list_decodes_terminal_output(monkeypatch):


@pytest.mark.asyncio
async def test_get_emotion_list_decodes_terminal_output(monkeypatch):
async def test_get_emotion_list_decodes_terminal_output(monkeypatch: pytest.MonkeyPatch) -> None:
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")


@pytest.mark.asyncio
async def test_get_narrator_list_uses_each_terminal_output(monkeypatch):
async def test_get_narrator_list_uses_each_terminal_output(monkeypatch: pytest.MonkeyPatch) -> None:
client = create_client(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)

Expand All @@ -79,9 +76,9 @@ async def test_get_narrator_list_uses_each_terminal_output(monkeypatch):


@pytest.mark.asyncio
async def test_say_text_returns_decoded_terminal_output(monkeypatch):
async def test_say_text_returns_decoded_terminal_output(monkeypatch: pytest.MonkeyPatch) -> None:
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")
Expand All @@ -100,19 +97,19 @@ async def test_say_text_returns_decoded_terminal_output(monkeypatch):


@pytest.mark.asyncio
async def test_terminal_error_is_decoded_as_runtime_error(monkeypatch):
async def test_terminal_error_is_decoded_as_runtime_error(monkeypatch: pytest.MonkeyPatch) -> None:
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"):
await client.get_emotion_list("unknown")


@pytest.mark.asyncio
async def test_say_text_accepts_pathlib_path_for_output_path(monkeypatch):
async def test_say_text_accepts_pathlib_path_for_output_path(monkeypatch: pytest.MonkeyPatch) -> None:
client = create_client(monkeypatch)
process = ProcessResult("completed\n".encode())
process = ProcessResult(b"completed\n")
create_subprocess = mock_process(monkeypatch, process)

await client.say_text("本日は晴天なり", output_path=Path("output.wav"))
Expand All @@ -129,9 +126,9 @@ async def test_say_text_accepts_pathlib_path_for_output_path(monkeypatch):


@pytest.mark.asyncio
async def test_say_textfile_accepts_pathlib_path(monkeypatch):
async def test_say_textfile_accepts_pathlib_path(monkeypatch: pytest.MonkeyPatch) -> None:
client = create_client(monkeypatch)
process = ProcessResult("completed\n".encode())
process = ProcessResult(b"completed\n")
create_subprocess = mock_process(monkeypatch, process)

await client.say_textfile(Path("input.txt"), output_path=Path("output.wav"))
Expand All @@ -148,9 +145,9 @@ async def test_say_textfile_accepts_pathlib_path(monkeypatch):


@pytest.mark.asyncio
async def test_voicepeak_accepts_pathlib_path_for_exe_path(monkeypatch):
async def test_voicepeak_accepts_pathlib_path_for_exe_path(monkeypatch: pytest.MonkeyPatch) -> None:
client = create_client_with_pathlib_path(monkeypatch)
process = ProcessResult("Japanese Female 1\n".encode())
process = ProcessResult(b"Japanese Female 1\n")
create_subprocess = mock_process(monkeypatch, process)

assert await client.get_narrator_name_list() == ("Japanese Female 1",)
Expand Down
Loading