offset-fixer_test.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #!/usr/bin/env python3
  2. import contextlib
  3. import importlib.util
  4. import io
  5. import pathlib
  6. import tempfile
  7. import unittest
  8. from unittest import mock
  9. _SCRIPT_PATH = pathlib.Path(__file__).with_name("offset-fixer.py")
  10. _SPEC = importlib.util.spec_from_file_location("offset_fixer", _SCRIPT_PATH)
  11. mod = importlib.util.module_from_spec(_SPEC)
  12. assert _SPEC and _SPEC.loader
  13. _SPEC.loader.exec_module(mod)
  14. class OffsetFixerTest(unittest.TestCase):
  15. def test_format_time_string(self) -> None:
  16. entry = mod.SubtitleEntry(1, "hello\n", "01:02:03,250", "01:02:04,500")
  17. self.assertEqual(
  18. "01:02:03,250 --> 01:02:04,500",
  19. entry.formatTimeString(),
  20. )
  21. def test_shift_updates_start_and_end(self) -> None:
  22. entry = mod.SubtitleEntry(1, "hello\n", "00:00:10,000", "00:00:12,500")
  23. entry.shift(1.25)
  24. self.assertEqual(
  25. "00:00:11,250 --> 00:00:13,750",
  26. entry.formatTimeString(),
  27. )
  28. def test_main_writes_shifted_output(self) -> None:
  29. srt = (
  30. "1\n"
  31. "00:00:01,000 --> 00:00:02,000\n"
  32. "Hello\n"
  33. "\n"
  34. "2\n"
  35. "00:00:03,500 --> 00:00:04,250\n"
  36. "Bye\n"
  37. "\n"
  38. )
  39. with tempfile.TemporaryDirectory() as td:
  40. in_path = pathlib.Path(td) / "input.srt"
  41. out_path = pathlib.Path(td) / "output.srt"
  42. in_path.write_text(srt, encoding="utf-8")
  43. argv = [
  44. "offset-fixer.py",
  45. "--seconds",
  46. "1.5",
  47. "--file",
  48. str(in_path),
  49. "--outfile",
  50. str(out_path),
  51. ]
  52. with mock.patch("sys.argv", argv), contextlib.redirect_stdout(io.StringIO()):
  53. mod.main()
  54. output = out_path.read_text(encoding="utf-8")
  55. self.assertIn("00:00:02,500 --> 00:00:03,500", output)
  56. self.assertIn("00:00:05,000 --> 00:00:05,750", output)
  57. self.assertIn("Hello", output)
  58. self.assertIn("Bye", output)
  59. def test_main_strip_first_and_last(self) -> None:
  60. srt = (
  61. "1\n"
  62. "00:00:01,000 --> 00:00:02,000\n"
  63. "First subtitle\n"
  64. "\n"
  65. "2\n"
  66. "00:00:03,000 --> 00:00:04,000\n"
  67. "Last subtitle\n"
  68. "\n"
  69. )
  70. with tempfile.TemporaryDirectory() as td:
  71. in_path = pathlib.Path(td) / "input.srt"
  72. out_path = pathlib.Path(td) / "output.srt"
  73. in_path.write_text(srt, encoding="utf-8")
  74. argv = [
  75. "offset-fixer.py",
  76. "--seconds",
  77. "0",
  78. "--file",
  79. str(in_path),
  80. "--outfile",
  81. str(out_path),
  82. "--strip-first",
  83. "--strip-last",
  84. ]
  85. with mock.patch("sys.argv", argv), contextlib.redirect_stdout(io.StringIO()):
  86. mod.main()
  87. output = out_path.read_text(encoding="utf-8")
  88. self.assertNotIn("First subtitle", output)
  89. self.assertNotIn("Last subtitle", output)
  90. self.assertIn("00:00:01,000 --> 00:00:02,000", output)
  91. self.assertIn("00:00:03,000 --> 00:00:04,000", output)
  92. def test_main_inplace_creates_backup_and_updates_source(self) -> None:
  93. srt = (
  94. "1\n"
  95. "00:00:01,000 --> 00:00:02,000\n"
  96. "Keep me\n"
  97. "\n"
  98. )
  99. with tempfile.TemporaryDirectory() as td:
  100. in_path = pathlib.Path(td) / "input.srt"
  101. in_path.write_text(srt, encoding="utf-8")
  102. argv = [
  103. "offset-fixer.py",
  104. "--seconds",
  105. "0.5",
  106. "--file",
  107. str(in_path),
  108. "--inplace",
  109. ]
  110. with mock.patch("sys.argv", argv), contextlib.redirect_stdout(io.StringIO()):
  111. mod.main()
  112. backup_text = (pathlib.Path(str(in_path) + ".backup")).read_text(encoding="utf-8")
  113. updated_text = in_path.read_text(encoding="utf-8")
  114. self.assertIn("00:00:01,000 --> 00:00:02,000", backup_text)
  115. self.assertIn("00:00:01,500 --> 00:00:02,500", updated_text)
  116. self.assertIn("Keep me", updated_text)
  117. def test_main_inplace_rejects_outfile(self) -> None:
  118. srt = (
  119. "1\n"
  120. "00:00:01,000 --> 00:00:02,000\n"
  121. "Hello\n"
  122. "\n"
  123. )
  124. with tempfile.TemporaryDirectory() as td:
  125. in_path = pathlib.Path(td) / "input.srt"
  126. out_path = pathlib.Path(td) / "output.srt"
  127. in_path.write_text(srt, encoding="utf-8")
  128. argv = [
  129. "offset-fixer.py",
  130. "--seconds",
  131. "1",
  132. "--file",
  133. str(in_path),
  134. "--inplace",
  135. "--outfile",
  136. str(out_path),
  137. ]
  138. with mock.patch("sys.argv", argv), contextlib.redirect_stdout(io.StringIO()):
  139. with self.assertRaises(SystemExit) as exc:
  140. mod.main()
  141. self.assertEqual(1, exc.exception.code)
  142. if __name__ == "__main__":
  143. unittest.main()