xonsh/tests/xoreutils/test_cat.py

120 lines
3.5 KiB
Python
Raw Normal View History

import io
2016-09-26 22:33:51 +03:00
import os
Add uname command, Update uptime comand (#3909) * Add uname support * Changelog addition * Migration to platform module * Update uptime.py for the last version Merge the original uptime module to a single file Change the bootime() return be stay compatible with xonsh Add support for Haiku , suppose to close #3882 Add dependency from uptime to support to MacOS 10.10 by add _posix.c file. * Update uptime.py for the last version Merge the original uptime module to a single file Change the bootime() return be stay compatible with xonsh Add support for Haiku , suppose to close #3882 Add dependency from uptime to support to MacOS 10.10 by add _posix.c file. * typo fix * black reformat * remove usage of print * black is black * add original test for uptime convert original test via 2to3 black reformat tests * strange syntaxe fixe for flaske8 * black the incredible tool it stop a bug fixe just because it THE tool it want a return for make less readable code * flake8 the famous tool it permit to stop a big fixe without any information's about the trouble * workaround about xonsh CI don't respect docstring specs * RISC OS only comment thing * black is a good jock in a CI * black is a good jock in a CI * roll back uptime.py * look if we can make it work * fixe all i understand * add command in corutils alias * reformat uptime.py with black * fixe version * try with xonsh xp.LIBC lib * black in a CI is a stupid thing * stupid Windows and it \r * use os.linestep * use newline simple wrapper * use newline simple wrapper * use newline simple wrapper * use newline simple wrapper * try osx rollback method * fixe * fixe * a test on window via the CI because i haven't the OS * a test on window via the CI because i haven't the OS * a test on window via the CI because i haven't the OS * fix: black: format * refactor: update uname command now has auto-completions * docs: update news item and fix qa error * refactor: remove unused file * fix: qa imports * refactor: update getting boottime fallback to monotonic time on unix * fix: update haiku compatibility in uptime * refactor: add uptime to aliases * refactor: move xoreutils tests * fix: call aliases using xonsh Co-authored-by: Tuux <tuxa@rtnp.org> Co-authored-by: Noortheen Raja <jnoortheen@gmail.com>
2022-01-08 13:58:46 +01:00
2019-03-31 17:33:35 +09:00
import pytest
2016-09-26 22:33:51 +03:00
2019-04-24 00:50:40 +09:00
from xonsh.platform import DEFAULT_ENCODING
Add uname command, Update uptime comand (#3909) * Add uname support * Changelog addition * Migration to platform module * Update uptime.py for the last version Merge the original uptime module to a single file Change the bootime() return be stay compatible with xonsh Add support for Haiku , suppose to close #3882 Add dependency from uptime to support to MacOS 10.10 by add _posix.c file. * Update uptime.py for the last version Merge the original uptime module to a single file Change the bootime() return be stay compatible with xonsh Add support for Haiku , suppose to close #3882 Add dependency from uptime to support to MacOS 10.10 by add _posix.c file. * typo fix * black reformat * remove usage of print * black is black * add original test for uptime convert original test via 2to3 black reformat tests * strange syntaxe fixe for flaske8 * black the incredible tool it stop a bug fixe just because it THE tool it want a return for make less readable code * flake8 the famous tool it permit to stop a big fixe without any information's about the trouble * workaround about xonsh CI don't respect docstring specs * RISC OS only comment thing * black is a good jock in a CI * black is a good jock in a CI * roll back uptime.py * look if we can make it work * fixe all i understand * add command in corutils alias * reformat uptime.py with black * fixe version * try with xonsh xp.LIBC lib * black in a CI is a stupid thing * stupid Windows and it \r * use os.linestep * use newline simple wrapper * use newline simple wrapper * use newline simple wrapper * use newline simple wrapper * try osx rollback method * fixe * fixe * a test on window via the CI because i haven't the OS * a test on window via the CI because i haven't the OS * a test on window via the CI because i haven't the OS * fix: black: format * refactor: update uname command now has auto-completions * docs: update news item and fix qa error * refactor: remove unused file * fix: qa imports * refactor: update getting boottime fallback to monotonic time on unix * fix: update haiku compatibility in uptime * refactor: add uptime to aliases * refactor: move xoreutils tests * fix: call aliases using xonsh Co-authored-by: Tuux <tuxa@rtnp.org> Co-authored-by: Noortheen Raja <jnoortheen@gmail.com>
2022-01-08 13:58:46 +01:00
from xonsh.xoreutils import cat
2019-03-31 17:33:35 +09:00
2019-04-24 00:50:40 +09:00
@pytest.fixture
def cat_env_fixture(xession):
with xession.env.swap(
2020-08-26 10:10:59 -05:00
XONSH_ENCODING=DEFAULT_ENCODING, XONSH_ENCODING_ERRORS="surrogateescape"
):
yield xession
2019-04-24 00:50:40 +09:00
class CatLimitedBuffer(io.BytesIO):
"""
This object cause KeyboardInterrupt when reached expected buffer size
"""
def __init__(self, limit=500, *args, **kwargs):
super().__init__(*args, **kwargs)
self.limited_size = limit
self.already_raised = False
def write(self, *args, **kwargs):
super().write(*args, **kwargs)
if not self.already_raised and self.tell() >= self.limited_size:
self.already_raised = True
raise KeyboardInterrupt()
class TestCatLimitedBuffer:
def test_write_buffer_correctly(self):
buf = CatLimitedBuffer(limit=500)
2020-08-26 10:10:59 -05:00
buf.write(b"0" * 499)
assert buf.getvalue() == b"0" * 499
def test_raise_keyboardinterrupt_when_reached(self):
buf = CatLimitedBuffer(limit=500)
2020-08-26 10:10:59 -05:00
buf.write(b"0" * 499)
with pytest.raises(KeyboardInterrupt):
2020-08-26 10:10:59 -05:00
buf.write(b"1")
def test_raise_allow_write_over_limit(self):
buf = CatLimitedBuffer(limit=500)
2020-08-26 10:10:59 -05:00
buf.write(b"0" * 400)
with pytest.raises(KeyboardInterrupt):
2020-08-26 10:10:59 -05:00
buf.write(b"1" * 200)
2020-08-26 10:10:59 -05:00
assert buf.getvalue() == (b"0" * 400 + b"1" * 200)
def test_not_raise_twice_time(self):
buf = CatLimitedBuffer(limit=500)
with pytest.raises(KeyboardInterrupt):
2020-08-26 10:10:59 -05:00
buf.write(b"1" * 1000)
try:
2020-08-26 10:10:59 -05:00
buf.write(b"2")
except KeyboardInterrupt:
pytest.fail("Unexpected KeyboardInterrupt")
2019-03-31 17:33:35 +09:00
class TestCat:
tempfile = None
def setup_method(self, _method):
import tempfile
2020-08-26 10:10:59 -05:00
2019-03-31 17:33:35 +09:00
tmpfile = tempfile.mkstemp()
self.tempfile = tmpfile[1]
2019-03-31 19:45:16 +09:00
os.close(tmpfile[0])
2019-03-31 17:33:35 +09:00
def teardown_method(self, _method):
os.remove(self.tempfile)
@pytest.mark.parametrize(
"content",
[
"this is a content\nfor testing xoreutil's cat",
"this is a content withe \\n\nfor testing xoreutil's cat\n",
"",
],
)
def test_cat_single_file_work_exist_content(self, cat_env_fixture, content):
2019-07-06 14:02:43 +09:00
with open(self.tempfile, "w") as f:
f.write(content)
expected_content = content.replace("\n", os.linesep)
stdin = io.StringIO()
stdout_buf = io.BytesIO()
stderr_buf = io.BytesIO()
stdout = io.TextIOWrapper(stdout_buf)
stderr = io.TextIOWrapper(stderr_buf)
opts = cat._cat_parse_args([])
cat._cat_single_file(opts, self.tempfile, stdin, stdout, stderr)
stdout.flush()
stderr.flush()
assert stdout_buf.getvalue() == bytes(expected_content, "utf-8")
2020-08-26 10:10:59 -05:00
assert stderr_buf.getvalue() == b""
2019-07-06 14:02:43 +09:00
2020-08-26 10:10:59 -05:00
@pytest.mark.skipif(
not os.path.exists("/dev/urandom"), reason="/dev/urandom doesn't exists"
)
def test_cat_dev_urandom(self, cat_env_fixture):
"""
test of cat (pseudo) device.
"""
stdin = io.StringIO()
stdout_buf = CatLimitedBuffer(limit=500)
stderr_buf = io.BytesIO()
stdout = io.TextIOWrapper(stdout_buf)
stderr = io.TextIOWrapper(stderr_buf)
opts = cat._cat_parse_args([])
cat._cat_single_file(opts, "/dev/urandom", stdin, stdout, stderr)
stdout.flush()
stderr.flush()
assert len(stdout_buf.getvalue()) >= 500