Skip to content

Fix invalid CSS selector test - #222

Merged
rushter merged 2 commits into
rushter:masterfrom
schoekek:master
Sep 29, 2026
Merged

rushter merged 2 commits into
rushter:masterfrom
schoekek:master

Conversation

@schoekek

Copy link
Copy Markdown
Contributor

Fix invalid CSS selector test

The test_css_selector_invalid_syntax test currently fails because the
test was changed to call root.css("[invalid") without handling the
exception raised by the CSS selector parser.

This change was introduced in commit 54c4818 ("Make ruff linter
happy"), which removed the broad except Exception block to satisfy
Ruff's BLE001 rule. However, root.css("[invalid") is expected to
raise SelectolaxError for invalid CSS syntax.

Use pytest.raises(SelectolaxError) instead of catching Exception.
This keeps the test compatible with Ruff while explicitly testing the
expected exception.

The CSS selector implementation itself is unchanged; this PR only
corrects the test.

@rushter

rushter commented Sep 22, 2026

Copy link
Copy Markdown
Owner

[invalid] is now a valid selector for the most recent lexbor, let's use something like [invalid&] so it fails for both versions.

@schoekek

Copy link
Copy Markdown
Contributor Author

Unfortunately, a simple root.css("[invalid&") doesn't work like that—or what do you think?

=================================== FAILURES ===================================
_______________________ test_css_selector_invalid_syntax _______________________

    def test_css_selector_invalid_syntax():
        html = "<div><p>Test</p></div>"
        parser = LexborHTMLParser(html)
        root = parser.root
        assert root is not None

>       root.css("[invalid&")

tests/test_lexbor.py:771:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
selectolax/lexbor/node.pxi:374: in selectolax.lexbor.LexborNode.css
    return self.parser.selector.find(query, self._get_node())
selectolax/lexbor/selection.pxi:70: in selectolax.lexbor.LexborCSSSelector.find
    cpdef list find(self, str query, LexborNode node):
selectolax/lexbor/selection.pxi:71: in selectolax.lexbor.LexborCSSSelector.find
    return self._find(query, node, 0)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

>   raise SelectolaxError("Can't parse CSS selector.")
E   selectolax.lexbor.SelectolaxError: Can't parse CSS selector.

selectolax/lexbor/selection.pxi:88: SelectolaxError
=========================== short test summary info ============================
FAILED tests/test_lexbor.py::test_css_selector_invalid_syntax - selectolax.le...

Comment thread tests/test_lexbor.py Outdated
@rushter

rushter commented Sep 29, 2026 •

Copy link
Copy Markdown
Owner

Unfortunately, a simple root.css("[invalid&") doesn't work like that—or what do you think?

=================================== FAILURES ===================================
_______________________ test_css_selector_invalid_syntax _______________________

    def test_css_selector_invalid_syntax():
        html = "<div><p>Test</p></div>"
        parser = LexborHTMLParser(html)
        root = parser.root
        assert root is not None

>       root.css("[invalid&")

tests/test_lexbor.py:771:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
selectolax/lexbor/node.pxi:374: in selectolax.lexbor.LexborNode.css
    return self.parser.selector.find(query, self._get_node())
selectolax/lexbor/selection.pxi:70: in selectolax.lexbor.LexborCSSSelector.find
    cpdef list find(self, str query, LexborNode node):
selectolax/lexbor/selection.pxi:71: in selectolax.lexbor.LexborCSSSelector.find
    return self._find(query, node, 0)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

>   raise SelectolaxError("Can't parse CSS selector.")
E   selectolax.lexbor.SelectolaxError: Can't parse CSS selector.

selectolax/lexbor/selection.pxi:88: SelectolaxError
=========================== short test summary info ============================
FAILED tests/test_lexbor.py::test_css_selector_invalid_syntax - selectolax.le...

We just needed to update selector, but keep the exception handling. I've applied the fix. Thanks.

@rushter
rushter merged commit bb1e604 into rushter:master Sep 29, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants