示例#1
0
def test_flush(tmppath):
    """Tests for flush()"""
    # Mostly it just ensures calling it doesn't crash the program.
    fd = get_tsta_file(tmppath)
    full_path, fc = fd['full_path'], fd['contents']
    xfile = XRootDFile(mkurl(full_path), 'w')

    writestr = 'whut'

    xfile.flush()
    xfile.seek(0, SEEK_END)
    xfile.write(writestr)
    xfile.flush()
    xfile.close()

    xfile = XRootDFile(mkurl(full_path), 'r')
    assert xfile.read() == writestr

    # Fake/mock an error response
    fake_status = {
        "status": 3,
        "code": 0,
        "ok": False,
        "errno": errno.EREMOTE,
        "error": True,
        "message": '[FATAL] Remote I/O Error',
        "fatal": True,
        "shellcode": 51
    }
    # Assign mock return value to the file's sync() function
    # (which is called by flush())
    xfile._file.sync = Mock(return_value=(XRootDStatus(fake_status), None))
    pytest.raises(IOError, xfile.flush)
示例#2
0
def test_readwrite_unicode(tmppath):
    """Test read/write unicode."""
    if sys.getdefaultencoding() != 'ascii':
        # Python 2 only problem
        raise AssertionError(
            "Default system encoding is not ascii. This is likely due to some"
            " imported module changing it using sys.setdefaultencoding."
        )

    fd = get_tsta_file(tmppath)
    fb = get_copy_file(fd)
    fp, dummy = fd['full_path'], fd['contents']
    fp2 = fb['full_path']

    unicodestr = u"æøå"

    pfile = open(fp2, 'w')
    xfile = XRootDFile(mkurl(fp), 'w')
    pytest.raises(UnicodeEncodeError, pfile.write, unicodestr)
    pytest.raises(UnicodeEncodeError, xfile.write, unicodestr)
    xfile.close()

    xfile = XRootDFile(mkurl(fp), 'w+', encoding='utf-8')
    xfile.write(unicodestr)
    xfile.flush()
    xfile.seek(0)
    assert unicodestr.encode('utf8') == xfile.read()
    xfile.close()

    xfile = XRootDFile(mkurl(fp), 'w+', errors='ignore')
    xfile.write(unicodestr)
    xfile.flush()
    xfile.seek(0)
    assert unicodestr.encode('ascii', 'ignore') == xfile.read()
    xfile.close()