1
mirror of https://github.com/streamlink/streamlink synced 2024-11-01 01:19:33 +01:00
streamlink/tests/test_console.py
beardypig c9fff48e9a Test coverage increase (#1646)
* tests: logger tests

* test: file stream

* test: missing sessions tests

* test: missing util tests

* tests: call can_handle_url for each plugin to ensure it won't error

* tests: speed up by 2 seconds, mocking sleep

* test for streamname lookups

* refactor test structure

* tests: rebase plugins tests

* tests: no need for Python 3.6 support

* tests: remove 2.6 from the travis build

* tests: rebase on master

* tests: rename plugin.api tests

* tests: add mock http resource

* move new plugin tests

* rebase master

* tests: coverage for ConsoleOutput
2018-06-29 11:31:37 -07:00

119 lines
4.1 KiB
Python

import unittest
try:
from io import StringIO
except ImportError:
from StringIO import StringIO
from tests.mock import Mock, patch
from streamlink_cli.console import ConsoleOutput
class _TestObj(object):
def __json__(self):
return {"test": 1}
class TestConsole(unittest.TestCase):
def test_msg_format(self):
output = StringIO()
console = ConsoleOutput(output, Mock())
console.msg("{0} - {1}", 1, 2)
self.assertEqual("1 - 2\n", output.getvalue())
def test_msg_format_kw(self):
output = StringIO()
console = ConsoleOutput(output, Mock())
console.msg("{test} - {what}", test=1, what=2)
self.assertEqual("1 - 2\n", output.getvalue())
def test_msg_json_not_set(self):
output = StringIO()
console = ConsoleOutput(output, Mock())
self.assertEqual(None, console.msg_json({"test": 1}))
self.assertEqual("", output.getvalue())
def test_msg_json(self):
output = StringIO()
console = ConsoleOutput(output, Mock(), json=True)
console.msg_json({"test": 1})
self.assertEqual('''{\n "test": 1\n}\n''', output.getvalue())
def test_msg_json_object(self):
output = StringIO()
test_obj = _TestObj()
console = ConsoleOutput(output, Mock(), json=True)
console.msg_json(test_obj)
self.assertEqual('''{\n "test": 1\n}\n''', output.getvalue())
@patch('streamlink_cli.console.sys.exit')
def test_msg_json_error(self, mock_exit):
output = StringIO()
console = ConsoleOutput(output, Mock(), json=True)
console.msg_json({"error": "bad"})
self.assertEqual('''{\n "error": "bad"\n}\n''', output.getvalue())
mock_exit.assert_called_with(1)
@patch('streamlink_cli.console.sys.exit')
def test_exit(self, mock_exit):
output = StringIO()
console = ConsoleOutput(output, Mock())
console.exit("error")
self.assertEqual("error: error\n", output.getvalue())
mock_exit.assert_called_with(1)
@patch('streamlink_cli.console.sys.exit')
def test_exit_json(self, mock_exit):
output = StringIO()
console = ConsoleOutput(output, Mock(), json=True)
console.exit("error")
self.assertEqual('''{\n "error": "error"\n}\n''', output.getvalue())
mock_exit.assert_called_with(1)
def test_set_level(self):
session = Mock()
console = ConsoleOutput(Mock(), session)
console.set_level("debug")
session.set_loglevel.assert_called_with("debug")
@patch('streamlink_cli.console.sys.stderr')
@patch('streamlink_cli.console.input')
@patch('streamlink_cli.console.sys.stdin.isatty')
def test_ask(self, isatty, input, stderr):
input.return_value = "hello"
isatty.return_value = True
self.assertEqual("hello", ConsoleOutput.ask("test: "))
stderr.write.assert_called_with("test: ")
@patch('streamlink_cli.console.sys.stderr')
@patch('streamlink_cli.console.input')
@patch('streamlink_cli.console.sys.stdin.isatty')
def test_ask_no_tty(self, isatty, input, stderr):
isatty.return_value = False
self.assertEqual("", ConsoleOutput.ask("test: "))
input.assert_not_called()
stderr.write.assert_not_called()
@patch('streamlink_cli.console.sys.stderr')
@patch('streamlink_cli.console.input')
@patch('streamlink_cli.console.sys.stdin.isatty')
def test_ask_input_exception(self, isatty, input, stderr):
isatty.return_value = True
input.side_effect = ValueError
self.assertEqual("", ConsoleOutput.ask("test: "))
stderr.write.assert_called_with("test: ")
@patch('streamlink_cli.console.getpass')
@patch('streamlink_cli.console.sys.stdin.isatty')
def test_askpass(self, isatty, getpass):
isatty.return_value = True
getpass.return_value = "hello"
self.assertEqual("hello", ConsoleOutput.askpass("test: "))
@patch('streamlink_cli.console.sys.stdin.isatty')
def test_askpass_no_tty(self, isatty):
isatty.return_value = False
self.assertEqual("", ConsoleOutput.askpass("test: "))