commit: 3677f495331523542cc397c2690292ae3c115119
Author: Brian Harring <ferringb <AT> gmail <DOT> com>
AuthorDate: Thu Nov 27 16:13:03 2025 +0000
Commit: Brian Harring <ferringb <AT> gmail <DOT> com>
CommitDate: Thu Nov 27 16:14:04 2025 +0000
URL:
https://gitweb.gentoo.org/proj/pkgcore/snakeoil.git/commit/?id=3677f495
chore: context decorators don't need to run what they decorate.
Just verify it's passing things down. Leave the testing of the
implementation to the tests that actually assert the implementation.
Also, this is slow and duplicative, hence me fixing it.
Signed-off-by: Brian Harring <ferringb <AT> gmail.com>
tests/test_decorators.py | 61 ++++++++++++++++++++++++------------------------
1 file changed, 31 insertions(+), 30 deletions(-)
diff --git a/tests/test_decorators.py b/tests/test_decorators.py
index 92c0fb8..8275462 100644
--- a/tests/test_decorators.py
+++ b/tests/test_decorators.py
@@ -1,9 +1,11 @@
-import errno
+import contextlib
import os
import socket
import sys
+from unittest import mock
import pytest
+
from snakeoil.decorators import coroutine, namespace, splitexec
@@ -21,37 +23,36 @@ class TestSplitExecDecorator:
not sys.platform.startswith("linux"), reason="supported on Linux only"
)
class TestNamespaceDecorator:
- @pytest.mark.skipif(
- not os.path.exists("/proc/self/ns/user"),
- reason="user namespace support required",
- )
+ @contextlib.contextmanager
+ def capture_call(self):
+ @contextlib.contextmanager
+ def fake_namespace():
+ yield
+
+ with mock.patch("snakeoil.contexts.Namespace") as m:
+ m.side_effect = lambda *a, **kw: fake_namespace()
+ yield object(), m
+
def test_user_namespace(self):
- @namespace(user=True)
- def do_test():
- assert os.getuid() == 0
-
- try:
- do_test()
- except PermissionError:
- pytest.skip("No permission to use user namespace")
-
- @pytest.mark.skipif(
- not (
- os.path.exists("/proc/self/ns/user") and
os.path.exists("/proc/self/ns/uts")
- ),
- reason="user and uts namespace support required",
- )
+ with self.capture_call() as (unique, m):
+
+ @namespace(user=True)
+ def do_test():
+ return unique
+
+ # do_test()
+ assert unique is do_test()
+ m.assert_called_once_with(user=True)
+
def test_uts_namespace(self):
- @namespace(user=True, uts=True, hostname="host")
- def do_test():
- ns_hostname, _, ns_domainname = socket.getfqdn().partition(".")
- assert ns_hostname == "host"
- assert ns_domainname == ""
-
- try:
- do_test()
- except PermissionError:
- pytest.skip("No permission to use user and uts namespace")
+ with self.capture_call() as (unique, m):
+
+ @namespace(user=True, uts=True, hostname="host")
+ def do_test():
+ return unique
+
+ assert unique is do_test()
+ m.assert_called_once_with(user=True, uts=True, hostname="host")
class TestCoroutineDecorator: