1from __future__ import division, print_function, unicode_literals
2
3from errno import ENOENT
4
5import pytest
6
7from libarchive import ArchiveError, ffi, memory_writer
8
9
10def test_add_files_nonexistent():
11    with memory_writer(bytes(bytearray(4096)), 'zip') as archive:
12        with pytest.raises(ArchiveError) as e:
13            archive.add_files('nonexistent')
14        assert e.value.msg
15        assert e.value.errno == ENOENT
16        assert e.value.retcode == -25
17
18
19def test_check_int_logs_warnings(monkeypatch):
20    calls = []
21    monkeypatch.setattr(ffi.logger, 'warning', lambda *_: calls.append(1))
22    archive_p = ffi.write_new()
23    ffi.check_int(ffi.ARCHIVE_WARN, print, [archive_p])
24    assert calls == [1]
25
26
27def test_check_null():
28    with pytest.raises(ArchiveError) as e:
29        ffi.check_null(None, print, [])
30    assert str(e)
31
32
33def test_error_string_decoding(monkeypatch):
34    monkeypatch.setattr(ffi, 'error_string', lambda *_: None)
35    r = ffi._error_string(None)
36    assert r is None
37    monkeypatch.setattr(ffi, 'error_string', lambda *_: b'a')
38    r = ffi._error_string(None)
39    assert isinstance(r, type(''))
40    monkeypatch.setattr(ffi, 'error_string', lambda *_: '\xe9'.encode('utf8'))
41    r = ffi._error_string(None)
42    assert isinstance(r, bytes)
43