diff options
author | Sergey Shepelev <temotor@gmail.com> | 2022-06-25 16:24:25 +0300 |
---|---|---|
committer | Sergey Shepelev <temotor@gmail.com> | 2022-06-25 16:24:25 +0300 |
commit | 6213752991c3efe2ee1d23984ace30231fd68b64 (patch) | |
tree | 85f2d08a81ef0181dccfb722cfa6fdfe9d630888 | |
parent | b9db6500ac6a9841e32a1a90c7eb217ab3118ca6 (diff) | |
download | eventlet-p758.tar.gz |
test cleanupp758
-rw-r--r-- | tests/__init__.py | 6 | ||||
-rw-r--r-- | tests/greenio_test.py | 76 |
2 files changed, 37 insertions, 45 deletions
diff --git a/tests/__init__.py b/tests/__init__.py index 1883667..4a5204c 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -488,3 +488,9 @@ def dns_tcp_server(ip_to_give, request_count=1): client.close() thread.join() server_socket.close() + + +def read_file(path, mode="rb"): + with open(path, mode) as f: + result = f.read() + return result diff --git a/tests/greenio_test.py b/tests/greenio_test.py index 14f65da..b8c5485 100644 --- a/tests/greenio_test.py +++ b/tests/greenio_test.py @@ -1024,54 +1024,40 @@ def test_pipe_context(): assert r.closed and w.closed -def read_file(filepath): - with open(filepath, 'r') as fr: - result = fr.read() - return result - +def test_greenpipe_write(): + expected = b"initial" + with tempfile.NamedTemporaryFile() as f: + with greenio.GreenPipe(f.name, "wb") as writer: + writer.write(expected) + + actual = tests.read_file(f.name) + assert actual == expected + + +def test_greenpipe_append(): + old_data = b"existing data..." + new_data = b"append with mode=a" + expected = old_data + new_data + with tempfile.NamedTemporaryFile() as f: + with open(f.name, "wb") as fw: + fw.write(old_data) -class TestGreenFileIO(tests.LimitedTestCase): - @tests.skip_on_windows - def setUp(self): - super(self.__class__, self).setUp() - self.tempdir = tempfile.mkdtemp('_green_file_io_test') + with greenio.GreenPipe(f.name, "ab") as writer: + writer.write(new_data) - def tearDown(self): - shutil.rmtree(self.tempdir) - super(self.__class__, self).tearDown() + actual = tests.read_file(f.name) + assert actual == expected - def test_write(self): - filepath = os.path.join(self.tempdir, 'test_file_w.txt') - writer = greenio.GreenPipe(filepath, 'w') - excepted = "Test write to file with mode 'w'." - writer.write(excepted) - writer.close() - actual = read_file(filepath) - self.assertEqual(excepted, actual, msg='actual=%s,excepted=%s' % (actual, excepted)) +def test_greenpipe_read_overwrite(): + old_data = b"existing data..." + new_data = b"overwrite with mode=r+" + with tempfile.NamedTemporaryFile() as f: + with greenio.GreenPipe(f.name, "wb") as writer: + writer.write(old_data) - def test_append(self): - filepath = os.path.join(self.tempdir, 'test_file_a.txt') - old_data = 'Exist data...\n' - with open(filepath, 'w')as fw: - fw.write(old_data) - writer = greenio.GreenPipe(filepath, 'a') - new_data = "Test write to file with mode 'a'." - writer.write(new_data) - writer.close() - - excepted = old_data + new_data - actual = read_file(filepath) - self.assertEqual(excepted, actual, msg='actual=%s,excepted=%s' % (actual, excepted)) - - def test_read_and_write(self): - filepath = os.path.join(self.tempdir, 'test_file_rw.txt') - with open(filepath, 'w'): - pass - writer = greenio.GreenPipe(filepath, 'r+') - excepted = "Test write to file with mode 'r+'." - writer.write(excepted) - writer.close() + with greenio.GreenPipe(f.name, "r+b") as writer: + writer.write(new_data) - actual = read_file(filepath) - self.assertEqual(excepted, actual, msg='actual=%s,excepted=%s' % (actual, excepted)) + actual = tests.read_file(f.name) + assert actual == new_data |