File: test_errors.py

package info (click to toggle)
python-libarchive-c 2.1-3.1~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 836 kB
  • sloc: python: 808; makefile: 26; sh: 8
file content (45 lines) | stat: -rw-r--r-- 1,470 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# This file is part of a program licensed under the terms of the GNU Lesser
# General Public License version 2 (or at your option any later version)
# as published by the Free Software Foundation: http://www.gnu.org/licenses/


from __future__ import division, print_function, unicode_literals

import pytest

from libarchive import ArchiveError, ffi, memory_writer


def test_add_files_nonexistent():
    with memory_writer(bytes(bytearray(4096)), 'zip') as archive:
        with pytest.raises(ArchiveError) as e:
            archive.add_files('nonexistent')
        assert e.value.msg
        assert e.value.errno == 2
        assert e.value.retcode == -25


def test_check_int_logs_warnings(monkeypatch):
    calls = []
    monkeypatch.setattr(ffi.logger, 'warning', lambda *_: calls.append(1))
    archive_p = ffi.write_new()
    ffi.check_int(ffi.ARCHIVE_WARN, print, [archive_p])
    assert calls == [1]


def test_check_null():
    with pytest.raises(ArchiveError) as e:
        ffi.check_null(None, print, [])
    assert str(e)


def test_error_string_decoding(monkeypatch):
    monkeypatch.setattr(ffi, 'error_string', lambda *_: None)
    r = ffi._error_string(None)
    assert r is None
    monkeypatch.setattr(ffi, 'error_string', lambda *_: b'a')
    r = ffi._error_string(None)
    assert isinstance(r, type(''))
    monkeypatch.setattr(ffi, 'error_string', lambda *_: '\xe9'.encode('utf8'))
    r = ffi._error_string(None)
    assert isinstance(r, bytes)