Coverage for tests / scripts / test_github_comment_utilities_encode.py: 100%
49 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 json_encode_body.py script.
4Tests the functionality of json_encode_body.py utility used by
5ci-post-pr-comment.sh.
7Google-style docstrings are used per project standards.
8"""
10from __future__ import annotations
12import json
13import subprocess
14from pathlib import Path
16import pytest
17from assertpy import assert_that
20@pytest.fixture
21def json_encode_script_path() -> Path:
22 """Get path to json_encode_body.py script.
24 Returns:
25 Path: Absolute path to the script.
26 """
27 return (
28 Path(__file__).parent.parent.parent
29 / "scripts"
30 / "utils"
31 / "json_encode_body.py"
32 )
35@pytest.fixture
36def sample_data_dir() -> Path:
37 """Get path to test_samples directory.
39 Returns:
40 Path: Absolute path to test_samples directory.
41 """
42 return Path(__file__).parent.parent.parent / "test_samples"
45# Tests for json_encode_body.py
48def test_json_encode_simple_body(
49 json_encode_script_path: Path,
50 sample_data_dir: Path,
51) -> None:
52 """Test JSON encoding of a simple comment body.
54 Args:
55 json_encode_script_path: Path to the json_encode_body.py script.
56 sample_data_dir: Path to the test_samples directory.
57 """
58 input_file = (
59 sample_data_dir / "fixtures" / "pr_comments" / "comment_body_simple.txt"
60 )
62 # Execute the script
63 result = subprocess.run(
64 ["python3", str(json_encode_script_path), str(input_file)],
65 capture_output=True,
66 text=True,
67 check=True,
68 )
70 # Parse the output as JSON
71 output_data = json.loads(result.stdout.strip())
73 # Verify the expected structure
74 assert_that(output_data).has_body(
75 "This is a simple comment body.\nIt has multiple lines.\n",
76 )
77 assert_that(len(output_data)).is_equal_to(1) # Should only have 'body' key
80def test_json_encode_special_chars(
81 json_encode_script_path: Path,
82 sample_data_dir: Path,
83) -> None:
84 """Test JSON encoding of comment body with special characters.
86 Args:
87 json_encode_script_path: Path to the json_encode_body.py script.
88 sample_data_dir: Path to the test_samples directory.
89 """
90 input_file = (
91 sample_data_dir / "fixtures" / "pr_comments" / "comment_body_with_quotes.txt"
92 )
94 # Execute the script
95 result = subprocess.run(
96 ["python3", str(json_encode_script_path), str(input_file)],
97 capture_output=True,
98 text=True,
99 check=True,
100 )
102 # Parse the output as JSON
103 output_data = json.loads(result.stdout.strip())
105 # Verify the expected structure
106 expected_body = (
107 "This comment has \"double quotes\" and 'single quotes'.\n"
108 "It also has special chars like <>&.\n"
109 )
110 assert_that(output_data).has_body(expected_body)
111 assert_that(len(output_data)).is_equal_to(1) # Should only have 'body' key
114def test_json_encode_from_stdin(json_encode_script_path: Path) -> None:
115 """Test JSON encoding when reading from stdin.
117 Args:
118 json_encode_script_path: Path to the json_encode_body.py script.
119 """
120 test_body = "This is a test body from stdin.\nWith multiple lines."
122 # Execute the script with input from stdin
123 result = subprocess.run(
124 ["python3", str(json_encode_script_path)],
125 input=test_body,
126 capture_output=True,
127 text=True,
128 check=True,
129 )
131 # Parse the output as JSON
132 output_data = json.loads(result.stdout.strip())
134 # Verify the expected structure
135 assert_that(output_data).has_body(test_body)
136 assert_that(len(output_data)).is_equal_to(1) # Should only have 'body' key
139def test_json_encode_nonexistent_file(json_encode_script_path: Path) -> None:
140 """Test handling when input file does not exist.
142 Args:
143 json_encode_script_path: Path to the json_encode_body.py script.
144 """
145 nonexistent_file = "/tmp/nonexistent_file.txt"
147 # Execute the script - should exit with code 1
148 result = subprocess.run(
149 ["python3", str(json_encode_script_path), nonexistent_file],
150 capture_output=True,
151 text=True,
152 )
154 # Should exit with code 1
155 assert_that(result.returncode).is_equal_to(1)
157 # Should output nothing to stdout
158 assert_that(result.stdout.strip()).is_empty()
160 # Should have error message in stderr
161 assert_that(result.stderr.strip()).contains("Error reading file")
164def test_json_encode_empty_body(json_encode_script_path: Path) -> None:
165 """Test JSON encoding of an empty comment body.
167 Args:
168 json_encode_script_path: Path to the json_encode_body.py script.
169 """
170 # Create a temporary empty file
171 import os
172 import tempfile
174 with tempfile.NamedTemporaryFile(mode="w", suffix=".txt", delete=False) as f:
175 f.write("")
176 empty_file = f.name
178 try:
179 # Execute the script
180 result = subprocess.run(
181 ["python3", str(json_encode_script_path), empty_file],
182 capture_output=True,
183 text=True,
184 check=True,
185 )
187 # Parse the output as JSON
188 output_data = json.loads(result.stdout.strip())
190 # Verify the expected structure
191 assert_that(output_data).has_body("")
192 assert_that(len(output_data)).is_equal_to(1) # Should only have 'body' key
194 finally:
195 os.unlink(empty_file)