Skip to content

Commit

Permalink
tests: mock user euid to be able to run tests as root
Browse files Browse the repository at this point in the history
When running test as root, there is an additional log done to warn the
user that it is running streamlink as root. This causes some logging
tests to fail.

For these logging test, ensure we run with a mocked user euid when not
testing for the root euid.
  • Loading branch information
amurzeau authored and bastimeyer committed May 9, 2022
1 parent d9bd451 commit 081595c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/streamlink_cli/main.py
Expand Up @@ -893,7 +893,7 @@ def setup_plugin_options(session: Streamlink, plugin: Type[Plugin]):


def log_root_warning():
if hasattr(os, "getuid"):
if hasattr(os, "geteuid"): # pragma: no branch
if os.geteuid() == 0:
log.info("streamlink is running as root! Be careful!")

Expand Down
9 changes: 5 additions & 4 deletions tests/test_cli_main.py
Expand Up @@ -572,7 +572,8 @@ def subject(cls, argv, **kwargs):
class StopTest(Exception):
pass

with patch("streamlink_cli.main.streamlink", session), \
with patch("streamlink_cli.main.os.geteuid", create=True, new=Mock(return_value=kwargs.get("euid", 1000))), \
patch("streamlink_cli.main.streamlink", session), \
patch("streamlink_cli.main.setup_signals", side_effect=StopTest), \
patch("streamlink_cli.main.CONFIG_FILES", []), \
patch("streamlink_cli.main.setup_streamlink"), \
Expand Down Expand Up @@ -694,9 +695,8 @@ def test_pipe_json(self, mock_stdout: Mock, mock_stderr: Mock):
class TestCLIMainLoggingInfos(_TestCLIMainLogging):
@unittest.skipIf(is_win32, "test only applicable on a POSIX OS")
@patch("streamlink_cli.main.log")
@patch("streamlink_cli.main.os.geteuid", Mock(return_value=0))
def test_log_root_warning(self, mock_log):
self.subject(["streamlink"])
self.subject(["streamlink"], euid=0)
self.assertEqual(mock_log.info.mock_calls, [call("streamlink is running as root! Be careful!")])

@patch("streamlink_cli.main.log")
Expand Down Expand Up @@ -902,7 +902,8 @@ def subject(self):
patch.object(Streamlink, "resolve_url_no_redirect") as mock_resolve_url_no_redirect:
session = Streamlink()
session.load_plugins(os.path.join(os.path.dirname(__file__), "plugin"))
with patch("streamlink_cli.main.streamlink", session), \
with patch("streamlink_cli.main.os.geteuid", create=True, new=Mock(return_value=1000)), \
patch("streamlink_cli.main.streamlink", session), \
patch("streamlink_cli.main.CONFIG_FILES", []), \
patch("streamlink_cli.main.setup_streamlink"), \
patch("streamlink_cli.main.setup_plugins"), \
Expand Down

0 comments on commit 081595c

Please sign in to comment.