Skip to content
This repository has been archived by the owner on Aug 30, 2019. It is now read-only.

Commit

Permalink
Fix unit tests for Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
vstinner committed Jan 21, 2013
1 parent ade1736 commit 6200136
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 25 deletions.
3 changes: 2 additions & 1 deletion sandbox/test/__init__.py
Expand Up @@ -8,7 +8,8 @@ class SkipTest(Exception):
pass

def createSandboxConfig(*features, **kw):
if createSandboxConfig.debug:
disable_debug = kw.pop('disable_debug', False)
if createSandboxConfig.debug and not disable_debug:
features += ("stdout", "stderr")
if (createSandboxConfig.cpython_restricted is not None) \
and ('cpython_restricted' not in kw):
Expand Down
15 changes: 10 additions & 5 deletions sandbox/test/test_execute.py
Expand Up @@ -3,6 +3,7 @@
from sandbox import Sandbox, HAVE_PYPY
from sandbox.test import createSandbox, createSandboxConfig, SkipTest
from sandbox.test.tools import capture_stdout
import os

def test_execute():
config = createSandboxConfig()
Expand Down Expand Up @@ -64,20 +65,22 @@ def test(*lines, **kw):
assert namespace == {'a': 1}, namespace

def test_execfile():
import sys

if version_info >= (3, 0):
raise SkipTest("execfile() only exists in Python 2.x")

def execfile_test(filename):
execfile(filename)

from tempfile import NamedTemporaryFile
from tempfile import mktemp

with NamedTemporaryFile() as script:
filename = mktemp()
with open(filename, "w") as script:
print >>script, "print('Hello World!')"
script.flush()

filename = script.name

try:
config = createSandboxConfig('stdout')
try:
Sandbox(config).call(execfile_test, filename)
Expand All @@ -88,10 +91,12 @@ def execfile_test(filename):

with capture_stdout() as stdout:
execfile_test(filename)
sys.stdout.flush()
stdout.seek(0)
output = stdout.read()
print(repr(output))
assert output.startswith('Hello World')
finally:
os.unlink(filename)

def test_compile():
import sys
Expand Down
7 changes: 4 additions & 3 deletions sandbox/test/test_interpreter.py
Expand Up @@ -30,9 +30,10 @@ def check_interpreter_stdout(code, expected, **kw):
% (process.returncode, stdout))
assert process.returncode == 0
stdout = stdout.splitlines()
assert stdout[0] == bytes_literal(''), stdout[0]
assert stdout[1] == bytes_literal(''), stdout[1]
stdout = stdout[2:]
start = 0
while not stdout[start]:
start += 1
stdout = stdout[start:]
assert stdout == expected, "%s != %s" % (stdout, expected)

def test_interpreter():
Expand Down
8 changes: 6 additions & 2 deletions sandbox/test/test_misc.py
Expand Up @@ -64,8 +64,10 @@ def system_exit_allowed():
assert False

def test_stdout():
import sys

config = createSandboxConfig(disable_debug=True)
with capture_stdout() as stdout:
config = SandboxConfig()
def print_denied():
print "Hello Sandbox 1"
try:
Expand All @@ -77,10 +79,12 @@ def print_denied():

def print_allowed():
print "Hello Sandbox 2"
Sandbox(createSandboxConfig('stdout')).call(print_allowed)
config2 = createSandboxConfig('stdout')
Sandbox(config2).call(print_allowed)

print "Hello Sandbox 3"

sys.stdout.flush()
stdout.seek(0)
output = stdout.read()

Expand Down
31 changes: 17 additions & 14 deletions sandbox/test/test_open.py
Expand Up @@ -4,6 +4,7 @@
SkipTest, TestException, skipIf)
from sandbox.test.tools import read_first_line, READ_FILENAME
from sys import version_info
import os

def _get_file_type(obj):
if version_info >= (3, 0):
Expand Down Expand Up @@ -43,25 +44,27 @@ def test_open_whitelist():
Sandbox(config).call(read_first_line, open)

def test_write_file():
from tempfile import mktemp

def write_file(filename):
with open(filename, "w") as fp:
fp.write("test")

from tempfile import NamedTemporaryFile
with NamedTemporaryFile("wb") as tempfile:
def write_denied():
try:
write_file(tempfile.name)
except ValueError, err:
assert str(err) == "Only read modes are allowed."
except IOError, err:
assert str(err) == "file() constructor not accessible in restricted mode"
else:
assert False, "writing to a file is not blocked"
createSandbox().call(write_denied)
filename = mktemp()
def write_denied(filename):
try:
write_file(filename)
except ValueError, err:
assert str(err) == "Only read modes are allowed."
except IOError, err:
assert str(err) == "file() constructor not accessible in restricted mode"
else:
assert False, "writing to a file is not blocked"
createSandbox().call(write_denied, filename)

with NamedTemporaryFile("wb") as tempfile:
write_file(tempfile.name)
filename = mktemp()
write_file(filename)
os.unlink(filename)

def test_filetype_from_sys_stdout():
def get_file_type_from_stdout():
Expand Down
2 changes: 2 additions & 0 deletions sandbox/test/tools.py
Expand Up @@ -57,9 +57,11 @@ def capture_stdout():
with tempfile.TemporaryFile(mode='w+b') as tmp:
stdout_copy = os.dup(stdout_fd)
try:
sys.stdout.flush()
os.dup2(tmp.fileno(), stdout_fd)
yield tmp
finally:
sys.stdout.flush()
os.dup2(stdout_copy, stdout_fd)
os.close(stdout_copy)

0 comments on commit 6200136

Please sign in to comment.