pr_comments_test.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. """Tests for pr_comments.py."""
  2. __copyright__ = """
  3. Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  4. Exceptions. See /LICENSE for license information.
  5. SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. """
  7. import os
  8. import unittest
  9. from unittest import mock
  10. from github_tools import pr_comments
  11. class TestPRComments(unittest.TestCase):
  12. def test_format_comment_short(self):
  13. created_at = "2001-02-03T04:05:06Z"
  14. self.assertEqual(
  15. pr_comments._Comment("author", created_at, "brief").format(False),
  16. " author: brief",
  17. )
  18. self.assertEqual(
  19. pr_comments._Comment("author", created_at, "brief\nwrap").format(
  20. False
  21. ),
  22. " author: brief¶ wrap",
  23. )
  24. self.assertEqual(
  25. pr_comments._Comment(
  26. "author", created_at, "brief\n\n\nwrap"
  27. ).format(False),
  28. " author: brief¶¶¶ wrap",
  29. )
  30. self.assertEqual(
  31. pr_comments._Comment(
  32. "author",
  33. created_at,
  34. "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed "
  35. "do eiusmo",
  36. ).format(False),
  37. " author: Lorem ipsum dolor sit amet, consectetur adipiscing "
  38. "elit, sed do eiusmo",
  39. )
  40. self.assertEqual(
  41. pr_comments._Comment(
  42. "author",
  43. created_at,
  44. "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed "
  45. "do eiusmod",
  46. ).format(False),
  47. " author: Lorem ipsum dolor sit amet, consectetur adipiscing "
  48. "elit, sed do eiu...",
  49. )
  50. def test_format_comment_long(self):
  51. created_at = "2001-02-03T04:05:06Z"
  52. self.assertEqual(
  53. pr_comments._Comment("author", created_at, "brief").format(True),
  54. " author at 2001-02-03 04:05:\n brief",
  55. )
  56. self.assertEqual(
  57. pr_comments._Comment("author", created_at, "brief\nwrap").format(
  58. True
  59. ),
  60. " author at 2001-02-03 04:05:\n brief\n wrap",
  61. )
  62. body = (
  63. "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed "
  64. "do eiusmod tempor incididunt ut labore et dolore magna "
  65. "aliqua.\n"
  66. "Ut enim ad minim veniam,"
  67. )
  68. self.assertEqual(
  69. pr_comments._Comment("author", created_at, body).format(True),
  70. " author at 2001-02-03 04:05:\n"
  71. " Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed "
  72. "do eiusmod\n"
  73. " tempor incididunt ut labore et dolore magna aliqua.\n"
  74. " Ut enim ad minim veniam,",
  75. )
  76. @staticmethod
  77. def fake_thread(**kwargs):
  78. with mock.patch.dict(os.environ, {}):
  79. parsed_args = pr_comments._parse_args(["83"])
  80. return pr_comments._Thread(
  81. parsed_args, TestPRComments.fake_thread_dict(**kwargs)
  82. )
  83. @staticmethod
  84. def fake_thread_dict(
  85. is_resolved=False,
  86. path="foo.md",
  87. line=3,
  88. created_at="2001-02-03T04:05:06Z",
  89. ):
  90. thread_dict = {
  91. "isResolved": is_resolved,
  92. "comments": {
  93. "nodes": [
  94. {
  95. "author": {"login": "author"},
  96. "body": "comment",
  97. "createdAt": created_at,
  98. "originalCommit": {"abbreviatedOid": "abcdef"},
  99. "originalPosition": line,
  100. "path": path,
  101. "url": "http://xyz",
  102. },
  103. {
  104. "author": {"login": "other"},
  105. "body": "reply",
  106. "createdAt": "2001-02-03T04:15:16Z",
  107. },
  108. ],
  109. },
  110. }
  111. if is_resolved:
  112. thread_dict["resolvedBy"] = {
  113. "login": "resolver",
  114. "createdAt": "2001-02-03T04:25:26Z",
  115. }
  116. return thread_dict
  117. def test_thread_format(self):
  118. self.assertEqual(
  119. self.fake_thread().format(False),
  120. "https://github.com/carbon-language/carbon-lang/pull/83/"
  121. "files/abcdef#diff-d8ca3b3d314d8209367af0eea2373b6fR3\n"
  122. " - line 3; unresolved\n"
  123. " - diff: https://github.com/carbon-language/carbon-lang/pull/83/"
  124. "files/abcdef..HEAD#diff-d8ca3b3d314d8209367af0eea2373b6fL3\n"
  125. " author: comment\n"
  126. " other: reply",
  127. )
  128. self.assertEqual(
  129. self.fake_thread().format(True),
  130. "https://github.com/carbon-language/carbon-lang/pull/83/files/"
  131. "abcdef#diff-d8ca3b3d314d8209367af0eea2373b6fR3\n"
  132. " - line 3; unresolved\n"
  133. " - diff: https://github.com/carbon-language/carbon-lang/pull/83/"
  134. "files/abcdef..HEAD#diff-d8ca3b3d314d8209367af0eea2373b6fL3\n"
  135. " author at 2001-02-03 04:05:\n"
  136. " comment\n"
  137. " other at 2001-02-03 04:15:\n"
  138. " reply",
  139. )
  140. self.assertEqual(
  141. self.fake_thread(is_resolved=True).format(False),
  142. "https://github.com/carbon-language/carbon-lang/pull/83/"
  143. "files/abcdef#diff-d8ca3b3d314d8209367af0eea2373b6fR3\n"
  144. " - line 3; resolved\n"
  145. " - diff: https://github.com/carbon-language/carbon-lang/pull/83/"
  146. "files/abcdef..HEAD#diff-d8ca3b3d314d8209367af0eea2373b6fL3\n"
  147. " author: comment\n"
  148. " other: reply\n"
  149. " resolver: <resolved>",
  150. )
  151. self.assertEqual(
  152. self.fake_thread(is_resolved=True).format(True),
  153. "https://github.com/carbon-language/carbon-lang/pull/83/"
  154. "files/abcdef#diff-d8ca3b3d314d8209367af0eea2373b6fR3\n"
  155. " - line 3; resolved\n"
  156. " - diff: https://github.com/carbon-language/carbon-lang/pull/83/"
  157. "files/abcdef..HEAD#diff-d8ca3b3d314d8209367af0eea2373b6fL3\n"
  158. " author at 2001-02-03 04:05:\n"
  159. " comment\n"
  160. " other at 2001-02-03 04:15:\n"
  161. " reply\n"
  162. " resolver at 2001-02-03 04:25:\n"
  163. " <resolved>",
  164. )
  165. def test_thread_lt(self):
  166. thread1 = self.fake_thread(line=2)
  167. thread2 = self.fake_thread()
  168. thread3 = self.fake_thread(created_at="2002-02-03T04:05:06Z")
  169. self.assertTrue(thread1 < thread2)
  170. self.assertFalse(thread2 < thread1)
  171. self.assertFalse(thread2 < thread2)
  172. self.assertTrue(thread2 < thread3)
  173. self.assertFalse(thread3 < thread2)
  174. def test_accumulate_thread(self):
  175. with mock.patch.dict(os.environ, {}):
  176. parsed_args = pr_comments._parse_args(["83"])
  177. threads_by_path = {}
  178. review_threads = [
  179. self.fake_thread_dict(line=2),
  180. self.fake_thread_dict(line=4),
  181. self.fake_thread_dict(path="other.md"),
  182. self.fake_thread_dict(),
  183. ]
  184. for thread in review_threads:
  185. pr_comments._accumulate_thread(
  186. parsed_args,
  187. threads_by_path,
  188. thread,
  189. )
  190. self.assertEqual(sorted(threads_by_path.keys()), ["foo.md", "other.md"])
  191. threads = sorted(threads_by_path["foo.md"])
  192. self.assertEqual(len(threads), 3)
  193. self.assertEqual(threads[0].line, 2)
  194. self.assertEqual(threads[1].line, 3)
  195. self.assertEqual(threads[2].line, 4)
  196. self.assertEqual(len(threads_by_path["other.md"]), 1)
  197. @staticmethod
  198. def fake_pr_comment(**kwargs):
  199. return pr_comments._PRComment(
  200. TestPRComments.fake_pr_comment_dict(**kwargs)
  201. )
  202. @staticmethod
  203. def fake_pr_comment_dict(
  204. body="comment",
  205. created_at="2001-02-03T04:05:06Z",
  206. ):
  207. pr_comment_dict = {
  208. "author": {"login": "author"},
  209. "body": body,
  210. "createdAt": created_at,
  211. "url": "http://xyz",
  212. }
  213. return pr_comment_dict
  214. def test_pr_comment_format(self):
  215. self.assertEqual(
  216. self.fake_pr_comment().format(False),
  217. "http://xyz\n author: comment",
  218. )
  219. self.assertEqual(
  220. self.fake_pr_comment().format(True),
  221. "http://xyz\n author at 2001-02-03 04:05:\n comment",
  222. )
  223. def test_pr_comment_lt(self):
  224. pr_comment1 = self.fake_pr_comment()
  225. pr_comment2 = self.fake_pr_comment(created_at="2002-02-03T04:05:06Z")
  226. self.assertTrue(pr_comment1 < pr_comment2)
  227. self.assertFalse(pr_comment2 < pr_comment1)
  228. self.assertFalse(pr_comment2 < pr_comment2)
  229. def test_accumulate_pr_comment(self):
  230. with mock.patch.dict(os.environ, {}):
  231. parsed_args = pr_comments._parse_args(["83"])
  232. raw_comments = [
  233. self.fake_pr_comment_dict(body="x"),
  234. self.fake_pr_comment_dict(body=""),
  235. self.fake_pr_comment_dict(
  236. body="y", created_at="2000-02-03T04:05:06Z"
  237. ),
  238. self.fake_pr_comment_dict(
  239. body="z", created_at="2002-02-03T04:05:06Z"
  240. ),
  241. ]
  242. comments = []
  243. for raw_comment in raw_comments:
  244. pr_comments._accumulate_pr_comment(
  245. parsed_args, comments, raw_comment
  246. )
  247. comments.sort()
  248. self.assertEqual(len(comments), 3)
  249. self.assertEqual(comments[0].body, "y")
  250. self.assertEqual(comments[1].body, "x")
  251. self.assertEqual(comments[2].body, "z")
  252. if __name__ == "__main__":
  253. unittest.main()