-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
tests: mock user euid to be able to run tests as root #4519
tests: mock user euid to be able to run tests as root #4519
Conversation
4425d87
to
3b3ab88
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR. I've already seen your patch in your debian source package repo yesterday...
Could you please update your branch to the following diff?
This fixes the syntax errors with the missing new=
keyword in the patch
calls. The signature of the _TestCLIMainLogging.subject
method should also not get changed, and in the streamlink_cli.main module, the hasattr argument should be equal to the method that's actually called. The missing else branch should also get ignored in the code coverage.
diff --git a/src/streamlink_cli/main.py b/src/streamlink_cli/main.py
index bb49b502..85b4da54 100644
--- a/src/streamlink_cli/main.py
+++ b/src/streamlink_cli/main.py
@@ -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!")
diff --git a/tests/test_cli_main.py b/tests/test_cli_main.py
index d72945f8..e77de698 100644
--- a/tests/test_cli_main.py
+++ b/tests/test_cli_main.py
@@ -572,7 +572,8 @@ class _TestCLIMainLogging(unittest.TestCase):
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"), \
@@ -694,9 +695,8 @@ class TestCLIMainLoggingStreams(_TestCLIMainLogging):
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")
@@ -902,7 +902,8 @@ class TestCLIMainPrint(unittest.TestCase):
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"), \
3b3ab88
to
2c485c0
Compare
Thanks ! I was not sure how to handle the Windows part. |
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.
2c485c0
to
41b851a
Compare
Thanks |
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.
I know that running as root is unusual, but it can happen when packaging for distributions.