test_execution.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #!/usr/bin/env python3
  2. # Allow direct execution
  3. import os
  4. import sys
  5. import unittest
  6. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  7. import contextlib
  8. import subprocess
  9. from yt_dlp.utils import Popen
  10. rootDir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  11. LAZY_EXTRACTORS = 'yt_dlp/extractor/lazy_extractors.py'
  12. class TestExecution(unittest.TestCase):
  13. def run_yt_dlp(self, exe=(sys.executable, 'yt_dlp/__main__.py'), opts=('--version', )):
  14. stdout, stderr, returncode = Popen.run(
  15. [*exe, '--ignore-config', *opts], cwd=rootDir, text=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  16. print(stderr, file=sys.stderr)
  17. self.assertEqual(returncode, 0)
  18. return stdout.strip(), stderr.strip()
  19. def test_main_exec(self):
  20. self.run_yt_dlp()
  21. def test_import(self):
  22. self.run_yt_dlp(exe=(sys.executable, '-c', 'import yt_dlp'))
  23. def test_module_exec(self):
  24. self.run_yt_dlp(exe=(sys.executable, '-m', 'yt_dlp'))
  25. def test_cmdline_umlauts(self):
  26. _, stderr = self.run_yt_dlp(opts=('ä', '--version'))
  27. self.assertFalse(stderr)
  28. def test_lazy_extractors(self):
  29. try:
  30. subprocess.check_call([sys.executable, 'devscripts/make_lazy_extractors.py', LAZY_EXTRACTORS],
  31. cwd=rootDir, stdout=subprocess.DEVNULL)
  32. self.assertTrue(os.path.exists(LAZY_EXTRACTORS))
  33. _, stderr = self.run_yt_dlp(opts=('-s', 'test:'))
  34. # `MIN_RECOMMENDED` emits a deprecated feature warning for deprecated Python versions
  35. if stderr and stderr.startswith('Deprecated Feature: Support for Python'):
  36. stderr = ''
  37. self.assertFalse(stderr)
  38. subprocess.check_call([sys.executable, 'test/test_all_urls.py'], cwd=rootDir, stdout=subprocess.DEVNULL)
  39. finally:
  40. with contextlib.suppress(OSError):
  41. os.remove(LAZY_EXTRACTORS)
  42. if __name__ == '__main__':
  43. unittest.main()