Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ Issues are labelled `v1` and `v2`, correspondingly.
To install the dev dependencies, run `pip install -e .[test,lint,docs]` from within the cloned repository. Then:

- To test your code, run `pytest .`
- To lint your code (required for CI/CD to pass), run: `black bibtexparser tests docs && isort bibtexparser tests docs --profile black
- To lint your code (required for CI/CD to pass), run: `black bibtexparser tests docs && isort bibtexparser tests docs --profile black`
- To build and preview the docs, navigate into `docs` and run `make html`. Then open the `index.html` file in the `docs/build/html` folder.
4 changes: 3 additions & 1 deletion bibtexparser/entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,20 @@ def parse_file(
path: str,
parse_stack: Optional[Iterable[Middleware]] = None,
append_middleware: Optional[Iterable[Middleware]] = None,
encoding: str = "UTF-8",
) -> Library:
"""Parse a BibTeX file

Args:
path (str): Path to BibTeX file
parse_stack (Optional[Iterable[Middleware]], optional): List of middleware to apply to the database after splitting. If None (default), a default stack will be used providing simple standard functionality will be used.
append_middleware (Optional[Iterable[Middleware]], optional): List of middleware to append to the default stack (ignored if a not-None parse_stack is passed).
encoding: Encoding of the .bib file. Default encoding is "UTF-8".

Returns:
Library: Parsed BibTeX library
"""
with open(path) as f:
with open(path, encoding=encoding) as f:
bibtex_str = f.read()
return parse_string(
bibtex_str, parse_stack=parse_stack, append_middleware=append_middleware
Expand Down
6 changes: 6 additions & 0 deletions tests/gbk_test.bib
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@ARTICLE{¿­Èö2013,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move this file into a (new) tests/resources folder.

author = {¿­Èö},
title = {Test Title},
year = {2013},
journal = {²âÊÔÆÚ¿¯}
}
17 changes: 17 additions & 0 deletions tests/test_encoding.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"""Testing the parse_file function."""
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we re-name this file to test_entrypoint and the module docstring accordingly - there's likely to be added more tests to it eventually.


import pytest

from bibtexparser import parse_file, writer


def test_gbk():
library = parse_file("tests/gbk_test.bib", encoding="gbk")
lines = writer.write(library).splitlines()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest not making the assertion on the "re-written" bibtex, but on the parsed entities instead.

e.g. assert library.entries[0]["author"] == ...

That way, we can keep the test independent of the writer.

assert len(lines) == 6
assert lines[0] == "@article{凯撒2013,"
assert lines[1] == "\tauthor = 凯撒,"
assert lines[2] == "\ttitle = Test Title,"
assert lines[3] == "\tyear = 2013,"
assert lines[4] == "\tjournal = 测试期刊"
assert lines[5] == "}"