From fbd5ce74c0ce03f2280c9adeaaa753de7262d9e0 Mon Sep 17 00:00:00 2001 From: c-basalt <117849907+c-basalt@users.noreply.github.com> Date: Mon, 28 Apr 2025 23:55:23 -0400 Subject: [PATCH] skip test when unavailable --- test/test_jsi_external.py | 26 ++++++++++++++++---------- yt_dlp/jsinterp/common.py | 2 +- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/test/test_jsi_external.py b/test/test_jsi_external.py index 20f0b0c12..e633974f8 100644 --- a/test/test_jsi_external.py +++ b/test/test_jsi_external.py @@ -51,13 +51,19 @@ def __eq__(self, other: NetscapeFields | http.cookiejar.Cookie): return all(getattr(self, attr) == getattr(other, attr) for attr in ['name', 'value', 'domain', 'path', 'secure', 'expires']) -def test_jsi_rumtimes(exclude=[]): +def use_jsi_rumtimes(exclude=[]): def inner(func: typing.Callable[[unittest.TestCase, type[JSI]], None]): @functools.wraps(func) def wrapper(self: unittest.TestCase): for key, jsi in get_included_jsi(exclude=exclude).items(): + def wrapped_jsi_with_unavaliable_auto_skip(*args, **kwargs): + instance = jsi(*args, **kwargs) + if not instance.is_available(): + self.skipTest(f'{key} is not available') + return instance + with self.subTest(key): - func(self, jsi) + func(self, wrapped_jsi_with_unavaliable_auto_skip) return wrapper return inner @@ -69,12 +75,12 @@ class TestExternalJSI(unittest.TestCase): def setUp(self): self.ydl = FakeYDL() - @test_jsi_rumtimes() + @use_jsi_rumtimes() def test_execute(self, jsi_cls: type[JSI]): jsi = jsi_cls(self.ydl, '', 10) self.assertEqual(jsi.execute('console.log("Hello, world!");'), 'Hello, world!') - @test_jsi_rumtimes() + @use_jsi_rumtimes() def test_user_agent(self, jsi_cls: type[JSI]): ua = self.ydl.params['http_headers']['User-Agent'] @@ -85,13 +91,13 @@ def test_user_agent(self, jsi_cls: type[JSI]): jsi = jsi_cls(self.ydl, '', 10, user_agent='test/ua') self.assertEqual(jsi.execute('console.log(navigator.userAgent);'), 'test/ua') - @test_jsi_rumtimes() + @use_jsi_rumtimes() def test_location(self, jsi_cls: type[JSI]): jsi = jsi_cls(self.ydl, 'https://example.com/123/456', 10) self.assertEqual(jsi.execute('console.log(JSON.stringify([location.href, location.hostname]));'), '["https://example.com/123/456","example.com"]') - @test_jsi_rumtimes(exclude=['Deno']) + @use_jsi_rumtimes(exclude=['Deno']) def test_execute_dom_parse(self, jsi_cls: type[JSI]): jsi = jsi_cls(self.ydl, '', 10) self.assertEqual(jsi.execute( @@ -99,7 +105,7 @@ def test_execute_dom_parse(self, jsi_cls: type[JSI]): html='