Commit e4151f31 by Ned Batchelder

Add a .coveragerc, and tweak to improve coverage results.

parent 50ad8f33
[run]
source = codejail
branch = true
omit = codejail/tests/doit.py
...@@ -130,7 +130,7 @@ def jail_code(command, code=None, files=None, argv=None, stdin=None): ...@@ -130,7 +130,7 @@ def jail_code(command, code=None, files=None, argv=None, stdin=None):
if not is_configured(command): if not is_configured(command):
raise Exception("jail_code needs to be configured for %r" % command) raise Exception("jail_code needs to be configured for %r" % command)
with temp_directory(delete_when_done=True) as tmpdir: with temp_directory() as tmpdir:
log.debug("Executing jailed code: %r", code) log.debug("Executing jailed code: %r", code)
...@@ -171,7 +171,7 @@ def jail_code(command, code=None, files=None, argv=None, stdin=None): ...@@ -171,7 +171,7 @@ def jail_code(command, code=None, files=None, argv=None, stdin=None):
return result return result
def set_process_limits(): def set_process_limits(): # pragma: no cover
""" """
Set limits on this processs, to be used first in a child process. Set limits on this processs, to be used first in a child process.
""" """
......
...@@ -119,7 +119,7 @@ def safe_exec(code, globals_dict, files=None, python_path=None): ...@@ -119,7 +119,7 @@ def safe_exec(code, globals_dict, files=None, python_path=None):
jailed_code = "".join(the_code) jailed_code = "".join(the_code)
# Turn this on to see what's being executed. # Turn this on to see what's being executed.
if LOG_ALL_CODE: if LOG_ALL_CODE: # pragma: no cover
log.debug("Jailed code: %s", jailed_code) log.debug("Jailed code: %s", jailed_code)
log.debug("Exec: %s", code) log.debug("Exec: %s", code)
log.debug("Stdin: %s", stdin) log.debug("Stdin: %s", stdin)
...@@ -179,7 +179,7 @@ def not_safe_exec(code, globals_dict, files=None, python_path=None): ...@@ -179,7 +179,7 @@ def not_safe_exec(code, globals_dict, files=None, python_path=None):
g_dict = json_safe(globals_dict) g_dict = json_safe(globals_dict)
with temp_directory(delete_when_done=True) as tmpdir: with temp_directory() as tmpdir:
with change_directory(tmpdir): with change_directory(tmpdir):
# Copy the files here. # Copy the files here.
for filename in files or (): for filename in files or ():
...@@ -203,5 +203,6 @@ def not_safe_exec(code, globals_dict, files=None, python_path=None): ...@@ -203,5 +203,6 @@ def not_safe_exec(code, globals_dict, files=None, python_path=None):
# Running Python code in the sandbox makes it difficult to debug. # Running Python code in the sandbox makes it difficult to debug.
if ALWAYS_BE_UNSAFE or not jail_code.is_configured("python"): NO_SAFE_PYTHON = not jail_code.is_configured("python")
if ALWAYS_BE_UNSAFE or NO_SAFE_PYTHON: # pragma: no cover
safe_exec = not_safe_exec safe_exec = not_safe_exec
...@@ -22,7 +22,7 @@ class SafeExecTests(unittest.TestCase): ...@@ -22,7 +22,7 @@ class SafeExecTests(unittest.TestCase):
give the tests something to test. give the tests something to test.
""" """
raise NotImplementedError raise NotImplementedError # pragma: no cover
def test_set_values(self): def test_set_values(self):
globs = {} globs = {}
...@@ -81,16 +81,22 @@ class SafeExecTests(unittest.TestCase): ...@@ -81,16 +81,22 @@ class SafeExecTests(unittest.TestCase):
class TestSafeExec(SafeExecTests, unittest.TestCase): class TestSafeExec(SafeExecTests, unittest.TestCase):
"""Run SafeExecTests, with the real safe_exec.""" """Run SafeExecTests, with the real safe_exec."""
__test__ = True
def safe_exec(self, *args, **kwargs): def safe_exec(self, *args, **kwargs):
safe_exec(*args, **kwargs) safe_exec(*args, **kwargs)
class TestNotSafeExec(SafeExecTests, unittest.TestCase): class TestNotSafeExec(SafeExecTests, unittest.TestCase):
"""Run SafeExecTests, with not_safe_exec.""" """Run SafeExecTests, with not_safe_exec."""
__test__ = True
def setUp(self): def setUp(self):
# If safe_exec is actually an alias to not_safe_exec, then there's no # If safe_exec is actually an alias to not_safe_exec, then there's no
# point running these tests. # point running these tests.
if safe_exec is not_safe_exec: if safe_exec is not_safe_exec: # pragma: no cover
raise SkipTest raise SkipTest
def safe_exec(self, *args, **kwargs): def safe_exec(self, *args, **kwargs):
......
...@@ -7,25 +7,23 @@ import tempfile ...@@ -7,25 +7,23 @@ import tempfile
class TempDirectory(object): class TempDirectory(object):
def __init__(self, delete_when_done=True): def __init__(self):
self.delete_when_done = delete_when_done
self.temp_dir = tempfile.mkdtemp(prefix="codejail-") self.temp_dir = tempfile.mkdtemp(prefix="codejail-")
# Make directory readable by other users ('sandbox' user needs to be able to read it) # Make directory readable by other users ('sandbox' user needs to be able to read it)
os.chmod(self.temp_dir, 0775) os.chmod(self.temp_dir, 0775)
def clean_up(self): def clean_up(self):
if self.delete_when_done: # if this errors, something is genuinely wrong, so don't ignore errors.
# if this errors, something is genuinely wrong, so don't ignore errors. shutil.rmtree(self.temp_dir)
shutil.rmtree(self.temp_dir)
@contextlib.contextmanager @contextlib.contextmanager
def temp_directory(delete_when_done=True): def temp_directory():
""" """
A context manager to make and use a temp directory. If `delete_when_done` A context manager to make and use a temp directory.
is true (the default), the directory will be removed when done. The directory will be removed when done.
""" """
tmp = TempDirectory(delete_when_done) tmp = TempDirectory()
try: try:
yield tmp.temp_dir yield tmp.temp_dir
finally: finally:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment