Coverage for tests / scripts / test_github_comment_utilities_find.py: 100%

49 statements  

« 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 find_comment_with_marker.py script. 

3 

4Tests the functionality of find_comment_with_marker.py utility used by 

5ci-post-pr-comment.sh. 

6 

7Google-style docstrings are used per project standards. 

8""" 

9 

10from __future__ import annotations 

11 

12import json 

13import subprocess 

14from pathlib import Path 

15 

16import pytest 

17from assertpy import assert_that 

18 

19 

20@pytest.fixture 

21def find_comment_script_path() -> Path: 

22 """Get path to find_comment_with_marker.py script. 

23 

24 Returns: 

25 Path: Absolute path to the script. 

26 """ 

27 return ( 

28 Path(__file__).parent.parent.parent 

29 / "scripts" 

30 / "utils" 

31 / "find_comment_with_marker.py" 

32 ) 

33 

34 

35@pytest.fixture 

36def sample_data_dir() -> Path: 

37 """Get path to test_samples directory. 

38 

39 Returns: 

40 Path: Absolute path to test_samples directory. 

41 """ 

42 return Path(__file__).parent.parent.parent / "test_samples" 

43 

44 

45# Tests for find_comment_with_marker.py 

46 

47 

48def test_find_comment_with_marker_success( 

49 find_comment_script_path: Path, 

50 sample_data_dir: Path, 

51) -> None: 

52 """Test successful comment finding with marker. 

53 

54 Args: 

55 find_comment_script_path: Path to the find_comment_with_marker.py script. 

56 sample_data_dir: Path to the test_samples directory. 

57 """ 

58 input_file = ( 

59 sample_data_dir / "fixtures" / "github" / "github_comments_with_marker.json" 

60 ) 

61 

62 # Execute the script with JSON via stdin 

63 with open(input_file) as f: 

64 result = subprocess.run( 

65 ["python3", str(find_comment_script_path), "<!-- lintro-report -->"], 

66 input=f.read(), 

67 capture_output=True, 

68 text=True, 

69 check=True, 

70 ) 

71 

72 # The script outputs just the comment ID 

73 output_id = result.stdout.strip() 

74 

75 # Verify the expected ID 

76 assert_that(output_id).is_equal_to("1234567890") 

77 

78 

79def test_find_comment_with_marker_paginated( 

80 find_comment_script_path: Path, 

81 sample_data_dir: Path, 

82) -> None: 

83 """Test comment finding with marker in paginated results. 

84 

85 Args: 

86 find_comment_script_path: Path to the find_comment_with_marker.py script. 

87 sample_data_dir: Path to the test_samples directory. 

88 """ 

89 input_file = ( 

90 sample_data_dir / "fixtures" / "github" / "github_comments_paginated.json" 

91 ) 

92 

93 # Execute the script with JSON via stdin 

94 with open(input_file) as f: 

95 result = subprocess.run( 

96 ["python3", str(find_comment_script_path), "<!-- lintro-report -->"], 

97 input=f.read(), 

98 capture_output=True, 

99 text=True, 

100 check=True, 

101 ) 

102 

103 # Parse the output as JSON 

104 json.loads(result.stdout.strip()) 

105 

106 # The script outputs just the comment ID 

107 output_id = result.stdout.strip() 

108 

109 # Verify the expected ID 

110 assert_that(output_id).is_equal_to("1234567892") 

111 

112 

113def test_find_comment_no_marker_found( 

114 find_comment_script_path: Path, 

115 sample_data_dir: Path, 

116) -> None: 

117 """Test when no comment with the specified marker is found. 

118 

119 Args: 

120 find_comment_script_path: Path to the find_comment_with_marker.py script. 

121 sample_data_dir: Path to the test_samples directory. 

122 """ 

123 input_file = ( 

124 sample_data_dir / "fixtures" / "github" / "github_comments_no_marker.json" 

125 ) 

126 

127 # Execute the script with JSON via stdin - should exit with code 1 

128 with open(input_file) as f: 

129 result = subprocess.run( 

130 ["python3", str(find_comment_script_path), "<!-- lintro-report -->"], 

131 input=f.read(), 

132 capture_output=True, 

133 text=True, 

134 ) 

135 

136 # Should exit with code 0 and output empty string when no marker found 

137 assert_that(result.returncode).is_equal_to(0) 

138 

139 # Should output empty string to stdout 

140 assert_that(result.stdout.strip()).is_empty() 

141 

142 

143def test_find_comment_invalid_json(find_comment_script_path: Path) -> None: 

144 """Test handling of invalid JSON input. 

145 

146 Args: 

147 find_comment_script_path: Path to the find_comment_with_marker.py script. 

148 """ 

149 # Create a temporary file with invalid JSON 

150 import os 

151 import tempfile 

152 

153 with tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False) as f: 

154 f.write('{"invalid": json}') 

155 invalid_json_file = f.name 

156 

157 try: 

158 # Execute the script with invalid JSON via stdin - should exit with code 1 

159 with open(invalid_json_file) as f: 

160 result = subprocess.run( 

161 ["python3", str(find_comment_script_path), "<!-- lintro-report -->"], 

162 input=f.read(), 

163 capture_output=True, 

164 text=True, 

165 ) 

166 

167 # Should exit with code 0 and output empty (invalid JSON handled gracefully) 

168 assert_that(result.returncode).is_equal_to(0) 

169 

170 # Should output nothing to stdout 

171 assert_that(result.stdout.strip()).is_empty() 

172 

173 finally: 

174 os.unlink(invalid_json_file) 

175 

176 

177def test_find_comment_empty_marker( 

178 find_comment_script_path: Path, 

179 sample_data_dir: Path, 

180) -> None: 

181 """Test behavior with an empty marker. 

182 

183 Args: 

184 find_comment_script_path: Path to the find_comment_with_marker.py script. 

185 sample_data_dir: Path to the test_samples directory. 

186 """ 

187 input_file = ( 

188 sample_data_dir / "fixtures" / "github" / "github_comments_with_marker.json" 

189 ) 

190 

191 # Execute the script with empty marker via stdin 

192 with open(input_file) as f: 

193 result = subprocess.run( 

194 ["python3", str(find_comment_script_path), ""], 

195 input=f.read(), 

196 capture_output=True, 

197 text=True, 

198 ) 

199 

200 # Should exit with code 0 and output empty (empty marker handled gracefully) 

201 assert_that(result.returncode).is_equal_to(0) 

202 

203 # Should output nothing to stdout 

204 assert_that(result.stdout.strip()).is_empty()