Skip to content

Commit

Permalink
Fix error checking and add missing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tiltingpenguin committed Oct 20, 2023
1 parent 8262ef5 commit 815c8be
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
4 changes: 3 additions & 1 deletion bindings/python3/docs/python-libeconf.3
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ level margin: \\n[rst2man-indent\\n[rst2man-indent-level]]
.\" new: \\n[rst2man-indent\\n[rst2man-indent-level]]
.in \\n[rst2man-indent\\n[rst2man-indent-level]]u
..
.TH "PYTHON-LIBECONF" "3" "Oct 19, 2023" "" "python-libeconf"
.TH "PYTHON-LIBECONF" "3" "Oct 20, 2023" "" "python-libeconf"
.sp
\fBpython\-libeconf\fP is a Python Library which offers Python bindings for
\fI\%libeconf\fP\&.
Expand Down Expand Up @@ -779,6 +779,8 @@ Nothing
.TP
.B econf.free_file(ef: EconfFile)
Free the memory of a given keyfile
.sp
This function is called automatically at the end of every objects lifetime and should not be used otherwise
.INDENT 7.0
.TP
.B Parameters
Expand Down
6 changes: 5 additions & 1 deletion bindings/python3/econf.py
Original file line number Diff line number Diff line change
Expand Up @@ -953,16 +953,20 @@ def err_location() -> Tuple[str, int]:
c_filename = c_char_p()
c_line_nr = c_uint64()
LIBECONF.econf_errLocation(byref(c_filename), byref(c_line_nr))
return c_filename.value, c_line_nr.value
return c_filename.value.decode("utf-8"), c_line_nr.value


def free_file(ef: EconfFile):
"""
Free the memory of a given keyfile
This function is called automatically at the end of every objects lifetime and should not be used otherwise
:param ef: EconfFile to be freed
:return: None
"""
if not isinstance(ef, EconfFile):
raise TypeError("Parameter must be an EconfFile object")
if not ef._ptr:
return
LIBECONF.econf_freeFile(ef._ptr)
Expand Down
48 changes: 48 additions & 0 deletions bindings/python3/test/test_econf.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,3 +287,51 @@ def test_write_file(tmp_path):
result = econf.write_file(FILE, d, name)

assert (tmp_path / "example.conf").exists()


@pytest.mark.parametrize(
"context,value,expected",
[
(does_not_raise(), 0, "Success"),
(does_not_raise(), 5, "Key not found"),
(does_not_raise(), 23, "Unknown libeconf error 23"),
(pytest.raises(TypeError), "", "")
]
)
def test_err_string(context, value, expected):
with context:
result = econf.err_string(value)

assert result == expected


def test_err_location():
file, line = econf.err_location()

assert isinstance(file, str)
assert isinstance(line, int)


@pytest.mark.parametrize(
"file,context",
[
#(FILE, does_not_raise()),
(econf.EconfFile(c_void_p(None)), does_not_raise()),
(5, pytest.raises(TypeError))
]
)
def test_free_file(file, context):
with context:
econf.free_file(file)

@pytest.mark.parametrize(
"context,list",
[
(does_not_raise(), ["/", "/conf.d/", None]),
(does_not_raise(), []),
(pytest.raises(TypeError), "")
]
)
def test_set_conf_dirs(context, list):
with context:
econf.set_conf_dirs(list)

0 comments on commit 815c8be

Please sign in to comment.