Files
Server-scripts/ffmpeg_handling/tests/test_cli.py
T

61 lines
2.3 KiB
Python

import tempfile
import unittest
from pathlib import Path
from unittest.mock import patch
from ffmpeg_handling import build_parser, main
ASSET_DIR = Path(__file__).resolve().parents[1] / "testfiles"
class CliTests(unittest.TestCase):
def test_test_command_is_not_registered(self):
with self.assertRaises(SystemExit) as error:
build_parser().parse_args(["test"])
self.assertEqual(error.exception.code, 2)
def test_short_command_alias_resolves_to_canonical_name(self):
args = build_parser().parse_args(["i", str(ASSET_DIR / "test_auto.mp4")])
self.assertEqual(args.command, "info")
def test_missing_ffmpeg_logs_error_and_returns_failure(self):
with patch("ffmpeg_handling.cli.shutil.which", return_value=None), \
patch("ffmpeg_handling.cli.setup_logging"), \
self.assertLogs("ffmpeg_handling", level="ERROR") as captured:
result = main(["info", str(ASSET_DIR / "test_auto_no_subs.mkv")])
self.assertEqual(result, 1)
self.assertIn("FFmpeg is not installed", captured.output[0])
def test_non_output_command_returns_success(self):
with patch("ffmpeg_handling.cli.setup_logging"), \
self.assertLogs("ffmpeg_handling", level="INFO"):
self.assertEqual(
main(["info", str(ASSET_DIR / "test_auto_no_subs.mkv")]), 0
)
def test_cli_dry_run_uses_requested_output_and_creates_nothing(self):
with tempfile.TemporaryDirectory() as temp_dir:
output = Path(temp_dir) / "dry_run.mkv"
with patch("ffmpeg_handling.cli.setup_logging"), \
self.assertLogs("ffmpeg_handling", level="INFO"):
result = main([
"downsize",
str(ASSET_DIR / "test_auto_no_subs.mkv"),
"-o",
str(output),
"--dry-run",
"-w",
"320",
"-he",
"180",
"-c",
"30",
])
self.assertEqual(result, 0)
self.assertFalse(output.exists())
if __name__ == "__main__":
unittest.main()