def pyqiodev(self): """Fixture providing a PyQIODevice with a QByteArray to test.""" data = QByteArray() f = QBuffer(data) qiodev = qtutils.PyQIODevice(f) yield qiodev qiodev.close()
def test_write_error_real(self): """Test a real write error with /dev/full on supported systems.""" qf = QFile('/dev/full') qf.open(QIODevice.WriteOnly | QIODevice.Unbuffered) dev = qtutils.PyQIODevice(qf) with pytest.raises(OSError, match='No space left on device'): dev.write(b'foo') qf.close()
def test_failing_open(self, tmpdir): """Test open() which fails (because it's an existent directory).""" qf = QFile(str(tmpdir)) dev = qtutils.PyQIODevice(qf) with pytest.raises(qtutils.QtOSError) as excinfo: dev.open(QIODevice.WriteOnly) assert excinfo.value.qt_errno == QFileDevice.OpenError assert dev.closed
def test_qprocess(self, py_proc): """Test PyQIODevice with a QProcess which is non-sequential. This also verifies seek() and tell() behave as expected. """ proc = QProcess() proc.start(*py_proc('print("Hello World")')) dev = qtutils.PyQIODevice(proc) assert not dev.closed with pytest.raises(OSError, match='Random access not allowed!'): dev.seek(0) with pytest.raises(OSError, match='Random access not allowed!'): dev.tell() proc.waitForFinished(1000) proc.kill() assert bytes(dev.read()).rstrip() == b'Hello World'
def test_qprocess(self): """Test PyQIODevice with a QProcess which is non-sequential. This also verifies seek() and tell() behave as expected. """ proc = QProcess() proc.start(sys.executable, ['-c', 'print("Hello World")']) dev = qtutils.PyQIODevice(proc) assert not dev.closed with pytest.raises(OSError) as excinfo: dev.seek(0) assert str(excinfo.value) == 'Random access not allowed!' with pytest.raises(OSError) as excinfo: dev.tell() assert str(excinfo.value) == 'Random access not allowed!' proc.waitForFinished(1000) proc.kill() assert bytes(dev.read()).rstrip() == b'Hello World'
def open(self, _fname, mode): """Open an in-memory PyQIODevice instead of a real file.""" modes = { 'wb': QIODevice.WriteOnly | QIODevice.Truncate, 'w': QIODevice.WriteOnly | QIODevice.Text | QIODevice.Truncate, 'rb': QIODevice.ReadOnly, 'r': QIODevice.ReadOnly | QIODevice.Text, } try: qt_mode = modes[mode] except KeyError: raise ValueError("Invalid mode {}!".format(mode)) f = QBuffer(self._data) f.open(qt_mode) qiodev = qtutils.PyQIODevice(f) # Make sure tests using name/mode don't blow up. qiodev.name = test_file.TESTFN qiodev.mode = mode # Create empty TESTFN file because the Python tests try to unlink # it.after the test. open(test_file.TESTFN, 'w', encoding='utf-8').close() return qiodev
def pyqiodev_failing(self): """Fixture providing a PyQIODevice with a FailingQIODevice to test.""" failing = FailingQIODevice() return qtutils.PyQIODevice(failing)