File: test_errors.py

package info (click to toggle)
python-libarchive-c 5.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 908 kB
  • sloc: python: 1,552; makefile: 7
file content (40 lines) | stat: -rw-r--r-- 1,206 bytes parent folder | download
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
from errno import ENOENT

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 == ENOENT
        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)