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, '')
Beispiel #2
0
def test_read_and_write(tmppath):
    """Tests that the XRDFile behaves like a regular python file."""
    fd = get_tsta_file(tmppath)
    fb = get_copy_file(fd)
    fp, fc = fd['full_path'], fd['contents']
    fp2 = fb['full_path']

    seekpoint = len(fc)//2
    writestr = "Come what may in May this day says Ray all gay like Jay"

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

    assert xfile.tell() == pfile.tell()
    assert xfile.read() == pfile.read()
    assert xfile.tell() == pfile.tell()

    xfile.seek(seekpoint), pfile.seek(seekpoint)
    assert xfile.tell() == pfile.tell()
    xfile.write(writestr), pfile.write(writestr)
    assert xfile.tell() == pfile.tell()
    assert xfile.read() == pfile.read()

    xfile.seek(0), pfile.seek(0)
    assert xfile.tell() == pfile.tell()
    assert xfile.read() == pfile.read()
Beispiel #3
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)
Beispiel #4
0
def test_truncate_read_write2(tmppath):
    """Tests behaviour of writing after seek(0) after
       reading after truncating."""
    fd = get_tsta_file(tmppath)
    fb = get_copy_file(fd)
    fp, fc = fd['full_path'], fd['contents']
    fp2 = fb['full_path']

    sp = len(fc)//2
    wstr = "I am the string"

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

    xfile.truncate(sp), pfile.truncate(sp)
    assert xfile.tell() == pfile.tell()
    assert xfile.read() == pfile.read()
    assert xfile.tell() == pfile.tell()

    xfile.seek(0), pfile.seek(0)
    assert xfile.tell() == pfile.tell()
    assert xfile.read() == pfile.read()
    xfile.seek(0), pfile.seek(0)

    xfile.write(wstr), pfile.write(wstr)
    assert xfile.tell() == pfile.tell()
    assert xfile.read() == pfile.read()
    xfile.seek(0), pfile.seek(0)
    assert xfile.read() == pfile.read()
Beispiel #5
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)
Beispiel #6
0
def test_read_and_write(tmppath):
    """Tests that the XRDFile behaves like a regular python file."""
    fd = get_tsta_file(tmppath)
    fb = get_copy_file(fd)
    fp, fc = fd['full_path'], fd['contents']
    fp2 = fb['full_path']

    seekpoint = len(fc)//2
    writestr = "Come what may in May this day says Ray all gay like Jay"

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

    assert xfile.tell() == pfile.tell()
    assert xfile.read() == pfile.read()
    assert xfile.tell() == pfile.tell()

    xfile.seek(seekpoint), pfile.seek(seekpoint)
    assert xfile.tell() == pfile.tell()
    xfile.write(writestr), pfile.write(writestr)
    assert xfile.tell() == pfile.tell()
    assert xfile.read() == pfile.read()

    xfile.seek(0), pfile.seek(0)
    assert xfile.tell() == pfile.tell()
    assert xfile.read() == pfile.read()
Beispiel #7
0
def test_truncate_read_write2(tmppath):
    """Tests behaviour of writing after seek(0) after
       reading after truncating."""
    fd = get_tsta_file(tmppath)
    fb = get_copy_file(fd)
    fp, fc = fd['full_path'], fd['contents']
    fp2 = fb['full_path']

    sp = len(fc)//2
    wstr = "I am the string"

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

    xfile.truncate(sp), pfile.truncate(sp)
    assert xfile.tell() == pfile.tell()
    assert xfile.read() == pfile.read()
    assert xfile.tell() == pfile.tell()

    xfile.seek(0), pfile.seek(0)
    assert xfile.tell() == pfile.tell()
    assert xfile.read() == pfile.read()
    xfile.seek(0), pfile.seek(0)

    xfile.write(wstr), pfile.write(wstr)
    assert xfile.tell() == pfile.tell()
    assert xfile.read() == pfile.read()
    xfile.seek(0), pfile.seek(0)
    assert xfile.read() == pfile.read()
