diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index d668aa71e..ceb7e29b6 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -60,3 +60,20 @@ jython-core: - export PATH="$HOME/jython/bin:$PATH" script: - ./devscripts/run_tests.sh + +# uncomment once the images are available +# https://github.com/microsoft/playwright-python/issues/338 +# playwright-tests-core: +# image: mcr.microsoft.com/playwright-python:focal +# variables: +# HDL_TEST_SET: core +# script: +# - ./devscripts/run_tests.sh + +# playwright-tests-download: +# image: mcr.microsoft.com/playwright-python:focal +# variables: +# HDL_TEST_SET: download +# HDL_TEST_PLAYWRIGHT_DOWNLOAD: 1 +# script: +# - ./devscripts/run_tests.sh diff --git a/devscripts/run_tests.sh b/devscripts/run_tests.sh index 307ce9e06..2ad1daab8 100755 --- a/devscripts/run_tests.sh +++ b/devscripts/run_tests.sh @@ -12,6 +12,9 @@ case "$HDL_TEST_SET" in ;; download) test_set="-I test_(?!$DOWNLOAD_TESTS).+\.py" + if [[ "$HDL_TEST_PLAYWRIGHT_DOWNLOAD" == "1" ]]; then + test_set="-I test_(?!download).+\.py" + fi multiprocess_args="--processes=4 --process-timeout=540" ;; *) diff --git a/haruhi_dl/extractor/common.py b/haruhi_dl/extractor/common.py index 21893ae73..db0e991a4 100644 --- a/haruhi_dl/extractor/common.py +++ b/haruhi_dl/extractor/common.py @@ -395,6 +395,7 @@ class InfoExtractor(object): _GEO_IP_BLOCKS = None _WORKING = True _SELFHOSTED = False + _REQUIRES_PLAYWRIGHT = False def __init__(self, downloader=None): """Constructor. Receives an optional downloader.""" diff --git a/haruhi_dl/playwright.py b/haruhi_dl/playwright.py new file mode 100644 index 000000000..957bdfe74 --- /dev/null +++ b/haruhi_dl/playwright.py @@ -0,0 +1,31 @@ +# coding: utf-8 +from __future__ import unicode_literals + +from .utils import ( + ExtractorError, +) + + +class PlaywrightHelper(): + _pw = None + _pw_version = None + + @classmethod + def _real_import_pw(cls): + from playwright import sync_playwright, _repo_version + cls._pw = sync_playwright + cls._pw_version = _repo_version.version + + @classmethod + def _import_pw(cls, fatal=True): + try: + cls._real_import_pw() + except ImportError as err: + if fatal is True: + raise ExtractorError('Playwright could not be imported: %s' % err.msg, expected=True) + + @classmethod + def _version(cls): + if not cls._pw_version: + cls._import_pw(fatal=False) + return cls._pw_version diff --git a/test/test_download.py b/test/test_download.py index 43eb13051..ed5bcdc66 100644 --- a/test/test_download.py +++ b/test/test_download.py @@ -37,7 +37,10 @@ from haruhi_dl.utils import ( UnavailableVideoError, ) from haruhi_dl.extractor import get_info_extractor +from haruhi_dl.playwright import PlaywrightHelper + +PLAYWRIGHT_INSTALLED = PlaywrightHelper._version() is not None RETRIES = 3 @@ -104,6 +107,14 @@ def generator(test_case, tname): print_skipping('IE marked as not _WORKING') return + if ie._REQUIRES_PLAYWRIGHT: + if not PLAYWRIGHT_INSTALLED: + print_skipping('Playwright is not installed') + return + elif os.environ.get('HDL_TEST_PLAYWRIGHT_DOWNLOAD') == '1': + print_skipping('Does not use Playwright') + return + for tc in test_cases: info_dict = tc.get('info_dict', {}) if not (info_dict.get('id') and info_dict.get('ext')): diff --git a/test/test_playwright.py b/test/test_playwright.py new file mode 100644 index 000000000..76d390c22 --- /dev/null +++ b/test/test_playwright.py @@ -0,0 +1,36 @@ +# coding: utf-8 +from __future__ import unicode_literals + +# Allow direct execution +import os +import sys +import unittest +sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) + +from haruhi_dl.compat import compat_str + +from haruhi_dl.playwright import PlaywrightHelper + + +class TestPlaywright(unittest.TestCase): + + def test_import_nonfatal(self): + PlaywrightHelper._import_pw(fatal=False) + + def test_import_fatal(self): + helper = PlaywrightHelper + try: + helper._import_pw(fatal=True) + self.assertIsNotNone(helper._pw) + self.assertIsInstance(helper._pw_version, compat_str) + except ImportError: + self.assertIsNone(helper._pw) + self.assertIsNone(helper._pw_version) + + def test_checking_version(self): + version = PlaywrightHelper._version() + self.assertIsInstance(version, (compat_str, None)) + + +if __name__ == '__main__': + unittest.main()