| 1 |
import io |
| 2 |
import os |
| 3 |
import re |
| 4 |
import shlex |
| 5 |
import subprocess |
| 6 |
import sys |
| 7 |
import unittest |
| 8 |
import webbrowser |
| 9 |
from functools import partial |
| 10 |
from test import support |
| 11 |
from test.support import import_helper |
| 12 |
from test.support import is_apple_mobile |
| 13 |
from test.support import os_helper |
| 14 |
from test.support import requires_subprocess |
| 15 |
from test.support import threading_helper |
| 16 |
from unittest import mock |
| 17 |
|
| 18 |
# The webbrowser module uses threading locks |
| 19 |
threading_helper.requires_working_threading(module=True) |
| 20 |
|
| 21 |
URL = 'https://www.example.com' |
| 22 |
CMD_NAME = 'test' |
| 23 |
|
| 24 |
|
| 25 |
class PopenMock(mock.MagicMock): |
| 26 |
|
| 27 |
def poll(self): |
| 28 |
return 0 |
| 29 |
|
| 30 |
def wait(self, seconds=None): |
| 31 |
return 0 |
| 32 |
|
| 33 |
|
| 34 |
@requires_subprocess() |
| 35 |
class CommandTestMixin: |
| 36 |
|
| 37 |
def _test(self, meth, *, args=[URL], kw={}, options, arguments): |
| 38 |
"""Given a web browser instance method name along with arguments and |
| 39 |
keywords for same (which defaults to the single argument URL), creates |
| 40 |
a browser instance from the class pointed to by self.browser, calls the |
| 41 |
indicated instance method with the indicated arguments, and compares |
| 42 |
the resulting options and arguments passed to Popen by the browser |
| 43 |
instance against the 'options' and 'args' lists. Options are compared |
| 44 |
in a position independent fashion, and the arguments are compared in |
| 45 |
sequence order to whatever is left over after removing the options. |
| 46 |
|
| 47 |
""" |
| 48 |
popen = PopenMock() |
| 49 |
support.patch(self, subprocess, 'Popen', popen) |
| 50 |
browser = self.browser_class(name=CMD_NAME) |
| 51 |
getattr(browser, meth)(*args, **kw) |
| 52 |
popen_args = subprocess.Popen.call_args[0][0] |
| 53 |
self.assertEqual(popen_args[0], CMD_NAME) |
| 54 |
popen_args.pop(0) |
| 55 |
for option in options: |
| 56 |
self.assertIn(option, popen_args) |
| 57 |
popen_args.pop(popen_args.index(option)) |
| 58 |
self.assertEqual(popen_args, arguments) |
| 59 |
|
| 60 |
def test_reject_dash_prefixes(self): |
| 61 |
browser = self.browser_class(name=CMD_NAME) |
| 62 |
with self.assertRaisesRegex( |
| 63 |
ValueError, |
| 64 |
r"^Invalid URL \(leading dash disallowed\): '--key=val http.*'$" |
| 65 |
): |
| 66 |
browser.open(f"--key=val {URL}") |
| 67 |
|
| 68 |
|
| 69 |
class GenericBrowserCommandTest(CommandTestMixin, unittest.TestCase): |
| 70 |
|
| 71 |
browser_class = webbrowser.GenericBrowser |
| 72 |
|
| 73 |
def test_open(self): |
| 74 |
self._test('open', |
| 75 |
options=[], |
| 76 |
arguments=[URL]) |
| 77 |
|
| 78 |
|
| 79 |
class BackgroundBrowserCommandTest(CommandTestMixin, unittest.TestCase): |
| 80 |
|
| 81 |
browser_class = webbrowser.BackgroundBrowser |
| 82 |
|
| 83 |
def test_open(self): |
| 84 |
self._test('open', |
| 85 |
options=[], |
| 86 |
arguments=[URL]) |
| 87 |
|
| 88 |
|
| 89 |
class ChromeCommandTest(CommandTestMixin, unittest.TestCase): |
| 90 |
|
| 91 |
browser_class = webbrowser.Chrome |
| 92 |
|
| 93 |
def test_open(self): |
| 94 |
self._test('open', |
| 95 |
options=[], |
| 96 |
arguments=[URL]) |
| 97 |
|
| 98 |
def test_open_with_autoraise_false(self): |
| 99 |
self._test('open', kw=dict(autoraise=False), |
| 100 |
options=[], |
| 101 |
arguments=[URL]) |
| 102 |
|
| 103 |
def test_open_new(self): |
| 104 |
self._test('open_new', |
| 105 |
options=['--new-window'], |
| 106 |
arguments=[URL]) |
| 107 |
|
| 108 |
def test_open_new_tab(self): |
| 109 |
self._test('open_new_tab', |
| 110 |
options=[], |
| 111 |
arguments=[URL]) |
| 112 |
|
| 113 |
def test_open_bad_new_parameter(self): |
| 114 |
with self.assertRaisesRegex(webbrowser.Error, |
| 115 |
re.escape("Bad 'new' parameter to open(); " |
| 116 |
"expected 0, 1, or 2, got 999")): |
| 117 |
self._test('open', |
| 118 |
options=[], |
| 119 |
arguments=[URL], |
| 120 |
kw=dict(new=999)) |
| 121 |
|
| 122 |
def test_reject_action_dash_prefixes(self): |
| 123 |
browser = self.browser_class(name=CMD_NAME) |
| 124 |
with self.assertRaises(ValueError): |
| 125 |
browser.open('%action--incognito') |
| 126 |
# new=1: action is "--new-window", so "%action" itself expands to |
| 127 |
# a dash-prefixed flag even with no dash in the original URL. |
| 128 |
with self.assertRaises(ValueError): |
| 129 |
browser.open('%action', new=1) |
| 130 |
|
| 131 |
|
| 132 |
class EdgeCommandTest(CommandTestMixin, unittest.TestCase): |
| 133 |
|
| 134 |
browser_class = webbrowser.Edge |
| 135 |
|
| 136 |
def test_open(self): |
| 137 |
self._test('open', |
| 138 |
options=[], |
| 139 |
arguments=[URL]) |
| 140 |
|
| 141 |
def test_open_with_autoraise_false(self): |
| 142 |
self._test('open', kw=dict(autoraise=False), |
| 143 |
options=[], |
| 144 |
arguments=[URL]) |
| 145 |
|
| 146 |
def test_open_new(self): |
| 147 |
self._test('open_new', |
| 148 |
options=['--new-window'], |
| 149 |
arguments=[URL]) |
| 150 |
|
| 151 |
def test_open_new_tab(self): |
| 152 |
self._test('open_new_tab', |
| 153 |
options=[], |
| 154 |
arguments=[URL]) |
| 155 |
|
| 156 |
|
| 157 |
class MozillaCommandTest(CommandTestMixin, unittest.TestCase): |
| 158 |
|
| 159 |
browser_class = webbrowser.Mozilla |
| 160 |
|
| 161 |
def test_open(self): |
| 162 |
self._test('open', |
| 163 |
options=[], |
| 164 |
arguments=[URL]) |
| 165 |
|
| 166 |
def test_open_with_autoraise_false(self): |
| 167 |
self._test('open', kw=dict(autoraise=False), |
| 168 |
options=[], |
| 169 |
arguments=[URL]) |
| 170 |
|
| 171 |
def test_open_new(self): |
| 172 |
self._test('open_new', |
| 173 |
options=[], |
| 174 |
arguments=['-new-window', URL]) |
| 175 |
|
| 176 |
def test_open_new_tab(self): |
| 177 |
self._test('open_new_tab', |
| 178 |
options=[], |
| 179 |
arguments=['-new-tab', URL]) |
| 180 |
|
| 181 |
|
| 182 |
class EpiphanyCommandTest(CommandTestMixin, unittest.TestCase): |
| 183 |
|
| 184 |
browser_class = webbrowser.Epiphany |
| 185 |
|
| 186 |
def test_open(self): |
| 187 |
self._test('open', |
| 188 |
options=['-n'], |
| 189 |
arguments=[URL]) |
| 190 |
|
| 191 |
def test_open_with_autoraise_false(self): |
| 192 |
self._test('open', kw=dict(autoraise=False), |
| 193 |
options=['-noraise', '-n'], |
| 194 |
arguments=[URL]) |
| 195 |
|
| 196 |
def test_open_new(self): |
| 197 |
self._test('open_new', |
| 198 |
options=['-w'], |
| 199 |
arguments=[URL]) |
| 200 |
|
| 201 |
def test_open_new_tab(self): |
| 202 |
self._test('open_new_tab', |
| 203 |
options=['-w'], |
| 204 |
arguments=[URL]) |
| 205 |
|
| 206 |
|
| 207 |
class OperaCommandTest(CommandTestMixin, unittest.TestCase): |
| 208 |
|
| 209 |
browser_class = webbrowser.Opera |
| 210 |
|
| 211 |
def test_open(self): |
| 212 |
self._test('open', |
| 213 |
options=[], |
| 214 |
arguments=[URL]) |
| 215 |
|
| 216 |
def test_open_with_autoraise_false(self): |
| 217 |
self._test('open', kw=dict(autoraise=False), |
| 218 |
options=[], |
| 219 |
arguments=[URL]) |
| 220 |
|
| 221 |
def test_open_new(self): |
| 222 |
self._test('open_new', |
| 223 |
options=['--new-window'], |
| 224 |
arguments=[URL]) |
| 225 |
|
| 226 |
def test_open_new_tab(self): |
| 227 |
self._test('open_new_tab', |
| 228 |
options=[], |
| 229 |
arguments=[URL]) |
| 230 |
|
| 231 |
|
| 232 |
class ELinksCommandTest(CommandTestMixin, unittest.TestCase): |
| 233 |
|
| 234 |
browser_class = webbrowser.Elinks |
| 235 |
|
| 236 |
def test_open(self): |
| 237 |
self._test('open', options=['-remote'], |
| 238 |
arguments=[f'openURL({URL})']) |
| 239 |
|
| 240 |
def test_open_with_autoraise_false(self): |
| 241 |
self._test('open', |
| 242 |
options=['-remote'], |
| 243 |
arguments=[f'openURL({URL})']) |
| 244 |
|
| 245 |
def test_open_new(self): |
| 246 |
self._test('open_new', |
| 247 |
options=['-remote'], |
| 248 |
arguments=[f'openURL({URL},new-window)']) |
| 249 |
|
| 250 |
def test_open_new_tab(self): |
| 251 |
self._test('open_new_tab', |
| 252 |
options=['-remote'], |
| 253 |
arguments=[f'openURL({URL},new-tab)']) |
| 254 |
|
| 255 |
|
| 256 |
@unittest.skipUnless(sys.platform == "ios", "Test only applicable to iOS") |
| 257 |
class IOSBrowserTest(unittest.TestCase): |
| 258 |
def _obj_ref(self, *args): |
| 259 |
# Construct a string representation of the arguments that can be used |
| 260 |
# as a proxy for object instance references |
| 261 |
return "|".join(str(a) for a in args) |
| 262 |
|
| 263 |
@unittest.skipIf(getattr(webbrowser, "objc", None) is None, |
| 264 |
"iOS Webbrowser tests require ctypes") |
| 265 |
def setUp(self): |
| 266 |
# Intercept the objc library. Wrap the calls to get the |
| 267 |
# references to classes and selectors to return strings, and |
| 268 |
# wrap msgSend to return stringified object references |
| 269 |
self.orig_objc = webbrowser.objc |
| 270 |
|
| 271 |
webbrowser.objc = mock.Mock() |
| 272 |
webbrowser.objc.objc_getClass = lambda cls: f"C#{cls.decode()}" |
| 273 |
webbrowser.objc.sel_registerName = lambda sel: f"S#{sel.decode()}" |
| 274 |
webbrowser.objc.objc_msgSend.side_effect = self._obj_ref |
| 275 |
|
| 276 |
def tearDown(self): |
| 277 |
webbrowser.objc = self.orig_objc |
| 278 |
|
| 279 |
def _test(self, meth, **kwargs): |
| 280 |
# The browser always gets focus, there's no concept of separate browser |
| 281 |
# windows, and there's no API-level control over creating a new tab. |
| 282 |
# Therefore, all calls to webbrowser are effectively the same. |
| 283 |
getattr(webbrowser, meth)(URL, **kwargs) |
| 284 |
|
| 285 |
# The ObjC String version of the URL is created with UTF-8 encoding |
| 286 |
url_string_args = [ |
| 287 |
"C#NSString", |
| 288 |
"S#stringWithCString:encoding:", |
| 289 |
b'https://www.example.com', |
| 290 |
4, |
| 291 |
] |
| 292 |
# The NSURL version of the URL is created from that string |
| 293 |
url_obj_args = [ |
| 294 |
"C#NSURL", |
| 295 |
"S#URLWithString:", |
| 296 |
self._obj_ref(*url_string_args), |
| 297 |
] |
| 298 |
# The openURL call is invoked on the shared application |
| 299 |
shared_app_args = ["C#UIApplication", "S#sharedApplication"] |
| 300 |
|
| 301 |
# Verify that the last call is the one that opens the URL. |
| 302 |
webbrowser.objc.objc_msgSend.assert_called_with( |
| 303 |
self._obj_ref(*shared_app_args), |
| 304 |
"S#openURL:options:completionHandler:", |
| 305 |
self._obj_ref(*url_obj_args), |
| 306 |
None, |
| 307 |
None |
| 308 |
) |
| 309 |
|
| 310 |
def test_open(self): |
| 311 |
self._test('open') |
| 312 |
|
| 313 |
def test_open_with_autoraise_false(self): |
| 314 |
self._test('open', autoraise=False) |
| 315 |
|
| 316 |
def test_open_new(self): |
| 317 |
self._test('open_new') |
| 318 |
|
| 319 |
def test_open_new_tab(self): |
| 320 |
self._test('open_new_tab') |
| 321 |
|
| 322 |
|
| 323 |
class MockPopenPipe: |
| 324 |
def __init__(self, cmd, mode): |
| 325 |
self.cmd = cmd |
| 326 |
self.mode = mode |
| 327 |
self.pipe = io.StringIO() |
| 328 |
self._closed = False |
| 329 |
|
| 330 |
def write(self, buf): |
| 331 |
self.pipe.write(buf) |
| 332 |
|
| 333 |
def close(self): |
| 334 |
self._closed = True |
| 335 |
return None |
| 336 |
|
| 337 |
|
| 338 |
@unittest.skipUnless(sys.platform == "darwin", "macOS specific test") |
| 339 |
@requires_subprocess() |
| 340 |
class MacOSXOSAScriptTest(unittest.TestCase): |
| 341 |
def setUp(self): |
| 342 |
# Ensure that 'BROWSER' is not set to 'open' or something else. |
| 343 |
# See: https://github.com/python/cpython/issues/131254. |
| 344 |
env = self.enterContext(os_helper.EnvironmentVarGuard()) |
| 345 |
env.unset("BROWSER") |
| 346 |
|
| 347 |
support.patch(self, os, "popen", self.mock_popen) |
| 348 |
self.browser = webbrowser.MacOSXOSAScript("default") |
| 349 |
|
| 350 |
def mock_popen(self, cmd, mode): |
| 351 |
self.popen_pipe = MockPopenPipe(cmd, mode) |
| 352 |
return self.popen_pipe |
| 353 |
|
| 354 |
def test_default(self): |
| 355 |
browser = webbrowser.get() |
| 356 |
assert isinstance(browser, webbrowser.MacOSXOSAScript) |
| 357 |
self.assertEqual(browser.name, "default") |
| 358 |
|
| 359 |
def test_default_open(self): |
| 360 |
url = "https://python.org" |
| 361 |
self.browser.open(url) |
| 362 |
self.assertTrue(self.popen_pipe._closed) |
| 363 |
self.assertEqual(self.popen_pipe.cmd, "/usr/bin/osascript") |
| 364 |
script = self.popen_pipe.pipe.getvalue() |
| 365 |
self.assertEqual(script.strip(), f'open location "{url}"') |
| 366 |
|
| 367 |
def test_url_quote(self): |
| 368 |
self.browser.open('https://python.org/"quote"') |
| 369 |
script = self.popen_pipe.pipe.getvalue() |
| 370 |
self.assertEqual( |
| 371 |
script.strip(), 'open location "https://python.org/%22quote%22"' |
| 372 |
) |
| 373 |
|
| 374 |
def test_default_browser_lookup(self): |
| 375 |
url = "file:///tmp/some-file.html" |
| 376 |
self.browser.open(url) |
| 377 |
script = self.popen_pipe.pipe.getvalue() |
| 378 |
# doesn't actually test the browser lookup works, |
| 379 |
# just that the branch is taken |
| 380 |
self.assertIn("URLForApplicationToOpenURL", script) |
| 381 |
self.assertIn(f'open location "{url}"', script) |
| 382 |
|
| 383 |
def test_explicit_browser(self): |
| 384 |
browser = webbrowser.MacOSXOSAScript("safari") |
| 385 |
browser.open("https://python.org") |
| 386 |
script = self.popen_pipe.pipe.getvalue() |
| 387 |
self.assertIn('tell application "safari"', script) |
| 388 |
self.assertIn('open location "https://python.org"', script) |
| 389 |
|
| 390 |
def test_reject_dash_prefixes(self): |
| 391 |
with self.assertRaisesRegex( |
| 392 |
ValueError, |
| 393 |
r"^Invalid URL \(leading dash disallowed\): '--key=val http.*'$" |
| 394 |
): |
| 395 |
self.browser.open(f"--key=val {URL}") |
| 396 |
|
| 397 |
|
| 398 |
class BrowserRegistrationTest(unittest.TestCase): |
| 399 |
|
| 400 |
def setUp(self): |
| 401 |
# Ensure we don't alter the real registered browser details |
| 402 |
self._saved_tryorder = webbrowser._tryorder |
| 403 |
webbrowser._tryorder = [] |
| 404 |
self._saved_browsers = webbrowser._browsers |
| 405 |
webbrowser._browsers = {} |
| 406 |
|
| 407 |
def tearDown(self): |
| 408 |
webbrowser._tryorder = self._saved_tryorder |
| 409 |
webbrowser._browsers = self._saved_browsers |
| 410 |
|
| 411 |
def _check_registration(self, preferred): |
| 412 |
class ExampleBrowser: |
| 413 |
pass |
| 414 |
|
| 415 |
expected_tryorder = [] |
| 416 |
expected_browsers = {} |
| 417 |
|
| 418 |
self.assertEqual(webbrowser._tryorder, expected_tryorder) |
| 419 |
self.assertEqual(webbrowser._browsers, expected_browsers) |
| 420 |
|
| 421 |
webbrowser.register('Example1', ExampleBrowser) |
| 422 |
expected_tryorder = ['Example1'] |
| 423 |
expected_browsers['example1'] = [ExampleBrowser, None] |
| 424 |
self.assertEqual(webbrowser._tryorder, expected_tryorder) |
| 425 |
self.assertEqual(webbrowser._browsers, expected_browsers) |
| 426 |
|
| 427 |
instance = ExampleBrowser() |
| 428 |
if preferred is not None: |
| 429 |
webbrowser.register('example2', ExampleBrowser, instance, |
| 430 |
preferred=preferred) |
| 431 |
else: |
| 432 |
webbrowser.register('example2', ExampleBrowser, instance) |
| 433 |
if preferred: |
| 434 |
expected_tryorder = ['example2', 'Example1'] |
| 435 |
else: |
| 436 |
expected_tryorder = ['Example1', 'example2'] |
| 437 |
expected_browsers['example2'] = [ExampleBrowser, instance] |
| 438 |
self.assertEqual(webbrowser._tryorder, expected_tryorder) |
| 439 |
self.assertEqual(webbrowser._browsers, expected_browsers) |
| 440 |
|
| 441 |
def test_register(self): |
| 442 |
self._check_registration(preferred=False) |
| 443 |
|
| 444 |
def test_register_default(self): |
| 445 |
self._check_registration(preferred=None) |
| 446 |
|
| 447 |
def test_register_preferred(self): |
| 448 |
self._check_registration(preferred=True) |
| 449 |
|
| 450 |
@unittest.skipUnless(sys.platform == "darwin", "macOS specific test") |
| 451 |
def test_no_xdg_settings_on_macOS(self): |
| 452 |
# On macOS webbrowser should not use xdg-settings to |
| 453 |
# look for X11 based browsers (for those users with |
| 454 |
# XQuartz installed) |
| 455 |
with mock.patch("subprocess.check_output") as ck_o: |
| 456 |
webbrowser.register_standard_browsers() |
| 457 |
|
| 458 |
ck_o.assert_not_called() |
| 459 |
|
| 460 |
|
| 461 |
class ImportTest(unittest.TestCase): |
| 462 |
def test_register(self): |
| 463 |
webbrowser = import_helper.import_fresh_module('webbrowser') |
| 464 |
self.assertIsNone(webbrowser._tryorder) |
| 465 |
self.assertFalse(webbrowser._browsers) |
| 466 |
|
| 467 |
class ExampleBrowser: |
| 468 |
pass |
| 469 |
webbrowser.register('Example1', ExampleBrowser) |
| 470 |
self.assertTrue(webbrowser._tryorder) |
| 471 |
self.assertEqual(webbrowser._tryorder[-1], 'Example1') |
| 472 |
self.assertTrue(webbrowser._browsers) |
| 473 |
self.assertIn('example1', webbrowser._browsers) |
| 474 |
self.assertEqual(webbrowser._browsers['example1'], [ExampleBrowser, None]) |
| 475 |
|
| 476 |
def test_get(self): |
| 477 |
webbrowser = import_helper.import_fresh_module('webbrowser') |
| 478 |
self.assertIsNone(webbrowser._tryorder) |
| 479 |
self.assertFalse(webbrowser._browsers) |
| 480 |
|
| 481 |
with self.assertRaises(webbrowser.Error): |
| 482 |
webbrowser.get('fakebrowser') |
| 483 |
self.assertIsNotNone(webbrowser._tryorder) |
| 484 |
|
| 485 |
@unittest.skipIf(" " in sys.executable, "test assumes no space in path (GH-114452)") |
| 486 |
def test_synthesize(self): |
| 487 |
webbrowser = import_helper.import_fresh_module('webbrowser') |
| 488 |
name = os.path.basename(sys.executable).lower() |
| 489 |
webbrowser.register(name, None, webbrowser.GenericBrowser(name)) |
| 490 |
webbrowser.get(sys.executable) |
| 491 |
|
| 492 |
@unittest.skipIf( |
| 493 |
is_apple_mobile, |
| 494 |
"Apple mobile doesn't allow modifying browser with environment" |
| 495 |
) |
| 496 |
def test_environment(self): |
| 497 |
webbrowser = import_helper.import_fresh_module('webbrowser') |
| 498 |
try: |
| 499 |
browser = webbrowser.get().name |
| 500 |
except webbrowser.Error as err: |
| 501 |
self.skipTest(str(err)) |
| 502 |
with os_helper.EnvironmentVarGuard() as env: |
| 503 |
env["BROWSER"] = browser |
| 504 |
webbrowser = import_helper.import_fresh_module('webbrowser') |
| 505 |
webbrowser.get() |
| 506 |
|
| 507 |
@unittest.skipIf( |
| 508 |
is_apple_mobile, |
| 509 |
"Apple mobile doesn't allow modifying browser with environment" |
| 510 |
) |
| 511 |
def test_environment_preferred(self): |
| 512 |
webbrowser = import_helper.import_fresh_module('webbrowser') |
| 513 |
try: |
| 514 |
webbrowser.get() |
| 515 |
least_preferred_browser = webbrowser.get(webbrowser._tryorder[-1]).name |
| 516 |
except (webbrowser.Error, IndexError) as err: |
| 517 |
self.skipTest(str(err)) |
| 518 |
|
| 519 |
with os_helper.EnvironmentVarGuard() as env: |
| 520 |
env["BROWSER"] = least_preferred_browser |
| 521 |
webbrowser = import_helper.import_fresh_module('webbrowser') |
| 522 |
self.assertEqual(webbrowser.get().name, least_preferred_browser) |
| 523 |
|
| 524 |
with os_helper.EnvironmentVarGuard() as env: |
| 525 |
env["BROWSER"] = sys.executable |
| 526 |
webbrowser = import_helper.import_fresh_module('webbrowser') |
| 527 |
self.assertEqual(webbrowser.get().name, sys.executable) |
| 528 |
|
| 529 |
|
| 530 |
class CliTest(unittest.TestCase): |
| 531 |
def test_parse_args(self): |
| 532 |
for command, url, new_win in [ |
| 533 |
# No optional arguments |
| 534 |
("https://example.com", "https://example.com", 0), |
| 535 |
# Each optional argument |
| 536 |
("https://example.com -n", "https://example.com", 1), |
| 537 |
("-n https://example.com", "https://example.com", 1), |
| 538 |
("https://example.com -t", "https://example.com", 2), |
| 539 |
("-t https://example.com", "https://example.com", 2), |
| 540 |
# Long form |
| 541 |
("https://example.com --new-window", "https://example.com", 1), |
| 542 |
("--new-window https://example.com", "https://example.com", 1), |
| 543 |
("https://example.com --new-tab", "https://example.com", 2), |
| 544 |
("--new-tab https://example.com", "https://example.com", 2), |
| 545 |
]: |
| 546 |
args = webbrowser.parse_args(shlex.split(command)) |
| 547 |
|
| 548 |
self.assertEqual(args.url, url) |
| 549 |
self.assertEqual(args.new_win, new_win) |
| 550 |
|
| 551 |
def test_parse_args_error(self): |
| 552 |
for command in [ |
| 553 |
# Arguments must not both be given |
| 554 |
"https://example.com -n -t", |
| 555 |
"https://example.com --new-window --new-tab", |
| 556 |
"https://example.com -n --new-tab", |
| 557 |
"https://example.com --new-window -t", |
| 558 |
]: |
| 559 |
with support.captured_stderr() as stderr: |
| 560 |
with self.assertRaises(SystemExit): |
| 561 |
webbrowser.parse_args(shlex.split(command)) |
| 562 |
self.assertIn( |
| 563 |
'error: argument -t/--new-tab: not allowed with argument -n/--new-window', |
| 564 |
stderr.getvalue(), |
| 565 |
) |
| 566 |
|
| 567 |
# Ensure ambiguous shortening fails |
| 568 |
with support.captured_stderr() as stderr: |
| 569 |
with self.assertRaises(SystemExit): |
| 570 |
webbrowser.parse_args(shlex.split("https://example.com --new")) |
| 571 |
self.assertIn( |
| 572 |
'error: ambiguous option: --new could match --new-window, --new-tab', |
| 573 |
stderr.getvalue() |
| 574 |
) |
| 575 |
|
| 576 |
def test_main(self): |
| 577 |
for command, expected_url, expected_new_win in [ |
| 578 |
# No optional arguments |
| 579 |
("https://example.com", "https://example.com", 0), |
| 580 |
# Each optional argument |
| 581 |
("https://example.com -n", "https://example.com", 1), |
| 582 |
("-n https://example.com", "https://example.com", 1), |
| 583 |
("https://example.com -t", "https://example.com", 2), |
| 584 |
("-t https://example.com", "https://example.com", 2), |
| 585 |
# Long form |
| 586 |
("https://example.com --new-window", "https://example.com", 1), |
| 587 |
("--new-window https://example.com", "https://example.com", 1), |
| 588 |
("https://example.com --new-tab", "https://example.com", 2), |
| 589 |
("--new-tab https://example.com", "https://example.com", 2), |
| 590 |
]: |
| 591 |
with ( |
| 592 |
mock.patch("webbrowser.open", return_value=None) as mock_open, |
| 593 |
mock.patch("builtins.print", return_value=None), |
| 594 |
): |
| 595 |
webbrowser.main(shlex.split(command)) |
| 596 |
mock_open.assert_called_once_with(expected_url, expected_new_win) |
| 597 |
|
| 598 |
|
| 599 |
if __name__ == '__main__': |
| 600 |
unittest.main() |