Beispiel #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, '')
Beispiel #9
0
def test_truncate3(tmppath):
    """Test truncate(0 < size < self._size)."""
    fd = get_mltl_file(tmppath)
    full_path, fc = fd['full_path'], fd['contents']
    xfile = XRootDPyFile(mkurl(full_path), 'r+')

    initcp = xfile.tell()

    newsiz = len(fc)//2
    xfile.truncate(newsiz)
    assert xfile.tell() == initcp
    xfile.seek(0)  # reset the internal pointer before reading
    assert xfile.read() == fc[:newsiz]
Beispiel #10
0
def test_truncate3(tmppath):
    """Test truncate(0 < size < self._size)."""
    fd = get_mltl_file(tmppath)
    full_path, fc = fd['full_path'], fd['contents']
    xfile = XRootDPyFile(mkurl(full_path), 'r+')

    initcp = xfile.tell()

    newsiz = len(fc)//2
    xfile.truncate(newsiz)
    assert xfile.tell() == initcp
    xfile.seek(0)  # reset the internal pointer before reading
    assert xfile.read() == fc[:newsiz]
Beispiel #11
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()
Beispiel #12
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()
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()
Beispiel #14
0
def test_init_appendread(tmppath):
    """Test for files opened in mode 'a+'."""
    fd = get_tsta_file(tmppath)
    fp, fc = fd['full_path'], fd['contents']
    xfile = XRootDPyFile(mkurl(fp), 'a+')
    assert xfile.mode == 'a+'
    assert xfile.tell() == len(fc)
    assert xfile.read() == u''

    # 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)
    xfile.seek(0)
    assert xfile.read() == fc + newcont
    xfile.write(fc)
    xfile.seek(0)
    xfile.read() == fc + newcont + fc
Beispiel #15
0
def test_init_appendread(tmppath):
    """Test for files opened in mode 'a+'."""
    fd = get_tsta_file(tmppath)
    fp, fc = fd['full_path'], fd['contents']
    xfile = XRootDPyFile(mkurl(fp), 'a+')
    assert xfile.mode == 'a+'
    assert xfile.tell() == len(fc)
    assert xfile.read() == u''

    # 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)
    xfile.seek(0)
    assert xfile.read() == fc + newcont
    xfile.write(fc)
    xfile.seek(0)
    xfile.read() == fc + newcont + fc
Beispiel #16
0
def test_seek_args(tmppath):
    """Test seek() with a non-default whence argument."""
    fd = get_tsta_file(tmppath)
    fb = get_copy_file(fd)
    full_path, fc = fd['full_path'], fd['contents']

    xfile = XRootDPyFile(mkurl(full_path), 'r+')
    pfile = open(fb['full_path'], 'r+')

    xfile.truncate(3), pfile.truncate(3)
    xfile.seek(2, SEEK_END), pfile.seek(2, SEEK_END)
    assert xfile.tell() == pfile.tell()

    xfile.seek(3, SEEK_CUR), pfile.seek(3, SEEK_CUR)
    assert xfile.tell() == pfile.tell()

    xfile.seek(8, SEEK_SET), pfile.seek(8, SEEK_SET)
    assert xfile.tell() == pfile.tell()

    xfile.truncate(3), pfile.truncate(3)
    xfile.read(), pfile.read()
    assert xfile.tell() == pfile.tell()
    xfile.seek(8, SEEK_END), pfile.seek(8, SEEK_END)
    assert xfile.tell() == pfile.tell()

    xfile.seek(4, SEEK_CUR), pfile.seek(4, SEEK_CUR)
    assert xfile.tell() == pfile.tell()

    pytest.raises(NotImplementedError, xfile.seek, 0, 8)
