1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
from __future__ import unicode_literals
import uuid
import functools
from .assertions import assert_equal
__all__ = ["OpenTestSet"]
def with_shell(test_func):
@functools.wraps(test_func)
def run_test(self, *args, **kwargs):
with self.create_shell() as shell:
test_func(shell)
return run_test
class OpenTestSet(object):
@with_shell
def test_can_write_to_files_opened_by_open(shell):
path = "/tmp/{0}".format(uuid.uuid4())
f = shell.open(path, "w")
try:
f.write("hello")
f.flush()
assert_equal(b"hello", shell.run(["cat", path]).output)
finally:
f.close()
shell.run(["rm", path])
@with_shell
def test_can_read_files_opened_by_open(shell):
path = "/tmp/{0}".format(uuid.uuid4())
shell.run(["sh", "-c", "echo hello > '{0}'".format(path)])
f = shell.open(path)
try:
assert_equal("hello\n", f.read())
finally:
f.close()
shell.run(["rm", path])
@with_shell
def test_open_can_be_used_as_context_manager(shell):
path = "/tmp/{0}".format(uuid.uuid4())
shell.run(["sh", "-c", "echo hello > '{0}'".format(path)])
with shell.open(path) as f:
assert_equal("hello\n", f.read())
@with_shell
def test_files_can_be_opened_in_binary_mode(shell):
path = "/tmp/{0}".format(uuid.uuid4())
shell.run(["sh", "-c", "echo hello > '{0}'".format(path)])
with shell.open(path, "rb") as f:
assert_equal(b"hello\n", f.read())
|