Пример #1
0
def test_open_read_raises_if_not_existing(fsx_fake):
    # TODO: split into 2 tests executable on all OSes
    if sys.platform == 'win32':
        with pytest.raises(WindowsError):
            fsx.open('doesnt-exist', 'r').read()
    else:
        with pytest.raises(OSError):
            fsx.open('doesnt-exist', 'r').read()
Пример #2
0
def test_open_append_raises_error_if_parentnode_doesnt_exist(fsx_fake):
    # TODO: split into 2 tests executable on all OSes
    if sys.platform == 'win32':
        with pytest.raises(WindowsError):
            fsx.open('p/f', 'a')
    else:
        with pytest.raises(OSError):
            fsx.open('p/f', 'a')
Пример #3
0
def test_storbinary_creates_file_on_server(fp, server_path, content, fsx_fake):
    fsx_fake.add_dict({'ftp://ftp.python.org': {}, 'src.txt': content})
    ftp = fsx.ftplib.FTP('ftp.python.org')
    ftp.login('user', 'passwd')
    ftp.prot_p()

    ftp.storbinary('STOR ' + fp, fsx.open('src.txt', 'rb'))

    res = fsx.open(server_path, 'r').read()
    assert res == content
Пример #4
0
def test_retrbinary_works(fsdict, file_path, exp, fsx_fake):
    fsx_fake.add_dict(fsdict)
    ftp = fsx.ftplib.FTP('ftp.python.org')
    ftp.login('user', 'passwd')
    ftp.prot_p()

    with fsx.open('C:/tmp/f1', 'wb') as file_:
        ftp.retrbinary('RETR ' + file_path, file_.write)
    res = fsx.open('C:/tmp/f1').read()
    assert res == exp
Пример #5
0
def test_open_append_as_ctxmgr_works(fsx_fake):
    fsx_fake.add_file('f', '1')

    with fsx.open('f', 'a') as file_:
        file_.write('23')

    with fsx.open('f', 'r') as file_:
        res = file_.read()

    assert res == '123'
Пример #6
0
def test_open_write_as_ctxmgr_works(fsx_fake):
    text = 'test'

    with fsx.open('f', 'w') as file_:
        file_.write('123')

    with fsx.open('f', 'r') as file_:
        res = file_.read()

    assert res == '123'
Пример #7
0
    def storbinary(self, command, fp):
        if not self._logged_in:
            raise ftplib.error_perm('530 Please log in with USER and PASS first.')

        parts = command.split(' ', 1)
        if len(parts) == 2 and parts[0] == 'STOR':
            dst_path = _get_abs_url_path(self._url, parts[1])
            try:
                content = fp.read()
                fsx.open(dst_path, 'wb').write(content)
            except EnvironmentError as exc:
                msg = '{exc.__class__.__name__}: {exc}'.format(exc=exc)
                raise ftplib.error_perm(msg)
        else:
            raise NotImplementedError("Commands other than 'STOR' are currently not implemented.")
Пример #8
0
    def test_ZipFile_read_works_on_fake_files_received_an_empty_archive_as_content(
            self, fsx_fake):
        io_obj = io.BytesIO()
        zipfile.ZipFile(io_obj, 'w').close()
        zipcontent = io_obj.getvalue()

        fsx_fake.add_file('arch.zip', zipcontent)
        res = fsx.open('arch.zip', 'rb').read()
        assert res == zipcontent
Пример #9
0
    def retrbinary(self, argument, callback):
        if not self._logged_in:
            raise ftplib.error_perm('530 Please log in with USER and PASS first.')

        parts = argument.split(' ', 1)
        if len(parts) == 2 and parts[0] == 'RETR':
            path = _get_abs_url_path(self._url, parts[1])
            try:
                with fsx.open(path, 'rb') as file_:
                    callback(file_.read())
            except EnvironmentError as exc:
                msg = '{exc.__class__.__name__}: {exc}'.format(exc=exc)
                raise ftplib.error_perm(msg)
        else:
            raise NotImplementedError("Commands other than 'RETR' are currently not implemented.")
Пример #10
0
    def test_write_with_delete_false_can_be_read_after(self, fsx_fake):
        with fsx.tempfile.NamedTemporaryFile(delete=False) as file_:
            file_.write('test')

        assert fsx.open(file_.name, 'r').read() == 'test'
Пример #11
0
    def test_read_works_on_persistent_file(self, fsx_fake):
        file_ = fsx.tempfile.NamedTemporaryFile(delete=False)
        file_.write('test')
        file_.close()

        assert fsx.open(file_.name, 'r').read() == 'test'
Пример #12
0
def test_open_read_works_on_fake_files_which_received_content(fsx_fake):
    fsx_fake.add_file('f', content='test')
    res = fsx.open('f', 'r').read()
    assert res == 'test'
Пример #13
0
def test_open_append_works(fsx_fake):
    fsx_fake.add_file('f', '1')
    fsx.open('f', 'a').write('23')
    res = fsx.open('f', 'r').read()
    assert res == '123'
Пример #14
0
def test_open_append_raises_error_if_parent_directory_doesnt_exist(
        fsx_fake, monkeypatch):
    monkeypatch.setattr('sys.platform', 'win32')
    with pytest.raises(WindowsError):
        fsx.open('d/f', 'a').write('test')
Пример #15
0
def test_open_write_can_be_read_again(fsx_fake):
    text = 'test'
    fsx.open('f', 'w').write(text)
    res = fsx.open('f', 'r').read()
    assert res == text
Пример #16
0
def test_open_write_creates_file(fsx_fake):
    text = 'test'
    fsx.open('f', 'w').write(text)
    assert fsx.exists('f') == True