def test_write(tmppath):
    """Test write()."""
    # With a new file.
    xfile = XRootDPyFile(mkurl(join(tmppath, 'data/nuts')), 'w+')
    assert xfile.size == 0
    conts = xfile.read()
    assert not conts

    nconts = 'Write.'
    xfile.write(nconts)
    assert xfile.tell() == len(nconts)
    assert not xfile.closed
    xfile.seek(0)
    assert xfile.size == len(nconts)
    assert xfile.read() == nconts.encode()
    xfile.close()

    # Verify persistence after closing.
    xfile = XRootDPyFile(mkurl(join(tmppath, 'data/nuts')), 'r+')
    assert xfile.size == len(nconts)
    assert xfile.read() == nconts.encode()

    # Seek(x>0) followed by a write
    nc2 = 'hello'
    cntr = len(nconts) // 2
    xfile.seek(cntr)
    xfile.write(nc2)
    assert xfile.tell() == len(nc2) + cntr
    xfile.seek(0)
    expected = nconts[:cntr] + nc2
    assert xfile.read() == expected.encode()
    xfile.close()

    # Seek(x>0) followed by a write of len < size-x
    fd = get_tsta_file(tmppath)
    fp, fc = fd['full_path'], fd['contents']
    xfile = XRootDPyFile(mkurl(fp), 'r+')
    assert xfile.read() == fc.encode()
    xfile.seek(2)
    nc = 'yo'
    xfile.write(nc)
    assert xfile.tell() == len(nc) + 2
    assert xfile.read() == fc[2 + len(nc):].encode()

    # run w/ flushing == true
    xfile.write('', True)

    # Mock an error, yayy!
    fake_status = {
        "status": 3,
        "code": 0,
        "ok": False,
        "errno": errno.EREMOTE,
        "error": True,
        "message": '[FATAL] Remote I/O Error',
        "fatal": True,
        "shellcode": 51
    }
    xfile._file.write = Mock(return_value=(XRootDStatus(fake_status), None))
    pytest.raises(IOError, xfile.write, '')
Example #2
0
def test_truncate1(tmppath):
    """Test truncate(0)."""
    fd = get_tsta_file(tmppath)
    full_path, fc = fd['full_path'], fd['contents']
    xfile = XRootDPyFile(mkurl(full_path), 'r+')
    # r+ opens for r/w, and won't truncate the file automatically.
    assert xfile.read() == fc
    assert xfile.tell() == len(fc)
    xfile.seek(0)  # Reset ipp.
    assert xfile.tell() == 0

    # Truncate it to size 0.
    xfile.truncate(0)
    assert xfile.size == 0
    assert xfile.tell() == 0
    assert xfile.read() == ''
    assert xfile.tell() == 0
    xfile.close()

    # Re-open same file.
    xfile = XRootDPyFile(mkurl(full_path), 'r+')
    assert xfile.size == 0
    assert xfile.read() == ''

    # Truncate it again!
    xfile.truncate(0)
    assert xfile.size == 0
    assert xfile.read() == ''

    # Truncate it twice.
    xfile.truncate(0)
    assert xfile.size == 0
    assert xfile.read() == ''

    # Truncate to 1.
    xfile.truncate(1)
    assert xfile.tell() == 0
    assert xfile.size == 1
    xfile.seek(0)
    assert xfile.read() == '\x00'
    assert xfile.tell() == 1
    xfile.close()

    xfile = XRootDPyFile(mkurl(full_path), 'r+')
    assert xfile.size == 1
    assert xfile.read() == '\x00'

    # Mock it.
    fake_status = {
        "status": 3,
        "code": 0,
        "ok": False,
        "errno": errno.EREMOTE,
        "error": True,
        "message": '[FATAL] Remote I/O Error',
        "fatal": True,
        "shellcode": 51
    }
    xfile._file.truncate = Mock(return_value=(XRootDStatus(fake_status), None))
    pytest.raises(IOError, xfile.truncate, 0)
Example #3
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 = XRootDPyFile(mkurl(full_path), 'w')

    writestr = 'whut'

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

    xfile = XRootDPyFile(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)
Example #4
0
def test_init_append(tmppath):
    """Test for files opened 'a'"""
    fd = get_tsta_file(tmppath)
    fp, fc = fd['full_path'], fd['contents']
    xfile = XRootDPyFile(mkurl(fp), 'a')
    assert xfile.mode == 'a'
    pytest.raises(IOError, xfile.read)
    assert xfile.tell() == len(fc)

    # Seeking is allowed, but writes still go on the end.
    xfile.seek(0)
    assert xfile.tell() == 0
    newcont = u'butterflies'
    xfile.write(newcont)
    assert xfile.tell() == len(fc) + len(newcont)
    # Can't read in this mode.
    xfile.close()
    xfile = XRootDPyFile(mkurl(fp), 'r')
    assert xfile.read() == fc + newcont

    xfile.close()
    xfile = XRootDPyFile(mkurl(fp), 'a')
    xfile.write(fc)
    xfile.seek(0)
    pytest.raises(IOError, xfile.read)
Example #5
0
def test_truncate1(tmppath):
    """Test truncate(0)."""
    fd = get_tsta_file(tmppath)
    full_path, fc = fd['full_path'], fd['contents']
    xfile = XRootDPyFile(mkurl(full_path), 'r+')
    # r+ opens for r/w, and won't truncate the file automatically.
    assert xfile.read() == fc
    assert xfile.tell() == len(fc)
    xfile.seek(0)  # Reset ipp.
    assert xfile.tell() == 0

    # Truncate it to size 0.
    xfile.truncate(0)
    assert xfile.size == 0
    assert xfile.tell() == 0
    assert xfile.read() == ''
    assert xfile.tell() == 0
    xfile.close()

    # Re-open same file.
    xfile = XRootDPyFile(mkurl(full_path), 'r+')
    assert xfile.size == 0
    assert xfile.read() == ''

    # Truncate it again!
    xfile.truncate(0)
    assert xfile.size == 0
    assert xfile.read() == ''

    # Truncate it twice.
    xfile.truncate(0)
    assert xfile.size == 0
    assert xfile.read() == ''

    # Truncate to 1.
    xfile.truncate(1)
    assert xfile.tell() == 0
    assert xfile.size == 1
    xfile.seek(0)
    assert xfile.read() == '\x00'
    assert xfile.tell() == 1
    xfile.close()

    xfile = XRootDPyFile(mkurl(full_path), 'r+')
    assert xfile.size == 1
    assert xfile.read() == '\x00'

    # Mock it.
    fake_status = {
        "status": 3,
        "code": 0,
        "ok": False,
        "errno": errno.EREMOTE,
        "error": True,
        "message": '[FATAL] Remote I/O Error',
        "fatal": True,
        "shellcode": 51
    }
    xfile._file.truncate = Mock(return_value=(XRootDStatus(fake_status), None))
    pytest.raises(IOError, xfile.truncate, 0)
Example #6
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 = XRootDPyFile(mkurl(full_path), 'w')

    writestr = 'whut'

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

    xfile = XRootDPyFile(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)
Example #7
0
def test_init_append(tmppath):
    """Test for files opened 'a'"""
    fd = get_tsta_file(tmppath)
    fp, fc = fd['full_path'], fd['contents']
    xfile = XRootDPyFile(mkurl(fp), 'a')
    assert xfile.mode == 'a'
    pytest.raises(IOError, xfile.read)
    assert xfile.tell() == len(fc)

    # Seeking is allowed, but writes still go on the end.
    xfile.seek(0)
    assert xfile.tell() == 0
    newcont = u'butterflies'
    xfile.write(newcont)
    assert xfile.tell() == len(fc) + len(newcont)
    # Can't read in this mode.
    xfile.close()
    xfile = XRootDPyFile(mkurl(fp), 'r')
    assert xfile.read() == fc + newcont

    xfile.close()
    xfile = XRootDPyFile(mkurl(fp), 'a')
    xfile.write(fc)
    xfile.seek(0)
    pytest.raises(IOError, xfile.read)
Example #8
0
def test_write(tmppath):
    """Test write()."""
    # With a new file.
    xfile = XRootDPyFile(mkurl(join(tmppath, 'data/nuts')), 'w+')
    assert xfile.size == 0
    conts = xfile.read()
    assert not conts

    nconts = 'Write.'
    xfile.write(nconts)
    assert xfile.tell() == len(nconts)
    assert not xfile.closed
    xfile.seek(0)
    assert xfile.size == len(nconts)
    assert xfile.read() == nconts
    xfile.close()

    # Verify persistence after closing.
    xfile = XRootDPyFile(mkurl(join(tmppath, 'data/nuts')), 'r+')
    assert xfile.size == len(nconts)
    assert xfile.read() == nconts

    # Seek(x>0) followed by a write
    nc2 = 'hello'
    cntr = len(nconts)//2
    xfile.seek(cntr)
    xfile.write(nc2)
    assert xfile.tell() == len(nc2) + cntr
    xfile.seek(0)
    assert xfile.read() == nconts[:cntr] + nc2
    xfile.close()

    # Seek(x>0) followed by a write of len < size-x
    fd = get_tsta_file(tmppath)
    fp, fc = fd['full_path'], fd['contents']
    xfile = XRootDPyFile(mkurl(fp), 'r+')
    assert xfile.read() == fc
    xfile.seek(2)
    nc = 'yo'
    xfile.write(nc)
    assert xfile.tell() == len(nc) + 2
    assert xfile.read() == fc[2+len(nc):]

    # run w/ flushing == true
    xfile.write('', True)

    # Mock an error, yayy!
    fake_status = {
        "status": 3,
        "code": 0,
        "ok": False,
        "errno": errno.EREMOTE,
        "error": True,
        "message": '[FATAL] Remote I/O Error',
        "fatal": True,
        "shellcode": 51
    }
    xfile._file.write = Mock(return_value=(XRootDStatus(fake_status), None))
    pytest.raises(IOError, xfile.write, '')
Example #9
0
def test__is_open(tmppath):
    """Test _is_open()"""
    fd = get_tsta_file(tmppath)
    full_path = fd['full_path']
    xfile = XRootDPyFile(mkurl(full_path))
    assert not xfile.closed
    xfile.close()
    assert xfile.closed
Example #10
0
def test__is_open(tmppath):
    """Test _is_open()"""
    fd = get_tsta_file(tmppath)
    full_path = fd['full_path']
    xfile = XRootDPyFile(mkurl(full_path))
    assert not xfile.closed
    xfile.close()
    assert xfile.closed
Example #11
0
def test_open_close(tmppath):
    """Test close() on an open file."""
    fd = get_tsta_file(tmppath)
    full_path = fd['full_path']
    xfile = XRootDPyFile(mkurl(full_path))
    assert xfile
    assert not xfile.closed
    xfile.close()
    assert xfile.closed
