Repository navigation
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The existing suite (
tests/unit_test.py, fixture files and doctests) covers loading the bundled examples and load → dump → load round trips. This PR adds pytest coverage for the parts of the public API that had none, with all assertions taken from the library's current behaviour:frontmatter/util.py(previously untested) —u()normalises bytes/str to unicode and CRLF → LF, and stringifies non-string input;is_readable/is_writable/can_openclassification of file-like objects, paths and other valuesparse()semantics — the (metadata, content) tuple, keyword defaults being used and overridden by front matter, non-dict YAML front matter (lists/scalars) being silently ignored, unclosed or non-leading front matter treated as plain text, and a handler whosesplitraisesValueErrorbailing out to defaults + untouched textcheck/checks/detect_format— string inputs, file-like objects, detection order (YAML → JSON → TOML), and unopenable argument typesload/dumpwith an unopenable argument raiseValueError; invalid YAML/JSON/TOML front matter propagates the underlying parser errors instead of being swalloweddatetime.date, quoted numbers staying strings, anchors/aliases, delimiter variants (----, trailing spaces), later---lines staying in the content, export key sorting, and thesort_keys=Falsepassthrough indumpsdumpschanging the output formatPost— the full metadata mapping protocol ([],in, set/delete,get,keys,values),to_dict()returning a copy withcontentadded,str/bytes, and the handler fallback to YAML whenpost.handlerisNoneBaseHandlersubclass (a;;;-delimitedkey = valueformat) driven end to end, registration infrontmatter.handlers, detection precedence, andBaseHandlerraisingNotImplementedErrorwithout a boundaryCurrent-behaviour quirks pinned by tests (not changed):
{"a": 1}) is not detected as front matter, because the JSON handler's boundary only matches a line that is exactly{or}frontmatter.check()on a missing file raisesFileNotFoundErrorinstead of returningFalse---\n{}\n---(YAML),{}plus an extra blank line from the shared template (JSON), and+++\n\n+++(TOML)Post(None).contentis the string"None"(content is coerced withstr())All tests live in new files under the existing
tests/directory; no production code is changed. The TOML tests skip cleanly if the optionaltomlpackage is not installed. No newconftest.pyis needed: the package is installed by the project's test setup (uv sync/ thetestextra, which also providestoml).Verification (Python 3.12, PyYAML + toml installed):