Beispiel #17
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)
Beispiel #18
0
def test_truncate5(tmppath):
    """Test truncate() (no arg)."""
    fd = get_tsta_file(tmppath)
    fb = get_copy_file(fd)
    fp, fc = fd['full_path'], fd['contents']
    fp2 = fb['full_path']

    xfa = XRootDPyFile(mkurl(fp), 'r+')
    xfb = XRootDPyFile(mkurl(fp2), 'r+')

    acnts = xfa.read()
    assert acnts == xfb.read()

    # internal pointer starts at 0 in all 'r' modes.
    xtell = xfa.tell()
    assert xfa.tell() == xfb.tell()
    # f.truncate() and f.truncate(self.tell()) should be equivalent
    xfa.truncate(), xfb.truncate(xfb.tell())
    assert xfa.size == xfb.size
    assert xfa.tell() == xtell
    assert xfb.tell() == xtell
    assert xfb.read() == u''
    assert xfa.read() == u''

    xfa.seek(0), xfb.seek(0)
    are = xfa.read()
    assert are == fc
    assert are == xfb.read()
Beispiel #19
0
def test_write_and_read(tmppath):
    """Tests that the XRootDPyFile behaves like a regular python file in w+."""
    fd = get_tsta_file(tmppath)
    fb = get_copy_file(fd)
    fp, fc = fd['full_path'], fd['contents']
    fp2 = fb['full_path']

    writestr = "Hello fair mare what fine stairs."
    seekpoint = len(writestr)//2
    # In 'w' (and variant modes) the file's contents are deleted upon opening.

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

    assert xfile.tell() == pfile.tell()
    assert xfile.read() == pfile.read()
    assert xfile.tell() == pfile.tell()

    xfile.write(writestr), pfile.write(writestr)
    assert xfile.tell() == pfile.tell()
    assert xfile.read() == pfile.read()
    xfile.seek(0), pfile.seek(0)
    assert xfile.read() == pfile.read()
    assert xfile.tell() == pfile.tell()

    xfile.seek(seekpoint), pfile.seek(seekpoint)
    assert xfile.tell() == pfile.tell()
    assert xfile.read() == pfile.read()
    assert xfile.tell() == pfile.tell()
Beispiel #20
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)
Beispiel #21
0
def test_write_and_read(tmppath):
    """Tests that the XRootDPyFile behaves like a regular python file in w+."""
    fd = get_tsta_file(tmppath)
    fb = get_copy_file(fd)
    fp, fc = fd['full_path'], fd['contents']
    fp2 = fb['full_path']

    writestr = "Hello fair mare what fine stairs."
    seekpoint = len(writestr)//2
    # In 'w' (and variant modes) the file's contents are deleted upon opening.

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

    assert xfile.tell() == pfile.tell()
    assert xfile.read() == pfile.read()
    assert xfile.tell() == pfile.tell()

    xfile.write(writestr), pfile.write(writestr)
    assert xfile.tell() == pfile.tell()
    assert xfile.read() == pfile.read()
    xfile.seek(0), pfile.seek(0)
    assert xfile.read() == pfile.read()
    assert xfile.tell() == pfile.tell()

    xfile.seek(seekpoint), pfile.seek(seekpoint)
    assert xfile.tell() == pfile.tell()
    assert xfile.read() == pfile.read()
    assert xfile.tell() == pfile.tell()
Beispiel #22
0
def test_seek_args(tmppath):
    """Test seek() with a non-default whence argument."""
    fd = get_tsta_file(tmppath)
    fb = get_copy_file(fd)
    full_path, fc = fd['full_path'], fd['contents']

    xfile = XRootDPyFile(mkurl(full_path), 'r+')
    pfile = open(fb['full_path'], 'r+')

    xfile.truncate(3), pfile.truncate(3)
    xfile.seek(2, SEEK_END), pfile.seek(2, SEEK_END)
    assert xfile.tell() == pfile.tell()

    xfile.seek(3, SEEK_CUR), pfile.seek(3, SEEK_CUR)
    assert xfile.tell() == pfile.tell()

    xfile.seek(8, SEEK_SET), pfile.seek(8, SEEK_SET)
    assert xfile.tell() == pfile.tell()

    xfile.truncate(3), pfile.truncate(3)
    xfile.read(), pfile.read()
    assert xfile.tell() == pfile.tell()
    xfile.seek(8, SEEK_END), pfile.seek(8, SEEK_END)
    assert xfile.tell() == pfile.tell()

    xfile.seek(4, SEEK_CUR), pfile.seek(4, SEEK_CUR)
    assert xfile.tell() == pfile.tell()

    pytest.raises(NotImplementedError, xfile.seek, 0, 8)
Beispiel #23
0
def test_truncate5(tmppath):
    """Test truncate() (no arg)."""
    fd = get_tsta_file(tmppath)
    fb = get_copy_file(fd)
    fp, fc = fd['full_path'], fd['contents']
    fp2 = fb['full_path']

    xfa = XRootDPyFile(mkurl(fp), 'r+')
    xfb = XRootDPyFile(mkurl(fp2), 'r+')

    acnts = xfa.read()
    assert acnts == xfb.read()

    # internal pointer starts at 0 in all 'r' modes.
    xtell = xfa.tell()
    assert xfa.tell() == xfb.tell()
    # f.truncate() and f.truncate(self.tell()) should be equivalent
    xfa.truncate(), xfb.truncate(xfb.tell())
    assert xfa.size == xfb.size
    assert xfa.tell() == xtell
    assert xfb.tell() == xtell
    assert xfb.read() == u''
    assert xfa.read() == u''

    xfa.seek(0), xfb.seek(0)
    are = xfa.read()
    assert are == fc
    assert are == xfb.read()
Beispiel #24
0
def test_seek_and_tell(tmppath):
    """Basic tests for seek() and tell()."""
    fd = get_tsta_file(tmppath)
    full_path, fc = fd['full_path'], fd['contents']
    xfile = XRootDPyFile(mkurl(full_path))
    assert xfile.tell() == 0

    # Read file, then check the internal position pointer.
    conts = xfile.read()
    assert xfile.tell() == len(fc)
    assert conts == fc

    # Seek to beginning, then verify ipp.
    xfile.seek(0)
    assert xfile.tell() == 0
    assert xfile.read() == fc

    newpos = len(fc)//2
    xfile.seek(newpos)
    conts2 = xfile.read()
    assert conts2 == conts[newpos:]
    assert xfile.tell() == len(fc)

    # # Now with a multiline file!
    fd = get_mltl_file(tmppath)
    full_path, fc = fd['full_path'], fd['contents']
    xfile = XRootDPyFile(mkurl(full_path))

    assert xfile.tell() == 0
    newpos = len(fc)//3
    xfile.seek(newpos)
    assert xfile.tell() == newpos
    nconts = xfile.read()
    assert xfile.tell() == len(fc)
    assert nconts == fc[newpos:]

    # Negative offsets raise an error
    pytest.raises(IOError, xfile.seek, -1)

    # floating point offsets are converted to integers
    xfile.seek(1.1)
    assert xfile.tell() == 1
    xfile.seek(0.999999)
    assert xfile.tell() == 0
Beispiel #25
0
def test_seek_and_tell(tmppath):
    """Basic tests for seek() and tell()."""
    fd = get_tsta_file(tmppath)
    full_path, fc = fd['full_path'], fd['contents']
    xfile = XRootDPyFile(mkurl(full_path))
    assert xfile.tell() == 0

    # Read file, then check the internal position pointer.
    conts = xfile.read()
    assert xfile.tell() == len(fc)
    assert conts == fc

    # Seek to beginning, then verify ipp.
    xfile.seek(0)
    assert xfile.tell() == 0
    assert xfile.read() == fc

    newpos = len(fc)//2
    xfile.seek(newpos)
    conts2 = xfile.read()
    assert conts2 == conts[newpos:]
    assert xfile.tell() == len(fc)

    # # Now with a multiline file!
    fd = get_mltl_file(tmppath)
    full_path, fc = fd['full_path'], fd['contents']
    xfile = XRootDPyFile(mkurl(full_path))

    assert xfile.tell() == 0
    newpos = len(fc)//3
    xfile.seek(newpos)
    assert xfile.tell() == newpos
    nconts = xfile.read()
    assert xfile.tell() == len(fc)
    assert nconts == fc[newpos:]

    # Negative offsets raise an error
    pytest.raises(IOError, xfile.seek, -1)

    # floating point offsets are converted to integers
    xfile.seek(1.1)
    assert xfile.tell() == 1
    xfile.seek(0.999999)
    assert xfile.tell() == 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
Beispiel #27
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
Beispiel #28
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
Beispiel #29
0
def test_truncate2(tmppath):
    """Test truncate(self._size)."""
    fd = get_tsta_file(tmppath)
    full_path, fc = fd['full_path'], fd['contents']
    xfile = XRootDPyFile(mkurl(full_path), 'r+')
    conts = xfile.read()
    assert conts == fc

    newsize = xfile.size
    xfile.truncate(newsize)
    assert xfile.tell() == newsize
    assert xfile.size == len(fc)
    xfile.seek(0)
    assert xfile.read() == conts
Beispiel #30
0
def test_truncate2(tmppath):
    """Test truncate(self._size)."""
    fd = get_tsta_file(tmppath)
    full_path, fc = fd['full_path'], fd['contents']
    xfile = XRootDPyFile(mkurl(full_path), 'r+')
    conts = xfile.read()
    assert conts == fc

    newsize = xfile.size
    xfile.truncate(newsize)
    assert xfile.tell() == newsize
    assert xfile.size == len(fc)
    xfile.seek(0)
    assert xfile.read() == conts
def test_seek_past_eof_wr(tmppath):
    """Tests read/write/truncate behaviour after seeking past the EOF, 'w+'"""
    fd = get_tsta_file(tmppath)
    fb = get_copy_file(fd)
    fp, fc = fd['full_path'], u''
    fp2 = fb['full_path']

    wstr = "www"
    eof = len(fc)
    skpnt = len(fc) + 4

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

    xfile.seek(skpnt), pfile.seek(skpnt)
    assert xfile.tell() == pfile.tell()
    assert xfile.read() == pfile.read().encode()
    assert xfile.tell() == pfile.tell()
    assert xfile.tell() == skpnt

    xfile.write(wstr), pfile.write(wstr)
    assert xfile.tell() == pfile.tell()
    xfile.seek(eof), pfile.seek(eof)
    expected = '\x00' * (skpnt - eof) + wstr
    assert xfile.read() == pfile.read().encode() == expected.encode()
    assert xfile.tell() == pfile.tell()

    xfile.seek(0), pfile.seek(0)
    assert xfile.read() == pfile.read().encode()

    xfile.truncate(skpnt), pfile.truncate(skpnt)
    assert xfile.tell() == pfile.tell() == skpnt + len(wstr)

    xfile.write(wstr), pfile.write(wstr)
    expected = fc + '\x00' * (skpnt - eof + len(wstr)) + wstr
    xfile.seek(0), pfile.seek(0)
    assert xfile.read() == pfile.read().encode() == expected.encode()
def test_seek_past_eof_rw(tmppath):
    """Tests read/write/truncate behaviour after seeking past the EOF, 'r+'."""
    fd = get_tsta_file(tmppath)
    fb = get_copy_file(fd)
    fp, fc = fd["full_path"], fd["contents"]
    fp2 = fb["full_path"]

    wstr = "www"
    eof = len(fc)
    skpnt = len(fc) + 4

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

    xfile.seek(skpnt), pfile.seek(skpnt)
    assert xfile.tell() == pfile.tell()
    assert xfile.read() == pfile.read()
    assert xfile.tell() == pfile.tell()
    assert xfile.tell() == skpnt

    xfile.write(wstr), pfile.write(wstr)
    assert xfile.tell() == pfile.tell()
    xfile.seek(eof), pfile.seek(eof)
    assert xfile.read() == pfile.read() == "\x00" * (skpnt - eof) + wstr
    assert xfile.tell() == pfile.tell()

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

    xfile.truncate(skpnt), pfile.truncate(skpnt)
    assert xfile.tell() == pfile.tell() == skpnt + len(wstr)

    xfile.write(wstr), pfile.write(wstr)
    expected = fc + "\x00" * (skpnt - eof + len(wstr)) + wstr
    xfile.seek(0), pfile.seek(0)
    assert xfile.read() == pfile.read() == expected
Beispiel #33
0
def test_seek_past_eof_wr(tmppath):
    """Tests read/write/truncate behaviour after seeking past the EOF, 'w+'"""
    fd = get_tsta_file(tmppath)
    fb = get_copy_file(fd)
    fp, fc = fd['full_path'], u''
    fp2 = fb['full_path']

    wstr = "www"
    eof = len(fc)
    skpnt = len(fc)+4

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

    xfile.seek(skpnt), pfile.seek(skpnt)
    assert xfile.tell() == pfile.tell()
    assert xfile.read() == pfile.read()
    assert xfile.tell() == pfile.tell()
    assert xfile.tell() == skpnt

    xfile.write(wstr), pfile.write(wstr)
    assert xfile.tell() == pfile.tell()
    xfile.seek(eof), pfile.seek(eof)
    assert xfile.read() == pfile.read() == '\x00'*(skpnt-eof) + wstr
    assert xfile.tell() == pfile.tell()

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

    xfile.truncate(skpnt), pfile.truncate(skpnt)
    assert xfile.tell() == pfile.tell() == skpnt + len(wstr)

    xfile.write(wstr), pfile.write(wstr)
    expected = fc + '\x00'*(skpnt-eof+len(wstr)) + wstr
    xfile.seek(0), pfile.seek(0)
    assert xfile.read() == pfile.read() == expected
def test_init_writemode_basic(tmppath):
    # Non-existing file is created.
    fn, fp, fc = "nope", "data/", ""
    full_path = join(tmppath, fp, fn)
    xfile = XRootDPyFile(mkurl(full_path), mode="w+")
    assert xfile
    assert xfile.read() == fc

    # Existing file is truncated
    fd = get_tsta_file(tmppath)
    full_path = fd["full_path"]
    xfile = XRootDPyFile(mkurl(full_path), mode="w+")
    assert xfile
    assert xfile.read() == ""
    assert xfile.size == 0
    assert xfile.tell() == 0
Beispiel #35
0
def test_init_writemode_basic(tmppath):
    # Non-existing file is created.
    fn, fp, fc = 'nope', 'data/', ''
    full_path = join(tmppath, fp, fn)
    xfile = XRootDPyFile(mkurl(full_path), mode='w+')
    assert xfile
    assert xfile.read() == fc

    # Existing file is truncated
    fd = get_tsta_file(tmppath)
    full_path = fd['full_path']
    xfile = XRootDPyFile(mkurl(full_path), mode='w+')
    assert xfile
    assert xfile.read() == ''
    assert xfile.size == 0
    assert xfile.tell() == 0
Beispiel #36
0
def test_init_writemode_basic(tmppath):
    # Non-existing file is created.
    fn, fp, fc = 'nope', 'data/', ''
    full_path = join(tmppath, fp, fn)
    xfile = XRootDPyFile(mkurl(full_path), mode='w+')
    assert xfile
    assert xfile.read() == fc

    # Existing file is truncated
    fd = get_tsta_file(tmppath)
    full_path = fd['full_path']
    xfile = XRootDPyFile(mkurl(full_path), mode='w+')
    assert xfile
    assert xfile.read() == ''
    assert xfile.size == 0
    assert xfile.tell() == 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
Beispiel #38
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_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()
Beispiel #40
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
Beispiel #41
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