def test_open_close(tmppath):
    """Test close() on an open file."""
    fd = get_tsta_file(tmppath)
    full_path = fd["full_path"]
    xfile = XRootDPyFile(mkurl(full_path))
    assert xfile
    assert not xfile.closed
    xfile.close()
    assert xfile.closed
Example #13
0
def test_open_close(tmppath):
    """Test close() on an open file."""
    fd = get_tsta_file(tmppath)
    full_path = fd['full_path']
    xfile = XRootDPyFile(mkurl(full_path))
    assert xfile
    assert not xfile.closed
    xfile.close()
    assert xfile.closed
    # Multiple calls to closed do nothing.
    xfile.close()
def test_reading_whole_big_file(tmppath):
    """Tests reading end of big file."""
    f = "data/big_file.txt"
    create_big_file(tmppath, f)

    xfile = XRootDPyFile(mkurl(join(tmppath, f)), 'r')

    pytest.raises(IOError, xfile.read)
    xfile.close()

    remove_file(tmppath, f)
Example #15
0
def test_truncate4(tmppath):
    """Verifies that truncate() raises errors on non-truncatable files."""
    fd = get_mltl_file(tmppath)
    full_path, fc = fd['full_path'], fd['contents']

    xfile = XRootDPyFile(mkurl(full_path), 'r')
    pytest.raises(IOError, xfile.truncate, 0)

    xfile.close()
    xfile = XRootDPyFile(mkurl(full_path), 'w-')
    pytest.raises(IOError, xfile.truncate, 0)
Example #16
0
def test_truncate4(tmppath):
    """Verifies that truncate() raises errors on non-truncatable files."""
    fd = get_mltl_file(tmppath)
    full_path, fc = fd['full_path'], fd['contents']

    xfile = XRootDPyFile(mkurl(full_path), 'r')
    pytest.raises(IOError, xfile.truncate, 0)

    xfile.close()
    xfile = XRootDPyFile(mkurl(full_path), 'w-')
    pytest.raises(IOError, xfile.truncate, 0)
def test_reading_begining_of_big_file(tmppath):
    """Tests reading end of big file."""
    f = "data/big_file.txt"
    create_big_file(tmppath, f, frontfile_content="test")

    xfile = XRootDPyFile(mkurl(join(tmppath, f)), 'r')

    data = xfile.read(4)
    assert data == b"test"
    xfile.close()

    remove_file(tmppath, f)
def test_init_newline(tmppath):
    """Tests fs.open() with specified newline parameter."""
    fd = get_tsta_file(tmppath)
    fp, fc = fd["full_path"], fd["contents"]

    xfile = XRootDPyFile(mkurl(fp))
    assert xfile._newline == "\n"
    xfile.close()

    xfile = XRootDPyFile(mkurl(fp), newline="\n")
    assert xfile._newline == "\n"
    xfile.close()

    pytest.raises(UnsupportedError, XRootDPyFile, mkurl(fp), mode="r", newline="what")
Example #19
0
def test_write_binary(tmppath):
    """Tests for writing binary data to file."""
    fd = get_bin_testfile(tmppath)
    fp, fc = fd['full_path'], fd['contents']

    # Test w/ confirmed binary data read from a binary file
    xf_new = XRootDPyFile(mkurl(join(tmppath, 'data/tmp_bin')), 'wb+')
    xf_new.write(fc), xf_new.seek(0)
    assert xf_new.read() == fc

    xf_new.close()
    # Verify persistence.
    xf_new = XRootDPyFile(mkurl(join(tmppath, 'data/tmp_bin')), 'r+')
    assert xf_new.read() == fc

    # Test truncate
    xf_new.truncate()
    xf_new.seek(0)
    assert xf_new.read() == fc
    xf_new.close()

    # Test with bytearray
    xf_new = XRootDPyFile(mkurl(join(tmppath, 'data/tmp_bin')), 'wb+')
    barr = bytearray(range(0, 5))
    xf_new.write(barr), xf_new.seek(0)
    assert xf_new.read() == barr
    xf_new.close()

    # Verify persistence.
    xf_new = XRootDPyFile(mkurl(join(tmppath, 'data/tmp_bin')), 'r')
    assert xf_new.read() == barr
    xf_new.close()
def test_readwrite_diffrent_encodings_fails(tmppath):
    """Test read/write a unicode str in non unicode files."""
    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')  # default encoding is ANSI
    pytest.raises(UnicodeEncodeError, pfile.write, unicodestr)
    xfile = XRootDPyFile(mkurl(fp), 'w', encoding='ascii')
    pytest.raises(UnicodeEncodeError, xfile.write, unicodestr)
    xfile.close()
