pr_comments_test.py 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. 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/files/abcdef#diff-d8ca3b3d314d8209367af0eea2373b6fR3\n"
  121. " - line 3; unresolved\n"
  122. " - diff: https://github.com/carbon-language/carbon-lang/pull/83/files/abcdef..HEAD#diff-d8ca3b3d314d8209367af0eea2373b6fL3\n"
  123. " author: comment\n"
  124. " other: reply",
  125. )
  126. self.assertEqual(
  127. self.fake_thread().format(True),
  128. "https://github.com/carbon-language/carbon-lang/pull/83/files/abcdef#diff-d8ca3b3d314d8209367af0eea2373b6fR3\n"
  129. " - line 3; unresolved\n"
  130. " - diff: https://github.com/carbon-language/carbon-lang/pull/83/files/abcdef..HEAD#diff-d8ca3b3d314d8209367af0eea2373b6fL3\n"
  131. " author at 2001-02-03 04:05:\n"
  132. " comment\n"
  133. " other at 2001-02-03 04:15:\n"
  134. " reply",
  135. )
  136. self.assertEqual(
  137. self.fake_thread(is_resolved=True).format(False),
  138. "https://github.com/carbon-language/carbon-lang/pull/83/files/abcdef#diff-d8ca3b3d314d8209367af0eea2373b6fR3\n"
  139. " - line 3; resolved\n"
  140. " - diff: https://github.com/carbon-language/carbon-lang/pull/83/files/abcdef..HEAD#diff-d8ca3b3d314d8209367af0eea2373b6fL3\n"
  141. " author: comment\n"
  142. " other: reply\n"
  143. " resolver: <resolved>",
  144. )
  145. self.assertEqual(
  146. self.fake_thread(is_resolved=True).format(True),
  147. "https://github.com/carbon-language/carbon-lang/pull/83/files/abcdef#diff-d8ca3b3d314d8209367af0eea2373b6fR3\n"
  148. " - line 3; resolved\n"
  149. " - diff: https://github.com/carbon-language/carbon-lang/pull/83/files/abcdef..HEAD#diff-d8ca3b3d314d8209367af0eea2373b6fL3\n"
  150. " author at 2001-02-03 04:05:\n"
  151. " comment\n"
  152. " other at 2001-02-03 04:15:\n"
  153. " reply\n"
  154. " resolver at 2001-02-03 04:25:\n"
  155. " <resolved>",
  156. )
  157. def test_thread_lt(self):
  158. thread1 = self.fake_thread(line=2)
  159. thread2 = self.fake_thread()
  160. thread3 = self.fake_thread(created_at="2002-02-03T04:05:06Z")
  161. self.assertTrue(thread1 < thread2)
  162. self.assertFalse(thread2 < thread1)
  163. self.assertFalse(thread2 < thread2)
  164. self.assertTrue(thread2 < thread3)
  165. self.assertFalse(thread3 < thread2)
  166. def test_accumulate_thread(self):
  167. with mock.patch.dict(os.environ, {}):
  168. parsed_args = pr_comments._parse_args(["83"])
  169. threads_by_path = {}
  170. review_threads = [
  171. self.fake_thread_dict(line=2),
  172. self.fake_thread_dict(line=4),
  173. self.fake_thread_dict(path="other.md"),
  174. self.fake_thread_dict(),
  175. ]
  176. for thread in review_threads:
  177. pr_comments._accumulate_thread(
  178. parsed_args,
  179. threads_by_path,
  180. thread,
  181. )
  182. self.assertEqual(sorted(threads_by_path.keys()), ["foo.md", "other.md"])
  183. threads = sorted(threads_by_path["foo.md"])
  184. self.assertEqual(len(threads), 3)
  185. self.assertEqual(threads[0].line, 2)
  186. self.assertEqual(threads[1].line, 3)
  187. self.assertEqual(threads[2].line, 4)
  188. self.assertEqual(len(threads_by_path["other.md"]), 1)
  189. @staticmethod
  190. def fake_pr_comment(**kwargs):
  191. return pr_comments._PRComment(
  192. TestPRComments.fake_pr_comment_dict(**kwargs)
  193. )
  194. @staticmethod
  195. def fake_pr_comment_dict(
  196. body="comment",
  197. created_at="2001-02-03T04:05:06Z",
  198. ):
  199. pr_comment_dict = {
  200. "author": {"login": "author"},
  201. "body": body,
  202. "createdAt": created_at,
  203. "url": "http://xyz",
  204. }
  205. return pr_comment_dict
  206. def test_pr_comment_format(self):
  207. self.assertEqual(
  208. self.fake_pr_comment().format(False),
  209. "http://xyz\n author: comment",
  210. )
  211. self.assertEqual(
  212. self.fake_pr_comment().format(True),
  213. "http://xyz\n author at 2001-02-03 04:05:\n comment",
  214. )
  215. def test_pr_comment_lt(self):
  216. pr_comment1 = self.fake_pr_comment()
  217. pr_comment2 = self.fake_pr_comment(created_at="2002-02-03T04:05:06Z")
  218. self.assertTrue(pr_comment1 < pr_comment2)
  219. self.assertFalse(pr_comment2 < pr_comment1)
  220. self.assertFalse(pr_comment2 < pr_comment2)
  221. def test_accumulate_pr_comment(self):
  222. with mock.patch.dict(os.environ, {}):
  223. parsed_args = pr_comments._parse_args(["83"])
  224. raw_comments = [
  225. self.fake_pr_comment_dict(body="x"),
  226. self.fake_pr_comment_dict(body=""),
  227. self.fake_pr_comment_dict(
  228. body="y", created_at="2000-02-03T04:05:06Z"
  229. ),
  230. self.fake_pr_comment_dict(
  231. body="z", created_at="2002-02-03T04:05:06Z"
  232. ),
  233. ]
  234. comments = []
  235. for raw_comment in raw_comments:
  236. pr_comments._accumulate_pr_comment(
  237. parsed_args, comments, raw_comment
  238. )
  239. comments.sort()
  240. self.assertEqual(len(comments), 3)
  241. self.assertEqual(comments[0].body, "y")
  242. self.assertEqual(comments[1].body, "x")
  243. self.assertEqual(comments[2].body, "z")
  244. if __name__ == "__main__":
  245. unittest.main()