Merge pull request #428 from michael-lazar/rtv_browser

Rtv browser
This commit is contained in:
Michael Lazar
2017-09-06 01:45:31 -04:00
committed by GitHub
7 changed files with 170 additions and 51 deletions

View File

@@ -5,19 +5,48 @@ import time
import curses
from collections import OrderedDict
import os
import six
import pytest
import requests
from six.moves import reload_module
from rtv import exceptions
from rtv.objects import Color, Controller, Navigator, Command, KeyMap, \
curses_session
curses_session, patch_webbrowser
try:
from unittest import mock
except ImportError:
import mock
# webbrowser's command to check if a file exists is different for py2/py3
if six.PY3:
mock_isfile = mock.patch('shutil.which', return_value=None)
else:
mock_isfile = mock.patch('os.path.isfile', return_value=None)
@mock.patch.dict(os.environ, {'BROWSER': 'safari'})
@mock.patch('sys.platform', 'darwin')
@mock_isfile
def test_patch_webbrowser(*_):
# Make sure that webbrowser re-generates the browser list using the
# mocked environment
import webbrowser
webbrowser = reload_module(webbrowser)
# By default, we expect that BROWSER will be loaded as a generic browser
# This is because "safari" is not a valid script in the system PATH
assert isinstance(webbrowser.get(), webbrowser.GenericBrowser)
# After patching, the default webbrowser should now be interpreted as an
# OSAScript browser
patch_webbrowser()
assert isinstance(webbrowser.get(), webbrowser.MacOSXOSAScript)
assert webbrowser._tryorder[0] == 'safari'
@pytest.mark.parametrize('use_ascii', [True, False])
def test_objects_load_screen(terminal, stdscr, use_ascii):

View File

@@ -10,7 +10,7 @@ import six
import pytest
from rtv.docs import HELP, COMMENT_EDIT_FILE
from rtv.exceptions import TemporaryFileError
from rtv.exceptions import TemporaryFileError, BrowserError
try:
from unittest import mock
@@ -464,22 +464,38 @@ def test_open_link_subprocess(terminal):
assert get_error()
def test_open_browser(terminal):
url = 'http://www.test.com'
def test_open_browser_display(terminal):
terminal._display = True
with mock.patch('subprocess.Popen', autospec=True) as Popen:
Popen.return_value.poll.return_value = 0
terminal.open_browser(url)
assert Popen.called
with mock.patch('webbrowser.open_new_tab', autospec=True) as open_new_tab:
terminal.open_browser('http://www.test.com')
# open_new_tab() will be executed in the child process so we can't
# directly check if the was called from here or not.
# open_new_tab.assert_called_with('http://www.test.com')
# Shouldn't suspend curses
assert not curses.endwin.called
assert not curses.doupdate.called
def test_open_browser_display_no_response(terminal):
terminal._display = True
with mock.patch('rtv.terminal.Process', autospec=True) as Process:
Process.return_value.is_alive.return_value = 1
terminal.open_browser('http://www.test.com')
assert isinstance(terminal.loader.exception, BrowserError)
def test_open_browser_no_display(terminal):
terminal._display = False
with mock.patch('webbrowser.open_new_tab', autospec=True) as open_new_tab:
terminal.open_browser(url)
open_new_tab.assert_called_with(url)
terminal.open_browser('http://www.test.com')
open_new_tab.assert_called_with('http://www.test.com')
# Should suspend curses to give control of the terminal to the browser
assert curses.endwin.called
assert curses.doupdate.called