Example #21
0
def test_write_binary(tmppath):
    """Tests for writing binary data to file."""
    fd = get_bin_testfile(tmppath)
    fp, fc = fd['full_path'], fd['contents']

    # Test w/ confirmed binary data read from a binary file
    xf_new = XRootDPyFile(mkurl(join(tmppath, 'data/tmp_bin')), 'wb+')
    xf_new.write(fc), xf_new.seek(0)
    assert xf_new.read() == fc

    xf_new.close()
    # Verify persistence.
    xf_new = XRootDPyFile(mkurl(join(tmppath, 'data/tmp_bin')), 'r+')
    assert xf_new.read() == fc

    # Test truncate
    xf_new.truncate()
    xf_new.seek(0)
    assert xf_new.read() == fc
    xf_new.close()

    # Test with bytearray
    xf_new = XRootDPyFile(mkurl(join(tmppath, 'data/tmp_bin')), 'wb+')
    barr = bytearray(range(0, 5))
    xf_new.write(barr), xf_new.seek(0)
    assert xf_new.read() == barr
    xf_new.close()

    # Verify persistence.
    xf_new = XRootDPyFile(mkurl(join(tmppath, 'data/tmp_bin')), 'r')
    assert xf_new.read() == barr
    xf_new.close()
Example #22
0
def test_init_newline(tmppath):
    """Tests fs.open() with specified newline parameter."""
    fd = get_tsta_file(tmppath)
    fp, fc = fd['full_path'], fd['contents']

    xfile = XRootDPyFile(mkurl(fp))
    assert xfile._newline == '\n'
    xfile.close()

    xfile = XRootDPyFile(mkurl(fp), newline='\n')
    assert xfile._newline == '\n'
    xfile.close()

    pytest.raises(UnsupportedError, XRootDPyFile, mkurl(fp), mode='r',
                  newline='what')
Example #23
0
def test_init_newline(tmppath):
    """Tests fs.open() with specified newline parameter."""
    fd = get_tsta_file(tmppath)
    fp, fc = fd['full_path'], fd['contents']

    xfile = XRootDPyFile(mkurl(fp))
    assert xfile._newline == '\n'
    xfile.close()

    xfile = XRootDPyFile(mkurl(fp), newline='\n')
    assert xfile._newline == '\n'
    xfile.close()

    pytest.raises(UnsupportedError, XRootDPyFile, mkurl(fp), mode='r',
                  newline='what')
def test_reading_end_of_big_file(tmppath):
    """Tests reading end of big file."""
    f = "data/big_file.txt"
    create_big_file(tmppath, f, endfile_content="test\0")

    xfile = XRootDPyFile(mkurl(join(tmppath, f)), 'r')

    xfile.seek(xfile.size - 10)
    data = xfile.read()
    assert xfile.size == 2 * 1024 * 1024 * 1024
    assert len(data) == 10
    assert data[-5:-1] == b"test"
    xfile.close()

    remove_file(tmppath, f)
Example #25
0
def test_init_writemode(tmppath):
    """Tests for opening files in 'w(+)'"""
    fd = get_tsta_file(tmppath)
    fp, fc = fd['full_path'], fd['contents']
    xfile = XRootDPyFile(mkurl(fp), 'w')
    pytest.raises(IOError, xfile.read)

    xfile.seek(1)
    conts = 'what'
    xfile.write(conts)
    assert xfile.tell() == 1 + len(conts)
    assert xfile.size == 1 + len(conts)
    xfile.close()
    xfile = XRootDPyFile(mkurl(fp), 'r')
    fc = xfile.read()
    assert fc == '\x00'+conts
    assert not fc == conts
def test_init_writemode(tmppath):
    """Tests for opening files in 'w(+)'"""
    fd = get_tsta_file(tmppath)
    fp, fc = fd["full_path"], fd["contents"]
    xfile = XRootDPyFile(mkurl(fp), "w")
    pytest.raises(IOError, xfile.read)

    xfile.seek(1)
    conts = "what"
    xfile.write(conts)
    assert xfile.tell() == 1 + len(conts)
    assert xfile.size == 1 + len(conts)
    xfile.close()
    xfile = XRootDPyFile(mkurl(fp), "r")
    fc = xfile.read()
    assert fc == "\x00" + conts
    assert not fc == conts
def create_big_file(tmppath,
                    filename,
                    size=2 * 1024 * 1024 * 1024,
                    frontfile_content=None,
                    endfile_content=None):
    xfile = XRootDPyFile(mkurl(join(tmppath, filename)), 'w')
    if endfile_content:
        endfile_length = len(endfile_content)
    else:
        endfile_content = '\0'
        endfile_length = 1
    # Prepare big file for testing
    if frontfile_content:
        xfile.write(frontfile_content)
    xfile.seek(size - endfile_length)
    xfile.write(endfile_content)
    xfile.close()
def test_size_len(tmppath):
    """Tests for the size and len property."""
    fd = get_tsta_file(tmppath)
    full_path, fc = fd['full_path'], fd['contents']
    xfile = XRootDPyFile(mkurl(full_path))

    assert xfile.size == len(fc)
    assert len(xfile) == len(fc)

    # Length of empty file
    xfile = XRootDPyFile(mkurl(join(tmppath, fd['dir'], 'whut')), 'w+')
    assert xfile.size == len('')
    assert len(xfile) == len('')

    # Length of multiline file
    fd = get_mltl_file(tmppath)
    fpp, fc = fd['full_path'], fd['contents']
    xfile = XRootDPyFile(mkurl(fpp))
    assert xfile.size == len(fc)
    assert len(xfile) == len(fc)

    # Mock the error
    fake_status = {
        "status": 3,
        "code": 0,
        "ok": False,
        "errno": errno.EREMOTE,
        "error": True,
        "message": '[FATAL] Remote I/O Error',
        "fatal": True,
        "shellcode": 51
    }
    xfile.close()
    xfile = XRootDPyFile(mkurl(full_path))
    xfile._file.stat = Mock(return_value=(XRootDStatus(fake_status), None))
    try:
        xfile.size
        assert False
    except IOError:
        assert True

    try:
        len(xfile)
        assert False
    except IOError:
        assert True
