From 725a7ff47126af086610eaeb015ad0b15d78ef72 Mon Sep 17 00:00:00 2001 From: Michael Kim Date: Tue, 23 Apr 2019 15:27:43 -0700 Subject: [PATCH 1/4] remove print statement --- stream/util/testing.py | 1 - 1 file changed, 1 deletion(-) diff --git a/stream/util/testing.py b/stream/util/testing.py index 358e6b8..47d71aa 100644 --- a/stream/util/testing.py +++ b/stream/util/testing.py @@ -17,5 +17,4 @@ def assert_time( yield finally: t = _time() - start - print(t) assert -margin <= t - expected_time <= margin From 629d05a933a92c4a3b9650f5e0a4682abe3927c1 Mon Sep 17 00:00:00 2001 From: Michael Kim Date: Tue, 23 Apr 2019 15:42:45 -0700 Subject: [PATCH 2/4] remove pytest-dependency --- setup.cfg | 1 - setup.py | 1 - tests/functions/control/test_base.py | 7 ------ tests/functions/control/test_threading.py | 15 ------------ tests/functions/test_filter.py | 5 ---- tests/functions/test_unzip.py | 9 -------- tests/io/test_base.py | 7 ------ tests/io/test_copy.py | 6 ----- tests/io/test_iterable.py | 13 ----------- tests/io/test_local.py | 7 ------ tests/io/test_std.py | 4 ---- tests/io/test_wrapper.py | 2 -- tests/io/util.py | 10 -------- tests/test_operators.py | 28 ----------------------- 14 files changed, 115 deletions(-) delete mode 100644 tests/io/util.py diff --git a/setup.cfg b/setup.cfg index 2655270..3336781 100644 --- a/setup.cfg +++ b/setup.cfg @@ -2,5 +2,4 @@ test = pytest [tool:pytest] -automark_dependency = true addopts = -ra diff --git a/setup.py b/setup.py index 82ed998..1280318 100644 --- a/setup.py +++ b/setup.py @@ -13,7 +13,6 @@ 'pytest>=4', 'pytest-runner>=4', 'pytest-cov>=2', - 'pytest-dependency @ https://github.com/SelfHacked/pytest-dependency/archive/master.zip', 'cached-property', ] + extra_sql extra_dev = extra_all + extra_test diff --git a/tests/functions/control/test_base.py b/tests/functions/control/test_base.py index 519372a..87f4882 100644 --- a/tests/functions/control/test_base.py +++ b/tests/functions/control/test_base.py @@ -1,7 +1,5 @@ from time import sleep -import pytest - from stream.functions.control import Preload from stream.util.testing import assert_time @@ -11,11 +9,6 @@ def dummy_iterable(): yield 0 -@pytest.mark.dependency( - depends=[ - ('session', 'tests/util/test_testing.py::test_assert_time'), - ], -) def test_preload_all(): with assert_time(0.1): iterator = Preload(n=None)(dummy_iterable()) diff --git a/tests/functions/control/test_threading.py b/tests/functions/control/test_threading.py index 609f4be..f38d225 100644 --- a/tests/functions/control/test_threading.py +++ b/tests/functions/control/test_threading.py @@ -28,29 +28,14 @@ def _test_prefetch(prefetch): next(iterator) -@pytest.mark.dependency( - depends=[ - ('session', 'tests/util/test_testing.py::test_assert_time'), - ], -) def test_prefetch_one(): _test_prefetch(Prefetch()) -@pytest.mark.dependency( - depends=[ - ('session', 'tests/util/test_testing.py::test_assert_time'), - ], -) def test_prefetch_all(): _test_prefetch(Prefetch(n=None)) -@pytest.mark.dependency( - depends=[ - ('session', 'tests/util/test_testing.py::test_assert_time'), - ], -) def test_timeout(): iterator = Prefetch(n=None, timeout=1)(dummy_iterable()) tuple(iterator) diff --git a/tests/functions/test_filter.py b/tests/functions/test_filter.py index 27e8bf1..230104d 100644 --- a/tests/functions/test_filter.py +++ b/tests/functions/test_filter.py @@ -1,5 +1,3 @@ -import pytest - from stream.functions.filter import Filter, remove_empty @@ -8,9 +6,6 @@ def test_filter(): assert tuple(f(range(10))) == tuple(range(0, 10, 2)) -@pytest.mark.dependency( - depends=['test_filter'], -) def test_filter_logical(): f1 = Filter(lambda x: x % 2 == 0) f2 = Filter(lambda x: x % 3 == 0) diff --git a/tests/functions/test_unzip.py b/tests/functions/test_unzip.py index a8ec6d5..626d31a 100644 --- a/tests/functions/test_unzip.py +++ b/tests/functions/test_unzip.py @@ -1,18 +1,9 @@ import gzip -import pytest - from stream.functions.bytes import un_gzip from stream.io.local import LocalFile -@pytest.mark.dependency( - scope='session', - depends=[ - 'tests/io/test_base.py::test_stream', - 'tests/test_operators.py::test_or', - ], -) def test_un_gzip(tmpdir): file = str(tmpdir / '0.txt.gz') with gzip.open(file, mode='wb') as f: diff --git a/tests/io/test_base.py b/tests/io/test_base.py index 45a0070..9417444 100644 --- a/tests/io/test_base.py +++ b/tests/io/test_base.py @@ -1,7 +1,6 @@ import pytest from stream.io import BinaryFile, TextFile -from .util import depends_with class DummyFile(BinaryFile): @@ -78,7 +77,6 @@ def test_with(): pass -@depends_with() def test_read(): with DummyFile() as f: assert f.read(0) == b'' @@ -87,7 +85,6 @@ def test_read(): assert f.read() == b'45\n\n6\n' -@depends_with() def test_readline(): with DummyFile() as f: assert f.readline(0) == b'' @@ -96,7 +93,6 @@ def test_readline(): assert f.readline(100) == b'45\n' -@depends_with() def test_readlines(): with DummyFile() as f: assert f.readlines(0) == [] @@ -105,20 +101,17 @@ def test_readlines(): assert f.readlines() == [b'6\n'] -@depends_with() def test_iter(): with DummyFile() as f: assert next(f) == b'123\n' assert tuple(f) == (b'45\n', b'\n', b'6\n') -@depends_with() def test_stream(): with DummyTextFile() as f: assert tuple(f.stream) == ('123\n', 'abc\n') -@depends_with() def test_stream_not_readable(): with DummyNotReadable() as f: with pytest.raises(DummyNotReadable.NotSupported): diff --git a/tests/io/test_copy.py b/tests/io/test_copy.py index f06d788..6c5b787 100644 --- a/tests/io/test_copy.py +++ b/tests/io/test_copy.py @@ -4,14 +4,12 @@ from stream.io.local import LocalFile from stream.io.std import StdIn, StdOut, StdErr -from .util import depends_with txt = """123 abc """ -@depends_with() def test_copy_local(tmpdir): file = tmpdir / '0.txt' file.write_text(txt, encoding='utf-8') @@ -24,7 +22,6 @@ def test_copy_local(tmpdir): assert file2.read_text(encoding='utf-8') == txt -@depends_with() def test_same_file(tmpdir): file = tmpdir / '0.txt' file.write_text(txt, encoding='utf-8') @@ -35,7 +32,6 @@ def test_same_file(tmpdir): f1.copy_to(f2) -@depends_with() def test_from_stdin(tmpdir, monkeypatch): monkeypatch.setattr('sys.stdin', StringIO(txt)) file = tmpdir / '0.txt' @@ -47,7 +43,6 @@ def test_from_stdin(tmpdir, monkeypatch): assert file.read_text(encoding='utf-8') == txt -@depends_with() def test_to_stdout(tmpdir, capsys): file = tmpdir / '0.txt' file.write_text(txt, encoding='utf-8') @@ -61,7 +56,6 @@ def test_to_stdout(tmpdir, capsys): assert captured.err == '' -@depends_with() def test_to_stderr(tmpdir, capsys): file = tmpdir / '0.txt' file.write_text(txt, encoding='utf-8') diff --git a/tests/io/test_iterable.py b/tests/io/test_iterable.py index 85f491a..fdcbcd2 100644 --- a/tests/io/test_iterable.py +++ b/tests/io/test_iterable.py @@ -3,7 +3,6 @@ import pytest from stream.io.iterable import IterableFile -from .util import depends_with def get() -> IterableFile: @@ -14,7 +13,6 @@ def get() -> IterableFile: """]) -@depends_with() def test_readonly(): with get() as f: assert f.readable() @@ -27,9 +25,6 @@ def test_readonly(): f.flush() -@depends_with( - ('session', 'tests/io/test_base.py::test_read'), -) def test_tell(): with get() as f: assert f.tell() == 0 @@ -41,7 +36,6 @@ def test_tell(): assert f.tell() == 10 -@depends_with() def test_seekable(): with get() as f: assert f.seekable() @@ -49,7 +43,6 @@ def test_seekable(): f.truncate() -@depends_with() def test_seek_forward_only(): with get() as f: with pytest.raises(OSError): @@ -61,10 +54,6 @@ def test_seek_forward_only(): f.seek(-1, 1) -@depends_with( - ('session', 'tests/io/test_base.py::test_read'), - 'test_tell', -) def test_seek(): with get() as f: f.seek(2) @@ -75,7 +64,6 @@ def test_seek(): assert f.read(1) == b'5' -@depends_with() def test_os(): with get() as f: assert f.mode == 'rb' @@ -85,7 +73,6 @@ def test_os(): assert not f.isatty() -@depends_with() def test_eq(): with get() as f1, get() as f2: assert f1 != f2 diff --git a/tests/io/test_local.py b/tests/io/test_local.py index 9691569..14bbb28 100644 --- a/tests/io/test_local.py +++ b/tests/io/test_local.py @@ -1,7 +1,6 @@ import pytest from stream.io.local import LocalFile -from .util import depends_with txt = """123 abc @@ -11,7 +10,6 @@ """ -@depends_with() def test_read_file(tmpdir): file = tmpdir / '0.txt' file.write_text(txt, encoding='utf-8') @@ -20,7 +18,6 @@ def test_read_file(tmpdir): assert f.read() == txt -@depends_with() def test_write_file(tmpdir): file = tmpdir / '0.txt' @@ -30,7 +27,6 @@ def test_write_file(tmpdir): assert file.read_text(encoding='utf-8') == txt -@depends_with() def test_read_buffer(tmpdir): file = tmpdir / '0.txt' file.write_text(txt, encoding='utf-8') @@ -39,7 +35,6 @@ def test_read_buffer(tmpdir): assert f.buffer.read() == bin -@depends_with() def test_write_buffer(tmpdir): file = tmpdir / '0.txt' @@ -49,7 +44,6 @@ def test_write_buffer(tmpdir): assert file.read_text(encoding='utf-8') == txt -@depends_with() def test_no_buffer(tmpdir): file = tmpdir / '0.txt' file.write_text(txt, encoding='utf-8') @@ -63,7 +57,6 @@ def test_no_buffer(tmpdir): f2.buffer -@depends_with() def test_next(tmpdir): file = tmpdir / '0.txt' file.write_text(txt, encoding='utf-8') diff --git a/tests/io/test_std.py b/tests/io/test_std.py index 27a136c..a0ca83f 100644 --- a/tests/io/test_std.py +++ b/tests/io/test_std.py @@ -1,10 +1,8 @@ from io import StringIO from stream.io.std import StdIn, StdOut, StdErr -from .util import depends_with -@depends_with() def test_stdin(monkeypatch): monkeypatch.setattr('sys.stdin', StringIO('abc\n123\n')) @@ -12,7 +10,6 @@ def test_stdin(monkeypatch): assert tuple(f) == ('abc\n', '123\n') -@depends_with() def test_stdin_buffer(tmpdir, monkeypatch): file = tmpdir / '0.txt' file.write_text('abc\n123\n', encoding='utf-8') @@ -23,7 +20,6 @@ def test_stdin_buffer(tmpdir, monkeypatch): assert tuple(f.buffer) == (b'abc\n', b'123\n') -@depends_with() def test_stdout_stderr(capsys): with StdOut() as f1: f1.write('hello') diff --git a/tests/io/test_wrapper.py b/tests/io/test_wrapper.py index 8e63c32..df52fda 100644 --- a/tests/io/test_wrapper.py +++ b/tests/io/test_wrapper.py @@ -1,12 +1,10 @@ from stream.io.local import LocalFile -from .util import depends_with txt = """123 abc """ -@depends_with() def test_buffer_eq(tmpdir): file = tmpdir / '0.txt' file.write_text(txt, encoding='utf-8') diff --git a/tests/io/util.py b/tests/io/util.py deleted file mode 100644 index fc1c4d8..0000000 --- a/tests/io/util.py +++ /dev/null @@ -1,10 +0,0 @@ -import pytest - - -def depends_with(*other_dependencies): - return pytest.mark.dependency( - depends=[ - ('session', 'tests/io/test_base.py::test_with'), - *other_dependencies, - ] - ) diff --git a/tests/test_operators.py b/tests/test_operators.py index 6a03a31..f276a3a 100644 --- a/tests/test_operators.py +++ b/tests/test_operators.py @@ -1,40 +1,21 @@ -import pytest - from stream import Stream, IterStream from stream.functions.each import ApplyEach from stream.functions.filter import remove_empty from stream.functions.strings import strip, remove_comments -@pytest.mark.dependency( - depends=[ - ('session', 'tests/test_base.py::test_iter_stream'), - ], -) def test_or(): s1 = IterStream('123') s2 = s1 | strip assert isinstance(s2, Stream) -@pytest.mark.dependency( - depends=[ - ('session', 'tests/test_base.py::test_iter_stream'), - ], -) def test_gt(): s1 = IterStream('123') tu = s1 > tuple assert tu == ('1', '2', '3') -@pytest.mark.dependency( - depends=[ - ('session', 'tests/test_base.py::test_iter_stream'), - 'test_or', - ('session', 'tests/functions/test_each.py::test_apply_each'), - ], -) def test_call(): s = IterStream('abc') li = [] @@ -43,15 +24,6 @@ def test_call(): assert li == ['a', 'b', 'c'] -@pytest.mark.dependency( - depends=[ - ('session', 'tests/test_base.py::test_iter_stream'), - 'test_or', - ('session', 'tests/functions/test_strings.py::test_strip'), - ('session', 'tests/functions/test_filter.py::test_remove_empty'), - ('session', 'tests/functions/test_strings.py::test_remove_comments'), - ], -) def test_chain(tmpdir): assert tuple( IterStream("""#123 From 975171bd6dd071065ea9895543c1267f3875a297 Mon Sep 17 00:00:00 2001 From: Michael Kim Date: Tue, 23 Apr 2019 16:09:27 -0700 Subject: [PATCH 3/4] add writelines with line endings to io --- stream/io/__init__.py | 25 +++++++++++++++++++++++-- stream/io/local.py | 14 ++++++++++++++ tests/io/test_base.py | 9 +++++++++ tests/io/test_local.py | 30 ++++++++++++++++++++++++++++++ 4 files changed, 76 insertions(+), 2 deletions(-) diff --git a/stream/io/__init__.py b/stream/io/__init__.py index e22d7ea..e6dad38 100644 --- a/stream/io/__init__.py +++ b/stream/io/__init__.py @@ -21,6 +21,15 @@ def stream(self) -> _Stream[_typing.AnyStr]: raise self.NotSupported return _IterStream(self) + def input(self, lines: _typing.Iterable[_typing.AnyStr]) -> None: + """ + Write a sequence of lines. + Line endings are added. + """ + for line in lines: + self.write(line) + self.write(self.newline_str) + class SameFile(ValueError): pass @@ -107,6 +116,10 @@ def _read_character(self) -> Character: def newline(self) -> Character: raise NotImplementedError # pragma: no cover + @property + def newline_str(self) -> _typing.AnyStr: + raise NotImplementedError # pragma: no cover + def _read_iter( self, *, @@ -204,7 +217,11 @@ def flush(self) -> None: class TextFile(File[str], _typing.TextIO): @property - def newline(self) -> Character: + def newline(self) -> str: + return '\n' + + @property + def newline_str(self) -> str: return '\n' def _read( @@ -240,9 +257,13 @@ def newlines(self) -> _typing.Union[str, _typing.Tuple[str, ...], None]: class BinaryFile(File[bytes], _typing.BinaryIO): @property - def newline(self) -> Character: + def newline(self) -> int: return 10 # b'\n' + @property + def newline_str(self) -> bytes: + return b'\n' + def _read( self, *, diff --git a/stream/io/local.py b/stream/io/local.py index ee13ac3..71190dc 100644 --- a/stream/io/local.py +++ b/stream/io/local.py @@ -33,6 +33,20 @@ def path(self) -> str: def text(self) -> bool: return 'b' not in self.__mode + @cached_property + def newline(self): + if self.text: + return '\n' + else: + return 10 + + @cached_property + def newline_str(self): + if self.text: + return '\n' + else: + return b'\n' + def __eq__(self, other: _File): if not isinstance(other, LocalFile): return False diff --git a/tests/io/test_base.py b/tests/io/test_base.py index 9417444..f42e9ca 100644 --- a/tests/io/test_base.py +++ b/tests/io/test_base.py @@ -116,3 +116,12 @@ def test_stream_not_readable(): with DummyNotReadable() as f: with pytest.raises(DummyNotReadable.NotSupported): f.stream + + +def test_newline(): + bin = DummyFile() + assert bin.newline == 10 + assert bin.newline_str == b'\n' + txt = DummyTextFile() + assert txt.newline == '\n' + assert txt.newline_str == '\n' diff --git a/tests/io/test_local.py b/tests/io/test_local.py index 14bbb28..1ff7099 100644 --- a/tests/io/test_local.py +++ b/tests/io/test_local.py @@ -63,3 +63,33 @@ def test_next(tmpdir): with LocalFile(str(file)) as f: assert next(f) == '123\n' + + +def test_call_text(tmpdir): + file = tmpdir / '0.txt' + + with LocalFile(str(file), 'w') as f: + f.input(txt.splitlines(keepends=False)) + + assert file.read_text(encoding='utf-8') == txt + + +def test_call_bytes(tmpdir): + file = tmpdir / '0.txt' + + with LocalFile(str(file), 'wb') as f: + f.input(bin.splitlines(keepends=False)) + + assert file.read_text(encoding='utf-8') == txt + + +def test_newline(tmpdir): + file = tmpdir / '0.txt' + + with LocalFile(str(file), 'w') as f1: + assert f1.newline == '\n' + assert f1.newline_str == '\n' + + with LocalFile(str(file), 'wb') as f2: + assert f2.newline == 10 + assert f2.newline_str == b'\n' From 1a48294de8fd445ff440caf769cf5d4c2830a9dd Mon Sep 17 00:00:00 2001 From: Michael Kim Date: Tue, 23 Apr 2019 16:13:21 -0700 Subject: [PATCH 4/4] add std variables --- stream/io/std.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/stream/io/std.py b/stream/io/std.py index be63022..d85f9dd 100644 --- a/stream/io/std.py +++ b/stream/io/std.py @@ -42,3 +42,8 @@ def __eq__(self, other): def __exit__(self, exc_type, exc_val, exc_tb): # don't close pass + + +stdin = StdIn() +stdout = StdOut() +stderr = StdErr()