|
| 1 | +"""Test that ``gitingest.ingest()`` emits a concise, 5-or-6-line summary.""" |
| 2 | + |
| 3 | +import re |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from gitingest import ingest |
| 9 | + |
| 10 | +REPO = "pallets/flask" |
| 11 | + |
| 12 | +PATH_CASES = [ |
| 13 | + ("tree", "/examples/celery"), |
| 14 | + ("blob", "/examples/celery/make_celery.py"), |
| 15 | +] |
| 16 | + |
| 17 | +REF_CASES = [ |
| 18 | + ("Branch", "main"), |
| 19 | + ("Branch", "stable"), |
| 20 | + ("Tag", "3.0.3"), |
| 21 | + ("Commit", "e9741288637e0d9abe95311247b4842a017f7d5c"), |
| 22 | +] |
| 23 | + |
| 24 | + |
| 25 | +@pytest.mark.parametrize(("path_type", "path"), PATH_CASES) |
| 26 | +@pytest.mark.parametrize(("ref_type", "ref"), REF_CASES) |
| 27 | +def test_ingest_summary(path_type: str, path: str, ref_type: str, ref: str) -> None: |
| 28 | + """Assert that ``gitingest.ingest()`` emits a concise, 5-or-6-line summary. |
| 29 | +
|
| 30 | + - Non-“main” refs → 5 key/value pairs + blank line (6 total). |
| 31 | + - “main” branch → ref line omitted (5 total). |
| 32 | + - Required keys: |
| 33 | + - Repository |
| 34 | + - ``ref_type`` (absent on “main”) |
| 35 | + - File│Subpath (chosen by ``path_type``) |
| 36 | + - Lines│Files analyzed (chosen by ``path_type``) |
| 37 | + - Estimated tokens (positive integer) |
| 38 | +
|
| 39 | + Any missing key, wrong value, or incorrect line count should fail. |
| 40 | +
|
| 41 | + Parameters |
| 42 | + ---------- |
| 43 | + path_type : {"tree", "blob"} |
| 44 | + GitHub object type under test. |
| 45 | + path : str |
| 46 | + The repository sub-path or file path to feed into the URL. |
| 47 | + ref_type : {"Branch", "Tag", "Commit"} |
| 48 | + Label expected on line 2 of the summary (absent if `ref` is "main"). |
| 49 | + ref : str |
| 50 | + Actual branch name, tag, or commit hash. |
| 51 | +
|
| 52 | + """ |
| 53 | + is_main_branch = ref == "main" |
| 54 | + is_blob = path_type == "blob" |
| 55 | + expected_lines = 6 - int(is_main_branch) |
| 56 | + expected_parsed = 5 - int(is_main_branch) |
| 57 | + |
| 58 | + summary, *_ = ingest(f"https://github.com/{REPO}/{path_type}/{ref}{path}") |
| 59 | + lines = summary.splitlines() |
| 60 | + parsed = dict(line.split(": ", 1) for line in lines if ": " in line) |
| 61 | + |
| 62 | + assert parsed["Repository"] == REPO |
| 63 | + |
| 64 | + if is_main_branch: |
| 65 | + # We omit the 'Branch' line for 'main' branches. |
| 66 | + assert ref_type not in parsed |
| 67 | + else: |
| 68 | + assert parsed[ref_type] == ref |
| 69 | + |
| 70 | + if is_blob: |
| 71 | + assert parsed["File"] == Path(path).name |
| 72 | + assert "Lines" in parsed |
| 73 | + else: # 'tree' |
| 74 | + assert parsed["Subpath"] == path |
| 75 | + assert "Files analyzed" in parsed |
| 76 | + |
| 77 | + token_match = re.search(r"\d+", parsed["Estimated tokens"]) |
| 78 | + assert token_match, "'Estimated tokens' should contain a number" |
| 79 | + assert int(token_match.group()) > 0 |
| 80 | + |
| 81 | + assert len(lines) == expected_lines |
| 82 | + assert len(parsed) == expected_parsed |
0 commit comments