Example #29
0
def test_init_writemode(tmppath):
    """Tests for opening files in 'w(+)'"""
    fd = get_tsta_file(tmppath)
    fp, fc = fd['full_path'], fd['contents']
    xfile = XRootDPyFile(mkurl(fp), 'w')
    pytest.raises(IOError, xfile.read)

    xfile.seek(1)
    conts = 'what'
    xfile.write(conts)
    assert xfile.tell() == 1 + len(conts)
    assert xfile.size == 1 + len(conts)
    xfile.close()
    xfile = XRootDPyFile(mkurl(fp), 'r')
    fc = xfile.read()
    assert fc == '\x00'+conts
    assert not fc == conts
Example #30
0
def test_readlines(tmppath):
    """Tests readlines()"""
    fd = get_mltl_file(tmppath)
    fb = get_copy_file(fd)
    fp, fc = fd['full_path'], fd['contents']
    fp2 = fb['full_path']

    xfile, pfile = XRootDPyFile(mkurl(fp), 'r'), open(fp2, 'r')

    assert xfile.readlines() == pfile.readlines()

    xfile.seek(0), pfile.seek(0)
    assert pfile.readlines() == xfile.readlines()

    xfile.close(), pfile.close()

    xfile, pfile = XRootDPyFile(mkurl(fp), 'w+'), open(fp2, 'w+')
    xfile.seek(0), pfile.seek(0)
    assert xfile.readlines() == pfile.readlines()
Example #31
0
def test_readlines(tmppath):
    """Tests readlines()"""
    fd = get_mltl_file(tmppath)
    fb = get_copy_file(fd)
    fp, fc = fd['full_path'], fd['contents']
    fp2 = fb['full_path']

    xfile, pfile = XRootDPyFile(mkurl(fp), 'r'), open(fp2, 'r')

    assert xfile.readlines() == pfile.readlines()

    xfile.seek(0), pfile.seek(0)
    assert pfile.readlines() == xfile.readlines()

    xfile.close(), pfile.close()

    xfile, pfile = XRootDPyFile(mkurl(fp), 'w+'), open(fp2, 'w+')
    xfile.seek(0), pfile.seek(0)
    assert xfile.readlines() == pfile.readlines()
def test_readlines(tmppath):
    """Tests readlines()"""
    fd = get_mltl_file(tmppath)
    fb = get_copy_file(fd)
    fp, fc = fd["full_path"], fd["contents"]
    fp2 = fb["full_path"]

    xfile, pfile = XRootDPyFile(mkurl(fp), "r"), open(fp2, "r")

    assert xfile.readlines() == pfile.readlines()

    xfile.seek(0), pfile.seek(0)
    assert pfile.readlines() == xfile.readlines()

    xfile.close(), pfile.close()

    xfile, pfile = XRootDPyFile(mkurl(fp), "w+"), open(fp2, "w+")
    xfile.seek(0), pfile.seek(0)
    assert xfile.readlines() == pfile.readlines()
def test__assert_mode(tmppath):
    """Tests for _assert_mode"""
    fd = get_tsta_file(tmppath)
    full_path, fc = fd["full_path"], fd["contents"]
    mode = "r"
    xfile = XRootDPyFile(mkurl(full_path), mode)

    assert xfile.mode == mode
    assert xfile._assert_mode(mode)
    delattr(xfile, "mode")
    pytest.raises(AttributeError, xfile._assert_mode, mode)

    xfile.close()
    xfile = XRootDPyFile(mkurl(full_path), "r")
    assert xfile._assert_mode("r")
    pytest.raises(IOError, xfile._assert_mode, "w")

    xfile.close()
    xfile = XRootDPyFile(mkurl(full_path), "w-")
    assert xfile._assert_mode("w-")
    pytest.raises(IOError, xfile._assert_mode, "r")

    xfile.close()
    xfile = XRootDPyFile(mkurl(full_path), "a")
    assert xfile._assert_mode("w")
    pytest.raises(IOError, xfile._assert_mode, "r")
Example #34
0
def test__assert_mode(tmppath):
    """Tests for _assert_mode"""
    fd = get_tsta_file(tmppath)
    full_path, fc = fd['full_path'], fd['contents']
    mode = 'r'
    xfile = XRootDPyFile(mkurl(full_path), mode)

    assert xfile.mode == mode
    assert xfile._assert_mode(mode)
    delattr(xfile, 'mode')
    pytest.raises(AttributeError, xfile._assert_mode, mode)

    xfile.close()
    xfile = XRootDPyFile(mkurl(full_path), 'r')
    assert xfile._assert_mode('r')
    pytest.raises(IOError, xfile._assert_mode, 'w')

    xfile.close()
    xfile = XRootDPyFile(mkurl(full_path), 'w-')
    assert xfile._assert_mode('w-')
    pytest.raises(IOError, xfile._assert_mode, 'r')

    xfile.close()
    xfile = XRootDPyFile(mkurl(full_path), 'a')
    assert xfile._assert_mode('w')
    pytest.raises(IOError, xfile._assert_mode, 'r')
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 = XRootDPyFile(mkurl(fp), "w")
    pytest.raises(UnicodeEncodeError, pfile.write, unicodestr)
    pytest.raises(UnicodeEncodeError, xfile.write, unicodestr)
    xfile.close()

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

    xfile = XRootDPyFile(mkurl(fp), "w+", errors="ignore")
    xfile.write(unicodestr)
    xfile.flush()
    xfile.seek(0)
    assert unicodestr.encode("ascii", "ignore") == xfile.read()
    xfile.close()
