- query language: substrings, phrases, /regex/, AND/OR/NOT (+ UND/ODER/NICHT), parentheses; case-insensitive; ReDoS protection via regex timeouts; syntax errors with stable code and position - SearchEngine over in-memory blocks: filters, predicate, grouping, sorting, paging, time budget, automatic reload, highlighted snippets - Markdown loader with split modes file/heading/list/paragraph - grep-like CLI (mdgrep), JSON output, grep exit codes - Flask blueprint/app with JSON API and widget assets, dev server - framework-free JS widget: live search, URL state, filters, paging, syntax help, en/de labels, CSS custom properties for theming Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
60 lines
1.8 KiB
Python
60 lines
1.8 KiB
Python
import json
|
|
|
|
import pytest
|
|
|
|
from mdgrep.cli import main
|
|
|
|
|
|
@pytest.fixture
|
|
def docs(tmp_path):
|
|
(tmp_path / "a.md").write_text("# A\n\nRust is fast.\n\nGo is simple.\n", encoding="utf-8")
|
|
(tmp_path / "b.md").write_text("- rust crypto wallet\n- python typing\n", encoding="utf-8")
|
|
return tmp_path
|
|
|
|
|
|
def run(capsys, *args):
|
|
code = main([*args, "--color", "never"])
|
|
out, err = capsys.readouterr()
|
|
return code, out, err
|
|
|
|
|
|
def test_match_output_and_exit_code(capsys, docs):
|
|
code, out, _ = run(capsys, "rust -crypto", str(docs))
|
|
assert code == 0
|
|
assert f"{docs / 'a.md'}:3" in out
|
|
assert "Rust is fast." in out
|
|
assert "crypto" not in out
|
|
|
|
|
|
def test_no_match_exit_1(capsys, docs):
|
|
code, out, _ = run(capsys, "haskell", str(docs))
|
|
assert code == 1 and out == ""
|
|
|
|
|
|
def test_syntax_error_exit_2_with_caret(capsys, docs):
|
|
code, _, err = run(capsys, "(rust", str(docs))
|
|
assert code == 2
|
|
assert "Missing closing parenthesis" in err and "^" in err
|
|
|
|
|
|
def test_count_and_files(capsys, docs):
|
|
assert run(capsys, "rust OR python", str(docs))[0] == 0
|
|
_, out, _ = run(capsys, "rust OR python", str(docs), "--split", "list", "-c")
|
|
assert out.strip() == "3"
|
|
_, out, _ = run(capsys, "rust", str(docs), "-l")
|
|
assert out.split() == [str(docs / "a.md"), str(docs / "b.md")]
|
|
|
|
|
|
def test_json_output(capsys, docs):
|
|
code, out, _ = run(capsys, "/fa.t/", str(docs), "--json")
|
|
data = json.loads(out)
|
|
assert code == 0 and data["total_blocks"] == 1
|
|
block = data["results"][0]["block"]
|
|
assert {"t": "fast", "hit": True} in block["segments"]
|
|
assert "markdown" not in block["meta"]
|
|
|
|
|
|
def test_color_highlight(capsys, docs):
|
|
main(["rust", str(docs / "a.md"), "--color", "always"])
|
|
out = capsys.readouterr().out
|
|
assert "\033[1;31mRust\033[0m" in out
|