Coverage for tests / scripts / test_github_comment_utilities_extract.py: 100%
41 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#!/usr/bin/env python3
2"""Tests for GitHub comment utility extract_comment_body.py script.
4Tests the functionality of extract_comment_body.py utility used by
5ci-post-pr-comment.sh.
7Google-style docstrings are used per project standards.
8"""
10from __future__ import annotations
12import subprocess
13from pathlib import Path
15import pytest
16from assertpy import assert_that
19@pytest.fixture
20def extract_comment_script_path() -> Path:
21 """Get path to extract_comment_body.py script.
23 Returns:
24 Path: Absolute path to the script.
25 """
26 return (
27 Path(__file__).parent.parent.parent
28 / "scripts"
29 / "utils"
30 / "extract_comment_body.py"
31 )
34@pytest.fixture
35def sample_data_dir() -> Path:
36 """Get path to test_samples directory.
38 Returns:
39 Path: Absolute path to test_samples directory.
40 """
41 return Path(__file__).parent.parent.parent / "test_samples"
44# Tests for extract_comment_body.py
47def test_extract_comment_body_success(
48 extract_comment_script_path: Path,
49 sample_data_dir: Path,
50) -> None:
51 """Test successful comment body extraction.
53 Args:
54 extract_comment_script_path: Path to the extract_comment_body.py script.
55 sample_data_dir: Path to the test_samples directory.
56 """
57 input_file = (
58 sample_data_dir / "fixtures" / "github" / "github_comments_with_marker.json"
59 )
61 # Execute the script with JSON via stdin
62 with open(input_file) as f:
63 result = subprocess.run(
64 ["python3", str(extract_comment_script_path), "1234567890"],
65 input=f.read(),
66 capture_output=True,
67 text=True,
68 check=True,
69 )
71 # Should output the comment body
72 expected_body = (
73 "## Lintro Report\n\n### Summary\n- Total issues: 42\n<!-- lintro-report -->"
74 )
75 assert_that(result.stdout.strip()).is_equal_to(expected_body)
78def test_extract_comment_body_paginated(
79 extract_comment_script_path: Path,
80 sample_data_dir: Path,
81) -> None:
82 """Test comment body extraction from paginated results.
84 Args:
85 extract_comment_script_path: Path to the extract_comment_body.py script.
86 sample_data_dir: Path to the test_samples directory.
87 """
88 input_file = (
89 sample_data_dir / "fixtures" / "github" / "github_comments_paginated.json"
90 )
92 # Execute the script with JSON via stdin
93 with open(input_file) as f:
94 result = subprocess.run(
95 ["python3", str(extract_comment_script_path), "1234567892"],
96 input=f.read(),
97 capture_output=True,
98 text=True,
99 check=True,
100 )
102 # Should output the comment body
103 expected_body = (
104 "## Lintro Report (Page 2)\n\n### Summary\n- Total issues: 15\n"
105 "<!-- lintro-report -->"
106 )
107 assert_that(result.stdout.strip()).is_equal_to(expected_body)
110def test_extract_comment_body_not_found(
111 extract_comment_script_path: Path,
112 sample_data_dir: Path,
113) -> None:
114 """Test when the specified comment ID is not found.
116 Args:
117 extract_comment_script_path: Path to the extract_comment_body.py script.
118 sample_data_dir: Path to the test_samples directory.
119 """
120 input_file = (
121 sample_data_dir / "fixtures" / "github" / "github_comments_with_marker.json"
122 )
124 # Execute the script with non-existent comment ID via stdin
125 with open(input_file) as f:
126 result = subprocess.run(
127 ["python3", str(extract_comment_script_path), "9999999999"],
128 input=f.read(),
129 capture_output=True,
130 text=True,
131 )
133 # Should exit with code 0 but output empty body
134 assert_that(result.returncode).is_equal_to(0)
136 # Should output nothing to stdout
137 assert_that(result.stdout.strip()).is_empty()
140def test_extract_comment_body_invalid_json(extract_comment_script_path: Path) -> None:
141 """Test handling of invalid JSON input for extract_comment_body.py.
143 Args:
144 extract_comment_script_path: Path to the extract_comment_body.py script.
145 """
146 # Create a temporary file with invalid JSON
147 import os
148 import tempfile
150 with tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False) as f:
151 f.write('{"invalid": json}')
152 invalid_json_file = f.name
154 try:
155 # Execute the script with invalid JSON via stdin
156 with open(invalid_json_file) as f:
157 result = subprocess.run(
158 ["python3", str(extract_comment_script_path), "1234567890"],
159 input=f.read(),
160 capture_output=True,
161 text=True,
162 )
164 # Should exit with code 0 but output empty body
165 # (invalid JSON handled gracefully)
166 assert_that(result.returncode).is_equal_to(0)
168 # Should output nothing to stdout
169 assert_that(result.stdout.strip()).is_empty()
171 finally:
172 os.unlink(invalid_json_file)