1+ import io
2+ import os
13import webbrowser
24import unittest
3- import os
45import sys
56import subprocess
67from unittest import mock
@@ -49,6 +50,14 @@ def _test(self, meth, *, args=[URL], kw={}, options, arguments):
4950 popen_args .pop (popen_args .index (option ))
5051 self .assertEqual (popen_args , arguments )
5152
53+ def test_reject_dash_prefixes (self ):
54+ browser = self .browser_class (name = CMD_NAME )
55+ with self .assertRaisesRegex (
56+ ValueError ,
57+ r"^Invalid URL \(leading dash disallowed\): '--key=val http.*'$"
58+ ):
59+ browser .open (f"--key=val { URL } " )
60+
5261
5362class GenericBrowserCommandTest (CommandTestMixin , unittest .TestCase ):
5463
@@ -59,11 +68,6 @@ def test_open(self):
5968 options = [],
6069 arguments = [URL ])
6170
62- def test_reject_dash_prefixes (self ):
63- browser = self .browser_class (name = CMD_NAME )
64- with self .assertRaises (ValueError ):
65- browser .open (f"--key=val { URL } " )
66-
6771
6872class BackgroundBrowserCommandTest (CommandTestMixin , unittest .TestCase ):
6973
@@ -224,6 +228,71 @@ def test_open_new_tab(self):
224228 arguments = ['openURL({},new-tab)' .format (URL )])
225229
226230
231+ class MockPopenPipe :
232+ def __init__ (self , cmd , mode ):
233+ self .cmd = cmd
234+ self .mode = mode
235+ self .pipe = io .StringIO ()
236+ self ._closed = False
237+
238+ def write (self , buf ):
239+ self .pipe .write (buf )
240+
241+ def close (self ):
242+ self ._closed = True
243+ return None
244+
245+
246+ @unittest .skipUnless (sys .platform == "darwin" , "macOS specific test" )
247+ class MacOSXOSAScriptTest (unittest .TestCase ):
248+ def setUp (self ):
249+ # Ensure that 'BROWSER' is not set to 'open' or something else.
250+ # See: https://github.com/python/cpython/issues/131254.
251+ env = self .enterContext (os_helper .EnvironmentVarGuard ())
252+ env .unset ("BROWSER" )
253+
254+ support .patch (self , os , "popen" , self .mock_popen )
255+ self .browser = webbrowser .MacOSXOSAScript ("default" )
256+
257+ def mock_popen (self , cmd , mode ):
258+ self .popen_pipe = MockPopenPipe (cmd , mode )
259+ return self .popen_pipe
260+
261+ def test_default (self ):
262+ browser = webbrowser .get ()
263+ assert isinstance (browser , webbrowser .MacOSXOSAScript )
264+ self .assertEqual (browser .name , "default" )
265+
266+ def test_default_open (self ):
267+ url = "https://python.org"
268+ self .browser .open (url )
269+ self .assertTrue (self .popen_pipe ._closed )
270+ self .assertEqual (self .popen_pipe .cmd , "osascript" )
271+ script = self .popen_pipe .pipe .getvalue ()
272+ self .assertEqual (script .strip (), f'open location "{ url } "' )
273+
274+ def test_url_quote (self ):
275+ self .browser .open ('https://python.org/"quote"' )
276+ script = self .popen_pipe .pipe .getvalue ()
277+ self .assertEqual (
278+ script .strip (), 'open location "https://python.org/%22quote%22"'
279+ )
280+
281+ def test_explicit_browser (self ):
282+ browser = webbrowser .MacOSXOSAScript ("safari" )
283+ browser .open ("https://python.org" )
284+ script = self .popen_pipe .pipe .getvalue ()
285+ self .assertIn ('tell application "safari"' , script )
286+ self .assertIn ('open location "https://python.org"' , script )
287+
288+ def test_reject_dash_prefixes (self ):
289+ with self .assertRaisesRegex (
290+ ValueError ,
291+ r"^Invalid URL \(leading dash disallowed\): '--key=val http.*'$"
292+ ):
293+ self .browser .open (f"--key=val { URL } " )
294+
295+
227296class BrowserRegistrationTest (unittest .TestCase ):
228297
229298 def setUp (self ):
0 commit comments