107 lines
3.1 KiB
Python
107 lines
3.1 KiB
Python
from __future__ import annotations
|
|
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
|
|
from tests.support.ffx_bundle import SourceTrackSpec, create_source_fixture
|
|
|
|
from ffx.track_type import TrackType
|
|
|
|
try:
|
|
import pytest
|
|
except ImportError: # pragma: no cover - unittest-only environments
|
|
pytest = None
|
|
|
|
if pytest is not None:
|
|
pytestmark = [pytest.mark.integration]
|
|
|
|
|
|
SRC_ROOT = Path(__file__).resolve().parents[2] / "src"
|
|
|
|
|
|
def run_ffx_unmux(workdir: Path, home_dir: Path, database_path: Path, *args: str) -> subprocess.CompletedProcess[str]:
|
|
env = os.environ.copy()
|
|
env["HOME"] = str(home_dir)
|
|
existing_pythonpath = env.get("PYTHONPATH", "")
|
|
env["PYTHONPATH"] = str(SRC_ROOT) if not existing_pythonpath else f"{SRC_ROOT}{os.pathsep}{existing_pythonpath}"
|
|
|
|
command = [
|
|
sys.executable,
|
|
"-m",
|
|
"ffx",
|
|
"--database-file",
|
|
str(database_path),
|
|
"unmux",
|
|
*args,
|
|
]
|
|
return subprocess.run(command, cwd=workdir, env=env, capture_output=True, text=True)
|
|
|
|
|
|
class UnmuxCliTests(unittest.TestCase):
|
|
def setUp(self):
|
|
self.tempdir = tempfile.TemporaryDirectory()
|
|
self.workdir = Path(self.tempdir.name)
|
|
self.home_dir = self.workdir / "home"
|
|
self.home_dir.mkdir()
|
|
self.database_path = self.workdir / "test.db"
|
|
|
|
def tearDown(self):
|
|
self.tempdir.cleanup()
|
|
|
|
def write_config(self, data: dict) -> None:
|
|
config_dir = self.home_dir / ".local" / "etc"
|
|
config_dir.mkdir(parents=True, exist_ok=True)
|
|
(config_dir / "ffx.json").write_text(json.dumps(data), encoding="utf-8")
|
|
|
|
def assertCompleted(self, completed):
|
|
if completed.returncode != 0:
|
|
self.fail(
|
|
"FFX unmux failed\n"
|
|
f"STDOUT:\n{completed.stdout}\n"
|
|
f"STDERR:\n{completed.stderr}"
|
|
)
|
|
|
|
def test_subtitles_only_without_output_directory_uses_configured_base_plus_label(self):
|
|
self.write_config(
|
|
{
|
|
"subtitlesDirectory": "~/.local/var/sync/subtitles",
|
|
}
|
|
)
|
|
source_filename = "unmux_s01e01.mkv"
|
|
source_path = create_source_fixture(
|
|
self.workdir,
|
|
source_filename,
|
|
[
|
|
SourceTrackSpec(TrackType.VIDEO, identity="video-0"),
|
|
SourceTrackSpec(
|
|
TrackType.SUBTITLE,
|
|
identity="subtitle-1",
|
|
language="eng",
|
|
subtitle_lines=("subtitle payload",),
|
|
),
|
|
],
|
|
)
|
|
|
|
completed = run_ffx_unmux(
|
|
self.workdir,
|
|
self.home_dir,
|
|
self.database_path,
|
|
"--subtitles-only",
|
|
"--label",
|
|
"dball",
|
|
str(source_path),
|
|
)
|
|
self.assertCompleted(completed)
|
|
|
|
expected_directory = self.home_dir / ".local" / "var" / "sync" / "subtitles" / "dball"
|
|
self.assertTrue(expected_directory.is_dir(), expected_directory)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|