Example #36
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 = XRootDPyFile(mkurl(fp), 'w')
    pytest.raises(UnicodeEncodeError, pfile.write, unicodestr)
    pytest.raises(UnicodeEncodeError, xfile.write, unicodestr)
    xfile.close()

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

    xfile = XRootDPyFile(mkurl(fp), 'w+', errors='ignore')
    xfile.write(unicodestr)
    xfile.flush()
    xfile.seek(0)
    assert unicodestr.encode('ascii', 'ignore') == xfile.read()
    xfile.close()
Example #37
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 = XRootDPyFile(mkurl(fp), 'w')
    pytest.raises(UnicodeEncodeError, pfile.write, unicodestr)
    pytest.raises(UnicodeEncodeError, xfile.write, unicodestr)
    xfile.close()

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

    xfile = XRootDPyFile(mkurl(fp), 'w+', errors='ignore')
    xfile.write(unicodestr)
    xfile.flush()
    xfile.seek(0)
    assert unicodestr.encode('ascii', 'ignore') == xfile.read()
    xfile.close()
Example #38
0
def test__assert_mode(tmppath):
    """Tests for _assert_mode"""
    fd = get_tsta_file(tmppath)
    full_path, fc = fd['full_path'], fd['contents']
    mode = 'r'
    xfile = XRootDPyFile(mkurl(full_path), mode)

    assert xfile.mode == mode
    assert xfile._assert_mode(mode)
    delattr(xfile, 'mode')
    pytest.raises(AttributeError, xfile._assert_mode, mode)

    xfile.close()
    xfile = XRootDPyFile(mkurl(full_path), 'r')
    assert xfile._assert_mode('r')
    pytest.raises(IOError, xfile._assert_mode, 'w')

    xfile.close()
    xfile = XRootDPyFile(mkurl(full_path), 'w-')
    assert xfile._assert_mode('w-')
    pytest.raises(IOError, xfile._assert_mode, 'r')

    xfile.close()
    xfile = XRootDPyFile(mkurl(full_path), 'a')
    assert xfile._assert_mode('w')
    pytest.raises(IOError, xfile._assert_mode, 'r')
Example #39
0
def test_init_streammodes(tmppath):
    fd = get_tsta_file(tmppath)
    fp, fc = fd['full_path'], fd['contents']
    xfile = XRootDPyFile(mkurl(fp), 'r-')
    pytest.raises(IOError, xfile.seek, 3)
    assert xfile.size == len(fc)
    assert xfile.tell() == 0
    assert xfile.read() == fc
    assert xfile.tell() == len(fc)

    xfile.close()
    xfile = XRootDPyFile(mkurl(fp), 'w-')
    pytest.raises(IOError, xfile.read)
    pytest.raises(IOError, xfile.seek, 3)
    assert xfile.tell() == 0
    assert xfile.size == 0
    conts = 'hugs are delightful'
    xfile.write(conts)
    assert xfile.tell() == len(conts)
    xfile.close()
    xfile = XRootDPyFile(mkurl(fp), 'r')
    assert xfile.read() == conts
def test_init_streammodes(tmppath):
    fd = get_tsta_file(tmppath)
    fp, fc = fd["full_path"], fd["contents"]
    xfile = XRootDPyFile(mkurl(fp), "r-")
    pytest.raises(IOError, xfile.seek, 3)
    assert xfile.size == len(fc)
    assert xfile.tell() == 0
    assert xfile.read() == fc
    assert xfile.tell() == len(fc)

    xfile.close()
    xfile = XRootDPyFile(mkurl(fp), "w-")
    pytest.raises(IOError, xfile.read)
    pytest.raises(IOError, xfile.seek, 3)
    assert xfile.tell() == 0
    assert xfile.size == 0
    conts = "hugs are delightful"
    xfile.write(conts)
    assert xfile.tell() == len(conts)
    xfile.close()
    xfile = XRootDPyFile(mkurl(fp), "r")
    assert xfile.read() == conts
def test_readwrite_unicode(tmppath):
    """Test read/write a unicode str in unicode files."""
    fd = get_tsta_file(tmppath)
    fb = get_copy_file(fd)
    fp, dummy = fd['full_path'], fd['contents']
    fp2 = fb['full_path']

    unicodestr = u"æøå"

    xfile = XRootDPyFile(mkurl(fp), 'w+')  # default encoding is UTF-8
    xfile.write(unicodestr)
    xfile.flush()
    xfile.seek(0)
    assert unicodestr.encode('utf8') == xfile.read()
    xfile.close()

    xfile = XRootDPyFile(mkurl(fp), 'w+', encoding='ascii', errors='ignore')
    xfile.write(unicodestr)
    xfile.flush()
    xfile.seek(0)
    assert unicodestr.encode('ascii', 'ignore') == xfile.read()
    xfile.close()
