From fba8ee7abecb12726293438e8af19d7c3fa54bb8 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sun, 4 Jan 2015 12:22:53 -0500 Subject: Remove try/except/fail - Exceptions are failures by default. --- setuptools/tests/test_sandbox.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'setuptools/tests/test_sandbox.py') diff --git a/setuptools/tests/test_sandbox.py b/setuptools/tests/test_sandbox.py index 6e5ce04a..cadc4812 100644 --- a/setuptools/tests/test_sandbox.py +++ b/setuptools/tests/test_sandbox.py @@ -33,10 +33,8 @@ class TestSandbox: target = os.path.join(gen_py, 'test_write') sandbox = DirectorySandbox(str(tmpdir)) try: - try: - sandbox.run(self._file_writer(target)) - except SandboxViolation: - self.fail("Could not create gen_py file due to SandboxViolation") + # attempt to create gen_py file + sandbox.run(self._file_writer(target)) finally: if os.path.exists(target): os.remove(target) -- cgit v1.2.1 From 19c95d3ef6dba78bf6431b6b13d000bc19e84cd6 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Wed, 14 Jan 2015 21:24:17 -0500 Subject: Remove unused import --- setuptools/tests/test_sandbox.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'setuptools/tests/test_sandbox.py') diff --git a/setuptools/tests/test_sandbox.py b/setuptools/tests/test_sandbox.py index cadc4812..9a2a6b7b 100644 --- a/setuptools/tests/test_sandbox.py +++ b/setuptools/tests/test_sandbox.py @@ -7,7 +7,7 @@ import pytest import pkg_resources import setuptools.sandbox -from setuptools.sandbox import DirectorySandbox, SandboxViolation +from setuptools.sandbox import DirectorySandbox class TestSandbox: -- cgit v1.2.1 From c10a38e97daf7aad2167b3b444be8542dccc652e Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Wed, 14 Jan 2015 21:32:39 -0500 Subject: Add tests for new ExceptionSaver --- setuptools/tests/test_sandbox.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'setuptools/tests/test_sandbox.py') diff --git a/setuptools/tests/test_sandbox.py b/setuptools/tests/test_sandbox.py index 9a2a6b7b..33fb3329 100644 --- a/setuptools/tests/test_sandbox.py +++ b/setuptools/tests/test_sandbox.py @@ -54,3 +54,31 @@ class TestSandbox: with setup_py.open('wb') as stream: stream.write(b'"degenerate script"\r\n') setuptools.sandbox._execfile(str(setup_py), globals()) + + +class TestExceptionSaver: + def test_exception_trapped(self): + with setuptools.sandbox.ExceptionSaver(): + raise ValueError("details") + + def test_exception_resumed(self): + with setuptools.sandbox.ExceptionSaver() as saved_exc: + raise ValueError("details") + + with pytest.raises(ValueError) as caught: + saved_exc.resume() + + assert isinstance(caught.value, ValueError) + assert str(caught.value) == 'details' + + def test_exception_reconstructed(self): + orig_exc = ValueError("details") + + with setuptools.sandbox.ExceptionSaver() as saved_exc: + raise orig_exc + + with pytest.raises(ValueError) as caught: + saved_exc.resume() + + assert isinstance(caught.value, ValueError) + assert caught.value is not orig_exc -- cgit v1.2.1 From a33a5ba6ed5be3affc10fd0e789799b7490d2053 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Wed, 14 Jan 2015 21:38:25 -0500 Subject: Adding test for non-exceptional condition. --- setuptools/tests/test_sandbox.py | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'setuptools/tests/test_sandbox.py') diff --git a/setuptools/tests/test_sandbox.py b/setuptools/tests/test_sandbox.py index 33fb3329..1340cecf 100644 --- a/setuptools/tests/test_sandbox.py +++ b/setuptools/tests/test_sandbox.py @@ -82,3 +82,9 @@ class TestExceptionSaver: assert isinstance(caught.value, ValueError) assert caught.value is not orig_exc + + def test_no_exception_passes_quietly(self): + with setuptools.sandbox.ExceptionSaver() as saved_exc: + pass + + saved_exc.resume() -- cgit v1.2.1 From 9c3d6750cbef8e0673e4123aa39149e28a8a098e Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Wed, 14 Jan 2015 21:49:12 -0500 Subject: Add test capturing failure when the Exception is not pickleable. Ref #329. --- setuptools/tests/test_sandbox.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'setuptools/tests/test_sandbox.py') diff --git a/setuptools/tests/test_sandbox.py b/setuptools/tests/test_sandbox.py index 1340cecf..6e1e9e1c 100644 --- a/setuptools/tests/test_sandbox.py +++ b/setuptools/tests/test_sandbox.py @@ -88,3 +88,15 @@ class TestExceptionSaver: pass saved_exc.resume() + + def test_unpickleable_exception(self): + class CantPickleThis(Exception): + "This Exception is unpickleable because it's not in globals" + + with setuptools.sandbox.ExceptionSaver() as saved_exc: + raise CantPickleThis('detail') + + with pytest.raises(setuptools.sandbox.UnpickleableException) as caught: + saved_exc.resume() + + assert str(caught.value) == "CantPickleThis('detail',)" -- cgit v1.2.1 From a627cf9a72f8fca78b0520b72a5b44a0bbea9b8c Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sun, 13 Dec 2015 10:05:24 -0500 Subject: Add test capturing infinite recursion. Ref #440. --- setuptools/tests/test_sandbox.py | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'setuptools/tests/test_sandbox.py') diff --git a/setuptools/tests/test_sandbox.py b/setuptools/tests/test_sandbox.py index 6e1e9e1c..624954df 100644 --- a/setuptools/tests/test_sandbox.py +++ b/setuptools/tests/test_sandbox.py @@ -100,3 +100,13 @@ class TestExceptionSaver: saved_exc.resume() assert str(caught.value) == "CantPickleThis('detail',)" + + def test_unpickleable_exception_when_hiding_setuptools(self): + """ + As revealed in #440, an infinite recursion can occur if an unpickleable + exception while setuptools is hidden. Ensure this doesn't happen. + """ + sandbox = setuptools.sandbox + with sandbox.save_modules(): + sandbox.hide_setuptools() + raise sandbox.SandboxViolation('test') -- cgit v1.2.1 From bf8331685d1d5bb88cc6cf09236601f0dbe62330 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sun, 13 Dec 2015 10:45:04 -0500 Subject: Refine test to not make additional references to exceptions in setuptools.sandbox, but instead create a bespoke unpickleable exception. Ref #440. --- setuptools/tests/test_sandbox.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'setuptools/tests/test_sandbox.py') diff --git a/setuptools/tests/test_sandbox.py b/setuptools/tests/test_sandbox.py index 624954df..a54c2fa0 100644 --- a/setuptools/tests/test_sandbox.py +++ b/setuptools/tests/test_sandbox.py @@ -106,7 +106,15 @@ class TestExceptionSaver: As revealed in #440, an infinite recursion can occur if an unpickleable exception while setuptools is hidden. Ensure this doesn't happen. """ - sandbox = setuptools.sandbox - with sandbox.save_modules(): - sandbox.hide_setuptools() - raise sandbox.SandboxViolation('test') + class ExceptionUnderTest(Exception): + """ + An unpickleable exception (not in globals). + """ + + with pytest.raises(setuptools.sandbox.UnpickleableException) as exc: + with setuptools.sandbox.save_modules(): + setuptools.sandbox.hide_setuptools() + raise ExceptionUnderTest() + + msg, = exc.value.args + assert msg == 'ExceptionUnderTest()' -- cgit v1.2.1 From ce481836f31142b939f4b93a6eba90630ba354fd Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sun, 13 Dec 2015 11:00:02 -0500 Subject: caught is a better name here --- setuptools/tests/test_sandbox.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'setuptools/tests/test_sandbox.py') diff --git a/setuptools/tests/test_sandbox.py b/setuptools/tests/test_sandbox.py index a54c2fa0..40bd2ebe 100644 --- a/setuptools/tests/test_sandbox.py +++ b/setuptools/tests/test_sandbox.py @@ -111,10 +111,10 @@ class TestExceptionSaver: An unpickleable exception (not in globals). """ - with pytest.raises(setuptools.sandbox.UnpickleableException) as exc: + with pytest.raises(setuptools.sandbox.UnpickleableException) as caught: with setuptools.sandbox.save_modules(): setuptools.sandbox.hide_setuptools() raise ExceptionUnderTest() - msg, = exc.value.args + msg, = caught.value.args assert msg == 'ExceptionUnderTest()' -- cgit v1.2.1 From 3915be7722713c51aab291ed9720c7b83585d2b5 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sun, 13 Dec 2015 11:10:33 -0500 Subject: Add another test capturing violated expectation that SandboxViolation would be raised in a sandbox/hidden_setuptools context. Ref #440. --- setuptools/tests/test_sandbox.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'setuptools/tests/test_sandbox.py') diff --git a/setuptools/tests/test_sandbox.py b/setuptools/tests/test_sandbox.py index 40bd2ebe..fefd46f7 100644 --- a/setuptools/tests/test_sandbox.py +++ b/setuptools/tests/test_sandbox.py @@ -118,3 +118,24 @@ class TestExceptionSaver: msg, = caught.value.args assert msg == 'ExceptionUnderTest()' + + def test_sandbox_violation_raised_hiding_setuptools(self, tmpdir): + """ + When in a sandbox with setuptools hidden, a SandboxViolation + should reflect a proper exception and not be wrapped in + an UnpickleableException. + """ + def write_file(): + "Trigger a SandboxViolation by writing outside the sandbox" + with open('/etc/foo', 'w'): + pass + sandbox = DirectorySandbox(str(tmpdir)) + with pytest.raises(setuptools.sandbox.SandboxViolation) as caught: + with setuptools.sandbox.save_modules(): + setuptools.sandbox.hide_setuptools() + sandbox.run(write_file) + + cmd, args, kwargs = caught.value.args + assert cmd == 'open' + assert args == ('/etc/foo', 'w') + assert kwargs == {} -- cgit v1.2.1