Coverage for tests / scripts / test_delete_previous_lintro_comments.py: 96%
46 statements
« prev ^ index » next coverage.py v7.13.0, created at 2026-04-03 18:53 +0000
« prev ^ index » next coverage.py v7.13.0, created at 2026-04-03 18:53 +0000
1"""Tests for the delete-previous-lintro-comments.py script.
3This module tests that the script correctly deletes only comments containing the marker.
4"""
6import importlib.util
7import sys
8from pathlib import Path
9from types import ModuleType
10from typing import Any
11from unittest import mock
13import pytest
14from assertpy import assert_that
16script_path = (
17 Path(__file__).parent.parent.parent
18 / "scripts"
19 / "utils"
20 / "delete-previous-lintro-comments.py"
21)
22spec = importlib.util.spec_from_file_location("del_script", str(script_path))
23if spec is None or spec.loader is None:
24 raise ImportError("Failed to load delete-previous-lintro-comments.py script")
25del_script: ModuleType = importlib.util.module_from_spec(spec)
26sys.modules["del_script"] = del_script
27spec.loader.exec_module(del_script)
30@pytest.fixture(autouse=True)
31def patch_env(monkeypatch: pytest.MonkeyPatch) -> None:
32 """Patch environment variables for the script.
34 Args:
35 monkeypatch: Pytest fixture for monkeypatching.
36 """
37 monkeypatch.setenv("GITHUB_TOKEN", "dummy-token")
38 monkeypatch.setenv("GITHUB_REPOSITORY", "owner/repo")
39 monkeypatch.setenv("PR_NUMBER", "123")
42def test_deletes_only_marker_comments(monkeypatch: pytest.MonkeyPatch) -> None:
43 """Test that only comments with the marker are deleted.
45 Args:
46 monkeypatch: Pytest fixture for monkeypatching.
47 """
48 comments: list[dict[str, Any]] = [
49 {"id": 1, "body": "Hello world"},
50 {"id": 2, "body": "<!-- lintro-report --> Lint results"},
51 {"id": 3, "body": "<!-- lintro-report --> Another lint comment"},
52 {"id": 4, "body": "Unrelated comment"},
53 ]
54 deleted: list[int] = []
56 def mock_get_pr_comments(
57 repo: str,
58 pr_number: str,
59 token: str,
60 ) -> list[dict[str, Any]]:
61 return comments
63 def mock_delete_comment(repo: str, comment_id: int, token: str) -> None:
64 deleted.append(comment_id)
66 monkeypatch.setattr(del_script, "get_pr_comments", mock_get_pr_comments)
67 monkeypatch.setattr(del_script, "delete_comment", mock_delete_comment)
68 import sys
70 monkeypatch.setattr(
71 sys,
72 "argv",
73 ["delete-previous-lintro-comments.py", "<!-- lintro-report -->"],
74 )
75 with mock.patch("sys.stdout", new_callable=lambda: sys.__stdout__):
76 del_script.main()
77 assert_that(set(deleted)).is_equal_to({2, 3})
80def test_no_marker_comments(monkeypatch: pytest.MonkeyPatch) -> None:
81 """Test that script prints message if no marker comments are found.
83 Args:
84 monkeypatch: Pytest fixture for monkeypatching.
85 """
86 comments: list[dict[str, Any]] = [
87 {"id": 1, "body": "Hello world"},
88 {"id": 4, "body": "Unrelated comment"},
89 ]
90 deleted: list[int] = []
92 def mock_get_pr_comments(
93 repo: str,
94 pr_number: str,
95 token: str,
96 ) -> list[dict[str, Any]]:
97 return comments
99 def mock_delete_comment(repo: str, comment_id: int, token: str) -> None:
100 deleted.append(comment_id)
102 monkeypatch.setattr(del_script, "get_pr_comments", mock_get_pr_comments)
103 monkeypatch.setattr(del_script, "delete_comment", mock_delete_comment)
104 with mock.patch("sys.stdout", new_callable=lambda: sys.__stdout__):
105 del_script.main()
106 assert_that(deleted).is_equal_to([])