Example #42
0
def test_init_streammodes(tmppath):
    fd = get_tsta_file(tmppath)
    fp, fc = fd['full_path'], fd['contents']
    xfile = XRootDPyFile(mkurl(fp), 'r-')
    pytest.raises(IOError, xfile.seek, 3)
    assert xfile.size == len(fc)
    assert xfile.tell() == 0
    assert xfile.read() == fc
    assert xfile.tell() == len(fc)

    xfile.close()
    xfile = XRootDPyFile(mkurl(fp), 'w-')
    pytest.raises(IOError, xfile.read)
    pytest.raises(IOError, xfile.seek, 3)
    assert xfile.tell() == 0
    assert xfile.size == 0
    conts = 'hugs are delightful'
    xfile.write(conts)
    assert xfile.tell() == len(conts)
    xfile.close()
    xfile = XRootDPyFile(mkurl(fp), 'r')
    assert xfile.read() == conts
Example #43
0
def test_size(tmppath):
    """Tests for the size property size."""
    fd = get_tsta_file(tmppath)
    full_path, fc = fd['full_path'], fd['contents']
    xfile = XRootDPyFile(mkurl(full_path))

    assert xfile.size == len(fc)

    # Length of empty file
    xfile = XRootDPyFile(mkurl(join(tmppath, fd['dir'], 'whut')), 'w+')
    assert xfile.size == len('')

    # Length of multiline file
    fd = get_mltl_file(tmppath)
    fpp, fc = fd['full_path'], fd['contents']
    xfile = XRootDPyFile(mkurl(fpp))
    assert xfile.size == len(fc)

    # Mock the error
    fake_status = {
        "status": 3,
        "code": 0,
        "ok": False,
        "errno": errno.EREMOTE,
        "error": True,
        "message": '[FATAL] Remote I/O Error',
        "fatal": True,
        "shellcode": 51
    }
    xfile.close()
    xfile = XRootDPyFile(mkurl(full_path))
    xfile._file.stat = Mock(return_value=(XRootDStatus(fake_status), None))
    try:
        xfile.size
        assert False
    except IOError:
        assert True
def test_readline(tmppath):
    """Tests for readline()."""
    fd = get_mltl_file(tmppath)
    fb = get_copy_file(fd)
    fp, fc = fd['full_path'], fd['contents']
    fp2 = fb['full_path']

    xfile, pfile = XRootDPyFile(mkurl(fp), 'r'), opener.open(fp2, 'r')

    assert xfile.readline() == pfile.readline().encode()
    assert xfile.readline() == pfile.readline().encode()
    assert xfile.readline() == pfile.readline().encode()

    xfile.close(), pfile.close()
    xfile, pfile = XRootDPyFile(mkurl(fp), 'r'), opener.open(fp2, 'r')
    assert xfile.readline() == pfile.readline().encode()
    xfile.seek(0), pfile.seek(0)
    assert xfile.readline() == pfile.readline().encode()
    assert xfile.tell(), pfile.tell()

    xfile.close(), pfile.close()
    xfile = XRootDPyFile(mkurl(fp), 'w+')

    str1 = 'hello\n'
    str2 = 'bye\n'

    xfile.write(str1 + str2)
    xfile.seek(0)
    assert xfile.readline() == str1.encode()
    assert xfile.readline() == str2.encode()
    assert xfile.readline() == b''
    assert xfile.readline() == b''

    xfile.seek(100)
    assert xfile.readline() == b''

    xfile.close()
    xfile = XRootDPyFile(mkurl(fp), 'w+')

    xfile.write(str2)
    xfile.seek(len(str2) + 1)
    xfile.write(str2)
    xfile.seek(0)
    _value = u'\x00' + str2
    assert xfile.readline() == str2.encode()
    assert xfile.readline() == _value.encode()
Example #45
0
def test_readline(tmppath):
    """Tests for readline()."""
    fd = get_mltl_file(tmppath)
    fb = get_copy_file(fd)
    fp, fc = fd['full_path'], fd['contents']
    fp2 = fb['full_path']

    xfile, pfile = XRootDPyFile(mkurl(fp), 'r'), opener.open(fp2, 'r')

    assert xfile.readline() == pfile.readline()
    assert xfile.readline() == pfile.readline()
    assert xfile.readline() == pfile.readline()

    xfile.close(), pfile.close()
    xfile, pfile = XRootDPyFile(mkurl(fp), 'r'), opener.open(fp2, 'r')
    assert xfile.readline() == pfile.readline()
    xfile.seek(0), pfile.seek(0)
    assert xfile.readline() == pfile.readline()
    assert xfile.tell(), pfile.tell()

    xfile.close(), pfile.close()
    xfile = XRootDPyFile(mkurl(fp), 'w+')

    str1 = 'hello\n'
    str2 = 'bye\n'

    xfile.write(str1+str2)
    xfile.seek(0)
    assert xfile.readline() == str1
    assert xfile.readline() == str2
    assert xfile.readline() == ''
    assert xfile.readline() == ''

    xfile.seek(100)
    assert xfile.readline() == ''

    xfile.close()
    xfile = XRootDPyFile(mkurl(fp), 'w+')

    xfile.write(str2)
    xfile.seek(len(str2)+1)
    xfile.write(str2)
    xfile.seek(0)
    assert xfile.readline() == str2
    assert xfile.readline() == u'\x00'+str2
def test_readline(tmppath):
    """Tests for readline()."""
    fd = get_mltl_file(tmppath)
    fb = get_copy_file(fd)
    fp, fc = fd["full_path"], fd["contents"]
    fp2 = fb["full_path"]

    xfile, pfile = XRootDPyFile(mkurl(fp), "r"), opener.open(fp2, "r")

    assert xfile.readline() == pfile.readline()
    assert xfile.readline() == pfile.readline()
    assert xfile.readline() == pfile.readline()

    xfile.close(), pfile.close()
    xfile, pfile = XRootDPyFile(mkurl(fp), "r"), opener.open(fp2, "r")
    assert xfile.readline() == pfile.readline()
    xfile.seek(0), pfile.seek(0)
    assert xfile.readline() == pfile.readline()
    assert xfile.tell(), pfile.tell()

    xfile.close(), pfile.close()
    xfile = XRootDPyFile(mkurl(fp), "w+")

    str1 = "hello\n"
    str2 = "bye\n"

    xfile.write(str1 + str2)
    xfile.seek(0)
    assert xfile.readline() == str1
    assert xfile.readline() == str2
    assert xfile.readline() == ""
    assert xfile.readline() == ""

    xfile.seek(100)
    assert xfile.readline() == ""

    xfile.close()
    xfile = XRootDPyFile(mkurl(fp), "w+")

    xfile.write(str2)
    xfile.seek(len(str2) + 1)
    xfile.write(str2)
    xfile.seek(0)
    assert xfile.readline() == str2
    assert xfile.readline() == u"\x00" + str2
Example #47
0
def test_tell_after_open(tmppath):
    """Tests for tell's init values in the various file modes."""
    fd = get_tsta_file(tmppath)
    full_path, fc = fd['full_path'], fd['contents']

    xfile = XRootDPyFile(mkurl(full_path), 'r')
    assert xfile.tell() == 0
    xfile.close()

    xfile = XRootDPyFile(mkurl(full_path), 'r+')
    assert xfile.tell() == 0
    xfile.close()

    xfile = XRootDPyFile(mkurl(full_path), 'r-')
    assert xfile.tell() == 0
    xfile.close()

    xfile = XRootDPyFile(mkurl(full_path), 'a')
    assert xfile.tell() == len(fc)
    xfile.close()

    xfile = XRootDPyFile(mkurl(full_path), 'a+')
    assert xfile.tell() == len(fc)
    xfile.close()

    xfile = XRootDPyFile(mkurl(full_path), 'w')
    assert xfile.tell() == 0
    xfile.close()

    xfile = XRootDPyFile(mkurl(full_path), 'w-')
    assert xfile.tell() == 0
    xfile.close()
Example #48
0
def test_read_errors(tmppath):
    fd = get_tsta_file(tmppath)
    fp, fc = fd['full_path'], fd['contents']
    xfile = XRootDPyFile(mkurl(fp), 'r')
    xfile.close()
    pytest.raises(ValueError, xfile.read)
Example #49
0
def test_read_errors(tmppath):
    fd = get_tsta_file(tmppath)
    fp, fc = fd['full_path'], fd['contents']
    xfile = XRootDPyFile(mkurl(fp), 'r')
    xfile.close()
    pytest.raises(ValueError, xfile.read)
def test_tell_after_open(tmppath):
    """Tests for tell's init values in the various file modes."""
    fd = get_tsta_file(tmppath)
    full_path, fc = fd["full_path"], fd["contents"]

    xfile = XRootDPyFile(mkurl(full_path), "r")
    assert xfile.tell() == 0
    xfile.close()

    xfile = XRootDPyFile(mkurl(full_path), "r+")
    assert xfile.tell() == 0
    xfile.close()

    xfile = XRootDPyFile(mkurl(full_path), "r-")
    assert xfile.tell() == 0
    xfile.close()

    xfile = XRootDPyFile(mkurl(full_path), "a")
    assert xfile.tell() == len(fc)
    xfile.close()

    xfile = XRootDPyFile(mkurl(full_path), "a+")
    assert xfile.tell() == len(fc)
    xfile.close()

    xfile = XRootDPyFile(mkurl(full_path), "w")
    assert xfile.tell() == 0
    xfile.close()

    xfile = XRootDPyFile(mkurl(full_path), "w-")
    assert xfile.tell() == 0
    xfile.close()
def test_read_errors(tmppath):
    fd = get_tsta_file(tmppath)
    fp, fc = fd["full_path"], fd["contents"]
    xfile = XRootDPyFile(mkurl(fp), "r")
    xfile.close()
    pytest.raises(ValueError, xfile.read)
Example #52
0
def test_tell_after_open(tmppath):
    """Tests for tell's init values in the various file modes."""
    fd = get_tsta_file(tmppath)
    full_path, fc = fd['full_path'], fd['contents']

    xfile = XRootDPyFile(mkurl(full_path), 'r')
    assert xfile.tell() == 0
    xfile.close()

    xfile = XRootDPyFile(mkurl(full_path), 'r+')
    assert xfile.tell() == 0
    xfile.close()

    xfile = XRootDPyFile(mkurl(full_path), 'r-')
    assert xfile.tell() == 0
    xfile.close()

    xfile = XRootDPyFile(mkurl(full_path), 'a')
    assert xfile.tell() == len(fc)
    xfile.close()

    xfile = XRootDPyFile(mkurl(full_path), 'a+')
    assert xfile.tell() == len(fc)
    xfile.close()

    xfile = XRootDPyFile(mkurl(full_path), 'w')
    assert xfile.tell() == 0
    xfile.close()

    xfile = XRootDPyFile(mkurl(full_path), 'w-')
    assert xfile.tell() == 0
    xfile.close()